Skip to content

Commit

Permalink
Merge pull request #25 from m-Peter/eth-get-transaction-count-endpoint
Browse files Browse the repository at this point in the history
Implement the `eth_getTransactionCount` JSON-RPC endpoint
  • Loading branch information
franklywatson authored Jan 24, 2024
2 parents 1b1548a + 703476c commit 8881c00
Show file tree
Hide file tree
Showing 7 changed files with 185 additions and 72 deletions.
2 changes: 1 addition & 1 deletion api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ func (s *BlockChainAPI) GetTransactionCount(
address common.Address,
blockNumberOrHash *rpc.BlockNumberOrHash,
) (*hexutil.Uint64, error) {
nonce := uint64(1050510)
nonce := s.Store.GetAccountNonce(context.Background(), address)
return (*hexutil.Uint64)(&nonce), nil
}

Expand Down
48 changes: 47 additions & 1 deletion api/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ import (
"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/eth/filters"
"github.com/ethereum/go-ethereum/rpc"
"github.com/onflow/cadence"
"github.com/onflow/cadence/runtime/stdlib"
"github.com/onflow/flow-evm-gateway/api"
"github.com/onflow/flow-evm-gateway/storage"
"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -165,14 +167,58 @@ func TestBlockChainAPI(t *testing.T) {
key, _ := crypto.GenerateKey()
addr := crypto.PubkeyToAddress(key.PublicKey)
blockNumberOrHash := rpc.BlockNumberOrHashWithNumber(rpc.LatestBlockNumber)

txCount, err := blockchainAPI.GetTransactionCount(
context.Background(),
addr,
&blockNumberOrHash,
)
require.NoError(t, err)

nonce := uint64(1050510)
nonce := uint64(0)
assert.Equal(t, txCount, (*hexutil.Uint64)(&nonce))

evt := cadence.Event{
EventType: cadence.NewEventType(
stdlib.FlowLocation{},
"evm.TransactionExecuted",
[]cadence.Field{
cadence.NewField("blockHeight", cadence.UInt64Type{}),
cadence.NewField("transactionHash", cadence.StringType{}),
cadence.NewField("transaction", cadence.StringType{}),
cadence.NewField("failed", cadence.BoolType{}),
cadence.NewField("transactionType", cadence.UInt8Type{}),
cadence.NewField("gasConsumed", cadence.UInt64Type{}),
cadence.NewField("deployedContractAddress", cadence.StringType{}),
cadence.NewField("returnedValue", cadence.StringType{}),
cadence.NewField("logs", cadence.StringType{}),
},
nil,
),
Fields: []cadence.Value{
cadence.NewUInt64(3),
cadence.String("0xb47d74ea64221eb941490bdc0c9a404dacd0a8573379a45c992ac60ee3e83c3c"),
cadence.String("b88c02f88982029a01808083124f809499466ed2e37b892a2ee3e9cd55a98b68f5735db280a4c6888fa10000000000000000000000000000000000000000000000000000000000000006c001a0f84168f821b427dc158c4d8083bdc4b43e178cf0977a2c5eefbcbedcc4e351b0a066a747a38c6c266b9dc2136523cef04395918de37773db63d574aabde59c12eb"),
cadence.NewBool(false),
cadence.NewUInt8(2),
cadence.NewUInt64(22514),
cadence.String("0000000000000000000000000000000000000000"),
cadence.String("000000000000000000000000000000000000000000000000000000000000002a"),
cadence.String("f85af8589499466ed2e37b892a2ee3e9cd55a98b68f5735db2e1a024abdb5865df5079dcc5ac590ff6f01d5c16edbc5fab4e195d9febd1114503daa0000000000000000000000000000000000000000000000000000000000000002a"),
},
}

store := blockchainAPI.Store
store.UpdateAccountNonce(context.Background(), evt)

txCount, err = blockchainAPI.GetTransactionCount(
context.Background(),
common.HexToAddress("0x658Bdf435d810C91414eC09147DAA6DB62406379"),
&blockNumberOrHash,
)
require.NoError(t, err)

nonce = uint64(1)
assert.Equal(t, txCount, (*hexutil.Uint64)(&nonce))
})

Expand Down
68 changes: 34 additions & 34 deletions api/fixtures/eth_json_rpc_requests.json
Original file line number Diff line number Diff line change
@@ -1,35 +1,35 @@
{"jsonrpc":"2.0","id":1,"method":"eth_chainId","params": []}
{"jsonrpc":"2.0","id":2,"method":"eth_blockNumber","params": []}
{"jsonrpc":"2.0","id":3,"method":"eth_syncing","params": []}
{"jsonrpc":"2.0","id":4,"method":"eth_sendRawTransaction","params":["0xd46e8dd67c5d32be8d46e8dd67c5d32be8058bb8eb970870f072445675058bb8eb970870f072445675"]}
{"jsonrpc":"2.0","id":5,"method":"eth_gasPrice","params":[]}
{"jsonrpc":"2.0","id":6,"method":"eth_getBalance","params":["0x407d73d8a49eeb85d32cf465507dd71d507100c1","latest"]}
{"jsonrpc":"2.0","id":7,"method":"eth_getCode","params":["0xa94f5374fce5edbc8e2a8697c15331677e6ebf0b","0x2"]}
{"jsonrpc":"2.0","id":8,"method":"eth_getStorageAt","params":["0x295a70b2de5e3953354a6a8344e616ed314d7251","0x6661e9d6d8b923d5bbaab1b96e1dd51ff6ea2a93520fdc9eb75d059238b8c5e9","latest"]}
{"jsonrpc":"2.0","id":9,"method":"eth_getTransactionCount","params":["0x407d73d8a49eeb85d32cf465507dd71d507100c1","latest"]}
{"jsonrpc":"2.0","id":10,"method":"eth_getTransactionByHash","params":["0x88df016429689c079f3b2f6ad39fa052532c56795b733da78a91ebe6a713944b"]}
{"jsonrpc":"2.0","id":11,"method":"eth_getTransactionByBlockHashAndIndex","params":["0xc6ef2fc5426d6ad6fd9e2a26abeab0aa2411b7ab17f30a99d3cb96aed1d1055b", "0x0"]}
{"jsonrpc":"2.0","id":12,"method":"eth_getTransactionByBlockNumberAndIndex","params":["0x29c", "0x0"]}
{"jsonrpc":"2.0","id":13,"method":"eth_getTransactionReceipt","params":["0x85d995eba9763907fdf35cd2034144dd9d53ce32cbec21349d4b12823c6860c5"]}
{"jsonrpc":"2.0","id":14,"method":"eth_coinbase"}
{"jsonrpc":"2.0","id":15,"method":"eth_getBlockByHash","params":["0xdc0818cf78f21a8e70579cb46a43643f78291264dda342ae31049421c82d21ae",false]}
{"jsonrpc":"2.0","id":16,"method":"eth_getBlockByNumber","params":["0x1b4",true]}
{"jsonrpc":"2.0","id":17,"method":"eth_getBlockTransactionCountByHash","params":["0xb903239f8543d04b5dc1ba6579132b143087c68db1b2168786408fcbce568238"]}
{"jsonrpc":"2.0","id":18,"method":"eth_getBlockTransactionCountByNumber","params":["0xe8"]}
{"jsonrpc":"2.0","id":19,"method":"eth_getUncleCountByBlockHash","params":["0xb903239f8543d04b5dc1ba6579132b143087c68db1b2168786408fcbce568238"]}
{"jsonrpc":"2.0","id":20,"method":"eth_getUncleCountByBlockNumber","params":["0xe8"]}
{"jsonrpc":"2.0","id":21,"method":"eth_getLogs","params":[{"topics":["0x000000000000000000000000a94f5374fce5edbc8e2a8697c15331677e6ebf0b"]}]}
{"jsonrpc":"2.0","id":22,"method":"eth_newFilter","params":[{"topics":["0x000000000000000000000000a94f5374fce5edbc8e2a8697c15331677e6ebf0b"]}]}
{"jsonrpc":"2.0","id":23,"method":"eth_uninstallFilter","params":["0xb"]}
{"jsonrpc":"2.0","id":24,"method":"eth_getFilterLogs","params":["0x16"]}
{"jsonrpc":"2.0","id":25,"method":"eth_getFilterChanges","params":["0x16"]}
{"jsonrpc":"2.0","id":26,"method":"eth_newBlockFilter","params":[]}
{"jsonrpc":"2.0","id":27,"method":"eth_newPendingTransactionFilter","params":[]}
{"jsonrpc":"2.0","id":28,"method":"eth_accounts","params":[]}
{"jsonrpc":"2.0","id":29,"method":"eth_call","params":[{"from":"0xb60e8dd61c5d32be8058bb8eb970870f07233155","to":"0xd46e8dd67c5d32be8058bb8eb970870f07244567","gas":"0x76c0","gasPrice":"0x9184e72a000","value":"0x9184e72a","input":"0xd46e8dd67c5d32be8d46e8dd67c5d32be8058bb8eb970870f072445675058bb8eb970870f072445675"}]}
{"jsonrpc":"2.0","id":30,"method":"eth_estimateGas","params":[{"from":"0xb60e8dd61c5d32be8058bb8eb970870f07233155","to":"0xd46e8dd67c5d32be8058bb8eb970870f07244567","gas":"0x76c0","gasPrice":"0x9184e72a000","value":"0x9184e72a","input":"0xd46e8dd67c5d32be8d46e8dd67c5d32be8058bb8eb970870f072445675058bb8eb970870f072445675"}]}
{"jsonrpc":"2.0","id":31,"method":"eth_getUncleByBlockHashAndIndex","params":["0xc6ef2fc5426d6ad6fd9e2a26abeab0aa2411b7ab17f30a99d3cb96aed1d1055b", "0x45"]}
{"jsonrpc":"2.0","id":32,"method":"eth_getUncleByBlockNumberAndIndex","params":["0xe8", "0x45"]}
{"jsonrpc":"2.0","id":33,"method":"eth_sign","params":["0x9b2055d370f73ec7d8a03e965129118dc8f5bf83","0xdeadbeaf"]}
{"jsonrpc":"2.0","id":34,"method":"eth_signTransaction","params":[{"from":"0x3b7252d007059ffc82d16d022da3cbf9992d2f70", "to":"0x0f54f47bf9b8e317b214ccd6a7c3e38b893cd7f0", "value":"0x16345785d8a0000", "gasLimit":"0x5208", "gasPrice":"0x55ae82600"}]}
{"jsonrpc":"2.0","id":35,"method":"eth_sendTransaction","params":[{"from":"0x3b7252d007059ffc82d16d022da3cbf9992d2f70", "to":"0x0f54f47bf9b8e317b214ccd6a7c3e38b893cd7f0", "value":"0x16345785d8a0000", "gasLimit":"0x5208", "gasPrice":"0x55ae82600"}]}
{"jsonrpc":"2.0","id":1,"method":"eth_blockNumber","params": []}
{"jsonrpc":"2.0","id":1,"method":"eth_syncing","params": []}
{"jsonrpc":"2.0","id":1,"method":"eth_sendRawTransaction","params":["0xd46e8dd67c5d32be8d46e8dd67c5d32be8058bb8eb970870f072445675058bb8eb970870f072445675"]}
{"jsonrpc":"2.0","id":1,"method":"eth_gasPrice","params":[]}
{"jsonrpc":"2.0","id":1,"method":"eth_getBalance","params":["0x407d73d8a49eeb85d32cf465507dd71d507100c1","latest"]}
{"jsonrpc":"2.0","id":1,"method":"eth_getCode","params":["0xa94f5374fce5edbc8e2a8697c15331677e6ebf0b","0x2"]}
{"jsonrpc":"2.0","id":1,"method":"eth_getStorageAt","params":["0x295a70b2de5e3953354a6a8344e616ed314d7251","0x6661e9d6d8b923d5bbaab1b96e1dd51ff6ea2a93520fdc9eb75d059238b8c5e9","latest"]}
{"jsonrpc":"2.0","id":1,"method":"eth_getTransactionCount","params":["0x407d73d8a49eeb85d32cf465507dd71d507100c1","latest"]}
{"jsonrpc":"2.0","id":1,"method":"eth_getTransactionByHash","params":["0x88df016429689c079f3b2f6ad39fa052532c56795b733da78a91ebe6a713944b"]}
{"jsonrpc":"2.0","id":1,"method":"eth_getTransactionByBlockHashAndIndex","params":["0xc6ef2fc5426d6ad6fd9e2a26abeab0aa2411b7ab17f30a99d3cb96aed1d1055b", "0x0"]}
{"jsonrpc":"2.0","id":1,"method":"eth_getTransactionByBlockNumberAndIndex","params":["0x29c", "0x0"]}
{"jsonrpc":"2.0","id":1,"method":"eth_getTransactionReceipt","params":["0x85d995eba9763907fdf35cd2034144dd9d53ce32cbec21349d4b12823c6860c5"]}
{"jsonrpc":"2.0","id":1,"method":"eth_coinbase"}
{"jsonrpc":"2.0","id":1,"method":"eth_getBlockByHash","params":["0xdc0818cf78f21a8e70579cb46a43643f78291264dda342ae31049421c82d21ae",false]}
{"jsonrpc":"2.0","id":1,"method":"eth_getBlockByNumber","params":["0x1b4",true]}
{"jsonrpc":"2.0","id":1,"method":"eth_getBlockTransactionCountByHash","params":["0xb903239f8543d04b5dc1ba6579132b143087c68db1b2168786408fcbce568238"]}
{"jsonrpc":"2.0","id":1,"method":"eth_getBlockTransactionCountByNumber","params":["0xe8"]}
{"jsonrpc":"2.0","id":1,"method":"eth_getUncleCountByBlockHash","params":["0xb903239f8543d04b5dc1ba6579132b143087c68db1b2168786408fcbce568238"]}
{"jsonrpc":"2.0","id":1,"method":"eth_getUncleCountByBlockNumber","params":["0xe8"]}
{"jsonrpc":"2.0","id":1,"method":"eth_getLogs","params":[{"topics":["0x000000000000000000000000a94f5374fce5edbc8e2a8697c15331677e6ebf0b"]}]}
{"jsonrpc":"2.0","id":1,"method":"eth_newFilter","params":[{"topics":["0x000000000000000000000000a94f5374fce5edbc8e2a8697c15331677e6ebf0b"]}]}
{"jsonrpc":"2.0","id":1,"method":"eth_uninstallFilter","params":["0xb"]}
{"jsonrpc":"2.0","id":1,"method":"eth_getFilterLogs","params":["0x16"]}
{"jsonrpc":"2.0","id":1,"method":"eth_getFilterChanges","params":["0x16"]}
{"jsonrpc":"2.0","id":1,"method":"eth_newBlockFilter","params":[]}
{"jsonrpc":"2.0","id":1,"method":"eth_newPendingTransactionFilter","params":[]}
{"jsonrpc":"2.0","id":1,"method":"eth_accounts","params":[]}
{"jsonrpc":"2.0","id":1,"method":"eth_call","params":[{"from":"0xb60e8dd61c5d32be8058bb8eb970870f07233155","to":"0xd46e8dd67c5d32be8058bb8eb970870f07244567","gas":"0x76c0","gasPrice":"0x9184e72a000","value":"0x9184e72a","input":"0xd46e8dd67c5d32be8d46e8dd67c5d32be8058bb8eb970870f072445675058bb8eb970870f072445675"}]}
{"jsonrpc":"2.0","id":1,"method":"eth_estimateGas","params":[{"from":"0xb60e8dd61c5d32be8058bb8eb970870f07233155","to":"0xd46e8dd67c5d32be8058bb8eb970870f07244567","gas":"0x76c0","gasPrice":"0x9184e72a000","value":"0x9184e72a","input":"0xd46e8dd67c5d32be8d46e8dd67c5d32be8058bb8eb970870f072445675058bb8eb970870f072445675"}]}
{"jsonrpc":"2.0","id":1,"method":"eth_getUncleByBlockHashAndIndex","params":["0xc6ef2fc5426d6ad6fd9e2a26abeab0aa2411b7ab17f30a99d3cb96aed1d1055b", "0x45"]}
{"jsonrpc":"2.0","id":1,"method":"eth_getUncleByBlockNumberAndIndex","params":["0xe8", "0x45"]}
{"jsonrpc":"2.0","id":1,"method":"eth_sign","params":["0x9b2055d370f73ec7d8a03e965129118dc8f5bf83","0xdeadbeaf"]}
{"jsonrpc":"2.0","id":1,"method":"eth_signTransaction","params":[{"from":"0x3b7252d007059ffc82d16d022da3cbf9992d2f70", "to":"0x0f54f47bf9b8e317b214ccd6a7c3e38b893cd7f0", "value":"0x16345785d8a0000", "gasLimit":"0x5208", "gasPrice":"0x55ae82600"}]}
{"jsonrpc":"2.0","id":1,"method":"eth_sendTransaction","params":[{"from":"0x3b7252d007059ffc82d16d022da3cbf9992d2f70", "to":"0x0f54f47bf9b8e317b214ccd6a7c3e38b893cd7f0", "value":"0x16345785d8a0000", "gasLimit":"0x5208", "gasPrice":"0x55ae82600"}]}
Loading

0 comments on commit 8881c00

Please sign in to comment.