Skip to content

Commit

Permalink
Merge pull request #382 from oasisprotocol/kostko/fix/get-logs-latest
Browse files Browse the repository at this point in the history
Fix eth_getLogs filter with genesis/latest block height
  • Loading branch information
kostko committed Apr 13, 2023
2 parents f2b38f1 + 64b2b29 commit 862fe73
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 51 deletions.
42 changes: 33 additions & 9 deletions rpc/eth/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -596,6 +596,17 @@ func (api *publicAPI) GetTransactionReceipt(ctx context.Context, txHash common.H
return receipt, nil
}

// resolveLatest ensures that the special "latest round" marker is resolved into an actual value.
func (api *publicAPI) resolveLatest(ctx context.Context, round uint64) (uint64, error) {
switch round {
case client.RoundLatest:
// Resolve to actual latest round.
return api.backend.BlockNumber(ctx)
default:
return round, nil
}
}

// getStartEndRounds is a helper for fetching start and end rounds parameters.
func (api *publicAPI) getStartEndRounds(ctx context.Context, logger *logging.Logger, filter filters.FilterCriteria) (uint64, uint64, error) {
if filter.BlockHash != nil {
Expand All @@ -606,21 +617,34 @@ func (api *publicAPI) getStartEndRounds(ctx context.Context, logger *logging.Log
return round, round, nil
}

start := client.RoundLatest
end := client.RoundLatest
// Determine start round. If nothing passed, this is the genesis round.
blockNum := ethrpc.EarliestBlockNumber
if filter.FromBlock != nil {
round, err := api.roundParamFromBlockNum(ctx, logger, ethrpc.BlockNumber(filter.FromBlock.Int64()))
if err != nil {
return 0, 0, err
}
start = round
blockNum = ethrpc.BlockNumber(filter.FromBlock.Int64())
}
start, err := api.roundParamFromBlockNum(ctx, logger, blockNum)
if err != nil {
return 0, 0, err
}

// Determine end round. If nothing passed, this is the latest round.
end := client.RoundLatest
if filter.ToBlock != nil {
round, err := api.roundParamFromBlockNum(ctx, logger, ethrpc.BlockNumber(filter.ToBlock.Int64()))
end, err = api.roundParamFromBlockNum(ctx, logger, ethrpc.BlockNumber(filter.ToBlock.Int64()))
if err != nil {
return 0, 0, err
}
end = round
}

// Ensure we have concrete round numbers here before proceeding as these rounds are used to
// determine whether the range of blocks is too large.
start, err = api.resolveLatest(ctx, start)
if err != nil {
return 0, 0, err
}
end, err = api.resolveLatest(ctx, end)
if err != nil {
return 0, 0, err
}

return start, end, nil
Expand Down
44 changes: 24 additions & 20 deletions tests/rpc/rpc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -259,10 +259,6 @@ func TestEth_BlockNumber(t *testing.T) {
}

func TestEth_GetTransactionByHash(t *testing.T) {
if tests.TestsConfig.Gateway.ExposeOasisRPCs {
t.Skip("contract tests w/ c10lity require compat lib to be integrated")
return
}
ctx, cancel := context.WithTimeout(context.Background(), OasisBlockTimeout)
defer cancel()

Expand Down Expand Up @@ -296,10 +292,6 @@ func TestEth_GetTransactionByHash(t *testing.T) {
}

func TestEth_GetTransactionByBlockAndIndex(t *testing.T) {
if tests.TestsConfig.Gateway.ExposeOasisRPCs {
t.Skip("contract tests w/ c10lity require compat lib to be integrated")
return
}
ctx, cancel := context.WithTimeout(context.Background(), OasisBlockTimeout)
defer cancel()

Expand Down Expand Up @@ -333,10 +325,6 @@ func TestEth_GetTransactionByBlockAndIndex(t *testing.T) {
}

func TestEth_GetBlockByHashRawResponses(t *testing.T) {
if tests.TestsConfig.Gateway.ExposeOasisRPCs {
t.Skip("contract tests w/ c10lity require compat lib to be integrated")
return
}
ctx, cancel := context.WithTimeout(context.Background(), OasisBlockTimeout)
defer cancel()

Expand Down Expand Up @@ -374,10 +362,6 @@ func TestEth_GetBlockByHashRawResponses(t *testing.T) {
}

func TestEth_GetTransactionReceiptRawResponses(t *testing.T) {
if tests.TestsConfig.Gateway.ExposeOasisRPCs {
t.Skip("contract tests w/ c10lity require compat lib to be integrated")
return
}
ctx, cancel := context.WithTimeout(context.Background(), OasisBlockTimeout)
defer cancel()

Expand Down Expand Up @@ -407,6 +391,30 @@ func TestEth_GetLogsWithoutBlockhash(t *testing.T) {
require.NoError(t, err, "getLogs without explicit block hash")
}

func TestEth_GetLogsWithLatestHeight(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), OasisBlockTimeout)
defer cancel()

ec := localClient(t, false)

// Submit test transaction.
receipt := submitTestTransaction(ctx, t)
require.Equal(t, uint64(1), receipt.Status)
require.NotNil(t, receipt)

_, err := ec.FilterLogs(ctx, ethereum.FilterQuery{FromBlock: receipt.BlockNumber, ToBlock: nil})
require.NoError(t, err, "getLogs from specific block to latest block")
}

func TestEth_GetLogsWithGenesisHeight(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), OasisBlockTimeout)
defer cancel()

ec := localClient(t, false)
_, err := ec.FilterLogs(ctx, ethereum.FilterQuery{FromBlock: nil, ToBlock: big.NewInt(10)})
require.NoError(t, err, "getLogs from genesis block to specific block")
}

func TestEth_GetLogsWithFilters(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), OasisBlockTimeout)
defer cancel()
Expand Down Expand Up @@ -467,10 +475,6 @@ func TestEth_GetLogsWithFilters(t *testing.T) {
}

func TestEth_GetLogsMultiple(t *testing.T) {
if tests.TestsConfig.Gateway.ExposeOasisRPCs {
t.Skip("contract tests w/ c10lity require compat lib to be integrated")
return
}
ctx, cancel := context.WithTimeout(context.Background(), OasisBlockTimeout)
defer cancel()
ec := localClient(t, false)
Expand Down
32 changes: 10 additions & 22 deletions tests/rpc/tx_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,19 +130,11 @@ func testContractCreation(t *testing.T, value *big.Int) uint64 {
}

func TestContractCreation(t *testing.T) {
if tests.TestsConfig.Gateway.ExposeOasisRPCs {
t.Skip("contract tests w/ c10lity require compat lib to be integrated")
return
}
status := testContractCreation(t, big.NewInt(0))
require.Equal(t, uint64(1), status)
}

func TestContractFailCreation(t *testing.T) {
if tests.TestsConfig.Gateway.ExposeOasisRPCs {
t.Skip("contract tests w/ c10lity require compat lib to be integrated")
return
}
status := testContractCreation(t, big.NewInt(1))
require.Equal(t, uint64(0), status)
}
Expand Down Expand Up @@ -201,10 +193,6 @@ func TestEth_EstimateGas(t *testing.T) {
}

func TestEth_GetCode(t *testing.T) {
if tests.TestsConfig.Gateway.ExposeOasisRPCs {
t.Skip("contract tests w/ c10lity require compat lib to be integrated")
return
}
ec := localClient(t, false)

code := common.FromHex(strings.TrimSpace(evmSolTestCompiledHex))
Expand Down Expand Up @@ -236,7 +224,13 @@ func TestEth_GetCode(t *testing.T) {
t.Logf("SignedTx hash: %s", signedTx.Hash().Hex())
t.Logf("Contract address: %s", receipt.ContractAddress)

require.EqualValues(t, uint64(103630), receipt.GasUsed, "expected contract creation gas used")
// NOTE: Gas used may vary depending on the nonce value was so this may need updates if other
// tests submit more or less transactions.
if tests.TestsConfig.Gateway.ExposeOasisRPCs {
require.EqualValues(t, uint64(103629), receipt.GasUsed, "expected contract creation gas used")
} else {
require.EqualValues(t, uint64(103630), receipt.GasUsed, "expected contract creation gas used")
}
require.Equal(t, uint64(1), receipt.Status)

storedCode, err := ec.CodeAt(context.Background(), receipt.ContractAddress, nil)
Expand All @@ -245,10 +239,6 @@ func TestEth_GetCode(t *testing.T) {
}

func TestEth_Call(t *testing.T) {
if tests.TestsConfig.Gateway.ExposeOasisRPCs {
t.Skip("contract tests w/ c10lity require compat lib to be integrated")
return
}
abidata := `
[
{
Expand Down Expand Up @@ -333,10 +323,6 @@ func TestEth_Call(t *testing.T) {
// }
// }
func TestERC20(t *testing.T) {
if tests.TestsConfig.Gateway.ExposeOasisRPCs {
t.Skip("contract tests w/ c10lity require compat lib to be integrated")
return
}
testabi, _ := abi.JSON(strings.NewReader(erc20abi))

ec := localClient(t, false)
Expand Down Expand Up @@ -391,7 +377,9 @@ func TestERC20(t *testing.T) {
require.NoError(t, err)
require.Equal(t, uint64(1), receipt.Status)
require.NotEmpty(t, receipt.Logs, "ERC20-transfer receipt should contain the emitted log")
require.EqualValues(t, uint64(49700), receipt.GasUsed, "ERC20-transfer expected gas use")
// NOTE: Gas used may vary depending on the nonce value was so this may need updates if other
// tests submit more or less transactions.
require.EqualValues(t, uint64(49699), receipt.GasUsed, "ERC20-transfer expected gas use")

// Get balance of token receiver
balanceOfCall, err := testabi.Pack("balanceOf", common.Address{1})
Expand Down

0 comments on commit 862fe73

Please sign in to comment.