pass hooks to precompile call

This commit is contained in:
Tony Chen 2025-04-16 15:18:21 +08:00
parent 5d371b3aed
commit 1a348e12f8
2 changed files with 26 additions and 26 deletions

View file

@ -40,7 +40,7 @@ import (
// contract. // contract.
type PrecompiledContract interface { type PrecompiledContract interface {
RequiredGas(input []byte) uint64 // RequiredPrice calculates the contract gas use RequiredGas(input []byte) uint64 // RequiredPrice calculates the contract gas use
Run(evm *EVM, sender common.Address, callingContract common.Address, input []byte, value *big.Int, readOnly bool, isFromDelegateCall bool) ([]byte, error) // Run runs the precompiled contract Run(evm *EVM, sender common.Address, callingContract common.Address, input []byte, value *big.Int, readOnly bool, isFromDelegateCall bool, logger *tracing.Hooks) ([]byte, error) // Run runs the precompiled contract
} }
type DynamicGasPrecompiledContract interface { type DynamicGasPrecompiledContract interface {
@ -187,7 +187,7 @@ func RunPrecompiledContract(p PrecompiledContract, evm *EVM, sender common.Addre
logger.OnGasChange(suppliedGas, suppliedGas-gasCost, tracing.GasChangeCallPrecompiledContract) logger.OnGasChange(suppliedGas, suppliedGas-gasCost, tracing.GasChangeCallPrecompiledContract)
} }
suppliedGas -= gasCost suppliedGas -= gasCost
output, err := p.Run(evm, sender, callingContract, input, value, readOnly, isFromDelegateCall) output, err := p.Run(evm, sender, callingContract, input, value, readOnly, isFromDelegateCall, logger)
return output, suppliedGas, err return output, suppliedGas, err
} }
@ -198,7 +198,7 @@ func (c *Ecrecover) RequiredGas(input []byte) uint64 {
return params.EcrecoverGas return params.EcrecoverGas
} }
func (c *Ecrecover) Run(_ *EVM, _ common.Address, _ common.Address, input []byte, _ *big.Int, _ bool, _ bool) ([]byte, error) { func (c *Ecrecover) Run(_ *EVM, _ common.Address, _ common.Address, input []byte, _ *big.Int, _ bool, _ bool, _ *tracing.Hooks) ([]byte, error) {
const ecRecoverInputLength = 128 const ecRecoverInputLength = 128
input = common.RightPadBytes(input, ecRecoverInputLength) input = common.RightPadBytes(input, ecRecoverInputLength)
@ -239,7 +239,7 @@ type Sha256hash struct{}
func (c *Sha256hash) RequiredGas(input []byte) uint64 { func (c *Sha256hash) RequiredGas(input []byte) uint64 {
return uint64(len(input)+31)/32*params.Sha256PerWordGas + params.Sha256BaseGas return uint64(len(input)+31)/32*params.Sha256PerWordGas + params.Sha256BaseGas
} }
func (c *Sha256hash) Run(_ *EVM, _ common.Address, _ common.Address, input []byte, _ *big.Int, _ bool, _ bool) ([]byte, error) { func (c *Sha256hash) Run(_ *EVM, _ common.Address, _ common.Address, input []byte, _ *big.Int, _ bool, _ bool, _ *tracing.Hooks) ([]byte, error) {
h := sha256.Sum256(input) h := sha256.Sum256(input)
return h[:], nil return h[:], nil
} }
@ -254,7 +254,7 @@ type Ripemd160hash struct{}
func (c *Ripemd160hash) RequiredGas(input []byte) uint64 { func (c *Ripemd160hash) RequiredGas(input []byte) uint64 {
return uint64(len(input)+31)/32*params.Ripemd160PerWordGas + params.Ripemd160BaseGas return uint64(len(input)+31)/32*params.Ripemd160PerWordGas + params.Ripemd160BaseGas
} }
func (c *Ripemd160hash) Run(_ *EVM, _ common.Address, _ common.Address, input []byte, _ *big.Int, _ bool, _ bool) ([]byte, error) { func (c *Ripemd160hash) Run(_ *EVM, _ common.Address, _ common.Address, input []byte, _ *big.Int, _ bool, _ bool, _ *tracing.Hooks) ([]byte, error) {
ripemd := ripemd160.New() ripemd := ripemd160.New()
ripemd.Write(input) ripemd.Write(input)
return common.LeftPadBytes(ripemd.Sum(nil), 32), nil return common.LeftPadBytes(ripemd.Sum(nil), 32), nil
@ -270,7 +270,7 @@ type DataCopy struct{}
func (c *DataCopy) RequiredGas(input []byte) uint64 { func (c *DataCopy) RequiredGas(input []byte) uint64 {
return uint64(len(input)+31)/32*params.IdentityPerWordGas + params.IdentityBaseGas return uint64(len(input)+31)/32*params.IdentityPerWordGas + params.IdentityBaseGas
} }
func (c *DataCopy) Run(_ *EVM, _ common.Address, _ common.Address, in []byte, _ *big.Int, _ bool, _ bool) ([]byte, error) { func (c *DataCopy) Run(_ *EVM, _ common.Address, _ common.Address, in []byte, _ *big.Int, _ bool, _ bool, _ *tracing.Hooks) ([]byte, error) {
return common.CopyBytes(in), nil return common.CopyBytes(in), nil
} }
@ -396,7 +396,7 @@ func (c *BigModExp) RequiredGas(input []byte) uint64 {
return gas.Uint64() return gas.Uint64()
} }
func (c *BigModExp) Run(_ *EVM, _ common.Address, _ common.Address, input []byte, _ *big.Int, _ bool, _ bool) ([]byte, error) { func (c *BigModExp) Run(_ *EVM, _ common.Address, _ common.Address, input []byte, _ *big.Int, _ bool, _ bool, _ *tracing.Hooks) ([]byte, error) {
var ( var (
baseLen = new(big.Int).SetBytes(getData(input, 0, 32)).Uint64() baseLen = new(big.Int).SetBytes(getData(input, 0, 32)).Uint64()
expLen = new(big.Int).SetBytes(getData(input, 32, 32)).Uint64() expLen = new(big.Int).SetBytes(getData(input, 32, 32)).Uint64()
@ -476,7 +476,7 @@ func (c *Bn256AddIstanbul) RequiredGas(input []byte) uint64 {
return params.Bn256AddGasIstanbul return params.Bn256AddGasIstanbul
} }
func (c *Bn256AddIstanbul) Run(_ *EVM, _ common.Address, _ common.Address, input []byte, _ *big.Int, _ bool, _ bool) ([]byte, error) { func (c *Bn256AddIstanbul) Run(_ *EVM, _ common.Address, _ common.Address, input []byte, _ *big.Int, _ bool, _ bool, _ *tracing.Hooks) ([]byte, error) {
return runBn256Add(input) return runBn256Add(input)
} }
@ -489,7 +489,7 @@ func (c *bn256AddByzantium) RequiredGas(input []byte) uint64 {
return params.Bn256AddGasByzantium return params.Bn256AddGasByzantium
} }
func (c *bn256AddByzantium) Run(_ *EVM, _ common.Address, _ common.Address, input []byte, _ *big.Int, _ bool, _ bool) ([]byte, error) { func (c *bn256AddByzantium) Run(_ *EVM, _ common.Address, _ common.Address, input []byte, _ *big.Int, _ bool, _ bool, _ *tracing.Hooks) ([]byte, error) {
return runBn256Add(input) return runBn256Add(input)
} }
@ -514,7 +514,7 @@ func (c *Bn256ScalarMulIstanbul) RequiredGas(input []byte) uint64 {
return params.Bn256ScalarMulGasIstanbul return params.Bn256ScalarMulGasIstanbul
} }
func (c *Bn256ScalarMulIstanbul) Run(_ *EVM, _ common.Address, _ common.Address, input []byte, _ *big.Int, _ bool, _ bool) ([]byte, error) { func (c *Bn256ScalarMulIstanbul) Run(_ *EVM, _ common.Address, _ common.Address, input []byte, _ *big.Int, _ bool, _ bool, _ *tracing.Hooks) ([]byte, error) {
return runBn256ScalarMul(input) return runBn256ScalarMul(input)
} }
@ -527,7 +527,7 @@ func (c *bn256ScalarMulByzantium) RequiredGas(input []byte) uint64 {
return params.Bn256ScalarMulGasByzantium return params.Bn256ScalarMulGasByzantium
} }
func (c *bn256ScalarMulByzantium) Run(_ *EVM, _ common.Address, _ common.Address, input []byte, _ *big.Int, _ bool, _ bool) ([]byte, error) { func (c *bn256ScalarMulByzantium) Run(_ *EVM, _ common.Address, _ common.Address, input []byte, _ *big.Int, _ bool, _ bool, _ *tracing.Hooks) ([]byte, error) {
return runBn256ScalarMul(input) return runBn256ScalarMul(input)
} }
@ -582,7 +582,7 @@ func (c *Bn256PairingIstanbul) RequiredGas(input []byte) uint64 {
return params.Bn256PairingBaseGasIstanbul + uint64(len(input)/192)*params.Bn256PairingPerPointGasIstanbul return params.Bn256PairingBaseGasIstanbul + uint64(len(input)/192)*params.Bn256PairingPerPointGasIstanbul
} }
func (c *Bn256PairingIstanbul) Run(_ *EVM, _ common.Address, _ common.Address, input []byte, _ *big.Int, _ bool, _ bool) ([]byte, error) { func (c *Bn256PairingIstanbul) Run(_ *EVM, _ common.Address, _ common.Address, input []byte, _ *big.Int, _ bool, _ bool, _ *tracing.Hooks) ([]byte, error) {
return runBn256Pairing(input) return runBn256Pairing(input)
} }
@ -595,7 +595,7 @@ func (c *bn256PairingByzantium) RequiredGas(input []byte) uint64 {
return params.Bn256PairingBaseGasByzantium + uint64(len(input)/192)*params.Bn256PairingPerPointGasByzantium return params.Bn256PairingBaseGasByzantium + uint64(len(input)/192)*params.Bn256PairingPerPointGasByzantium
} }
func (c *bn256PairingByzantium) Run(_ *EVM, _ common.Address, _ common.Address, input []byte, _ *big.Int, _ bool, _ bool) ([]byte, error) { func (c *bn256PairingByzantium) Run(_ *EVM, _ common.Address, _ common.Address, input []byte, _ *big.Int, _ bool, _ bool, _ *tracing.Hooks) ([]byte, error) {
return runBn256Pairing(input) return runBn256Pairing(input)
} }
@ -621,7 +621,7 @@ var (
ErrBlake2FInvalidFinalFlag = errors.New("invalid final flag") ErrBlake2FInvalidFinalFlag = errors.New("invalid final flag")
) )
func (c *Blake2F) Run(_ *EVM, _ common.Address, _ common.Address, input []byte, _ *big.Int, _ bool, _ bool) ([]byte, error) { func (c *Blake2F) Run(_ *EVM, _ common.Address, _ common.Address, input []byte, _ *big.Int, _ bool, _ bool, _ *tracing.Hooks) ([]byte, error) {
// Make sure the input is valid (correct length and final flag) // Make sure the input is valid (correct length and final flag)
if len(input) != Blake2FInputLength { if len(input) != Blake2FInputLength {
return nil, ErrBlake2FInvalidInputLength return nil, ErrBlake2FInvalidInputLength
@ -675,7 +675,7 @@ func (c *Bls12381G1Add) RequiredGas(input []byte) uint64 {
return params.Bls12381G1AddGas return params.Bls12381G1AddGas
} }
func (c *Bls12381G1Add) Run(_ *EVM, _ common.Address, _ common.Address, input []byte, _ *big.Int, _ bool, _ bool) ([]byte, error) { func (c *Bls12381G1Add) Run(_ *EVM, _ common.Address, _ common.Address, input []byte, _ *big.Int, _ bool, _ bool, _ *tracing.Hooks) ([]byte, error) {
// Implements EIP-2537 G1Add precompile. // Implements EIP-2537 G1Add precompile.
// > G1 addition call expects `256` bytes as an input that is interpreted as byte concatenation of two G1 points (`128` bytes each). // > G1 addition call expects `256` bytes as an input that is interpreted as byte concatenation of two G1 points (`128` bytes each).
// > Output is an encoding of addition operation result - single G1 point (`128` bytes). // > Output is an encoding of addition operation result - single G1 point (`128` bytes).
@ -713,7 +713,7 @@ func (c *Bls12381G1Mul) RequiredGas(input []byte) uint64 {
return params.Bls12381G1MulGas return params.Bls12381G1MulGas
} }
func (c *Bls12381G1Mul) Run(_ *EVM, _ common.Address, _ common.Address, input []byte, _ *big.Int, _ bool, _ bool) ([]byte, error) { func (c *Bls12381G1Mul) Run(_ *EVM, _ common.Address, _ common.Address, input []byte, _ *big.Int, _ bool, _ bool, _ *tracing.Hooks) ([]byte, error) {
// Implements EIP-2537 G1Mul precompile. // Implements EIP-2537 G1Mul precompile.
// > G1 multiplication call expects `160` bytes as an input that is interpreted as byte concatenation of encoding of G1 point (`128` bytes) and encoding of a scalar value (`32` bytes). // > G1 multiplication call expects `160` bytes as an input that is interpreted as byte concatenation of encoding of G1 point (`128` bytes) and encoding of a scalar value (`32` bytes).
// > Output is an encoding of multiplication operation result - single G1 point (`128` bytes). // > Output is an encoding of multiplication operation result - single G1 point (`128` bytes).
@ -763,7 +763,7 @@ func (c *Bls12381G1MultiExp) RequiredGas(input []byte) uint64 {
return (uint64(k) * params.Bls12381G1MulGas * discount) / 1000 return (uint64(k) * params.Bls12381G1MulGas * discount) / 1000
} }
func (c *Bls12381G1MultiExp) Run(_ *EVM, _ common.Address, _ common.Address, input []byte, _ *big.Int, _ bool, _ bool) ([]byte, error) { func (c *Bls12381G1MultiExp) Run(_ *EVM, _ common.Address, _ common.Address, input []byte, _ *big.Int, _ bool, _ bool, _ *tracing.Hooks) ([]byte, error) {
// Implements EIP-2537 G1MultiExp precompile. // Implements EIP-2537 G1MultiExp precompile.
// G1 multiplication call expects `160*k` bytes as an input that is interpreted as byte concatenation of `k` slices each of them being a byte concatenation of encoding of G1 point (`128` bytes) and encoding of a scalar value (`32` bytes). // G1 multiplication call expects `160*k` bytes as an input that is interpreted as byte concatenation of `k` slices each of them being a byte concatenation of encoding of G1 point (`128` bytes) and encoding of a scalar value (`32` bytes).
// Output is an encoding of multiexponentiation operation result - single G1 point (`128` bytes). // Output is an encoding of multiexponentiation operation result - single G1 point (`128` bytes).
@ -806,7 +806,7 @@ func (c *Bls12381G2Add) RequiredGas(input []byte) uint64 {
return params.Bls12381G2AddGas return params.Bls12381G2AddGas
} }
func (c *Bls12381G2Add) Run(_ *EVM, _ common.Address, _ common.Address, input []byte, _ *big.Int, _ bool, _ bool) ([]byte, error) { func (c *Bls12381G2Add) Run(_ *EVM, _ common.Address, _ common.Address, input []byte, _ *big.Int, _ bool, _ bool, _ *tracing.Hooks) ([]byte, error) {
// Implements EIP-2537 G2Add precompile. // Implements EIP-2537 G2Add precompile.
// > G2 addition call expects `512` bytes as an input that is interpreted as byte concatenation of two G2 points (`256` bytes each). // > G2 addition call expects `512` bytes as an input that is interpreted as byte concatenation of two G2 points (`256` bytes each).
// > Output is an encoding of addition operation result - single G2 point (`256` bytes). // > Output is an encoding of addition operation result - single G2 point (`256` bytes).
@ -844,7 +844,7 @@ func (c *Bls12381G2Mul) RequiredGas(input []byte) uint64 {
return params.Bls12381G2MulGas return params.Bls12381G2MulGas
} }
func (c *Bls12381G2Mul) Run(_ *EVM, _ common.Address, _ common.Address, input []byte, _ *big.Int, _ bool, _ bool) ([]byte, error) { func (c *Bls12381G2Mul) Run(_ *EVM, _ common.Address, _ common.Address, input []byte, _ *big.Int, _ bool, _ bool, _ *tracing.Hooks) ([]byte, error) {
// Implements EIP-2537 G2MUL precompile logic. // Implements EIP-2537 G2MUL precompile logic.
// > G2 multiplication call expects `288` bytes as an input that is interpreted as byte concatenation of encoding of G2 point (`256` bytes) and encoding of a scalar value (`32` bytes). // > G2 multiplication call expects `288` bytes as an input that is interpreted as byte concatenation of encoding of G2 point (`256` bytes) and encoding of a scalar value (`32` bytes).
// > Output is an encoding of multiplication operation result - single G2 point (`256` bytes). // > Output is an encoding of multiplication operation result - single G2 point (`256` bytes).
@ -894,7 +894,7 @@ func (c *Bls12381G2MultiExp) RequiredGas(input []byte) uint64 {
return (uint64(k) * params.Bls12381G2MulGas * discount) / 1000 return (uint64(k) * params.Bls12381G2MulGas * discount) / 1000
} }
func (c *Bls12381G2MultiExp) Run(_ *EVM, _ common.Address, _ common.Address, input []byte, _ *big.Int, _ bool, _ bool) ([]byte, error) { func (c *Bls12381G2MultiExp) Run(_ *EVM, _ common.Address, _ common.Address, input []byte, _ *big.Int, _ bool, _ bool, _ *tracing.Hooks) ([]byte, error) {
// Implements EIP-2537 G2MultiExp precompile logic // Implements EIP-2537 G2MultiExp precompile logic
// > G2 multiplication call expects `288*k` bytes as an input that is interpreted as byte concatenation of `k` slices each of them being a byte concatenation of encoding of G2 point (`256` bytes) and encoding of a scalar value (`32` bytes). // > G2 multiplication call expects `288*k` bytes as an input that is interpreted as byte concatenation of `k` slices each of them being a byte concatenation of encoding of G2 point (`256` bytes) and encoding of a scalar value (`32` bytes).
// > Output is an encoding of multiexponentiation operation result - single G2 point (`256` bytes). // > Output is an encoding of multiexponentiation operation result - single G2 point (`256` bytes).
@ -937,7 +937,7 @@ func (c *Bls12381Pairing) RequiredGas(input []byte) uint64 {
return params.Bls12381PairingBaseGas + uint64(len(input)/384)*params.Bls12381PairingPerPairGas return params.Bls12381PairingBaseGas + uint64(len(input)/384)*params.Bls12381PairingPerPairGas
} }
func (c *Bls12381Pairing) Run(_ *EVM, _ common.Address, _ common.Address, input []byte, _ *big.Int, _ bool, _ bool) ([]byte, error) { func (c *Bls12381Pairing) Run(_ *EVM, _ common.Address, _ common.Address, input []byte, _ *big.Int, _ bool, _ bool, _ *tracing.Hooks) ([]byte, error) {
// Implements EIP-2537 Pairing precompile logic. // Implements EIP-2537 Pairing precompile logic.
// > Pairing call expects `384*k` bytes as an inputs that is interpreted as byte concatenation of `k` slices. Each slice has the following structure: // > Pairing call expects `384*k` bytes as an inputs that is interpreted as byte concatenation of `k` slices. Each slice has the following structure:
// > - `128` bytes of G1 point encoding // > - `128` bytes of G1 point encoding
@ -1016,7 +1016,7 @@ func (c *Bls12381MapG1) RequiredGas(input []byte) uint64 {
return params.Bls12381MapG1Gas return params.Bls12381MapG1Gas
} }
func (c *Bls12381MapG1) Run(_ *EVM, _ common.Address, _ common.Address, input []byte, _ *big.Int, _ bool, _ bool) ([]byte, error) { func (c *Bls12381MapG1) Run(_ *EVM, _ common.Address, _ common.Address, input []byte, _ *big.Int, _ bool, _ bool, _ *tracing.Hooks) ([]byte, error) {
// Implements EIP-2537 Map_To_G1 precompile. // Implements EIP-2537 Map_To_G1 precompile.
// > Field-to-curve call expects `64` bytes an an input that is interpreted as a an element of the base field. // > Field-to-curve call expects `64` bytes an an input that is interpreted as a an element of the base field.
// > Output of this call is `128` bytes and is G1 point following respective encoding rules. // > Output of this call is `128` bytes and is G1 point following respective encoding rules.
@ -1051,7 +1051,7 @@ func (c *Bls12381MapG2) RequiredGas(input []byte) uint64 {
return params.Bls12381MapG2Gas return params.Bls12381MapG2Gas
} }
func (c *Bls12381MapG2) Run(_ *EVM, _ common.Address, _ common.Address, input []byte, _ *big.Int, _ bool, _ bool) ([]byte, error) { func (c *Bls12381MapG2) Run(_ *EVM, _ common.Address, _ common.Address, input []byte, _ *big.Int, _ bool, _ bool, _ *tracing.Hooks) ([]byte, error) {
// Implements EIP-2537 Map_FP2_TO_G2 precompile logic. // Implements EIP-2537 Map_FP2_TO_G2 precompile logic.
// > Field-to-curve call expects `128` bytes an an input that is interpreted as a an element of the quadratic extension field. // > Field-to-curve call expects `128` bytes an an input that is interpreted as a an element of the quadratic extension field.
// > Output of this call is `256` bytes and is G2 point following respective encoding rules. // > Output of this call is `256` bytes and is G2 point following respective encoding rules.
@ -1106,7 +1106,7 @@ var (
) )
// Run executes the point evaluation precompile. // Run executes the point evaluation precompile.
func (b *KzgPointEvaluation) Run(_ *EVM, _ common.Address, _ common.Address, input []byte, _ *big.Int, _ bool, _ bool) ([]byte, error) { func (b *KzgPointEvaluation) Run(_ *EVM, _ common.Address, _ common.Address, input []byte, _ *big.Int, _ bool, _ bool, _ *tracing.Hooks) ([]byte, error) {
if len(input) != blobVerifyInputLength { if len(input) != blobVerifyInputLength {
return nil, errBlobVerifyInvalidInputLength return nil, errBlobVerifyInvalidInputLength
} }

View file

@ -82,7 +82,7 @@ func fuzz(id byte, data []byte) int {
} }
cpy := make([]byte, len(data)) cpy := make([]byte, len(data))
copy(cpy, data) copy(cpy, data)
_, err := precompile.Run(nil, common.Address{}, common.Address{}, cpy, nil, false, false) _, err := precompile.Run(nil, common.Address{}, common.Address{}, cpy, nil, false, false, nil)
if !bytes.Equal(cpy, data) { if !bytes.Equal(cpy, data) {
panic(fmt.Sprintf("input data modified, precompile %d: %x %x", id, data, cpy)) panic(fmt.Sprintf("input data modified, precompile %d: %x %x", id, data, cpy))
} }