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

all: switch gas limits from big.Int to uint64 #15466

Merged
merged 1 commit into from
Jan 3, 2018

Conversation

karalabe
Copy link
Member

This PR continues the uint64 gas rewrite from a long time ago, by transitioning all remaining types (headers, blocks, receipts, transactions) to 64 bit gas fields. This is mostly needed to unify our APIs (old APIs use big.Ints, new ones use uint64s).

I also need this PR to revamp gas estimation (currently it's not automatic if no gas limit is provided), but that turned out to require yet again a large API overhaul, so I'll just submit this massive PR first, and follow up with the API reorgs afterwards.

@karalabe karalabe requested review from holiman, fjl and Arachnid November 13, 2017 11:58
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

contrib := new(big.Int).Mul(parent.GasUsed(), big.NewInt(3))
contrib = contrib.Div(contrib, big.NewInt(2))
contrib = contrib.Div(contrib, params.GasLimitBoundDivisor)
contrib := ((parent.GasUsed() / 2) * 3) / params.GasLimitBoundDivisor // div-mul order change is fine due to last 10 bit shift
Copy link
Contributor

Choose a reason for hiding this comment

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

Instead of changing the order, you could do

	(x/2 * 3) / 1024
eq	(x/2 + x) / 1024

Which could be further reduced to

	(x >> 1 + x) >> 10
or     x >> 11 + x >> 10

Where it's trivial to see that the last expression is overflow-safe.

If you want to maintain the params, you could do

parent.gasUsed / params.GasLimitBoundDivisor + (parent.gasUsed / 2 ) / params.GasLimitBoundDivisor

Copy link
Member Author

Choose a reason for hiding this comment

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

Fixed to your first suggestion.

Copy link
Contributor

Choose a reason for hiding this comment

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

So that means that there will be errors if gasUsed ever goes above 2/3 of a full uint64?

Which I think we can live with

i.Add(i, amount)
func (gp *GasPool) AddGas(amount uint64) *GasPool {
if uint64(*gp) > math.MaxUint64-amount {
panic("gas pool pushed above uint64")
Copy link
Contributor

Choose a reason for hiding this comment

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

I'm not sure a panic is right here - perhaps can be used to cause DoS over RPC, by performing calls with very large gas values?

@ricardohsd
Copy link
Contributor

Maybe I don't see the big picture. But wouldn't be better to create a type alias for gas? Then this kind of refactoring would be minimized.

@fjl
Copy link
Contributor

fjl commented Nov 27, 2017

@ricardohsd *big.Int -> uint64 needs changes beyond changing the actual type.

@ricardohsd
Copy link
Contributor

@fjl that's true. Thanks for the explanation.

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.

4 participants