Skip to content

Commit

Permalink
Merge pull request #1445 from dusk-network/mempool_extraction_delay
Browse files Browse the repository at this point in the history
Reintroduce artificial delay on block generator
  • Loading branch information
herr-seppia authored Jul 28, 2022
2 parents 398f63f + fd9be51 commit e1dab01
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 12 deletions.
1 change: 1 addition & 0 deletions harness/engine/profiles.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ func Profile1(index int, node *DuskNode, consensusKeysPath string) {
// viper.Set("mempool.preallocTxs", "100")
viper.Set("mempool.poolType", "diskpool")
viper.Set("mempool.diskpoolDir", node.Dir+"/mempool.db")
// viper.Set("mempool.extractionDelaySecs", "3")

viper.Set("mempool.maxInvItems", "10000")
viper.Set("mempool.propagateTimeout", "100ms")
Expand Down
3 changes: 3 additions & 0 deletions pkg/config/groups.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,9 @@ type mempoolConfiguration struct {

// Enables mempool updates at startup
Updates updates

// Artificial delay applied when mempool is empty
ExtractionDelaySecs int64
}

type updates struct {
Expand Down
1 change: 1 addition & 0 deletions pkg/config/loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,7 @@ func init() {
r.General.Network = devnet
r.Consensus.ConsensusTimeOut = DefaultConsensusTimeOutSeconds
r.Mempool.MaxInvItems = 10000
r.Mempool.ExtractionDelaySecs = 3
r.State.PersistEvery = 1
r.State.BlockGasLimit = DefaultBlockGasLimit
}
24 changes: 14 additions & 10 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 @@ -128,9 +128,14 @@ func (bg *generator) execute(ctx context.Context, txs []transactions.ContractCal
}

// fetchOrTimeout will keep trying to FetchMempoolTxs() until either
// we get some txs or or the timeout expires.
func (bg *generator) FetchOrTimeout(keys [][]byte) ([]transactions.ContractCall, error) {
ctx, cancel := context.WithTimeout(context.Background(), 15*time.Second)
// we get some txs or the timeout expires.
func (bg *generator) fetchOrTimeout(ctx context.Context, keys [][]byte) ([]transactions.ContractCall, error) {
delay := config.Get().Mempool.ExtractionDelaySecs
if delay == 0 || config.Get().Consensus.ConsensusTimeOut < delay {
return bg.FetchMempoolTxs(keys)
}

ctx, cancel := context.WithTimeout(ctx, time.Duration(delay)*time.Second)
defer cancel()

tick := time.NewTicker(500 * time.Millisecond)
Expand All @@ -139,8 +144,7 @@ func (bg *generator) FetchOrTimeout(keys [][]byte) ([]transactions.ContractCall,
for {
select {
case <-ctx.Done():
txs, err := bg.FetchMempoolTxs(keys)
return txs, err
return bg.FetchMempoolTxs(keys)
case <-tick.C:
txs, err := bg.FetchMempoolTxs(keys)
if err != nil {
Expand All @@ -156,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.FetchMempoolTxs(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 e1dab01

Please sign in to comment.