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"
|
|
|
|
"math/big"
|
|
|
|
|
2018-07-09 02:40:34 +00:00
|
|
|
gethTypes "github.com/ethereum/go-ethereum/core/types"
|
2018-06-20 03:59:02 +00:00
|
|
|
"github.com/ethereum/go-ethereum/event"
|
2018-07-17 18:39:04 +00:00
|
|
|
pb "github.com/prysmaticlabs/geth-sharding/proto/sharding/v1"
|
2018-07-07 17:23:19 +00:00
|
|
|
"github.com/prysmaticlabs/geth-sharding/sharding/database"
|
|
|
|
"github.com/prysmaticlabs/geth-sharding/sharding/mainchain"
|
|
|
|
"github.com/prysmaticlabs/geth-sharding/sharding/p2p"
|
|
|
|
"github.com/prysmaticlabs/geth-sharding/sharding/params"
|
|
|
|
"github.com/prysmaticlabs/geth-sharding/sharding/syncer"
|
|
|
|
"github.com/prysmaticlabs/geth-sharding/sharding/txpool"
|
2018-07-09 02:40:34 +00:00
|
|
|
"github.com/prysmaticlabs/geth-sharding/sharding/types"
|
2018-07-17 18:39:04 +00:00
|
|
|
"github.com/prysmaticlabs/geth-sharding/shared/legacyutil"
|
2018-07-10 02:27:23 +00:00
|
|
|
log "github.com/sirupsen/logrus"
|
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-29 00:56:51 +00:00
|
|
|
config *params.Config
|
|
|
|
client *mainchain.SMCClient
|
|
|
|
p2p *p2p.Server
|
|
|
|
txpool *txpool.TXPool
|
|
|
|
txpoolSub event.Subscription
|
|
|
|
dbService *database.ShardDB
|
|
|
|
shardID int
|
2018-07-09 02:40:34 +00:00
|
|
|
shard *types.Shard
|
2018-06-29 00:56:51 +00:00
|
|
|
ctx context.Context
|
|
|
|
cancel context.CancelFunc
|
2018-07-07 17:23:19 +00:00
|
|
|
sync *syncer.Syncer
|
2018-07-14 20:23:07 +00:00
|
|
|
msgChan chan p2p.Message
|
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-07-07 17:23:19 +00:00
|
|
|
func NewProposer(config *params.Config, client *mainchain.SMCClient, p2p *p2p.Server, txpool *txpool.TXPool, dbService *database.ShardDB, shardID int, sync *syncer.Syncer) (*Proposer, error) {
|
2018-06-20 03:59:02 +00:00
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
return &Proposer{
|
|
|
|
config,
|
|
|
|
client,
|
|
|
|
p2p,
|
|
|
|
txpool,
|
2018-07-17 18:39:04 +00:00
|
|
|
nil, // txpoolSub
|
2018-06-29 00:56:51 +00:00
|
|
|
dbService,
|
|
|
|
shardID,
|
2018-07-17 18:39:04 +00:00
|
|
|
nil, // shard
|
2018-06-20 03:59:02 +00:00
|
|
|
ctx,
|
2018-07-07 17:23:19 +00:00
|
|
|
cancel,
|
2018-07-14 20:23:07 +00:00
|
|
|
sync,
|
2018-07-17 18:39:04 +00:00
|
|
|
nil, // msgChan
|
|
|
|
}, 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-07-14 20:23:07 +00:00
|
|
|
p.shard = types.NewShard(big.NewInt(int64(p.shardID)), p.dbService.DB())
|
|
|
|
p.msgChan = make(chan p2p.Message, 20)
|
2018-07-17 18:39:04 +00:00
|
|
|
feed := p.p2p.Feed(pb.Transaction{})
|
2018-07-14 20:23:07 +00:00
|
|
|
p.txpoolSub = feed.Subscribe(p.msgChan)
|
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-07-10 02:27:23 +00:00
|
|
|
log.Warnf("Stopping proposer service in shard %d", p.shard.ShardID())
|
2018-06-20 03:59:02 +00:00
|
|
|
defer p.cancel()
|
2018-07-14 20:23:07 +00:00
|
|
|
defer close(p.msgChan)
|
2018-06-20 03:59:02 +00:00
|
|
|
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-07-17 18:39:04 +00:00
|
|
|
feed := p.p2p.Feed(pb.Transaction{})
|
|
|
|
ch := make(chan p2p.Message, 20)
|
|
|
|
sub := feed.Subscribe(ch)
|
|
|
|
defer sub.Unsubscribe()
|
|
|
|
defer close(ch)
|
2018-06-20 03:59:02 +00:00
|
|
|
for {
|
|
|
|
select {
|
2018-07-17 18:39:04 +00:00
|
|
|
case msg := <-ch:
|
|
|
|
tx, ok := msg.Data.(*pb.Transaction)
|
2018-07-13 16:47:57 +00:00
|
|
|
if !ok {
|
|
|
|
log.Error("Received incorrect p2p message. Wanted a transaction broadcast message")
|
|
|
|
break
|
|
|
|
}
|
2018-07-17 18:39:04 +00:00
|
|
|
// log.Debugf("Received transaction: %x", tx)
|
|
|
|
if err := p.createCollation(p.ctx, []*gethTypes.Transaction{legacyutil.TransformTransaction(tx)}); err != nil {
|
2018-07-10 02:27:23 +00:00
|
|
|
log.Errorf("Create collation failed: %v", err)
|
2018-06-20 03:59:02 +00:00
|
|
|
}
|
|
|
|
case <-p.ctx.Done():
|
2018-06-29 00:56:51 +00:00
|
|
|
log.Debug("Proposer context closed, exiting goroutine")
|
2018-06-20 03:59:02 +00:00
|
|
|
return
|
2018-06-29 00:56:51 +00:00
|
|
|
case <-p.txpoolSub.Err():
|
|
|
|
log.Debug("Subscriber closed")
|
2018-06-20 03:59:02 +00:00
|
|
|
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-07-09 02:40:34 +00:00
|
|
|
func (p *Proposer) createCollation(ctx context.Context, txs []*gethTypes.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 {
|
2018-07-10 02:27:23 +00:00
|
|
|
log.Errorf("Could not save collation to persistent storage: %v", err)
|
2018-06-21 03:03:02 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-07-10 02:27:23 +00:00
|
|
|
log.Infof("Saved collation with header hash %v to shardChainDB", collation.Header().Hash().Hex())
|
2018-06-21 03:03:02 +00:00
|
|
|
|
|
|
|
// 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-07-02 19:25:06 +00:00
|
|
|
AddHeader(p.client, 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
|
|
|
}
|