Skip to content

Commit

Permalink
Improve mempool extraction
Browse files Browse the repository at this point in the history
Resolves #1421
  • Loading branch information
herr-seppia committed Jun 20, 2022
1 parent 0a78d05 commit 9b31476
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 4 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Mempool discards any transaction with repeated nullifier [#1388]

### Changed
- Extraction from mempool consider Gas expenditure estimation [#1421]
- Add GasLimit to the Block's header [#1416]
- Reduce wire messages needed for propagating an Agreement vote
- Improve fallback procedure to revert multiple ephemeral blocks [#1343]
Expand All @@ -30,6 +31,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Fixed
- Default configuration values loading [#1419]

[#1421]: (https://github.com/dusk-network/dusk-blockchain/issues/1421)
[#1419]: (https://github.com/dusk-network/dusk-blockchain/issues/1419)
[#1418]: (https://github.com/dusk-network/dusk-blockchain/issues/1418)
[#1416]: (https://github.com/dusk-network/dusk-blockchain/issues/1416)
Expand Down
14 changes: 14 additions & 0 deletions pkg/core/data/ipc/transactions/payload_decoded.go
Original file line number Diff line number Diff line change
Expand Up @@ -193,3 +193,17 @@ func UnmarshalTransactionPayloadDecoded(r *bytes.Buffer, f *TransactionPayloadDe

return nil
}

// EstimatedGasSpent return the estimated amount of Gas that a transaction will
// spend. For now it uses `t.Call` to determine if it's a normal transfer or it
// is an intercontract call.
// TODO: further improvement should take care of the contractAddress to compare
// it against historical data.
func (p *TransactionPayloadDecoded) EstimatedGasSpent() uint64 {
if p.Call == nil {
// This is a normal transfer transaction
return 300_000_000
}
// This is an inter-contract call
return 1_200_000_000
}
31 changes: 27 additions & 4 deletions pkg/core/mempool/mempool.go
Original file line number Diff line number Diff line change
Expand Up @@ -436,6 +436,7 @@ func (m Mempool) processGetMempoolTxsRequest(r rpcbus.Request) (interface{}, err
// processGetMempoolTxsBySizeRequest returns a subset of verified mempool txs which
// 1. contains only highest fee txs
// 2. has total txs size not bigger than maxTxsSize (request param)
// 3. has total txs EstimatedGasSpent not bigger than BlockGasLimit+10%
// Called by BlockGenerator on generating a new candidate block.
func (m Mempool) processGetMempoolTxsBySizeRequest(r rpcbus.Request) (interface{}, error) {
// Read maxTxsSize param
Expand All @@ -447,19 +448,41 @@ func (m Mempool) processGetMempoolTxsBySizeRequest(r rpcbus.Request) (interface{
}

txs := make([]transactions.ContractCall, 0)
gasLimit := config.Get().State.BlockGasLimit
// The slippageGasLimit is the threshold that consider the "estimated gas
// spent" acceptable even if it exceeds the strict GasLimit. This is
// required to avoid to iterate the whole meempol until it fit perfectly
// the block GasLimit
slippageGasLimit := gasLimit + gasLimit/10

var totalSize uint32
var totalGas uint64

err := m.verified.RangeSort(func(k txHash, t TxDesc) (bool, error) {
var done bool
totalSize += uint32(t.size)
decoded, err := t.tx.Decode()
if err != nil {
// Cannot decode, skip the tx.
// This should never happen, keeping `err` to log it properly`
return false, err
}

totalGas += decoded.EstimatedGasSpent()
if totalGas > slippageGasLimit {
// Total gas exceeded the slippage threshold, skip the tx
return false, nil
}

totalSize += uint32(t.size)
if totalSize <= maxTxsSize {
txs = append(txs, t.tx)
} else {
done = true
}

// We stop to iterate the mempool if:
// 1. The totalGas exceeded the gasLimit (but still below the slippage
// threshold)
// 2. The totalSize exceeds the limit
done := totalGas >= gasLimit || totalSize >= maxTxsSize

return done, nil
})
if err != nil {
Expand Down

0 comments on commit 9b31476

Please sign in to comment.