Skip to content

Commit

Permalink
Minor patches to return valid data from ETH JSON-RPC (#762)
Browse files Browse the repository at this point in the history
validates using ETH RPC's length specifications:
https://eth.wiki/json-rpc/API#eth_getblockbyhash

add option to return earliest block on 0x0
The graph's own API doesn't handle it well when the block returned during it's origin search returns an invalid-block (since 0x0 doesn't exist on Neon)

The patch gives the option to return the earliest known block when 0x0 or 0 is passed to the eth_getBlockByNumber RPC
Enable using USE_EARLIEST_BLOCK_IF_0_PASSED env flag
  • Loading branch information
adiwajshing authored and afalaleev committed May 7, 2022
1 parent 5e2f054 commit 3dca0ad
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 9 deletions.
2 changes: 2 additions & 0 deletions proxy/environment.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@

EXTRA_GAS = int(os.environ.get("EXTRA_GAS", "0"))
LOG_NEON_CLI_DEBUG = os.environ.get("LOG_NEON_CLI_DEBUG", "NO") == "YES"
# uses the "earliest" tag if "0x0" or "0" is passed to the "eth_getBlockByNumber" RPC
USE_EARLIEST_BLOCK_IF_0_PASSED = os.environ.get("USE_EARLIEST_BLOCK_IF_0_PASSED", "NO") == "YES"
RETRY_ON_FAIL = int(os.environ.get("RETRY_ON_FAIL", "10"))
RETRY_ON_FAIL_ON_GETTING_CONFIRMED_TRANSACTION = max(int(os.environ.get("RETRY_ON_FAIL_ON_GETTING_CONFIRMED_TRANSACTION", "1000")), 1)
FUZZING_BLOCKHASH = os.environ.get("FUZZING_BLOCKHASH", "NO") == "YES"
Expand Down
8 changes: 5 additions & 3 deletions proxy/memdb/blocks_db.py
Original file line number Diff line number Diff line change
Expand Up @@ -286,13 +286,15 @@ def slot_hash(slot: int):
num_len = len(hex_num)
hex_num = '00' + hex_num.rjust(((num_len >> 1) + (num_len % 2)) << 1, '0')
return '0x' + hex_num.rjust(64, 'f')

hash = slot_hash(block_slot)
# TODO: return predictable information about block time
return SolanaBlockInfo(
slot=block_slot,
time=(block_time or 1),
hash=slot_hash(block_slot),
parent_hash=slot_hash(block_slot - 1),
hash=hash,
# return the prev block's computed hash if the block is not the first block
# if the block is the first block (0x0) -- then just return the hash of this block itself
parent_hash=slot_hash(block_slot - 1) if block_slot > 0 else hash,
is_fake=True
)

Expand Down
16 changes: 10 additions & 6 deletions proxy/neon_rpc_api_model/neon_rpc_api_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
from ..common_neon.utils import SolanaBlockInfo
from ..common_neon.types import NeonTxPrecheckResult, NeonEmulatingResult
from ..environment import SOLANA_URL, PP_SOLANA_URL, PYTH_MAPPING_ACCOUNT, NEON_EVM_VERSION, NEON_EVM_REVISION, \
CHAIN_ID, neon_cli, EVM_STEP_COUNT
CHAIN_ID, USE_EARLIEST_BLOCK_IF_0_PASSED, neon_cli, EVM_STEP_COUNT
from ..memdb.memdb import MemDB
from ..common_neon.gas_price_calculator import GasPriceCalculator
from ..statistics_exporter.proxy_metrics_interface import StatisticsExporter
Expand Down Expand Up @@ -115,10 +115,13 @@ def eth_estimateGas(self, param: dict) -> str:
def __repr__(self):
return str(self.__dict__)

def _should_return_starting_block(self, tag) -> bool:
return tag == 'earliest' \
or ((tag == '0x0' or str(tag) == '0') and USE_EARLIEST_BLOCK_IF_0_PASSED)
def _process_block_tag(self, tag) -> SolanaBlockInfo:
if tag in ("latest", "pending"):
block = self._db.get_latest_block()
elif tag == 'earliest':
elif self._should_return_starting_block(tag):
block = self._db.get_starting_block()
elif isinstance(tag, str):
try:
Expand Down Expand Up @@ -271,15 +274,16 @@ def _get_block_by_slot(self, block: SolanaBlockInfo, full: bool, skip_transactio
"gasLimit": '0xec8563e271ac',
"transactionsRoot": '0x' + '0' * 63 + '1',
"receiptsRoot": '0x' + '0' * 63 + '1',
"stateRoot": '0x' + '0' * 64 + '1',
"stateRoot": '0x' + '0' * 63 + '1',

"uncles": [],
"sha3Uncles": '0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347',

"miner": '0x' + '0' * 40,
"nonce": '0x0',
"mixHash": '0x0',
"size": '0x0',
# 8 byte nonce
"nonce": '0x0000000000000000',
"mixHash": '0x' + '0' * 63 + '1',
"size": '0x' + '0' * 63 + '1',

"gasUsed": hex(gas_used),
"hash": block.hash,
Expand Down

0 comments on commit 3dca0ad

Please sign in to comment.