Skip to content

Commit

Permalink
Address review comments
Browse files Browse the repository at this point in the history
- Check ExtractionDelaySecs against ConsensusTimeout
- Pass GenerateCandidateMessage's context to Generate fn
- Add comment to ExtractionDelaySecs
  • Loading branch information
herr-seppia committed Jul 28, 2022
1 parent 62f406a commit 614833c
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 11 deletions.
3 changes: 2 additions & 1 deletion pkg/config/groups.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
16 changes: 8 additions & 8 deletions pkg/core/consensus/blockgenerator/candidate/blockgenerator.go
Original file line number Diff line number Diff line change
Expand Up @@ -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).
Expand Down Expand Up @@ -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) {
Expand All @@ -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)
Expand All @@ -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
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/core/consensus/blockgenerator/candidate/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/core/consensus/blockgenerator/candidate/mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down

0 comments on commit 614833c

Please sign in to comment.