Merge branch 'ethereum:master' into portal

This commit is contained in:
Chen Kai 2024-02-23 14:35:53 +08:00 committed by GitHub
commit c8d7aa56ce
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 75 additions and 79 deletions

View file

@ -919,7 +919,7 @@ func (api *API) TraceCall(ctx context.Context, args ethapi.TransactionArgs, bloc
config.BlockOverrides.Apply(&vmctx) config.BlockOverrides.Apply(&vmctx)
} }
// Execute the trace // Execute the trace
msg, err := args.ToMessage(api.backend.RPCGasCap(), block.BaseFee()) msg, err := args.ToMessage(api.backend.RPCGasCap(), vmctx.BaseFee)
if err != nil { if err != nil {
return nil, err return nil, err
} }

View file

@ -453,7 +453,7 @@ func (s *PersonalAccountAPI) signTransaction(ctx context.Context, args *Transact
return nil, err return nil, err
} }
// Set some sanity defaults and terminate on failure // Set some sanity defaults and terminate on failure
if err := args.setDefaults(ctx, s.b); err != nil { if err := args.setDefaults(ctx, s.b, false); err != nil {
return nil, err return nil, err
} }
// Assemble the transaction and sign with the wallet // Assemble the transaction and sign with the wallet
@ -1093,14 +1093,14 @@ func doCall(ctx context.Context, b Backend, args TransactionArgs, state *state.S
defer cancel() defer cancel()
// Get a new instance of the EVM. // Get a new instance of the EVM.
msg, err := args.ToMessage(globalGasCap, header.BaseFee)
if err != nil {
return nil, err
}
blockCtx := core.NewEVMBlockContext(header, NewChainContext(ctx, b), nil) blockCtx := core.NewEVMBlockContext(header, NewChainContext(ctx, b), nil)
if blockOverrides != nil { if blockOverrides != nil {
blockOverrides.Apply(&blockCtx) blockOverrides.Apply(&blockCtx)
} }
msg, err := args.ToMessage(globalGasCap, blockCtx.BaseFee)
if err != nil {
return nil, err
}
evm := b.GetEVM(ctx, msg, state, header, &vm.Config{NoBaseFee: true}, &blockCtx) evm := b.GetEVM(ctx, msg, state, header, &vm.Config{NoBaseFee: true}, &blockCtx)
// Wait for the context to be done and cancel the evm. Even if the // Wait for the context to be done and cancel the evm. Even if the
@ -1485,14 +1485,9 @@ func AccessList(ctx context.Context, b Backend, blockNrOrHash rpc.BlockNumberOrH
if db == nil || err != nil { if db == nil || err != nil {
return nil, 0, nil, err return nil, 0, nil, err
} }
// If the gas amount is not set, default to RPC gas cap.
if args.Gas == nil {
tmp := hexutil.Uint64(b.RPCGasCap())
args.Gas = &tmp
}
// Ensure any missing fields are filled, extract the recipient and input data // Ensure any missing fields are filled, extract the recipient and input data
if err := args.setDefaults(ctx, b); err != nil { if err := args.setDefaults(ctx, b, true); err != nil {
return nil, 0, nil, err return nil, 0, nil, err
} }
var to common.Address var to common.Address
@ -1795,7 +1790,7 @@ func (s *TransactionAPI) SendTransaction(ctx context.Context, args TransactionAr
} }
// Set some sanity defaults and terminate on failure // Set some sanity defaults and terminate on failure
if err := args.setDefaults(ctx, s.b); err != nil { if err := args.setDefaults(ctx, s.b, false); err != nil {
return common.Hash{}, err return common.Hash{}, err
} }
// Assemble the transaction and sign with the wallet // Assemble the transaction and sign with the wallet
@ -1815,7 +1810,7 @@ func (s *TransactionAPI) FillTransaction(ctx context.Context, args TransactionAr
args.blobSidecarAllowed = true args.blobSidecarAllowed = true
// Set some sanity defaults and terminate on failure // Set some sanity defaults and terminate on failure
if err := args.setDefaults(ctx, s.b); err != nil { if err := args.setDefaults(ctx, s.b, false); err != nil {
return nil, err return nil, err
} }
// Assemble the transaction and obtain rlp // Assemble the transaction and obtain rlp
@ -1884,7 +1879,7 @@ func (s *TransactionAPI) SignTransaction(ctx context.Context, args TransactionAr
if args.Nonce == nil { if args.Nonce == nil {
return nil, errors.New("nonce not specified") return nil, errors.New("nonce not specified")
} }
if err := args.setDefaults(ctx, s.b); err != nil { if err := args.setDefaults(ctx, s.b, false); err != nil {
return nil, err return nil, err
} }
// Before actually sign the transaction, ensure the transaction fee is reasonable. // Before actually sign the transaction, ensure the transaction fee is reasonable.
@ -1933,7 +1928,7 @@ func (s *TransactionAPI) Resend(ctx context.Context, sendArgs TransactionArgs, g
if sendArgs.Nonce == nil { if sendArgs.Nonce == nil {
return common.Hash{}, errors.New("missing transaction nonce in transaction spec") return common.Hash{}, errors.New("missing transaction nonce in transaction spec")
} }
if err := sendArgs.setDefaults(ctx, s.b); err != nil { if err := sendArgs.setDefaults(ctx, s.b, false); err != nil {
return common.Hash{}, err return common.Hash{}, err
} }
matchTx := sendArgs.toTransaction() matchTx := sendArgs.toTransaction()

View file

@ -96,7 +96,7 @@ func (args *TransactionArgs) data() []byte {
} }
// setDefaults fills in default values for unspecified tx fields. // setDefaults fills in default values for unspecified tx fields.
func (args *TransactionArgs) setDefaults(ctx context.Context, b Backend) error { func (args *TransactionArgs) setDefaults(ctx context.Context, b Backend, skipGasEstimation bool) error {
if err := args.setBlobTxSidecar(ctx, b); err != nil { if err := args.setBlobTxSidecar(ctx, b); err != nil {
return err return err
} }
@ -136,30 +136,35 @@ func (args *TransactionArgs) setDefaults(ctx context.Context, b Backend) error {
} }
} }
// Estimate the gas usage if necessary.
if args.Gas == nil { if args.Gas == nil {
// These fields are immutable during the estimation, safe to if skipGasEstimation { // Skip gas usage estimation if a precise gas limit is not critical, e.g., in non-transaction calls.
// pass the pointer directly. gas := hexutil.Uint64(b.RPCGasCap())
data := args.data() if gas == 0 {
callArgs := TransactionArgs{ gas = hexutil.Uint64(math.MaxUint64 / 2)
From: args.From, }
To: args.To, args.Gas = &gas
GasPrice: args.GasPrice, } else { // Estimate the gas usage otherwise.
MaxFeePerGas: args.MaxFeePerGas, // These fields are immutable during the estimation, safe to
MaxPriorityFeePerGas: args.MaxPriorityFeePerGas, // pass the pointer directly.
Value: args.Value, data := args.data()
Data: (*hexutil.Bytes)(&data), callArgs := TransactionArgs{
AccessList: args.AccessList, From: args.From,
BlobFeeCap: args.BlobFeeCap, To: args.To,
BlobHashes: args.BlobHashes, GasPrice: args.GasPrice,
MaxFeePerGas: args.MaxFeePerGas,
MaxPriorityFeePerGas: args.MaxPriorityFeePerGas,
Value: args.Value,
Data: (*hexutil.Bytes)(&data),
AccessList: args.AccessList,
}
latestBlockNr := rpc.BlockNumberOrHashWithNumber(rpc.LatestBlockNumber)
estimated, err := DoEstimateGas(ctx, b, callArgs, latestBlockNr, nil, b.RPCGasCap())
if err != nil {
return err
}
args.Gas = &estimated
log.Trace("Estimate gas usage automatically", "gas", args.Gas)
} }
latestBlockNr := rpc.BlockNumberOrHashWithNumber(rpc.LatestBlockNumber)
estimated, err := DoEstimateGas(ctx, b, callArgs, latestBlockNr, nil, b.RPCGasCap())
if err != nil {
return err
}
args.Gas = &estimated
log.Trace("Estimate gas usage automatically", "gas", args.Gas)
} }
// If chain id is provided, ensure it matches the local chain id. Otherwise, set the local // If chain id is provided, ensure it matches the local chain id. Otherwise, set the local
@ -177,6 +182,14 @@ func (args *TransactionArgs) setDefaults(ctx context.Context, b Backend) error {
// setFeeDefaults fills in default fee values for unspecified tx fields. // setFeeDefaults fills in default fee values for unspecified tx fields.
func (args *TransactionArgs) setFeeDefaults(ctx context.Context, b Backend) error { func (args *TransactionArgs) setFeeDefaults(ctx context.Context, b Backend) error {
head := b.CurrentHeader()
// Sanity check the EIP-4844 fee parameters.
if args.BlobFeeCap != nil && args.BlobFeeCap.ToInt().Sign() == 0 {
return errors.New("maxFeePerBlobGas, if specified, must be non-zero")
}
if err := args.setCancunFeeDefaults(ctx, head, b); err != nil {
return err
}
// If both gasPrice and at least one of the EIP-1559 fee parameters are specified, error. // If both gasPrice and at least one of the EIP-1559 fee parameters are specified, error.
if args.GasPrice != nil && (args.MaxFeePerGas != nil || args.MaxPriorityFeePerGas != nil) { if args.GasPrice != nil && (args.MaxFeePerGas != nil || args.MaxPriorityFeePerGas != nil) {
return errors.New("both gasPrice and (maxFeePerGas or maxPriorityFeePerGas) specified") return errors.New("both gasPrice and (maxFeePerGas or maxPriorityFeePerGas) specified")
@ -186,7 +199,6 @@ func (args *TransactionArgs) setFeeDefaults(ctx context.Context, b Backend) erro
// other tx values. See https://github.com/ethereum/go-ethereum/pull/23274 // other tx values. See https://github.com/ethereum/go-ethereum/pull/23274
// for more information. // for more information.
eip1559ParamsSet := args.MaxFeePerGas != nil && args.MaxPriorityFeePerGas != nil eip1559ParamsSet := args.MaxFeePerGas != nil && args.MaxPriorityFeePerGas != nil
// Sanity check the EIP-1559 fee parameters if present. // Sanity check the EIP-1559 fee parameters if present.
if args.GasPrice == nil && eip1559ParamsSet { if args.GasPrice == nil && eip1559ParamsSet {
if args.MaxFeePerGas.ToInt().Sign() == 0 { if args.MaxFeePerGas.ToInt().Sign() == 0 {
@ -198,13 +210,7 @@ func (args *TransactionArgs) setFeeDefaults(ctx context.Context, b Backend) erro
return nil // No need to set anything, user already set MaxFeePerGas and MaxPriorityFeePerGas return nil // No need to set anything, user already set MaxFeePerGas and MaxPriorityFeePerGas
} }
// Sanity check the EIP-4844 fee parameters.
if args.BlobFeeCap != nil && args.BlobFeeCap.ToInt().Sign() == 0 {
return errors.New("maxFeePerBlobGas must be non-zero")
}
// Sanity check the non-EIP-1559 fee parameters. // Sanity check the non-EIP-1559 fee parameters.
head := b.CurrentHeader()
isLondon := b.ChainConfig().IsLondon(head.Number) isLondon := b.ChainConfig().IsLondon(head.Number)
if args.GasPrice != nil && !eip1559ParamsSet { if args.GasPrice != nil && !eip1559ParamsSet {
// Zero gas-price is not allowed after London fork // Zero gas-price is not allowed after London fork
@ -215,21 +221,14 @@ func (args *TransactionArgs) setFeeDefaults(ctx context.Context, b Backend) erro
} }
// Now attempt to fill in default value depending on whether London is active or not. // Now attempt to fill in default value depending on whether London is active or not.
if b.ChainConfig().IsCancun(head.Number, head.Time) { if isLondon {
if err := args.setCancunFeeDefaults(ctx, head, b); err != nil {
return err
}
} else if isLondon {
if args.BlobFeeCap != nil {
return errors.New("maxFeePerBlobGas is not valid before Cancun is active")
}
// London is active, set maxPriorityFeePerGas and maxFeePerGas. // London is active, set maxPriorityFeePerGas and maxFeePerGas.
if err := args.setLondonFeeDefaults(ctx, head, b); err != nil { if err := args.setLondonFeeDefaults(ctx, head, b); err != nil {
return err return err
} }
} else { } else {
if args.MaxFeePerGas != nil || args.MaxPriorityFeePerGas != nil || args.BlobFeeCap != nil { if args.MaxFeePerGas != nil || args.MaxPriorityFeePerGas != nil {
return errors.New("maxFeePerGas and maxPriorityFeePerGas and maxFeePerBlobGas are not valid before London is active") return errors.New("maxFeePerGas and maxPriorityFeePerGas are not valid before London is active")
} }
// London not active, set gas price. // London not active, set gas price.
price, err := b.SuggestGasTipCap(ctx) price, err := b.SuggestGasTipCap(ctx)
@ -245,15 +244,19 @@ func (args *TransactionArgs) setFeeDefaults(ctx context.Context, b Backend) erro
func (args *TransactionArgs) setCancunFeeDefaults(ctx context.Context, head *types.Header, b Backend) error { func (args *TransactionArgs) setCancunFeeDefaults(ctx context.Context, head *types.Header, b Backend) error {
// Set maxFeePerBlobGas if it is missing. // Set maxFeePerBlobGas if it is missing.
if args.BlobHashes != nil && args.BlobFeeCap == nil { if args.BlobHashes != nil && args.BlobFeeCap == nil {
var excessBlobGas uint64
if head.ExcessBlobGas != nil {
excessBlobGas = *head.ExcessBlobGas
}
// ExcessBlobGas must be set for a Cancun block. // ExcessBlobGas must be set for a Cancun block.
blobBaseFee := eip4844.CalcBlobFee(*head.ExcessBlobGas) blobBaseFee := eip4844.CalcBlobFee(excessBlobGas)
// Set the max fee to be 2 times larger than the previous block's blob base fee. // Set the max fee to be 2 times larger than the previous block's blob base fee.
// The additional slack allows the tx to not become invalidated if the base // The additional slack allows the tx to not become invalidated if the base
// fee is rising. // fee is rising.
val := new(big.Int).Mul(blobBaseFee, big.NewInt(2)) val := new(big.Int).Mul(blobBaseFee, big.NewInt(2))
args.BlobFeeCap = (*hexutil.Big)(val) args.BlobFeeCap = (*hexutil.Big)(val)
} }
return args.setLondonFeeDefaults(ctx, head, b) return nil
} }
// setLondonFeeDefaults fills in reasonable default fee values for unspecified fields. // setLondonFeeDefaults fills in reasonable default fee values for unspecified fields.

View file

@ -153,14 +153,14 @@ func TestSetFeeDefaults(t *testing.T) {
"legacy", "legacy",
&TransactionArgs{MaxFeePerGas: maxFee}, &TransactionArgs{MaxFeePerGas: maxFee},
nil, nil,
errors.New("maxFeePerGas and maxPriorityFeePerGas and maxFeePerBlobGas are not valid before London is active"), errors.New("maxFeePerGas and maxPriorityFeePerGas are not valid before London is active"),
}, },
{ {
"dynamic fee tx pre-London, priorityFee set", "dynamic fee tx pre-London, priorityFee set",
"legacy", "legacy",
&TransactionArgs{MaxPriorityFeePerGas: fortytwo}, &TransactionArgs{MaxPriorityFeePerGas: fortytwo},
nil, nil,
errors.New("maxFeePerGas and maxPriorityFeePerGas and maxFeePerBlobGas are not valid before London is active"), errors.New("maxFeePerGas and maxPriorityFeePerGas are not valid before London is active"),
}, },
{ {
"dynamic fee tx, maxFee < priorityFee", "dynamic fee tx, maxFee < priorityFee",
@ -207,20 +207,6 @@ func TestSetFeeDefaults(t *testing.T) {
errors.New("both gasPrice and (maxFeePerGas or maxPriorityFeePerGas) specified"), errors.New("both gasPrice and (maxFeePerGas or maxPriorityFeePerGas) specified"),
}, },
// EIP-4844 // EIP-4844
{
"set maxFeePerBlobGas pre cancun",
"london",
&TransactionArgs{BlobFeeCap: fortytwo},
nil,
errors.New("maxFeePerBlobGas is not valid before Cancun is active"),
},
{
"set maxFeePerBlobGas pre london",
"legacy",
&TransactionArgs{BlobFeeCap: fortytwo},
nil,
errors.New("maxFeePerGas and maxPriorityFeePerGas and maxFeePerBlobGas are not valid before London is active"),
},
{ {
"set gas price and maxFee for blob transaction", "set gas price and maxFee for blob transaction",
"cancun", "cancun",
@ -235,6 +221,13 @@ func TestSetFeeDefaults(t *testing.T) {
&TransactionArgs{BlobHashes: []common.Hash{}, BlobFeeCap: (*hexutil.Big)(big.NewInt(4)), MaxFeePerGas: maxFee, MaxPriorityFeePerGas: fortytwo}, &TransactionArgs{BlobHashes: []common.Hash{}, BlobFeeCap: (*hexutil.Big)(big.NewInt(4)), MaxFeePerGas: maxFee, MaxPriorityFeePerGas: fortytwo},
nil, nil,
}, },
{
"fill maxFeePerBlobGas when dynamic fees are set",
"cancun",
&TransactionArgs{BlobHashes: []common.Hash{}, MaxFeePerGas: maxFee, MaxPriorityFeePerGas: fortytwo},
&TransactionArgs{BlobHashes: []common.Hash{}, BlobFeeCap: (*hexutil.Big)(big.NewInt(4)), MaxFeePerGas: maxFee, MaxPriorityFeePerGas: fortytwo},
nil,
},
} }
ctx := context.Background() ctx := context.Background()
@ -244,11 +237,16 @@ func TestSetFeeDefaults(t *testing.T) {
} }
got := test.in got := test.in
err := got.setFeeDefaults(ctx, b) err := got.setFeeDefaults(ctx, b)
if err != nil && err.Error() == test.err.Error() { if err != nil {
// Test threw expected error. if test.err == nil {
t.Fatalf("test %d (%s): unexpected error: %s", i, test.name, err)
} else if err.Error() != test.err.Error() {
t.Fatalf("test %d (%s): unexpected error: (got: %s, want: %s)", i, test.name, err, test.err)
}
// Matching error.
continue continue
} else if err != nil { } else if test.err != nil {
t.Fatalf("test %d (%s): unexpected error: %s", i, test.name, err) t.Fatalf("test %d (%s): expected error: %s", i, test.name, test.err)
} }
if !reflect.DeepEqual(got, test.want) { if !reflect.DeepEqual(got, test.want) {
t.Fatalf("test %d (%s): did not fill defaults as expected: (got: %v, want: %v)", i, test.name, got, test.want) t.Fatalf("test %d (%s): did not fill defaults as expected: (got: %v, want: %v)", i, test.name, got, test.want)

View file

@ -23,7 +23,7 @@ import (
const ( const (
VersionMajor = 1 // Major version component of the current release VersionMajor = 1 // Major version component of the current release
VersionMinor = 13 // Minor version component of the current release VersionMinor = 13 // Minor version component of the current release
VersionPatch = 13 // Patch version component of the current release VersionPatch = 14 // Patch version component of the current release
VersionMeta = "unstable" // Version metadata to append to the version string VersionMeta = "unstable" // Version metadata to append to the version string
) )