2018-06-05 23:03:15 +00:00
|
|
|
// Package proposer defines all relevant functionality for a Proposer actor
|
2018-06-05 21:28:57 +00:00
|
|
|
// within the minimal sharding protocol.
|
2018-05-22 11:34:12 +00:00
|
|
|
package proposer
|
|
|
|
|
|
|
|
import (
|
2018-06-07 22:16:34 +00:00
|
|
|
"context"
|
|
|
|
"crypto/rand"
|
2018-06-11 22:21:24 +00:00
|
|
|
"fmt"
|
2018-06-07 22:16:34 +00:00
|
|
|
"math/big"
|
|
|
|
|
|
|
|
"github.com/ethereum/go-ethereum/common"
|
|
|
|
"github.com/ethereum/go-ethereum/core/types"
|
2018-06-08 21:45:26 +00:00
|
|
|
"github.com/ethereum/go-ethereum/ethdb"
|
2018-05-22 11:34:12 +00:00
|
|
|
"github.com/ethereum/go-ethereum/log"
|
2018-06-05 23:03:15 +00:00
|
|
|
"github.com/ethereum/go-ethereum/sharding/mainchain"
|
2018-06-13 12:44:33 +00:00
|
|
|
"github.com/ethereum/go-ethereum/sharding/p2p"
|
2018-06-12 23:03:20 +00:00
|
|
|
"github.com/ethereum/go-ethereum/sharding/params"
|
2018-06-13 19:06:51 +00:00
|
|
|
"github.com/ethereum/go-ethereum/sharding/txpool"
|
2018-05-22 11:34:12 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// Proposer holds functionality required to run a collation proposer
|
|
|
|
// in a sharded system. Must satisfy the Service interface defined in
|
|
|
|
// sharding/service.go.
|
2018-06-04 21:10:59 +00:00
|
|
|
type Proposer struct {
|
2018-06-13 17:37:23 +00:00
|
|
|
config *params.Config
|
2018-06-10 16:13:57 +00:00
|
|
|
client *mainchain.SMCClient
|
2018-06-13 12:44:33 +00:00
|
|
|
p2p *p2p.Server
|
|
|
|
txpool *txpool.TXPool
|
2018-06-08 21:45:26 +00:00
|
|
|
shardChainDb ethdb.Database
|
2018-06-11 18:00:31 +00:00
|
|
|
shardID int
|
2018-06-04 21:10:59 +00:00
|
|
|
}
|
2018-05-22 11:34:12 +00:00
|
|
|
|
2018-06-06 02:03:58 +00:00
|
|
|
// NewProposer creates a struct instance of a proposer service.
|
2018-06-13 12:44:33 +00:00
|
|
|
// It will have access to a mainchain client, a p2p network,
|
2018-06-06 02:03:58 +00:00
|
|
|
// and a shard transaction pool.
|
2018-06-13 17:37:23 +00:00
|
|
|
func NewProposer(config *params.Config, client *mainchain.SMCClient, p2p *p2p.Server, txpool *txpool.TXPool, shardChainDb ethdb.Database, shardID int) (*Proposer, error) {
|
|
|
|
return &Proposer{config, client, p2p, txpool, shardChainDb, shardID}, nil
|
2018-05-22 11:34:12 +00:00
|
|
|
}
|
|
|
|
|
2018-05-22 12:47:35 +00:00
|
|
|
// Start the main loop for proposing collations.
|
2018-06-11 22:21:24 +00:00
|
|
|
func (p *Proposer) Start() {
|
2018-06-11 18:00:31 +00:00
|
|
|
log.Info(fmt.Sprintf("Starting proposer service in shard %d", p.shardID))
|
2018-06-11 22:21:24 +00:00
|
|
|
go p.proposeCollations()
|
2018-05-22 12:47:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Stop the main loop for proposing collations.
|
|
|
|
func (p *Proposer) Stop() error {
|
2018-06-12 05:14:25 +00:00
|
|
|
log.Info(fmt.Sprintf("Stopping proposer service in shard %d", p.shardID))
|
2018-05-22 11:34:12 +00:00
|
|
|
return nil
|
|
|
|
}
|
2018-06-11 22:21:24 +00:00
|
|
|
|
|
|
|
func (p *Proposer) proposeCollations() {
|
2018-06-07 22:16:34 +00:00
|
|
|
|
|
|
|
// TODO: Receive TXs from shard TX generator or TXpool (Github Issues 153 and 161)
|
|
|
|
var txs []*types.Transaction
|
|
|
|
for i := 0; i < 10; i++ {
|
|
|
|
data := make([]byte, 1024)
|
|
|
|
rand.Read(data)
|
|
|
|
txs = append(txs, types.NewTransaction(0, common.HexToAddress("0x0"),
|
|
|
|
nil, 0, nil, data))
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get current block number.
|
|
|
|
blockNumber, err := p.client.ChainReader().BlockByNumber(context.Background(), nil)
|
|
|
|
if err != nil {
|
2018-06-11 22:21:24 +00:00
|
|
|
log.Error(fmt.Sprintf("Could not fetch current block number: %v", err))
|
|
|
|
return
|
2018-06-07 22:16:34 +00:00
|
|
|
}
|
2018-06-12 23:03:20 +00:00
|
|
|
period := new(big.Int).Div(blockNumber.Number(), big.NewInt(p.config.PeriodLength))
|
2018-06-07 22:16:34 +00:00
|
|
|
|
|
|
|
// Create collation.
|
2018-06-17 17:39:42 +00:00
|
|
|
collation, err := createCollation(p.client, p.client.Account(), p.client, big.NewInt(int64(p.shardID)), period, txs)
|
2018-06-07 22:16:34 +00:00
|
|
|
if err != nil {
|
2018-06-11 22:21:24 +00:00
|
|
|
log.Error(fmt.Sprintf("Could not create collation: %v", err))
|
|
|
|
return
|
2018-06-07 22:16:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Check SMC if we can submit header before addHeader
|
2018-06-11 18:00:31 +00:00
|
|
|
canAdd, err := checkHeaderAdded(p.client, big.NewInt(int64(p.shardID)), period)
|
2018-06-07 22:16:34 +00:00
|
|
|
if err != nil {
|
2018-06-11 22:21:24 +00:00
|
|
|
log.Error(fmt.Sprintf("Could not check if we can submit header: %v", err))
|
|
|
|
return
|
2018-06-07 22:16:34 +00:00
|
|
|
}
|
|
|
|
if canAdd {
|
|
|
|
addHeader(p.client, collation)
|
|
|
|
}
|
2018-05-22 11:34:12 +00:00
|
|
|
}
|