Skip to content

Commit

Permalink
fix lint, add notes
Browse files Browse the repository at this point in the history
  • Loading branch information
charles-cooper committed Jan 7, 2025
1 parent 76824f1 commit c189b40
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 16 deletions.
11 changes: 8 additions & 3 deletions boa/vm/py_evm.py
Original file line number Diff line number Diff line change
Expand Up @@ -415,9 +415,14 @@ def fork_rpc(self, rpc: RPC, block_identifier: str, force: bool = False, **kwarg
self.patch.timestamp = int(block_info["timestamp"], 16)
self.patch.block_number = int(block_info["number"], 16)
self.patch.chain_id = int(rpc.fetch("eth_chainId", []), 16)

self.patch.prev_hashes = [b"\x00" * 32] * 255 # placeholder not to fetch all prev hashes
self.patch.prev_hashes[0] = bytes.fromhex(block_info["parentHash"].removeprefix('0x')) # this one we fetched

# placeholder not to fetch all prev hashes
# (NOTE: we should document this)
self.patch.prev_hashes = [b"\x00" * 32] * 255
# this one we already fetched
self.patch.prev_hashes[0] = bytes.fromhex(
block_info["parentHash"].removeprefix("0x")
)

self.vm.state._account_db._rpc._init_db()

Expand Down
32 changes: 20 additions & 12 deletions tests/integration/fork/test_block_variables.py
Original file line number Diff line number Diff line change
@@ -1,49 +1,57 @@
import pytest

import boa


def test_block_number():
getter_contract = boa.loads("""
getter_contract = boa.loads(
"""
@external
def get_block_number() -> uint256:
return block.number
""")
"""
)
assert getter_contract.get_block_number() == boa.env.evm.patch.block_number


def test_block_timestamp():
getter_contract = boa.loads("""
getter_contract = boa.loads(
"""
@external
def get_block_timestamp() -> uint256:
return block.timestamp
""")
"""
)
assert getter_contract.get_block_timestamp() == boa.env.evm.patch.timestamp


def test_chain_id():
getter_contract = boa.loads("""
getter_contract = boa.loads(
"""
@external
def get_chain_id() -> uint256:
return chain.id
""")
"""
)
assert getter_contract.get_chain_id() == boa.env.evm.patch.chain_id


def test_prev_hash():
getter_contract = boa.loads("""
getter_contract = boa.loads(
"""
@external
def get_prevhash() -> bytes32:
return block.prevhash
""")
"""
)
assert getter_contract.get_prevhash() == list(boa.env.evm.patch.prev_hashes)[0]


def test_block_hash(): # only works for previous block
current_block = boa.env.evm.patch.block_number
getter_contract = boa.loads(f"""
getter_contract = boa.loads(
f"""
@external
def get_blockhash() -> bytes32:
return blockhash({current_block-1})
""")
"""
)
assert getter_contract.get_blockhash() == list(boa.env.evm.patch.prev_hashes)[0]
7 changes: 6 additions & 1 deletion tests/unitary/jupyter/test_browser.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,12 @@ def mock_fork(mock_callback):
mock_callback("evm_snapshot", "0x123456")
mock_callback("evm_revert", "0x12345678")
mock_callback("eth_chainId", "0x1")
data = {"number": "0x123", "timestamp": "0x65bbb460", "blockHash": "0x1234", "parentHash": "0x5678"}
data = {
"number": "0x123",
"timestamp": "0x65bbb460",
"blockHash": "0x1234",
"parentHash": "0x5678",
}
mock_callback("eth_getBlockByNumber", data)


Expand Down

0 comments on commit c189b40

Please sign in to comment.