Skip to content

Commit

Permalink
Merge pull request #3148 from jorgemmsilva/fix/effective-gas-price
Browse files Browse the repository at this point in the history
fix: fill effective gas price on evm receipts
  • Loading branch information
jorgemmsilva authored Dec 11, 2023
2 parents 5ab9d86 + 3745c56 commit aa8a3fb
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 10 deletions.
2 changes: 2 additions & 0 deletions packages/evm/jsonrpc/chainbackend.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"github.com/iotaledger/wasp/packages/parameters"
"github.com/iotaledger/wasp/packages/state"
"github.com/iotaledger/wasp/packages/trie"
"github.com/iotaledger/wasp/packages/vm/gas"
)

// ChainBackend provides access to the underlying ISC chain.
Expand All @@ -23,6 +24,7 @@ type ChainBackend interface {
EVMCall(aliasOutput *isc.AliasOutputWithID, callMsg ethereum.CallMsg) ([]byte, error)
EVMEstimateGas(aliasOutput *isc.AliasOutputWithID, callMsg ethereum.CallMsg) (uint64, error)
EVMTraceTransaction(aliasOutput *isc.AliasOutputWithID, blockTime time.Time, iscRequestsInBlock []isc.Request, txIndex uint64, tracer tracers.Tracer) error
FeePolicy(blockIndex uint32) (*gas.FeePolicy, error)
ISCChainID() *isc.ChainID
ISCCallView(chainState state.State, scName string, funName string, args dict.Dict) (dict.Dict, error)
ISCLatestAliasOutput() (*isc.AliasOutputWithID, error)
Expand Down
4 changes: 4 additions & 0 deletions packages/evm/jsonrpc/jsonrpctest/jsonrpc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
"github.com/iotaledger/wasp/packages/evm/evmutil"
"github.com/iotaledger/wasp/packages/evm/jsonrpc"
"github.com/iotaledger/wasp/packages/isc"
"github.com/iotaledger/wasp/packages/parameters"
"github.com/iotaledger/wasp/packages/solo"
"github.com/iotaledger/wasp/packages/testutil/testlogger"
"github.com/iotaledger/wasp/packages/vm/core/evm"
Expand Down Expand Up @@ -337,6 +338,9 @@ func TestRPCGetTxReceipt(t *testing.T) {
require.EqualValues(t, big.NewInt(2), receipt.BlockNumber)
require.EqualValues(t, env.BlockByNumber(big.NewInt(2)).Hash(), receipt.BlockHash)
require.EqualValues(t, 0, receipt.TransactionIndex)

expectedGasPrice := env.soloChain.GetGasFeePolicy().GasPriceWei(parameters.L1().BaseToken.Decimals)
require.EqualValues(t, expectedGasPrice, receipt.EffectiveGasPrice)
}

func TestRPCGetTxReceiptMissing(t *testing.T) {
Expand Down
11 changes: 9 additions & 2 deletions packages/evm/jsonrpc/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
"github.com/iotaledger/wasp/packages/evm/evmerrors"
"github.com/iotaledger/wasp/packages/isc"
"github.com/iotaledger/wasp/packages/metrics"
"github.com/iotaledger/wasp/packages/parameters"
vmerrors "github.com/iotaledger/wasp/packages/vm/core/errors"
)

Expand Down Expand Up @@ -194,11 +195,17 @@ func (e *EthService) GetTransactionReceipt(txHash common.Hash) (map[string]inter
if r == nil {
return nil, nil
}
tx, _, _, _, err := e.evmChain.TransactionByHash(txHash)
tx, _, blockNumber, _, err := e.evmChain.TransactionByHash(txHash)
if err != nil {
return nil, e.resolveError(err)
}
return RPCMarshalReceipt(r, tx), nil
// get fee policy at the same block and calculate effectiveGasPrice
feePolicy, err := e.evmChain.backend.FeePolicy(uint32(blockNumber))
if err != nil {
return nil, err
}
effectiveGasPrice := feePolicy.GasPriceWei(parameters.L1().BaseToken.Decimals)
return RPCMarshalReceipt(r, tx, effectiveGasPrice), nil
})
}

Expand Down
4 changes: 3 additions & 1 deletion packages/evm/jsonrpc/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ func parseBlockNumber(bn rpc.BlockNumber) *big.Int {
return big.NewInt(n)
}

