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

core/txpool/legacypool: use uint256.Int instead of big.Int #28606

Merged
merged 6 commits into from
Feb 13, 2024

Conversation

weiihann
Copy link
Contributor

@weiihann weiihann commented Nov 25, 2023

Related issue: #28577

This PR changes the use of big.Int to uint256.Int in the legacypool package. The changes are made primarily only on the internal functions of legacypool, without touching other dependencies and related interfaces.

@weiihann
Copy link
Contributor Author

Just realized this has been done in this PR: #28598

Closing this :)

@weiihann weiihann closed this Nov 25, 2023
@jwasinger
Copy link
Contributor

#28598 doesn't implement these changes (it just touches the legacypool because it changes the statedb API to not use big.Int)

@jwasinger jwasinger reopened this Nov 25, 2023
@fjl fjl changed the title legacypool: use uint256 instead of big legacypool: use uint256 instead of big~ Dec 5, 2023
@fjl fjl changed the title legacypool: use uint256 instead of big~ core/txpool/legacypool: use uint256.Int instead of big.Int Dec 5, 2023
@@ -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 {
Copy link
Contributor

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

Copy link
Contributor

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?

Copy link
Contributor Author

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

@@ -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 {
Copy link
Contributor

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.

Copy link
Contributor Author

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.

@@ -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()))
Copy link
Contributor

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

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 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.

@weiihann weiihann force-pushed the txpool-uint256 branch 3 times, most recently from ba22f0a to 943a771 Compare January 25, 2024 02:36
// 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 {
Copy link
Contributor

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

Copy link
Contributor

@holiman holiman left a comment

Choose a reason for hiding this comment

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

LGTM

@holiman holiman merged commit beb2954 into ethereum:master Feb 13, 2024
3 checks passed
@holiman holiman added this to the 1.13.13 milestone Feb 13, 2024
jorgemmsilva pushed a commit to iotaledger/go-ethereum that referenced this pull request Jun 17, 2024
…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]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants