Skip to content

Commit

Permalink
update the constants for vm
Browse files Browse the repository at this point in the history
  • Loading branch information
mapdev33 committed Jan 24, 2024
1 parent c087592 commit 0353ffb
Show file tree
Hide file tree
Showing 9 changed files with 70 additions and 62 deletions.
56 changes: 29 additions & 27 deletions core/vm/contracts.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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"
)
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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)
Expand All @@ -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()
Expand All @@ -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
Expand Down Expand Up @@ -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) {
Expand All @@ -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) {
Expand All @@ -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) {
Expand All @@ -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) {
Expand Down Expand Up @@ -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) {
Expand All @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion core/vm/eips.go
Original file line number Diff line number Diff line change
Expand Up @@ -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){
Expand Down
18 changes: 9 additions & 9 deletions core/vm/evm.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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 {
Expand Down
20 changes: 10 additions & 10 deletions core/vm/gas_table.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand Down
21 changes: 14 additions & 7 deletions core/vm/instructions.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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,
Expand All @@ -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()
Expand Down
2 changes: 1 addition & 1 deletion core/vm/jump_table.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
package vm

import (
"github.com/ethereum/go-ethereum/params"
"github.com/mapprotocol/atlas/params"
)

type (
Expand Down
Loading

0 comments on commit 0353ffb

Please sign in to comment.