Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/develop' into mempool-proposal-d…
Browse files Browse the repository at this point in the history
…elay
  • Loading branch information
kape1395 committed Apr 26, 2023
2 parents 672fdc5 + 78fdaa5 commit d86a501
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 35 deletions.
2 changes: 0 additions & 2 deletions packages/evm/jsonrpc/chainbackend.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
package jsonrpc

import (
"math/big"
"time"

"github.com/ethereum/go-ethereum"
Expand All @@ -29,6 +28,5 @@ type ChainBackend interface {
ISCLatestState() state.State
ISCStateByBlockIndex(blockIndex uint32) (state.State, error)
ISCStateByTrieRoot(trieRoot trie.Hash) (state.State, error)
EVMGasPrice() *big.Int
BaseToken() *parameters.BaseToken
}
17 changes: 16 additions & 1 deletion packages/evm/jsonrpc/evmchain.go
Original file line number Diff line number Diff line change
Expand Up @@ -404,7 +404,22 @@ func (e *EVMChain) EstimateGas(callMsg ethereum.CallMsg, blockNumberOrHash *rpc.

func (e *EVMChain) GasPrice() *big.Int {
e.log.Debugf("GasPrice()")
return e.backend.EVMGasPrice()

iscState := e.backend.ISCLatestState()
governancePartition := subrealm.NewReadOnly(iscState, kv.Key(governance.Contract.Hname().Bytes()))
feePolicy := governance.MustGetGasFeePolicy(governancePartition)

// convert to wei (18 decimals)
decimalsDifference := 18 - parameters.L1().BaseToken.Decimals
price := big.NewInt(10)
price.Exp(price, new(big.Int).SetUint64(uint64(decimalsDifference)), nil)

price.Mul(price, new(big.Int).SetUint64(uint64(feePolicy.GasPerToken.B)))
price.Div(price, new(big.Int).SetUint64(uint64(feePolicy.GasPerToken.A)))
price.Mul(price, new(big.Int).SetUint64(uint64(feePolicy.EVMGasRatio.A)))
price.Div(price, new(big.Int).SetUint64(uint64(feePolicy.EVMGasRatio.B)))

return price
}

func (e *EVMChain) StorageAt(address common.Address, key common.Hash, blockNumberOrHash *rpc.BlockNumberOrHash) (common.Hash, error) {
Expand Down
26 changes: 0 additions & 26 deletions packages/evm/jsonrpc/waspevmbackend.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ package jsonrpc

import (
"fmt"
"math/big"
"sync"
"time"

Expand All @@ -26,7 +25,6 @@ import (
"github.com/iotaledger/wasp/packages/trie"
"github.com/iotaledger/wasp/packages/util"
"github.com/iotaledger/wasp/packages/vm/core/governance"
"github.com/iotaledger/wasp/packages/vm/gas"
)

type WaspEVMBackend struct {
Expand Down Expand Up @@ -120,30 +118,6 @@ func (b *WaspEVMBackend) EVMTraceTransaction(
)
}

func (b *WaspEVMBackend) EVMGasPrice() *big.Int {
latestState := b.ISCLatestState()
res, err := chainutil.CallView(latestState, b.chain, governance.Contract.Hname(), governance.ViewGetFeePolicy.Hname(), nil)
if err != nil {
panic(fmt.Sprintf("couldn't call gasFeePolicy view: %s ", err.Error()))
}
feePolicy, err := gas.FeePolicyFromBytes(res.Get(governance.ParamFeePolicyBytes))
if err != nil {
panic(fmt.Sprintf("couldn't decode fee policy: %s ", err.Error()))
}

// convert to wei (18 decimals)
decimalsDifference := 18 - parameters.L1().BaseToken.Decimals
price := big.NewInt(10)
price.Exp(price, new(big.Int).SetUint64(uint64(decimalsDifference)), nil)

price.Mul(price, new(big.Int).SetUint64(uint64(feePolicy.EVMGasRatio.A)))
price.Div(price, new(big.Int).SetUint64(uint64(feePolicy.EVMGasRatio.B)))
price.Mul(price, new(big.Int).SetUint64(uint64(feePolicy.GasPerToken.A)))
price.Div(price, new(big.Int).SetUint64(uint64(feePolicy.GasPerToken.B)))

return price
}

func (b *WaspEVMBackend) ISCCallView(chainState state.State, scName, funName string, args dict.Dict) (dict.Dict, error) {
return chainutil.CallView(chainState, b.chain, isc.Hn(scName), isc.Hn(funName), args)
}
Expand Down
5 changes: 0 additions & 5 deletions packages/solo/evm.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package solo
import (
"crypto/ecdsa"
"fmt"
"math/big"
"time"

"github.com/ethereum/go-ethereum"
Expand Down Expand Up @@ -65,10 +64,6 @@ func (b *jsonRPCSoloBackend) EVMTraceTransaction(
)
}

func (b *jsonRPCSoloBackend) EVMGasPrice() *big.Int {
return big.NewInt(0)
}

func (b *jsonRPCSoloBackend) ISCCallView(chainState state.State, scName, funName string, args dict.Dict) (dict.Dict, error) {
return b.Chain.CallViewAtState(chainState, scName, funName, args)
}
Expand Down
27 changes: 27 additions & 0 deletions packages/vm/core/evm/evmtest/evm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1776,6 +1776,33 @@ func TestEVMCallViewGas(t *testing.T) {
require.NoError(t, err)
}

func TestGasPrice(t *testing.T) {
env := initEVM(t)

price1 := env.evmChain.GasPrice().Uint64()
require.NotZero(t, price1)

{
feePolicy := env.soloChain.GetGasFeePolicy()
feePolicy.GasPerToken.B *= 2 // 1 gas is paid with 2 tokens
err := env.setFeePolicy(*feePolicy)
require.NoError(t, err)
}

price2 := env.evmChain.GasPrice().Uint64()
require.EqualValues(t, price1*2, price2)

{
feePolicy := env.soloChain.GetGasFeePolicy()
feePolicy.EVMGasRatio.A *= 2 // 1 EVM gas unit consumes 2 ISC gas units
err := env.setFeePolicy(*feePolicy)
require.NoError(t, err)
}

price3 := env.evmChain.GasPrice().Uint64()
require.EqualValues(t, price2*2, price3)
}

func TestTraceTransaction(t *testing.T) {
env := initEVM(t)
ethKey, ethAddr := env.soloChain.NewEthereumAccountWithL2Funds()
Expand Down
2 changes: 1 addition & 1 deletion packages/vm/core/evm/evmtest/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ func (e *soloChainEnv) setEVMGasRatio(newGasRatio util.Ratio32, opts ...iscCallO
return err
}

func (e *soloChainEnv) setFeePolicy(p gas.FeePolicy, opts ...iscCallOptions) error {
func (e *soloChainEnv) setFeePolicy(p gas.FeePolicy, opts ...iscCallOptions) error { //nolint:unparam
opt := e.parseISCCallOptions(opts)
req := solo.NewCallParams(
governance.Contract.Name, governance.FuncSetFeePolicy.Name,
Expand Down

0 comments on commit d86a501

Please sign in to comment.