Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 48 additions & 1 deletion pyjamaz/merkle.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import math
from functools import lru_cache
from math import ceil
from typing import List, Tuple, Optional
from pyjamaz.hashing import blake2b_256_hash, keccak_256_hash
Expand Down Expand Up @@ -42,12 +43,32 @@ def merkle(self, data: List[Tuple[bytes, bytes]], index: int = 0) -> bytes:
if len(data) == 0:
return b'\0' * 32
if len(data) == 1:
key, value = data[0]
if (
type(key) is bytes and type(value) is bytes
and self._leaf is _TRIE_LEAF_ENCODER
and blake2b_256_hash is _TRIE_HASH_FUNCTION
):
input_size = len(key) + len(value)
if input_size <= _TRIE_LEAF_CACHE_MAX_INPUT_BYTES:
return _cached_trie_leaf_hash(key, value)
if input_size <= _TRIE_LARGE_LEAF_CACHE_MAX_INPUT_BYTES:
return _cached_trie_large_leaf_hash(key, value)
encoded = self._leaf(*data[0])
else:
left, right = [], []
for key, value in data:
(right if self._bit(key, index) else left).append((key, value))
encoded = self._branch(self.merkle(left, index + 1), self.merkle(right, index + 1))
left_hash = self.merkle(left, index + 1)
right_hash = self.merkle(right, index + 1)
if (
type(left_hash) is bytes and type(right_hash) is bytes
and len(left_hash) == len(right_hash) == 32
and self._branch is _TRIE_BRANCH_ENCODER
and blake2b_256_hash is _TRIE_HASH_FUNCTION
):
return _cached_trie_branch_hash(left_hash, right_hash)
encoded = self._branch(left_hash, right_hash)

return blake2b_256_hash(encoded)

Expand All @@ -58,6 +79,32 @@ def root(self, index=0) -> bytes:
return self.merkle(self.data, index=index)


# Bound retained leaf inputs to 24 MiB: 4096 small entries of at most 4 KiB,
# plus 32 larger entries of at most 256 KiB for common program/preimage values.
# Branch inputs and results are fixed-size; entry counts bound Python overhead.
# Leaves larger than the admission limit are hashed without retaining them.
_TRIE_LEAF_CACHE_MAX_INPUT_BYTES = 4096
_TRIE_LARGE_LEAF_CACHE_MAX_INPUT_BYTES = 256 * 1024
_TRIE_LEAF_ENCODER = PatriciaMerkleTrie._leaf
_TRIE_BRANCH_ENCODER = PatriciaMerkleTrie._branch
_TRIE_HASH_FUNCTION = blake2b_256_hash


@lru_cache(maxsize=4096)
def _cached_trie_leaf_hash(key: bytes, value: bytes) -> bytes:
return _TRIE_HASH_FUNCTION(_TRIE_LEAF_ENCODER(key, value))


@lru_cache(maxsize=32)
def _cached_trie_large_leaf_hash(key: bytes, value: bytes) -> bytes:
return _TRIE_HASH_FUNCTION(_TRIE_LEAF_ENCODER(key, value))


@lru_cache(maxsize=8192)
def _cached_trie_branch_hash(left: bytes, right: bytes) -> bytes:
return _TRIE_HASH_FUNCTION(_TRIE_BRANCH_ENCODER(left, right))


class MerkleMountainRange:
# D
# / \
Expand Down
9 changes: 5 additions & 4 deletions pyjamaz/pvm/interpreters/numba/interpreter_numba_jit.py
Original file line number Diff line number Diff line change
Expand Up @@ -491,7 +491,8 @@ def invoke_native(

# Gas model: track current block start for block-based gas charging
current_block_start = current_block_start_in
skip_block_charge = skip_first_block_charge
# Resolve the entry PC even when the caller restored an already-paid block.
lookup_block = True
n_blocks = len(block_starts_sorted)

# Local state array for logging: [inst_nr, opcode, pc, gas, start_time]
Expand All @@ -517,7 +518,7 @@ def invoke_native(
return sync_state_and_return(reg, registers_out, state_out, EXIT_PANIC, pc, gas, inst_nr, exit_value, skip_len, ERROR_PANIC_TRAP, current_block_start)

# GP-0.8.0-eq:A.8 - charge when entering a new basic block
if n_blocks > 0:
if n_blocks > 0 and (lookup_block or current_block_start < 0):
block_idx = get_block_index_jit(block_starts_sorted, I32(pc))
if block_idx >= 0:
if current_block_start < 0:
Expand All @@ -528,8 +529,8 @@ def invoke_native(

current_block_start = block_starts_sorted[block_idx]

# Clear skip flag after first iteration (must be outside gas model check)
skip_block_charge = False
# Reuse the block until a terminator resets current_block_start.
lookup_block = False

# Fetch opcode and decode
opcode = code[pc]
Expand Down
2 changes: 1 addition & 1 deletion pyjamaz/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

TEST_SUITE = 'tiny' # tiny, full or fuzz
GP_VERSION = '0.8.0'
APP_VERSION = '0.3.8'
APP_VERSION = '0.3.9'


def _apply_jam_fuzz_spec_override():
Expand Down
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ py-bip39-bindings~=0.3
rocksdict~=0.3

bandersnatch-vrfs==0.6.3
jamcodec~=0.1.7
jamcodec~=0.1.8

numpy==2.3.5
numba==0.63.0
Expand Down
Loading