func RPCMarshalReceipt(r *types.Receipt, tx *types.Transaction) map[string]interface{} {
func RPCMarshalReceipt(r *types.Receipt, tx *types.Transaction, effectiveGasPrice *big.Int) map[string]interface{} {
// fix for an already fixed bug where some old failed receipts contain non-empty logs
if r.Status != types.ReceiptStatusSuccessful {
r.Logs = []*types.Log{}
Expand All @@ -174,10 +174,12 @@ func RPCMarshalReceipt(r *types.Receipt, tx *types.Transaction) map[string]inter
"to": tx.To(),
"cumulativeGasUsed": hexutil.Uint64(r.CumulativeGasUsed),
"gasUsed": hexutil.Uint64(r.GasUsed),
"effectiveGasPrice": hexutil.EncodeBig(effectiveGasPrice),
"contractAddress": r.ContractAddress,
"logs": rpcMarshalLogs(r),
"logsBloom": r.Bloom,
"status": hexutil.Uint64(r.Status),
"type": hexutil.Uint64(types.LegacyTxType),
}
}

Expand Down
16 changes: 9 additions & 7 deletions packages/evm/jsonrpc/waspevmbackend.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,12 @@ import (
"github.com/iotaledger/wasp/packages/chainutil"
"github.com/iotaledger/wasp/packages/cryptolib"
"github.com/iotaledger/wasp/packages/isc"
"github.com/iotaledger/wasp/packages/kv/codec"
"github.com/iotaledger/wasp/packages/kv/dict"
"github.com/iotaledger/wasp/packages/parameters"
"github.com/iotaledger/wasp/packages/state"
"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"
)

// WaspEVMBackend is the implementation of [ChainBackend] for the production environment.
Expand All @@ -43,13 +42,16 @@ func NewWaspEVMBackend(ch chain.Chain, nodePubKey *cryptolib.PublicKey, baseToke
}
}

func (b *WaspEVMBackend) EVMGasRatio() (util.Ratio32, error) {
// TODO: Cache the gas ratio?
ret, err := b.ISCCallView(b.ISCLatestState(), governance.Contract.Name, governance.ViewGetEVMGasRatio.Name, nil)
func (b *WaspEVMBackend) FeePolicy(blockIndex uint32) (*gas.FeePolicy, error) {
state, err := b.ISCStateByBlockIndex(blockIndex)
if err != nil {
return util.Ratio32{}, err
return nil, err
}
return codec.DecodeRatio32(ret.Get(governance.ParamEVMGasRatio))
ret, err := b.ISCCallView(state, governance.Contract.Name, governance.ViewGetFeePolicy.Name, nil)
if err != nil {
return nil, err
}
return gas.FeePolicyFromBytes(ret.Get(governance.ParamFeePolicyBytes))
}

func (b *WaspEVMBackend) EVMSendTransaction(tx *types.Transaction) error {
Expand Down
14 changes: 14 additions & 0 deletions packages/solo/evm.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ import (
"github.com/iotaledger/wasp/packages/parameters"
"github.com/iotaledger/wasp/packages/state"
"github.com/iotaledger/wasp/packages/trie"
"github.com/iotaledger/wasp/packages/vm/core/governance"
"github.com/iotaledger/wasp/packages/vm/gas"
)

// jsonRPCSoloBackend is the implementation of [jsonrpc.ChainBackend] for Solo
Expand All @@ -35,6 +37,18 @@ func newJSONRPCSoloBackend(chain *Chain, baseToken *parameters.BaseToken) jsonrp
return &jsonRPCSoloBackend{Chain: chain, baseToken: baseToken}
}

func (b *jsonRPCSoloBackend) FeePolicy(blockIndex uint32) (*gas.FeePolicy, error) {
state, err := b.ISCStateByBlockIndex(blockIndex)
if err != nil {
return nil, err
}
ret, err := b.ISCCallView(state, governance.Contract.Name, governance.ViewGetFeePolicy.Name, nil)
if err != nil {
return nil, err
}
return gas.FeePolicyFromBytes(ret.Get(governance.ParamFeePolicyBytes))
}

func (b *jsonRPCSoloBackend) EVMSendTransaction(tx *types.Transaction) error {
_, err := b.Chain.PostEthereumTransaction(tx)
return err
Expand Down

0 comments on commit aa8a3fb

Please sign in to comment.