Skip to content

Commit

Permalink
Fix cacheable by tx hash caching group (#70)
Browse files Browse the repository at this point in the history
* Fix cacheable by tx hash caching group

* Add test case for eth_getTransactionReceipt method

* Add e2e test for error result scenario

* Added e2e tests for future blocks scenario

* Update CACHING.md

* Update CACHING.md
  • Loading branch information
evgeniy-scherbina authored Nov 15, 2023
1 parent de3431b commit 1070a75
Show file tree
Hide file tree
Showing 8 changed files with 638 additions and 12 deletions.
50 changes: 49 additions & 1 deletion architecture/CACHING.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ package provides two different middlewares:

## What requests are cached?

As of now we have 3 different groups of cacheable EVM methods:
As of now we have 4 different groups of cacheable EVM methods:
- cacheable by block number (for ex.: `eth_getBlockByNumber`)
- cacheable by block hash (for ex.: `eth_getBlockByHash`)
- static methods (for ex.: `eth_chainId`, `net_version`)
Expand Down Expand Up @@ -95,6 +95,54 @@ Cacheable by tx hash means that for specific:
- tx hash (which is part of params)
response won't change over time, so we can cache it indefinitely

`NOTE`: `eth_getTransactionByHash` has an unexpected behaviour, responses for `tx in mempool` and `tx in block` are different:

`tx in mempool` example:
```json
{
"jsonrpc": "2.0",
"id": 1,
"result": {
"blockHash": null,
"blockNumber": null,
"transactionIndex": null,
"from": "0x57852ef74abc9f0da78b49d16604bbf2d81c559e",
"gas": "0x5208",
...
}
}
```

`tx in block` example
```json
{
"jsonrpc": "2.0",
"id": 1,
"result": {
"blockHash": "0xcc62755636e265e1f40cc0ea757477a79a233b6a417e3a8813be2ffe6859c0aa",
"blockNumber": "0x7e8e5e",
"transactionIndex": "0x0",
"from": "0x57852ef74abc9f0da78b49d16604bbf2d81c559e",
"gas": "0x5208",
...
}
}
```

we can't cache `txs which is in mempool` (because response will change after `tx will be included in block`), so in source code we check if `tx is already in block`, and only if this is the case we cache the response

example how to check if tx is in a block:
```go
func (tx *tx) IsIncludedInBlock() bool {
return tx.BlockHash != nil &&
tx.BlockHash != "" &&
tx.BlockNumber != nil &&
tx.BlockNumber != "" &&
tx.TransactionIndex != nil &&
tx.TransactionIndex != ""
}
```

### Where to find list of methods for every group?

It can be found in source code: https://github.com/Kava-Labs/kava-proxy-service/blob/main/decode/evm_rpc.go
Expand Down
20 changes: 18 additions & 2 deletions docker/kava-validator/kava-validator-entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
# log all commands to stdout and stop the script on the first error
set -ex

evmFaucetMnemonic='canvas category slow immune screen van spirit ring blossom vanish mail pencil resource scan razor online gap void time marine topic swarm exhaust oak'
# Private Key in hex: 296da4e8defa5691077b310e10f0ed0ee4993e6418a0df86b155be5d24ae1b7c
# EVM Address in hex: 0x661C3ECC5bf3cdB64FC14c9fE9Fb64a21D24c51c

# exit early if geneis.json already exists
# which will happen if the kava docker container is stopped and later restarted
if test -f "/root/.kava/config/genesis.json"; then
Expand All @@ -14,17 +18,29 @@ else
# ensure evm api listens on all addresses
sed -i 's/address = "127.0.0.1:8545"/address = "0.0.0.0:8545"/g' /root/.kava/config/app.toml

# Replace stake with ukava
sed -in-place='' 's/stake/ukava/g' /root/.kava/config/genesis.json
# Replace the default evm denom of aphoton with ukava
sed -in-place='' 's/aphoton/akava/g' /root/.kava/config/genesis.json
sed -in-place='' 's/"max_gas": "-1"/"max_gas": "20000000"/' /root/.kava/config/genesis.json

# use the test backend to avoid prompts when storing and accessing keys
kava config keyring-backend test

# create an account for the delegator
kava keys add kava-localnet-delegator

# add the delegator account to the default genesis
kava add-genesis-account $(kava keys show kava-localnet-delegator -a) 1000000000stake
kava add-genesis-account $(kava keys show kava-localnet-delegator -a) 1000000000ukava

# create an account for the evm faucet
echo $evmFaucetMnemonic | kava keys add evm --eth --recover

# add the evm faucet account to the default genesis
kava add-genesis-account $(kava keys show evm -a) 1000000000ukava

# create genesis info for a validator staked by the delegator above
kava gentx kava-localnet-delegator 500000000stake \
kava gentx kava-localnet-delegator 500000000ukava \
--chain-id=localnet_7777-1 \
--moniker="kava-localnet-validator"

Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ go 1.20

require (
cosmossdk.io/math v1.0.0
github.com/cenkalti/backoff v2.2.1+incompatible
github.com/ethereum/go-ethereum v1.11.2
github.com/google/uuid v1.3.0
github.com/redis/go-redis/v9 v9.2.1
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ github.com/bsm/gomega v1.27.10 h1:yeMWxP2pV2fG3FgAODIY8EiRE3dy0aeFYt4l7wh6yKA=
github.com/btcsuite/btcd/btcec/v2 v2.2.0 h1:fzn1qaOt32TuLjFlkzYSsBC35Q3KUjT1SwPxiMSCF5k=
github.com/btcsuite/btcd/btcec/v2 v2.2.0/go.mod h1:U7MHm051Al6XmscBQ0BoNydpOTsFAn707034b5nY8zU=
github.com/btcsuite/btcd/chaincfg/chainhash v1.0.1 h1:q0rUy8C/TYNBQS1+CGKw68tLOFYSNEs0TFnxxnS9+4U=
github.com/cenkalti/backoff v2.2.1+incompatible h1:tNowT99t7UNflLxfYYSlKYsBpXdEet03Pg2g16Swow4=
github.com/cenkalti/backoff v2.2.1+incompatible/go.mod h1:90ReRw6GdpyfrHakVjL/QHaoyV4aDUVVkXQJJJ3NXXM=
github.com/cespare/xxhash/v2 v2.2.0 h1:DC2CZ1Ep5Y4k3ZQ899DldepgrayRUGE6BBZ/cd9Cj44=
github.com/cespare/xxhash/v2 v2.2.0/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
github.com/cockroachdb/errors v1.9.1 h1:yFVvsI0VxmRShfawbt/laCIDy/mtTqqnvoNgiy5bEV8=
Expand Down
Loading

0 comments on commit 1070a75

Please sign in to comment.