From 614833c94ea1a5ff06927240d8fe750e0da9a127 Mon Sep 17 00:00:00 2001 From: Herr Seppia Date: Thu, 28 Jul 2022 09:58:38 +0200 Subject: [PATCH] Address review comments - Check ExtractionDelaySecs against ConsensusTimeout - Pass GenerateCandidateMessage's context to Generate fn - Add comment to ExtractionDelaySecs --- pkg/config/groups.go | 3 ++- .../blockgenerator/candidate/blockgenerator.go | 16 ++++++++-------- .../blockgenerator/candidate/genesis.go | 2 +- .../consensus/blockgenerator/candidate/mock.go | 2 +- 4 files changed, 12 insertions(+), 11 deletions(-) diff --git a/pkg/config/groups.go b/pkg/config/groups.go index 1af48d137..4ad1fa0e6 100644 --- a/pkg/config/groups.go +++ b/pkg/config/groups.go @@ -138,7 +138,8 @@ type mempoolConfiguration struct { // Enables mempool updates at startup Updates updates - ExtractionDelaySecs uint32 + // Artificial delay applied when mempool is empty + ExtractionDelaySecs int64 } type updates struct { diff --git a/pkg/core/consensus/blockgenerator/candidate/blockgenerator.go b/pkg/core/consensus/blockgenerator/candidate/blockgenerator.go index 629be1b99..3300911fe 100644 --- a/pkg/core/consensus/blockgenerator/candidate/blockgenerator.go +++ b/pkg/core/consensus/blockgenerator/candidate/blockgenerator.go @@ -80,7 +80,7 @@ func (bg *generator) GenerateCandidateMessage(ctx context.Context, r consensus.R return nil, err } - blk, err := bg.Generate(seed, committee, r) + blk, err := bg.Generate(ctx, seed, committee, r) if err != nil { log. WithError(err). @@ -110,8 +110,8 @@ func (bg *generator) GenerateCandidateMessage(ctx context.Context, r consensus.R } // Generate a Block. -func (bg *generator) Generate(seed []byte, keys [][]byte, r consensus.RoundUpdate) (*block.Block, error) { - return bg.GenerateBlock(r.Round, seed, r.Hash, r.Timestamp, keys) +func (bg *generator) Generate(ctx context.Context, seed []byte, keys [][]byte, r consensus.RoundUpdate) (*block.Block, error) { + return bg.GenerateBlock(ctx, r.Round, seed, r.Hash, r.Timestamp, keys) } func (bg *generator) execute(ctx context.Context, txs []transactions.ContractCall, round uint64, gasLimit uint64) ([]transactions.ContractCall, []byte, error) { @@ -129,13 +129,13 @@ func (bg *generator) execute(ctx context.Context, txs []transactions.ContractCal // fetchOrTimeout will keep trying to FetchMempoolTxs() until either // we get some txs or the timeout expires. -func (bg *generator) fetchOrTimeout(keys [][]byte) ([]transactions.ContractCall, error) { +func (bg *generator) fetchOrTimeout(ctx context.Context, keys [][]byte) ([]transactions.ContractCall, error) { delay := config.Get().Mempool.ExtractionDelaySecs - if delay == 0 { + if delay == 0 || config.Get().Consensus.ConsensusTimeOut < delay { return bg.FetchMempoolTxs(keys) } - ctx, cancel := context.WithTimeout(context.Background(), time.Duration(delay)*time.Second) + ctx, cancel := context.WithTimeout(ctx, time.Duration(delay)*time.Second) defer cancel() tick := time.NewTicker(500 * time.Millisecond) @@ -160,8 +160,8 @@ func (bg *generator) fetchOrTimeout(keys [][]byte) ([]transactions.ContractCall, // GenerateBlock generates a candidate block, by constructing the header and filling it // with transactions from the mempool. -func (bg *generator) GenerateBlock(round uint64, seed, prevBlockHash []byte, prevBlockTimestamp int64, keys [][]byte) (*block.Block, error) { - txs, err := bg.fetchOrTimeout(keys) +func (bg *generator) GenerateBlock(ctx context.Context, round uint64, seed, prevBlockHash []byte, prevBlockTimestamp int64, keys [][]byte) (*block.Block, error) { + txs, err := bg.fetchOrTimeout(ctx, keys) if err != nil { return nil, err } diff --git a/pkg/core/consensus/blockgenerator/candidate/genesis.go b/pkg/core/consensus/blockgenerator/candidate/genesis.go index f106cbedb..c1c27bad6 100644 --- a/pkg/core/consensus/blockgenerator/candidate/genesis.go +++ b/pkg/core/consensus/blockgenerator/candidate/genesis.go @@ -34,7 +34,7 @@ func GenerateGenesisBlock(e *consensus.Emitter) (string, error) { // TODO: do we need to generate correct proof and score seed, _ := crypto.RandEntropy(33) - b, err := g.GenerateBlock(0, seed, make([]byte, 32), time.Now().Unix(), [][]byte{{0}}) + b, err := g.GenerateBlock(context.Background(), 0, seed, make([]byte, 32), time.Now().Unix(), [][]byte{{0}}) if err != nil { return "", err } diff --git a/pkg/core/consensus/blockgenerator/candidate/mock.go b/pkg/core/consensus/blockgenerator/candidate/mock.go index 0a8a6cb12..ca97dfe71 100644 --- a/pkg/core/consensus/blockgenerator/candidate/mock.go +++ b/pkg/core/consensus/blockgenerator/candidate/mock.go @@ -25,7 +25,7 @@ func (m *mock) MockCandidate(hdr header.Header, previousBlock []byte) block.Bloc seed, _ := crypto.RandEntropy(32) - b, err := m.GenerateBlock(hdr.Round, seed, previousBlock, 0, [][]byte{hdr.PubKeyBLS}) + b, err := m.GenerateBlock(context.Background(), hdr.Round, seed, previousBlock, 0, [][]byte{hdr.PubKeyBLS}) if err != nil { panic(err) }