Skip to content

Commit

Permalink
Add latest to blockHashOrNumber calls, force null values for empty …
Browse files Browse the repository at this point in the history
…contractAddresses, change FakeTX OpCode to CALL
  • Loading branch information
lmoe committed Oct 22, 2024
1 parent e503014 commit 3bc8146
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 26 deletions.
47 changes: 24 additions & 23 deletions packages/evm/jsonrpc/evmchain.go
Original file line number Diff line number Diff line change
Expand Up @@ -804,19 +804,29 @@ func (e *EVMChain) TraceBlockByNumber(blockNumber uint64, config *tracers.TraceC
return e.traceBlock(config, block)
}

func (e *EVMChain) GetRawBlock(blockNrOrHash rpc.BlockNumberOrHash) (hexutil.Bytes, error) {
var block *types.Block
var err error

func (e *EVMChain) getBlockByNumberOrHash(blockNrOrHash rpc.BlockNumberOrHash) (*types.Block, error) {
if h, ok := blockNrOrHash.Hash(); ok {
block = e.BlockByHash(h)
return e.BlockByHash(h), nil
} else if n, ok := blockNrOrHash.Number(); ok {
block, err = e.BlockByNumber(big.NewInt(n.Int64()))
if err != nil {
return nil, err
switch n {
case rpc.LatestBlockNumber:
return e.BlockByNumber(nil)
default:
if n < 0 {
return nil, fmt.Errorf("%v is unsupported", blockNrOrHash.String())
}

return e.BlockByNumber(big.NewInt(n.Int64()))
}
} else {
return nil, fmt.Errorf("block not found: %v", blockNrOrHash.String())
}

return nil, fmt.Errorf("block not found: %v", blockNrOrHash.String())
}

func (e *EVMChain) GetRawBlock(blockNrOrHash rpc.BlockNumberOrHash) (hexutil.Bytes, error) {
block, err := e.getBlockByNumberOrHash(blockNrOrHash)
if err != nil {
return nil, err
}

return rlp.EncodeToBytes(block)
Expand All @@ -825,21 +835,12 @@ func (e *EVMChain) GetRawBlock(blockNrOrHash rpc.BlockNumberOrHash) (hexutil.Byt
func (e *EVMChain) GetBlockReceipts(blockNrOrHash rpc.BlockNumberOrHash) ([]*types.Receipt, []*types.Transaction, error) {
e.log.Debugf("GetBlockReceipts(blockNumber=%v)", blockNrOrHash.String())

var block *types.Block
var err error

if h, ok := blockNrOrHash.Hash(); ok {
block = e.BlockByHash(h)
} else if n, ok := blockNrOrHash.Number(); ok {
block, err = e.BlockByNumber(parseBlockNumber(n))
if err != nil {
return nil, nil, err
}
} else {
return nil, nil, fmt.Errorf("block not found: %v", blockNrOrHash.String())
block, err := e.getBlockByNumberOrHash(blockNrOrHash)
if err != nil {
return nil, nil, err
}

chainState, err := e.iscStateFromEVMBlockNumberOrHash(&blockNrOrHash)
chainState, err := e.iscStateFromEVMBlockNumber(block.Number())
if err != nil {
return nil, nil, err
}
Expand Down
23 changes: 23 additions & 0 deletions packages/evm/jsonrpc/jsonrpctest/jsonrpc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -944,6 +944,29 @@ func TestRPCBlockReceipt(t *testing.T) {
require.Equal(t, big.NewInt(4), r1.BlockNumber)
require.Equal(t, uint64(1), r2.Status)
require.Equal(t, big.NewInt(4), r2.BlockNumber)

// Test "latest" block
err = env.RawClient.CallContext(
context.Background(),
&receipts,
"eth_getBlockReceipts",
"latest")
require.NoError(t, err)

require.Len(t, receipts, 2)

r1 = receipts[slices.IndexFunc(receipts, func(v *types.Receipt) bool {
return v.TxHash == tx1.Hash()
})]

r2 = receipts[slices.IndexFunc(receipts, func(v *types.Receipt) bool {
return v.TxHash == tx2.Hash()
})]

require.Equal(t, uint64(1), r1.Status)
require.Equal(t, big.NewInt(4), r1.BlockNumber)
require.Equal(t, uint64(1), r2.Status)
require.Equal(t, big.NewInt(4), r2.BlockNumber)
}

func BenchmarkRPCEstimateGas(b *testing.B) {
Expand Down
16 changes: 13 additions & 3 deletions packages/evm/jsonrpc/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"errors"
"fmt"
"math/big"
"slices"

"github.com/ethereum/go-ethereum"
"github.com/ethereum/go-ethereum/accounts/abi"
Expand Down Expand Up @@ -158,7 +159,7 @@ func parseBlockNumber(bn rpc.BlockNumber) *big.Int {
return big.NewInt(n)
}

const FakeTxOpcode = "STOP"
const FakeTxOpcode = "CALL"

func RPCMarshalTransactionTraceForFakeTX(tx *types.Transaction, effectiveGasPrice *big.Int) map[string]interface{} {
return map[string]interface{}{
Expand All @@ -167,6 +168,7 @@ func RPCMarshalTransactionTraceForFakeTX(tx *types.Transaction, effectiveGasPric
"gasUsed": hexutil.Uint64(tx.Gas()),
"to": tx.To(),
"type": FakeTxOpcode,
"input": "0x",
"value": hexutil.Big(*tx.Value()),
}
}
Expand All @@ -178,7 +180,7 @@ func RPCMarshalReceipt(r *types.Receipt, tx *types.Transaction, effectiveGasPric
r.Bloom = types.CreateBloom(types.Receipts{r})
}

return map[string]interface{}{
result := map[string]interface{}{
"transactionHash": r.TxHash,
"transactionIndex": hexutil.Uint64(r.TransactionIndex),
"blockHash": r.BlockHash,
Expand All @@ -188,12 +190,20 @@ func RPCMarshalReceipt(r *types.Receipt, tx *types.Transaction, effectiveGasPric
"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),
}

// Eth compatibility. Return "null" instead of "0x00000000000000000000000..."
if slices.Equal(r.ContractAddress.Bytes(), common.Address{}.Bytes()) {
result["contractAddress"] = nil
} else {
result["contractAddress"] = r.ContractAddress
}

return result
}

func rpcMarshalLogs(r *types.Receipt) []interface{} {
Expand Down

0 comments on commit 3bc8146

Please sign in to comment.