-
Notifications
You must be signed in to change notification settings - Fork 20.2k
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
core/txpool/legacypool: use uint256.Int instead of big.Int #28606
Conversation
Just realized this has been done in this PR: #28598 Closing this :) |
#28598 doesn't implement these changes (it just touches the legacypool because it changes the statedb API to not use |
60e9d06
to
b796c60
Compare
b796c60
to
00e93da
Compare
@@ -198,7 +199,7 @@ func validatePoolInternals(pool *LegacyPool) error { | |||
if nonce := pool.pendingNonces.get(addr); nonce != last+1 { | |||
return fmt.Errorf("pending nonce mismatch: have %v, want %v", nonce, last+1) | |||
} | |||
if txs.totalcost.Cmp(common.Big0) < 0 { | |||
if txs.totalcost.Cmp(common.Uint0) < 0 { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
uint256.Int has IsZero
, iirc
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
oh btw uint256 cannot be negative, so this whole clause needs to be replace dwith something else. Probably overflow-check?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
sorry for the late reply, I've added the new changes to check for overflow
00e93da
to
3e1015f
Compare
@@ -198,8 +200,8 @@ func validatePoolInternals(pool *LegacyPool) error { | |||
if nonce := pool.pendingNonces.get(addr); nonce != last+1 { | |||
return fmt.Errorf("pending nonce mismatch: have %v, want %v", nonce, last+1) | |||
} | |||
if txs.totalcost.Cmp(common.Big0) < 0 { | |||
return fmt.Errorf("totalcost went negative: %v", txs.totalcost) | |||
if txs.totalcost.Cmp(max) > 0 { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This does not make sense. A uint256.Int
is limited between 0
and max
(ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff
). It is inherently not possible for it to go above max
.
Making a change like this, where we swap out a type is not just a matter of making it compile and done. You need to actually spend some time considering the consequences of the changes. Some checks may no longer be required, and in some places maybe we need to add new checks.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
My bad, I should've been more careful. In my opinion, we can remove this check since it's not possible to check for an overflow here.
core/txpool/legacypool/list.go
Outdated
@@ -456,7 +458,7 @@ func (l *list) LastElement() *types.Transaction { | |||
// total cost of all transactions. | |||
func (l *list) subTotalCost(txs []*types.Transaction) { | |||
for _, tx := range txs { | |||
l.totalcost.Sub(l.totalcost, tx.Cost()) | |||
l.totalcost.Sub(l.totalcost, uint256.MustFromBig(tx.Cost())) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This call to Sub
can now overflow, which it could not previously. This can be handled in a couple of ways:
- Ignore it (what this PR does)
- Set to zero on overflow, instead of near infinite
panic
on overflow
Not sure what approach is the best, but it's definitely a point to consider
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think going with panic
on overflow is the best approach here. Considering the current uses of subTotalCost
, it subtracts the total cost of previously added txs, so it should not be possible to underflow.
ba22f0a
to
943a771
Compare
943a771
to
af03289
Compare
core/txpool/legacypool/list.go
Outdated
// Otherwise overwrite the old transaction with the current one | ||
l.txs.Put(tx) | ||
if cost := tx.Cost(); l.costcap.Cmp(cost) < 0 { | ||
if cost := uint256.MustFromBig(tx.Cost()); l.costcap.Cmp(cost) < 0 { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The Add
function is a public function on the list. It should not panic
, which it does if you use MustFromBig
like this. I'm adding a testcase and a fix
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
a01d18e
to
0099b05
Compare
eae806f
to
a5962f1
Compare
…28606) This change makes the legacy transaction pool use of `uint256.Int` instead of `big.Int`. The changes are made primarily only on the internal functions of legacypool. --------- Co-authored-by: Martin Holst Swende <[email protected]>
Related issue: #28577
This PR changes the use of
big.Int
touint256.Int
in the legacypool package. The changes are made primarily only on the internal functions of legacypool, without touching other dependencies and related interfaces.