Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve mempool extraction #1422

Merged
merged 1 commit into from
Jun 20, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
herr-seppia marked this conversation as resolved.
Show resolved Hide resolved
}
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
herr-seppia marked this conversation as resolved.
Show resolved Hide resolved

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