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/vm: set basefee to 0 internally on eth_call #28470

Merged
merged 8 commits into from
Nov 8, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
8 changes: 6 additions & 2 deletions core/vm/eips.go
Original file line number Diff line number Diff line change
Expand Up @@ -215,8 +215,12 @@ func opTstore(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]b

// opBaseFee implements BASEFEE opcode
func opBaseFee(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) {
baseFee, _ := uint256.FromBig(interpreter.evm.Context.BaseFee)
scope.Stack.push(baseFee)
if interpreter.evm.Config.NoBaseFee {
scope.Stack.push(new(uint256.Int))
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 checking for NoBaseFee in the production opcode, can't we just set the interpreter.evm.Context.BaseFee to a new(uint256.Int) in the NoBaseFee-situation?

Copy link
Member Author

Choose a reason for hiding this comment

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

I did consider that too. The only catch is that some code paths feed us the context directly, and we're not copying currently, so setting the basefee would modify the passed in context. We'd need to add some copying to avoid that.

Copy link
Contributor

Choose a reason for hiding this comment

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

Like this

diff --git a/core/vm/eips.go b/core/vm/eips.go
index 44d4ba9cff..35f0a3f7c2 100644
--- a/core/vm/eips.go
+++ b/core/vm/eips.go
@@ -215,12 +215,8 @@ func opTstore(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]b
 
 // opBaseFee implements BASEFEE opcode
 func opBaseFee(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) {
-	if interpreter.evm.Config.NoBaseFee {
-		scope.Stack.push(new(uint256.Int))
-	} else {
-		baseFee, _ := uint256.FromBig(interpreter.evm.Context.BaseFee)
-		scope.Stack.push(baseFee)
-	}
+	baseFee, _ := uint256.FromBig(interpreter.evm.Context.BaseFee)
+	scope.Stack.push(baseFee)
 	return nil, nil
 }
 
diff --git a/core/vm/evm.go b/core/vm/evm.go
index 2c6cc7d484..1f84ff7660 100644
--- a/core/vm/evm.go
+++ b/core/vm/evm.go
@@ -133,6 +133,9 @@ func NewEVM(blockCtx BlockContext, txCtx TxContext, statedb StateDB, chainConfig
 		chainConfig: chainConfig,
 		chainRules:  chainConfig.Rules(blockCtx.BlockNumber, blockCtx.Random != nil, blockCtx.Time),
 	}
+	if config.NoBaseFee {
+		evm.Context.BaseFee = new(big.Int)
+	}
 	evm.interpreter = NewEVMInterpreter(evm)
 	return evm
 }

Copy link
Contributor

Choose a reason for hiding this comment

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

I did consider that too.

Ah I didn't see this comment. But I don't think you are quite correct -- the blockCtx is passed by value, not ref, in the path I used.

Copy link
Member

Choose a reason for hiding this comment

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

Yeah, I think Martin's approach is better, BaseFee is an environment context.

We can also leave a TODO for BlobBaseFee, we might face the same problem for this opcode too.

Copy link
Member Author

Choose a reason for hiding this comment

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

I'll switch to Martin's approach, but we need to be aware that BaseFee form the context is used in many locations, each potentially becoming a footgun with 0.

Copy link
Member Author

Choose a reason for hiding this comment

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

@rjl493456442 Wrt the blob fee, I think we can piggie-back on the same flag. BaseFee, BlobBaseFee, the concept is the same and setting it to zero should also be the same.

Copy link
Member Author

Choose a reason for hiding this comment

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

Ugh, no, Martin's fix isn't good (neither is mine).

Currently the logic is that we only disable base fee checks if 1) NoBaseFee is set and 2) gas prices are 0. Both our suggested fixes unilaterally nuke it out always, which is bad.

Martin's proposal cannot work because you don't know during construction time if tx will be run with or without gas price. My solution could work if I pulled the gas price up too, but it's a bit ugh. Maybe a cleaner solution is to set/rest the field before actually executing the bytecode for each tx individually. .Will look into that.

Copy link
Member Author

Choose a reason for hiding this comment

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

Maybe Martin's does work. The EVM is reconstructed for each tx.

} else {
baseFee, _ := uint256.FromBig(interpreter.evm.Context.BaseFee)
scope.Stack.push(baseFee)
}
return nil, nil
}

Expand Down
38 changes: 38 additions & 0 deletions internal/ethapi/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -678,6 +678,44 @@ func TestEstimateGas(t *testing.T) {
},
expectErr: core.ErrInsufficientFunds,
},
// Test for a bug where the gas price was set to zero but the basefee non-zero
//
// contract Tipper {
// constructor() {
// tx.gasprice - block.basefee;
// }
// }
{
blockNumber: rpc.LatestBlockNumber,
call: TransactionArgs{
From: &accounts[0].addr,
Input: hex2Bytes("6080604052348015600f57600080fd5b50483a601a91906058565b506085565b6000819050919050565b7f4e487b7100000000000000000000000000000000000000000000000000000000600052601160045260246000fd5b6000606182601f565b9150606a83601f565b9250828203905081811115607f57607e6029565b5b92915050565b603f8060926000396000f3fe6080604052600080fdfea2646970667358221220389a4956b32bb784f6afd2e9db61cef06edaed0e2609234a291e7830e025853364736f6c63430008160033"),
GasPrice: (*hexutil.Big)(big.NewInt(1_000_000_000)), // Legacy as pricing
},
expectErr: nil,
want: 68757,
},
{
blockNumber: rpc.LatestBlockNumber,
call: TransactionArgs{
From: &accounts[0].addr,
Input: hex2Bytes("6080604052348015600f57600080fd5b50483a601a91906058565b506085565b6000819050919050565b7f4e487b7100000000000000000000000000000000000000000000000000000000600052601160045260246000fd5b6000606182601f565b9150606a83601f565b9250828203905081811115607f57607e6029565b5b92915050565b603f8060926000396000f3fe6080604052600080fdfea2646970667358221220389a4956b32bb784f6afd2e9db61cef06edaed0e2609234a291e7830e025853364736f6c63430008160033"),
MaxFeePerGas: (*hexutil.Big)(big.NewInt(1_000_000_000)), // 1559 gas pricing
},
expectErr: nil,
want: 68757,
},
{
blockNumber: rpc.LatestBlockNumber,
call: TransactionArgs{
From: &accounts[0].addr,
Input: hex2Bytes("6080604052348015600f57600080fd5b50483a601a91906058565b506085565b6000819050919050565b7f4e487b7100000000000000000000000000000000000000000000000000000000600052601160045260246000fd5b6000606182601f565b9150606a83601f565b9250828203905081811115607f57607e6029565b5b92915050565b603f8060926000396000f3fe6080604052600080fdfea2646970667358221220389a4956b32bb784f6afd2e9db61cef06edaed0e2609234a291e7830e025853364736f6c63430008160033"),
GasPrice: nil, // No legacy gas pricing
MaxFeePerGas: nil, // No 1559 gas pricing
},
expectErr: nil,
want: 68757,
},
}
for i, tc := range testSuite {
result, err := api.EstimateGas(context.Background(), tc.call, &rpc.BlockNumberOrHash{BlockNumber: &tc.blockNumber}, &tc.overrides)
Expand Down