Skip to content

mempool: Filter new txs if they have insufficient gas#2385

Merged
xla merged 10 commits intodevelopfrom
dev/add_gas_filter
Sep 22, 2018
Merged

mempool: Filter new txs if they have insufficient gas#2385
xla merged 10 commits intodevelopfrom
dev/add_gas_filter

Conversation

@ValarDragon
Copy link
Contributor

This also refactors the prior mempool to filter to be known as
"precheck filter" and this new filter is called "postcheck filter"

This PR also fixes a bug where the precheck filter previously didn't
account for the amino overhead, which could a maliciously sized tx to
halt blocks from getting any txs in them.

ref #2310

  • Updated all relevant documentation in docs
  • Updated all code comments where relevant
  • Wrote tests
  • Updated CHANGELOG_PENDING.md - changelog covered by previous PR

This also refactors the prior mempool to filter to be known as
"precheck filter" and this new filter is called "postcheck filter"

This PR also fixes a bug where the precheck filter previously didn't
account for the amino overhead, which could a maliciously sized tx to
halt blocks from getting any txs in them.
@codecov-io
Copy link

codecov-io commented Sep 13, 2018

Codecov Report

Merging #2385 into develop will decrease coverage by 0.04%.
The diff coverage is 59.21%.

@@             Coverage Diff             @@
##           develop    #2385      +/-   ##
===========================================
- Coverage    61.67%   61.62%   -0.05%     
===========================================
  Files          198      198              
  Lines        16312    16356      +44     
===========================================
+ Hits         10060    10080      +20     
- Misses        5419     5446      +27     
+ Partials       833      830       -3
Impacted Files Coverage Δ
node/node.go 65.04% <100%> (+0.82%) ⬆️
mempool/mempool.go 74.63% <35.48%> (-1.47%) ⬇️
state/services.go 42.85% <46.15%> (+4.39%) ⬆️
state/execution.go 76.66% <80.95%> (+1.15%) ⬆️
consensus/reactor.go 72.1% <0%> (-1.32%) ⬇️
consensus/state.go 77.27% <0%> (-0.6%) ⬇️
privval/socket.go 71.65% <0%> (+0.17%) ⬆️

node/node.go Outdated
return (len(tx) + aminoOverhead) <= maxDataBytes
}),
mempl.WithPostCheckFilter(func(tx types.Tx, res *abci.ResponseCheckTx) bool {
maxGas := state.ConsensusParams.BlockSize.MaxGas
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't this be outside? Otherwise there can be a DATA RACE. Note when it's outside, it's a copy. When inside, you're accessing state without mutex.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're right, thanks for catching this :)

return (len(tx) + aminoOverhead) <= maxDataBytes
}
postFilter := func(tx types.Tx, res *abci.ResponseCheckTx) bool {
maxGas := state.ConsensusParams.MaxGas
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same

Copy link
Contributor

@xla xla left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some changes of how we expose the filters and set them up.

// This is called before each check tx
preCheckFilter func(types.Tx) bool
// Filter mempool to only accept txs for which postCheckFilter(tx) returns true.
postCheckFilter func(types.Tx, *abci.ResponseCheckTx) bool
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These should be typed a la:

type preCheckFilterFunc func(types.Tx) bool

type postCheckFilterFunc func(types.Tx, *abci.ResponseCheckTx) bool

Likely also made public if the option functions are called from outside the mempool with it.

@xla xla added C:mempool Component: Mempool T:enhancement Type: Enhancement labels Sep 18, 2018
@xla xla added this to the launch milestone Sep 18, 2018
@xla xla changed the title mempool: Make the mempool filter new txs if they have insufficient gas mempool: Filter new txs if they have insufficient gas Sep 21, 2018
@ebuchman ebuchman mentioned this pull request Sep 21, 2018
}),
mempl.WithPreCheckFilter(
mempool.PreCheckAminoMaxBytes(
types.MaxDataBytesUnknownEvidence(
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this change makes it harder to read than having maxDataBytes defined above

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this a comment regarding your personal taste or is there strong evidence how one is superior to the other?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Personal preference, though I believe its founded on strong evidence. 3 function calls is less clear than two function calls on an explicitly defined variable name.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The hierarchical nesting can be followed step-by-step to understand what is invoced, where with a variable one has to backtrack. Addtionally the scope of the NewNode function is already quite large, so avoiding adding more variables is desriable. Either case should be fine, if you have strong aversion towards the current formatting we can change it.

Copy link
Contributor Author

@ValarDragon ValarDragon Sep 22, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nah no need to revert, I don't have a strong opinion :)

NewNode being large is a good point!

@xla xla self-assigned this Sep 22, 2018
@xla xla merged commit 111e627 into develop Sep 22, 2018
@xla xla deleted the dev/add_gas_filter branch September 22, 2018 00:50
@ebuchman ebuchman mentioned this pull request Sep 23, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

C:mempool Component: Mempool T:enhancement Type: Enhancement

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants