Skip to content

Commit 334c701

Browse files
committed
Make missing txs check atomic
1 parent 9042a3e commit 334c701

File tree

6 files changed

+127
-20
lines changed

6 files changed

+127
-20
lines changed

internal/consensus/replay_stubs.go

+4
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,10 @@ func (m emptyMempool) GetTxsForKeys(txKeys []types.TxKey) types.Txs {
2424
return types.Txs{}
2525
}
2626

27+
func (m emptyMempool) SafeGetTxsForKeys(txKeys []types.TxKey) (types.Txs, []types.TxKey) {
28+
return types.Txs{}, []types.TxKey{}
29+
}
30+
2731
var _ mempool.Mempool = emptyMempool{}
2832

2933
func (emptyMempool) TxStore() *mempool.TxStore { return nil }

internal/consensus/state.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -2502,13 +2502,12 @@ func (cs *State) tryCreateProposalBlock(ctx context.Context, height int64, round
25022502
// Build a proposal block from mempool txs. If cs.config.GossipTransactionKeyOnly=true
25032503
// proposals only contain txKeys so we rebuild the block using mempool txs
25042504
func (cs *State) buildProposalBlock(height int64, header types.Header, lastCommit *types.Commit, evidence []types.Evidence, proposerAddress types.Address, txKeys []types.TxKey) *types.Block {
2505-
missingTxs := cs.blockExec.GetMissingTxs(txKeys)
2505+
txs, missingTxs := cs.blockExec.SafeGetTxsByKeys(txKeys)
25062506
if len(missingTxs) > 0 {
25072507
cs.metrics.ProposalMissingTxs.Set(float64(len(missingTxs)))
25082508
cs.logger.Debug("Missing txs when trying to build block", "missing_txs", cs.blockExec.GetMissingTxs(txKeys))
25092509
return nil
25102510
}
2511-
txs := cs.blockExec.GetTxsForKeys(txKeys)
25122511
block := cs.state.MakeBlock(height, cs.blockExec.GetTxsForKeys(txKeys), lastCommit, evidence, proposerAddress)
25132512
block.Version = header.Version
25142513
block.Data.Txs = txs

internal/mempool/mempool.go

+17
Original file line numberDiff line numberDiff line change
@@ -405,6 +405,23 @@ func (txmp *TxMempool) GetTxsForKeys(txKeys []types.TxKey) types.Txs {
405405
return txs
406406
}
407407

408+
func (txmp *TxMempool) SafeGetTxsForKeys(txKeys []types.TxKey) (types.Txs, []types.TxKey) {
409+
txmp.mtx.RLock()
410+
defer txmp.mtx.RUnlock()
411+
412+
txs := make([]types.Tx, 0, len(txKeys))
413+
missing := []types.TxKey{}
414+
for _, txKey := range txKeys {
415+
wtx := txmp.txStore.GetTxByHash(txKey)
416+
if wtx == nil {
417+
missing = append(missing, txKey)
418+
continue
419+
}
420+
txs = append(txs, wtx.tx)
421+
}
422+
return txs, missing
423+
}
424+
408425
// Flush empties the mempool. It acquires a read-lock, fetches all the
409426
// transactions currently in the transaction store and removes each transaction
410427
// from the store and all indexes and finally resets the cache.

internal/mempool/mocks/mempool.go

+97-18
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

internal/mempool/types.go

+4
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,10 @@ type Mempool interface {
4242

4343
GetTxsForKeys(txKeys []types.TxKey) types.Txs
4444

45+
// Similar to GetTxsForKeys except that it would return a list
46+
// indicating missing keys.
47+
SafeGetTxsForKeys(txKeys []types.TxKey) (types.Txs, []types.TxKey)
48+
4549
// ReapMaxBytesMaxGas reaps transactions from the mempool up to maxBytes
4650
// bytes total with the condition that the total gasWanted must be less than
4751
// maxGas and that the total estimated gas used is less than maxGasEstimated.

internal/state/execution.go

+4
Original file line numberDiff line numberDiff line change
@@ -476,6 +476,10 @@ func (blockExec *BlockExecutor) GetMissingTxs(txKeys []types.TxKey) []types.TxKe
476476
return missingTxKeys
477477
}
478478

479+
func (blockExec *BlockExecutor) SafeGetTxsByKeys(txKeys []types.TxKey) (types.Txs, []types.TxKey) {
480+
return blockExec.mempool.SafeGetTxsForKeys(txKeys)
481+
}
482+
479483
func (blockExec *BlockExecutor) CheckTxFromPeerProposal(ctx context.Context, tx types.Tx) {
480484
// Ignore errors from CheckTx because there could be benign errors due to the same tx being
481485
// inserted into the mempool from gossiping. Since such simultaneous insertion could result in

0 commit comments

Comments
 (0)