From 0353ffbc184eb07229a0b9fd0184d66c911870ee Mon Sep 17 00:00:00 2001 From: mapdev33 <> Date: Wed, 24 Jan 2024 16:08:19 +0800 Subject: [PATCH] update the constants for vm --- core/vm/contracts.go | 56 ++++++++++++++++++++------------------ core/vm/eips.go | 2 +- core/vm/evm.go | 18 ++++++------ core/vm/gas_table.go | 20 +++++++------- core/vm/instructions.go | 21 +++++++++----- core/vm/jump_table.go | 2 +- core/vm/operations_acl.go | 2 +- core/vm/runtime/runtime.go | 9 +++--- core/vm/stack_table.go | 2 +- 9 files changed, 70 insertions(+), 62 deletions(-) diff --git a/core/vm/contracts.go b/core/vm/contracts.go index 07150195..9827ba41 100644 --- a/core/vm/contracts.go +++ b/core/vm/contracts.go @@ -22,15 +22,17 @@ import ( "encoding/binary" "errors" "fmt" + "github.com/mapprotocol/atlas/chains/eth2" "github.com/mapprotocol/atlas/helper/bls" + "math/big" + "github.com/ethereum/go-ethereum/common/hexutil" "github.com/ethereum/go-ethereum/rlp" "github.com/mapprotocol/atlas/core/types" blscrypto "github.com/mapprotocol/atlas/helper/bls" params2 "github.com/mapprotocol/atlas/params" - "math/big" "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/common/math" @@ -39,8 +41,8 @@ import ( "github.com/ethereum/go-ethereum/crypto/bls12381" "github.com/ethereum/go-ethereum/crypto/bn256" "github.com/ethereum/go-ethereum/log" - ethparams "github.com/ethereum/go-ethereum/params" "github.com/mapprotocol/atlas/params" + //lint:ignore SA1019 Needed for precompile "golang.org/x/crypto/ripemd160" ) @@ -288,7 +290,7 @@ func RunPrecompiledContract(evm *EVM, contract *Contract, p PrecompiledContract, type ecrecover struct{} func (c *ecrecover) RequiredGas(input []byte) uint64 { - return ethparams.EcrecoverGas + return params.EcrecoverGas } func (c *ecrecover) Run(evm *EVM, contract *Contract, input []byte) ([]byte, error) { @@ -330,7 +332,7 @@ type sha256hash struct{} // This method does not require any overflow checking as the input size gas costs // required for anything significant is so high it's impossible to pay for. func (c *sha256hash) RequiredGas(input []byte) uint64 { - return uint64(len(input)+31)/32*ethparams.Sha256PerWordGas + ethparams.Sha256BaseGas + return uint64(len(input)+31)/32*params.Sha256PerWordGas + params.Sha256BaseGas } func (c *sha256hash) Run(evm *EVM, contract *Contract, input []byte) ([]byte, error) { h := sha256.Sum256(input) @@ -345,7 +347,7 @@ type ripemd160hash struct{} // This method does not require any overflow checking as the input size gas costs // required for anything significant is so high it's impossible to pay for. func (c *ripemd160hash) RequiredGas(input []byte) uint64 { - return uint64(len(input)+31)/32*ethparams.Ripemd160PerWordGas + ethparams.Ripemd160BaseGas + return uint64(len(input)+31)/32*params.Ripemd160PerWordGas + params.Ripemd160BaseGas } func (c *ripemd160hash) Run(evm *EVM, contract *Contract, input []byte) ([]byte, error) { ripemd := ripemd160.New() @@ -361,7 +363,7 @@ type dataCopy struct{} // This method does not require any overflow checking as the input size gas costs // required for anything significant is so high it's impossible to pay for. func (c *dataCopy) RequiredGas(input []byte) uint64 { - return uint64(len(input)+31)/32*ethparams.IdentityPerWordGas + ethparams.IdentityBaseGas + return uint64(len(input)+31)/32*params.IdentityPerWordGas + params.IdentityBaseGas } func (c *dataCopy) Run(evm *EVM, contract *Contract, in []byte) ([]byte, error) { return in, nil @@ -560,7 +562,7 @@ type bn256AddIstanbul struct{} // RequiredGas returns the gas required to execute the pre-compiled contract. func (c *bn256AddIstanbul) RequiredGas(input []byte) uint64 { - return ethparams.Bn256AddGasIstanbul + return params.Bn256AddGasIstanbul } func (c *bn256AddIstanbul) Run(evm *EVM, contract *Contract, input []byte) ([]byte, error) { @@ -573,7 +575,7 @@ type bn256AddByzantium struct{} // RequiredGas returns the gas required to execute the pre-compiled contract. func (c *bn256AddByzantium) RequiredGas(input []byte) uint64 { - return ethparams.Bn256AddGasByzantium + return params.Bn256AddGasByzantium } func (c *bn256AddByzantium) Run(evm *EVM, contract *Contract, input []byte) ([]byte, error) { @@ -598,7 +600,7 @@ type bn256ScalarMulIstanbul struct{} // RequiredGas returns the gas required to execute the pre-compiled contract. func (c *bn256ScalarMulIstanbul) RequiredGas(input []byte) uint64 { - return ethparams.Bn256ScalarMulGasIstanbul + return params.Bn256ScalarMulGasIstanbul } func (c *bn256ScalarMulIstanbul) Run(evm *EVM, contract *Contract, input []byte) ([]byte, error) { @@ -611,7 +613,7 @@ type bn256ScalarMulByzantium struct{} // RequiredGas returns the gas required to execute the pre-compiled contract. func (c *bn256ScalarMulByzantium) RequiredGas(input []byte) uint64 { - return ethparams.Bn256ScalarMulGasByzantium + return params.Bn256ScalarMulGasByzantium } func (c *bn256ScalarMulByzantium) Run(evm *EVM, contract *Contract, input []byte) ([]byte, error) { @@ -666,7 +668,7 @@ type bn256PairingIstanbul struct{} // RequiredGas returns the gas required to execute the pre-compiled contract. func (c *bn256PairingIstanbul) RequiredGas(input []byte) uint64 { - return ethparams.Bn256PairingBaseGasIstanbul + uint64(len(input)/192)*ethparams.Bn256PairingPerPointGasIstanbul + return params.Bn256PairingBaseGasIstanbul + uint64(len(input)/192)*params.Bn256PairingPerPointGasIstanbul } func (c *bn256PairingIstanbul) Run(evm *EVM, contract *Contract, input []byte) ([]byte, error) { @@ -679,7 +681,7 @@ type bn256PairingByzantium struct{} // RequiredGas returns the gas required to execute the pre-compiled contract. func (c *bn256PairingByzantium) RequiredGas(input []byte) uint64 { - return ethparams.Bn256PairingBaseGasByzantium + uint64(len(input)/192)*ethparams.Bn256PairingPerPointGasByzantium + return params.Bn256PairingBaseGasByzantium + uint64(len(input)/192)*params.Bn256PairingPerPointGasByzantium } func (c *bn256PairingByzantium) Run(evm *EVM, contract *Contract, input []byte) ([]byte, error) { @@ -759,7 +761,7 @@ type bls12381G1Add struct{} // RequiredGas returns the gas required to execute the pre-compiled contract. func (c *bls12381G1Add) RequiredGas(input []byte) uint64 { - return ethparams.Bls12381G1AddGas + return params.Bls12381G1AddGas } func (c *bls12381G1Add) Run(evm *EVM, contract *Contract, input []byte) ([]byte, error) { @@ -797,7 +799,7 @@ type bls12381G1Mul struct{} // RequiredGas returns the gas required to execute the pre-compiled contract. func (c *bls12381G1Mul) RequiredGas(input []byte) uint64 { - return ethparams.Bls12381G1MulGas + return params.Bls12381G1MulGas } func (c *bls12381G1Mul) Run(evm *EVM, contract *Contract, input []byte) ([]byte, error) { @@ -841,13 +843,13 @@ func (c *bls12381G1MultiExp) RequiredGas(input []byte) uint64 { } // Lookup discount value for G1 point, scalar value pair length var discount uint64 - if dLen := len(ethparams.Bls12381MultiExpDiscountTable); k < dLen { - discount = ethparams.Bls12381MultiExpDiscountTable[k-1] + if dLen := len(params.Bls12381MultiExpDiscountTable); k < dLen { + discount = params.Bls12381MultiExpDiscountTable[k-1] } else { - discount = ethparams.Bls12381MultiExpDiscountTable[dLen-1] + discount = params.Bls12381MultiExpDiscountTable[dLen-1] } // Calculate gas and return the result - return (uint64(k) * ethparams.Bls12381G1MulGas * discount) / 1000 + return (uint64(k) * params.Bls12381G1MulGas * discount) / 1000 } func (c *bls12381G1MultiExp) Run(evm *EVM, contract *Contract, input []byte) ([]byte, error) { @@ -890,7 +892,7 @@ type bls12381G2Add struct{} // RequiredGas returns the gas required to execute the pre-compiled contract. func (c *bls12381G2Add) RequiredGas(input []byte) uint64 { - return ethparams.Bls12381G2AddGas + return params.Bls12381G2AddGas } func (c *bls12381G2Add) Run(evm *EVM, contract *Contract, input []byte) ([]byte, error) { @@ -928,7 +930,7 @@ type bls12381G2Mul struct{} // RequiredGas returns the gas required to execute the pre-compiled contract. func (c *bls12381G2Mul) RequiredGas(input []byte) uint64 { - return ethparams.Bls12381G2MulGas + return params.Bls12381G2MulGas } func (c *bls12381G2Mul) Run(evm *EVM, contract *Contract, input []byte) ([]byte, error) { @@ -972,13 +974,13 @@ func (c *bls12381G2MultiExp) RequiredGas(input []byte) uint64 { } // Lookup discount value for G2 point, scalar value pair length var discount uint64 - if dLen := len(ethparams.Bls12381MultiExpDiscountTable); k < dLen { - discount = ethparams.Bls12381MultiExpDiscountTable[k-1] + if dLen := len(params.Bls12381MultiExpDiscountTable); k < dLen { + discount = params.Bls12381MultiExpDiscountTable[k-1] } else { - discount = ethparams.Bls12381MultiExpDiscountTable[dLen-1] + discount = params.Bls12381MultiExpDiscountTable[dLen-1] } // Calculate gas and return the result - return (uint64(k) * ethparams.Bls12381G2MulGas * discount) / 1000 + return (uint64(k) * params.Bls12381G2MulGas * discount) / 1000 } func (c *bls12381G2MultiExp) Run(evm *EVM, contract *Contract, input []byte) ([]byte, error) { @@ -1021,7 +1023,7 @@ type bls12381Pairing struct{} // RequiredGas returns the gas required to execute the pre-compiled contract. func (c *bls12381Pairing) RequiredGas(input []byte) uint64 { - return ethparams.Bls12381PairingBaseGas + uint64(len(input)/384)*ethparams.Bls12381PairingPerPairGas + return params.Bls12381PairingBaseGas + uint64(len(input)/384)*params.Bls12381PairingPerPairGas } func (c *bls12381Pairing) Run(evm *EVM, contract *Contract, input []byte) ([]byte, error) { @@ -1100,7 +1102,7 @@ type bls12381MapG1 struct{} // RequiredGas returns the gas required to execute the pre-compiled contract. func (c *bls12381MapG1) RequiredGas(input []byte) uint64 { - return ethparams.Bls12381MapG1Gas + return params.Bls12381MapG1Gas } func (c *bls12381MapG1) Run(evm *EVM, contract *Contract, input []byte) ([]byte, error) { @@ -1135,7 +1137,7 @@ type bls12381MapG2 struct{} // RequiredGas returns the gas required to execute the pre-compiled contract. func (c *bls12381MapG2) RequiredGas(input []byte) uint64 { - return ethparams.Bls12381MapG2Gas + return params.Bls12381MapG2Gas } func (c *bls12381MapG2) Run(evm *EVM, contract *Contract, input []byte) ([]byte, error) { diff --git a/core/vm/eips.go b/core/vm/eips.go index 4070a2db..9c5c1efb 100644 --- a/core/vm/eips.go +++ b/core/vm/eips.go @@ -20,8 +20,8 @@ import ( "fmt" "sort" - "github.com/ethereum/go-ethereum/params" "github.com/holiman/uint256" + "github.com/mapprotocol/atlas/params" ) var activators = map[int]func(*JumpTable){ diff --git a/core/vm/evm.go b/core/vm/evm.go index 992cc113..4b327bff 100644 --- a/core/vm/evm.go +++ b/core/vm/evm.go @@ -17,15 +17,15 @@ package vm import ( - "github.com/mapprotocol/atlas/consensus/istanbul" - "github.com/mapprotocol/atlas/core/types" "math/big" "sync/atomic" "time" + "github.com/mapprotocol/atlas/consensus/istanbul" + "github.com/mapprotocol/atlas/core/types" + "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/crypto" - ethparams "github.com/ethereum/go-ethereum/params" "github.com/holiman/uint256" "github.com/mapprotocol/atlas/params" @@ -203,7 +203,7 @@ func (evm *EVM) Call(caller ContractRef, addr common.Address, input []byte, gas return nil, gas, nil } // Fail if we're trying to execute above the call depth limit - if evm.depth > int(ethparams.CallCreateDepth) { + if evm.depth > int(params.CallCreateDepth) { return nil, gas, ErrDepth } // Fail if we're trying to transfer more than the available balance @@ -288,7 +288,7 @@ func (evm *EVM) CallCode(caller ContractRef, addr common.Address, input []byte, return nil, gas, nil } // Fail if we're trying to execute above the call depth limit - if evm.depth > int(ethparams.CallCreateDepth) { + if evm.depth > int(params.CallCreateDepth) { return nil, gas, ErrDepth } // Fail if we're trying to transfer more than the available balance @@ -340,7 +340,7 @@ func (evm *EVM) DelegateCall(caller ContractRef, addr common.Address, input []by return nil, gas, nil } // Fail if we're trying to execute above the call depth limit - if evm.depth > int(ethparams.CallCreateDepth) { + if evm.depth > int(params.CallCreateDepth) { return nil, gas, ErrDepth } var snapshot = evm.StateDB.Snapshot() @@ -383,7 +383,7 @@ func (evm *EVM) StaticCall(caller ContractRef, addr common.Address, input []byte return nil, gas, nil } // Fail if we're trying to execute above the call depth limit - if evm.depth > int(ethparams.CallCreateDepth) { + if evm.depth > int(params.CallCreateDepth) { return nil, gas, ErrDepth } // We take a snapshot here. This is a bit counter-intuitive, and could probably be skipped. @@ -462,7 +462,7 @@ func (c *codeAndHash) Hash() common.Hash { func (evm *EVM) create(caller ContractRef, codeAndHash *codeAndHash, gas uint64, value *big.Int, address common.Address, typ OpCode) ([]byte, common.Address, uint64, error) { // Depth check execution. Fail if we're trying to execute above the // limit. - if evm.depth > int(ethparams.CallCreateDepth) { + if evm.depth > int(params.CallCreateDepth) { return nil, common.Address{}, gas, ErrDepth } if !evm.Context.CanTransfer(evm.StateDB, caller.Address(), value) { @@ -524,7 +524,7 @@ func (evm *EVM) create(caller ContractRef, codeAndHash *codeAndHash, gas uint64, // be stored due to not enough gas set an error and let it be handled // by the error checking condition below. if err == nil { - createDataGas := uint64(len(ret)) * ethparams.CreateDataGas + createDataGas := uint64(len(ret)) * params.CreateDataGas if contract.UseGas(createDataGas) { evm.StateDB.SetCode(address, ret) } else { diff --git a/core/vm/gas_table.go b/core/vm/gas_table.go index 19d2198a..cc88d74d 100644 --- a/core/vm/gas_table.go +++ b/core/vm/gas_table.go @@ -21,7 +21,7 @@ import ( "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/common/math" - "github.com/ethereum/go-ethereum/params" + "github.com/mapprotocol/atlas/params" ) // memoryGasCost calculates the quadratic gas for memory expansion. It does so @@ -162,19 +162,19 @@ func gasSStore(evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySi return params.NetSstoreDirtyGas, nil } -// 0. If *gasleft* is less than or equal to 2300, fail the current call. -// 1. If current value equals new value (this is a no-op), SLOAD_GAS is deducted. -// 2. If current value does not equal new value: -// 2.1. If original value equals current value (this storage slot has not been changed by the current execution context): +// 0. If *gasleft* is less than or equal to 2300, fail the current call. +// 1. If current value equals new value (this is a no-op), SLOAD_GAS is deducted. +// 2. If current value does not equal new value: +// 2.1. If original value equals current value (this storage slot has not been changed by the current execution context): // 2.1.1. If original value is 0, SSTORE_SET_GAS (20K) gas is deducted. // 2.1.2. Otherwise, SSTORE_RESET_GAS gas is deducted. If new value is 0, add SSTORE_CLEARS_SCHEDULE to refund counter. -// 2.2. If original value does not equal current value (this storage slot is dirty), SLOAD_GAS gas is deducted. Apply both of the following clauses: +// 2.2. If original value does not equal current value (this storage slot is dirty), SLOAD_GAS gas is deducted. Apply both of the following clauses: // 2.2.1. If original value is not 0: -// 2.2.1.1. If current value is 0 (also means that new value is not 0), subtract SSTORE_CLEARS_SCHEDULE gas from refund counter. -// 2.2.1.2. If new value is 0 (also means that current value is not 0), add SSTORE_CLEARS_SCHEDULE gas to refund counter. +// 2.2.1.1. If current value is 0 (also means that new value is not 0), subtract SSTORE_CLEARS_SCHEDULE gas from refund counter. +// 2.2.1.2. If new value is 0 (also means that current value is not 0), add SSTORE_CLEARS_SCHEDULE gas to refund counter. // 2.2.2. If original value equals new value (this storage slot is reset): -// 2.2.2.1. If original value is 0, add SSTORE_SET_GAS - SLOAD_GAS to refund counter. -// 2.2.2.2. Otherwise, add SSTORE_RESET_GAS - SLOAD_GAS gas to refund counter. +// 2.2.2.1. If original value is 0, add SSTORE_SET_GAS - SLOAD_GAS to refund counter. +// 2.2.2.2. Otherwise, add SSTORE_RESET_GAS - SLOAD_GAS gas to refund counter. func gasSStoreEIP2200(evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (uint64, error) { // If we fail the minimum gas availability invariant, fail (0) if contract.Gas <= params.SstoreSentryGasEIP2200 { diff --git a/core/vm/instructions.go b/core/vm/instructions.go index e9df5494..a4f5a002 100644 --- a/core/vm/instructions.go +++ b/core/vm/instructions.go @@ -18,9 +18,9 @@ package vm import ( "github.com/ethereum/go-ethereum/common" - "github.com/ethereum/go-ethereum/params" "github.com/holiman/uint256" "github.com/mapprotocol/atlas/core/types" + "github.com/mapprotocol/atlas/params" "golang.org/x/crypto/sha3" ) @@ -390,16 +390,21 @@ func opExtCodeCopy(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) // opExtCodeHash returns the code hash of a specified account. // There are several cases when the function is called, while we can relay everything // to `state.GetCodeHash` function to ensure the correctness. -// (1) Caller tries to get the code hash of a normal contract account, state +// +// (1) Caller tries to get the code hash of a normal contract account, state +// // should return the relative code hash and set it as the result. // -// (2) Caller tries to get the code hash of a non-existent account, state should +// (2) Caller tries to get the code hash of a non-existent account, state should +// // return common.Hash{} and zero will be set as the result. // -// (3) Caller tries to get the code hash for an account without contract code, +// (3) Caller tries to get the code hash for an account without contract code, +// // state should return emptyCodeHash(0xc5d246...) as the result. // -// (4) Caller tries to get the code hash of a precompiled account, the result +// (4) Caller tries to get the code hash of a precompiled account, the result +// // should be zero or emptyCodeHash. // // It is worth noting that in order to avoid unnecessary create and clean, @@ -408,10 +413,12 @@ func opExtCodeCopy(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) // If the precompile account is not transferred any amount on a private or // customized chain, the return value will be zero. // -// (5) Caller tries to get the code hash for an account which is marked as suicided +// (5) Caller tries to get the code hash for an account which is marked as suicided +// // in the current transaction, the code hash of this account should be returned. // -// (6) Caller tries to get the code hash for an account which is marked as deleted, +// (6) Caller tries to get the code hash for an account which is marked as deleted, +// // this account should be regarded as a non-existent account and zero should be returned. func opExtCodeHash(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) { slot := scope.Stack.peek() diff --git a/core/vm/jump_table.go b/core/vm/jump_table.go index 329ad77c..daabe242 100644 --- a/core/vm/jump_table.go +++ b/core/vm/jump_table.go @@ -17,7 +17,7 @@ package vm import ( - "github.com/ethereum/go-ethereum/params" + "github.com/mapprotocol/atlas/params" ) type ( diff --git a/core/vm/operations_acl.go b/core/vm/operations_acl.go index 483226ee..31032b28 100644 --- a/core/vm/operations_acl.go +++ b/core/vm/operations_acl.go @@ -21,7 +21,7 @@ import ( "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/common/math" - "github.com/ethereum/go-ethereum/params" + "github.com/mapprotocol/atlas/params" ) func makeGasSStoreFunc(clearingRefund uint64) gasFunc { diff --git a/core/vm/runtime/runtime.go b/core/vm/runtime/runtime.go index 8b39ea6e..9b362a6d 100644 --- a/core/vm/runtime/runtime.go +++ b/core/vm/runtime/runtime.go @@ -23,17 +23,16 @@ import ( "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/crypto" - "github.com/ethereum/go-ethereum/params" "github.com/mapprotocol/atlas/core/rawdb" "github.com/mapprotocol/atlas/core/state" "github.com/mapprotocol/atlas/core/vm" - params2 "github.com/mapprotocol/atlas/params" + "github.com/mapprotocol/atlas/params" ) // Config is a basic type specifying certain configuration flags for running // the EVM. type Config struct { - ChainConfig *params2.ChainConfig + ChainConfig *params.ChainConfig Difficulty *big.Int Origin common.Address Coinbase common.Address @@ -53,8 +52,8 @@ type Config struct { // sets defaults on the config func setDefaults(cfg *Config) { if cfg.ChainConfig == nil { - cfg.ChainConfig = ¶ms2.ChainConfig{ - ChainID: big.NewInt(int64(params2.MainNetChainID)), + cfg.ChainConfig = ¶ms.ChainConfig{ + ChainID: big.NewInt(int64(params.MainNetChainID)), HomesteadBlock: new(big.Int), DAOForkBlock: new(big.Int), DAOForkSupport: false, diff --git a/core/vm/stack_table.go b/core/vm/stack_table.go index 10c12901..a6b35f85 100644 --- a/core/vm/stack_table.go +++ b/core/vm/stack_table.go @@ -17,7 +17,7 @@ package vm import ( - "github.com/ethereum/go-ethereum/params" + "github.com/mapprotocol/atlas/params" ) func minSwapStack(n int) int {