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"
|
2018-06-11 22:21:24 +00:00
|
|
|
"fmt"
|
2018-06-07 22:16:34 +00:00
|
|
|
"math/big"
|
|
|
|
|
|
|
|
"github.com/ethereum/go-ethereum/core/types"
|
2018-06-21 03:03:02 +00:00
|
|
|
"github.com/ethereum/go-ethereum/ethdb"
|
2018-06-20 03:59:02 +00:00
|
|
|
"github.com/ethereum/go-ethereum/event"
|
2018-05-22 11:34:12 +00:00
|
|
|
"github.com/ethereum/go-ethereum/log"
|
2018-06-21 03:03:02 +00:00
|
|
|
"github.com/ethereum/go-ethereum/sharding"
|
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-20 03:59:02 +00:00
|
|
|
txpoolSub event.Subscription
|
2018-06-21 03:03:02 +00:00
|
|
|
shardChainDb ethdb.Database
|
|
|
|
shard *sharding.Shard
|
2018-06-20 03:59:02 +00:00
|
|
|
ctx context.Context
|
|
|
|
cancel context.CancelFunc
|
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-21 03:03:02 +00:00
|
|
|
func NewProposer(config *params.Config, client *mainchain.SMCClient, p2p *p2p.Server, txpool *txpool.TXPool, shardChainDB ethdb.Database, shardID int) (*Proposer, error) {
|
2018-06-20 03:59:02 +00:00
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
2018-06-21 03:03:02 +00:00
|
|
|
shard := sharding.NewShard(big.NewInt(int64(shardID)), shardChainDB)
|
2018-06-20 03:59:02 +00:00
|
|
|
return &Proposer{
|
|
|
|
config,
|
|
|
|
client,
|
|
|
|
p2p,
|
|
|
|
txpool,
|
|
|
|
nil,
|
2018-06-21 03:03:02 +00:00
|
|
|
shardChainDB,
|
|
|
|
shard,
|
2018-06-20 03:59:02 +00:00
|
|
|
ctx,
|
|
|
|
cancel}, 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-21 03:03:02 +00:00
|
|
|
log.Info("Starting proposer service")
|
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-21 03:03:02 +00:00
|
|
|
log.Info(fmt.Sprintf("Stopping proposer service in shard %d", p.shard.ShardID()))
|
2018-06-20 03:59:02 +00:00
|
|
|
defer p.cancel()
|
|
|
|
p.txpoolSub.Unsubscribe()
|
2018-05-22 11:34:12 +00:00
|
|
|
return nil
|
|
|
|
}
|
2018-06-11 22:21:24 +00:00
|
|
|
|
2018-06-20 03:59:02 +00:00
|
|
|
// proposeCollations listens to the transaction feed and submits collations over an interval.
|
2018-06-11 22:21:24 +00:00
|
|
|
func (p *Proposer) proposeCollations() {
|
2018-06-20 03:59:02 +00:00
|
|
|
requests := make(chan *types.Transaction)
|
|
|
|
p.txpoolSub = p.txpool.TransactionsFeed().Subscribe(requests)
|
2018-06-21 03:03:02 +00:00
|
|
|
defer close(requests)
|
2018-06-20 03:59:02 +00:00
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case tx := <-requests:
|
|
|
|
log.Info(fmt.Sprintf("Received transaction: %x", tx.Hash()))
|
|
|
|
if err := p.createCollation(p.ctx, []*types.Transaction{tx}); err != nil {
|
|
|
|
log.Error(fmt.Sprintf("Create collation failed: %v", err))
|
|
|
|
}
|
|
|
|
case <-p.ctx.Done():
|
|
|
|
log.Error("Proposer context closed, exiting goroutine")
|
|
|
|
return
|
|
|
|
case err := <-p.txpoolSub.Err():
|
|
|
|
log.Error(fmt.Sprintf("Subscriber closed: %v", err))
|
|
|
|
return
|
|
|
|
}
|
2018-06-07 22:16:34 +00:00
|
|
|
}
|
2018-06-20 03:59:02 +00:00
|
|
|
}
|
2018-06-07 22:16:34 +00:00
|
|
|
|
2018-06-20 03:59:02 +00:00
|
|
|
func (p *Proposer) createCollation(ctx context.Context, txs []*types.Transaction) error {
|
2018-06-07 22:16:34 +00:00
|
|
|
// Get current block number.
|
2018-06-20 03:59:02 +00:00
|
|
|
blockNumber, err := p.client.ChainReader().BlockByNumber(ctx, nil)
|
2018-06-07 22:16:34 +00:00
|
|
|
if err != nil {
|
2018-06-20 03:59:02 +00:00
|
|
|
return err
|
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-21 03:03:02 +00:00
|
|
|
collation, err := createCollation(p.client, p.client.Account(), p.client, p.shard.ShardID(), period, txs)
|
2018-06-07 22:16:34 +00:00
|
|
|
if err != nil {
|
2018-06-20 03:59:02 +00:00
|
|
|
return err
|
2018-06-07 22:16:34 +00:00
|
|
|
}
|
|
|
|
|
2018-06-21 03:03:02 +00:00
|
|
|
// Saves the collation to persistent storage in the shardDB.
|
|
|
|
if err := p.shard.SaveCollation(collation); err != nil {
|
|
|
|
log.Error(fmt.Sprintf("Could not save collation to persistent storage: %v", err))
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Info(fmt.Sprintf("Saved collation with header hash %v to shardChainDb", collation.Header().Hash().Hex()))
|
|
|
|
|
|
|
|
// Check SMC if we can submit header before addHeader.
|
|
|
|
canAdd, err := checkHeaderAdded(p.client, p.shard.ShardID(), period)
|
2018-06-07 22:16:34 +00:00
|
|
|
if err != nil {
|
2018-06-20 03:59:02 +00:00
|
|
|
return err
|
2018-06-07 22:16:34 +00:00
|
|
|
}
|
|
|
|
if canAdd {
|
2018-06-21 03:03:02 +00:00
|
|
|
AddHeader(p.client, collation)
|
2018-06-07 22:16:34 +00:00
|
|
|
}
|
2018-06-20 03:59:02 +00:00
|
|
|
|
|
|
|
return nil
|
2018-05-22 11:34:12 +00:00
|
|
|
}
|