prysm-pulse/sharding/proposer/service.go

88 lines
2.8 KiB
Go
Raw Normal View History

// Package proposer defines all relevant functionality for a Proposer actor
// within the minimal sharding protocol.
package proposer
import (
"context"
"crypto/rand"
"fmt"
"math/big"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/ethdb"
"github.com/ethereum/go-ethereum/log"
"github.com/ethereum/go-ethereum/sharding/mainchain"
"github.com/ethereum/go-ethereum/sharding/p2p"
"github.com/ethereum/go-ethereum/sharding/params"
"github.com/ethereum/go-ethereum/sharding/txpool"
)
// Proposer holds functionality required to run a collation proposer
// in a sharded system. Must satisfy the Service interface defined in
// sharding/service.go.
type Proposer struct {
config *params.Config
client *mainchain.SMCClient
p2p *p2p.Server
txpool *txpool.TXPool
shardChainDb ethdb.Database
shardID int
}
// NewProposer creates a struct instance of a proposer service.
// It will have access to a mainchain client, a p2p network,
// and a shard transaction pool.
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
}
// Start the main loop for proposing collations.
func (p *Proposer) Start() {
log.Info(fmt.Sprintf("Starting proposer service in shard %d", p.shardID))
go p.proposeCollations()
}
// Stop the main loop for proposing collations.
func (p *Proposer) Stop() error {
log.Info(fmt.Sprintf("Stopping proposer service in shard %d", p.shardID))
return nil
}
func (p *Proposer) proposeCollations() {
// 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 {
log.Error(fmt.Sprintf("Could not fetch current block number: %v", err))
return
}
period := new(big.Int).Div(blockNumber.Number(), big.NewInt(p.config.PeriodLength))
// Create collation.
collation, err := createCollation(p.client, p.client.Account(), p.client, big.NewInt(int64(p.shardID)), period, txs)
if err != nil {
log.Error(fmt.Sprintf("Could not create collation: %v", err))
return
}
// Check SMC if we can submit header before addHeader
canAdd, err := checkHeaderAdded(p.client, big.NewInt(int64(p.shardID)), period)
if err != nil {
log.Error(fmt.Sprintf("Could not check if we can submit header: %v", err))
return
}
if canAdd {
addHeader(p.client, collation)
}
}