Skip to content

Commit

Permalink
Merge pull request #968 from SamWilsn/markdown
Browse files Browse the repository at this point in the history
Markdown & RLP
  • Loading branch information
petertdavies authored Aug 13, 2024
2 parents 44ceab2 + 153e6f4 commit fcd1275
Show file tree
Hide file tree
Showing 49 changed files with 521 additions and 350 deletions.
2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ test =
lint =
types-setuptools>=68.1.0.1,<69
isort==5.13.2
mypy==1.5.1
mypy==1.10.0
black==23.12.0
flake8==6.1.0
flake8-bugbear==23.12.2
Expand Down
4 changes: 2 additions & 2 deletions src/ethereum/arrow_glacier/fork.py
Original file line number Diff line number Diff line change
Expand Up @@ -335,7 +335,7 @@ def generate_header_hash_for_pow(header: Header) -> Hash32:
hash : `Hash32`
The PoW valid rlp hash of the passed in header.
"""
header_data_without_pow_artefacts = [
header_data_without_pow_artefacts = (
header.parent_hash,
header.ommers_hash,
header.coinbase,
Expand All @@ -350,7 +350,7 @@ def generate_header_hash_for_pow(header: Header) -> Hash32:
header.timestamp,
header.extra_data,
header.base_fee_per_gas,
]
)

return rlp.rlp_hash(header_data_without_pow_artefacts)

Expand Down
15 changes: 7 additions & 8 deletions src/ethereum/arrow_glacier/trie.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
Sequence,
TypeVar,
Union,
cast,
)

from ethereum.crypto.hash import keccak256
Expand Down Expand Up @@ -78,7 +77,7 @@ class LeafNode:
"""Leaf node in the Merkle Trie"""

rest_of_key: Bytes
value: rlp.RLP
value: rlp.Extended


@slotted_freezable
Expand All @@ -87,22 +86,22 @@ class ExtensionNode:
"""Extension node in the Merkle Trie"""

key_segment: Bytes
subnode: rlp.RLP
subnode: rlp.Extended


@slotted_freezable
@dataclass
class BranchNode:
"""Branch node in the Merkle Trie"""

subnodes: List[rlp.RLP]
value: rlp.RLP
subnodes: List[rlp.Extended]
value: rlp.Extended


InternalNode = Union[LeafNode, ExtensionNode, BranchNode]


def encode_internal_node(node: Optional[InternalNode]) -> rlp.RLP:
def encode_internal_node(node: Optional[InternalNode]) -> rlp.Extended:
"""
Encodes a Merkle Trie node into its RLP form. The RLP will then be
serialized into a `Bytes` and hashed unless it is less that 32 bytes
Expand All @@ -121,7 +120,7 @@ def encode_internal_node(node: Optional[InternalNode]) -> rlp.RLP:
encoded : `rlp.RLP`
The node encoded as RLP.
"""
unencoded: rlp.RLP
unencoded: rlp.Extended
if node is None:
unencoded = b""
elif isinstance(node, LeafNode):
Expand Down Expand Up @@ -156,7 +155,7 @@ def encode_node(node: Node, storage_root: Optional[Bytes] = None) -> Bytes:
assert storage_root is not None
return encode_account(node, storage_root)
elif isinstance(node, (LegacyTransaction, Receipt, U256)):
return rlp.encode(cast(rlp.RLP, node))
return rlp.encode(node)
elif isinstance(node, Bytes):
return node
else:
Expand Down
23 changes: 12 additions & 11 deletions src/ethereum/base_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -652,6 +652,18 @@ def to_be_bytes(self) -> "Bytes":

# TODO: Implement neg, pos, abs ...

@classmethod
def from_be_bytes(cls: Type[T], buffer: "Bytes") -> T:
"""
Converts a sequence of bytes into a fixed sized unsigned integer
from its big endian representation.
"""
max_length = (7 + cls.MAX_VALUE.bit_length()) // 8
if len(buffer) > max_length:
raise ValueError()

return cls(int.from_bytes(buffer, "big"))


class U256(FixedUint):
"""
Expand All @@ -665,17 +677,6 @@ class U256(FixedUint):

__slots__ = ()

@classmethod
def from_be_bytes(cls: Type, buffer: "Bytes") -> "U256":
"""
Converts a sequence of bytes into a fixed sized unsigned integer
from its big endian representation.
"""
if len(buffer) > 32:
raise ValueError()

return cls(int.from_bytes(buffer, "big"))

@classmethod
def from_signed(cls: Type, value: int) -> "U256":
"""
Expand Down
4 changes: 2 additions & 2 deletions src/ethereum/berlin/fork.py
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,7 @@ def generate_header_hash_for_pow(header: Header) -> Hash32:
hash : `Hash32`
The PoW valid rlp hash of the passed in header.
"""
header_data_without_pow_artefacts = [
header_data_without_pow_artefacts = (
header.parent_hash,
header.ommers_hash,
header.coinbase,
Expand All @@ -273,7 +273,7 @@ def generate_header_hash_for_pow(header: Header) -> Hash32:
header.gas_used,
header.timestamp,
header.extra_data,
]
)

return rlp.rlp_hash(header_data_without_pow_artefacts)

Expand Down
15 changes: 7 additions & 8 deletions src/ethereum/berlin/trie.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
Sequence,
TypeVar,
Union,
cast,
)

from ethereum.crypto.hash import keccak256
Expand Down Expand Up @@ -78,7 +77,7 @@ class LeafNode:
"""Leaf node in the Merkle Trie"""

rest_of_key: Bytes
value: rlp.RLP
value: rlp.Extended


@slotted_freezable
Expand All @@ -87,22 +86,22 @@ class ExtensionNode:
"""Extension node in the Merkle Trie"""

key_segment: Bytes
subnode: rlp.RLP
subnode: rlp.Extended


@slotted_freezable
@dataclass
class BranchNode:
"""Branch node in the Merkle Trie"""

subnodes: List[rlp.RLP]
value: rlp.RLP
subnodes: List[rlp.Extended]
value: rlp.Extended


InternalNode = Union[LeafNode, ExtensionNode, BranchNode]


def encode_internal_node(node: Optional[InternalNode]) -> rlp.RLP:
def encode_internal_node(node: Optional[InternalNode]) -> rlp.Extended:
"""
Encodes a Merkle Trie node into its RLP form. The RLP will then be
serialized into a `Bytes` and hashed unless it is less that 32 bytes
Expand All @@ -121,7 +120,7 @@ def encode_internal_node(node: Optional[InternalNode]) -> rlp.RLP:
encoded : `rlp.RLP`
The node encoded as RLP.
"""
unencoded: rlp.RLP
unencoded: rlp.Extended
if node is None:
unencoded = b""
elif isinstance(node, LeafNode):
Expand Down Expand Up @@ -156,7 +155,7 @@ def encode_node(node: Node, storage_root: Optional[Bytes] = None) -> Bytes:
assert storage_root is not None
return encode_account(node, storage_root)
elif isinstance(node, (LegacyTransaction, Receipt, U256)):
return rlp.encode(cast(rlp.RLP, node))
return rlp.encode(node)
elif isinstance(node, Bytes):
return node
else:
Expand Down
4 changes: 2 additions & 2 deletions src/ethereum/byzantium/fork.py
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,7 @@ def generate_header_hash_for_pow(header: Header) -> Hash32:
hash : `Hash32`
The PoW valid rlp hash of the passed in header.
"""
header_data_without_pow_artefacts = [
header_data_without_pow_artefacts = (
header.parent_hash,
header.ommers_hash,
header.coinbase,
Expand All @@ -267,7 +267,7 @@ def generate_header_hash_for_pow(header: Header) -> Hash32:
header.gas_used,
header.timestamp,
header.extra_data,
]
)

return rlp.rlp_hash(header_data_without_pow_artefacts)

Expand Down
15 changes: 7 additions & 8 deletions src/ethereum/byzantium/trie.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
Sequence,
TypeVar,
Union,
cast,
)

from ethereum.crypto.hash import keccak256
Expand Down Expand Up @@ -78,7 +77,7 @@ class LeafNode:
"""Leaf node in the Merkle Trie"""

rest_of_key: Bytes
value: rlp.RLP
value: rlp.Extended


@slotted_freezable
Expand All @@ -87,22 +86,22 @@ class ExtensionNode:
"""Extension node in the Merkle Trie"""

key_segment: Bytes
subnode: rlp.RLP
subnode: rlp.Extended


@slotted_freezable
@dataclass
class BranchNode:
"""Branch node in the Merkle Trie"""

subnodes: List[rlp.RLP]
value: rlp.RLP
subnodes: List[rlp.Extended]
value: rlp.Extended


InternalNode = Union[LeafNode, ExtensionNode, BranchNode]


def encode_internal_node(node: Optional[InternalNode]) -> rlp.RLP:
def encode_internal_node(node: Optional[InternalNode]) -> rlp.Extended:
"""
Encodes a Merkle Trie node into its RLP form. The RLP will then be
serialized into a `Bytes` and hashed unless it is less that 32 bytes
Expand All @@ -121,7 +120,7 @@ def encode_internal_node(node: Optional[InternalNode]) -> rlp.RLP:
encoded : `rlp.RLP`
The node encoded as RLP.
"""
unencoded: rlp.RLP
unencoded: rlp.Extended
if node is None:
unencoded = b""
elif isinstance(node, LeafNode):
Expand Down Expand Up @@ -156,7 +155,7 @@ def encode_node(node: Node, storage_root: Optional[Bytes] = None) -> Bytes:
assert storage_root is not None
return encode_account(node, storage_root)
elif isinstance(node, (Transaction, Receipt, U256)):
return rlp.encode(cast(rlp.RLP, node))
return rlp.encode(node)
elif isinstance(node, Bytes):
return node
else:
Expand Down
17 changes: 8 additions & 9 deletions src/ethereum/cancun/trie.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
Sequence,
TypeVar,
Union,
cast,
)

from ethereum.crypto.hash import keccak256
Expand Down Expand Up @@ -81,7 +80,7 @@ class LeafNode:
"""Leaf node in the Merkle Trie"""

rest_of_key: Bytes
value: rlp.RLP
value: rlp.Extended


@slotted_freezable
Expand All @@ -90,22 +89,22 @@ class ExtensionNode:
"""Extension node in the Merkle Trie"""

key_segment: Bytes
subnode: rlp.RLP
subnode: rlp.Extended


@slotted_freezable
@dataclass
class BranchNode:
"""Branch node in the Merkle Trie"""

subnodes: List[rlp.RLP]
value: rlp.RLP
subnodes: List[rlp.Extended]
value: rlp.Extended


InternalNode = Union[LeafNode, ExtensionNode, BranchNode]


def encode_internal_node(node: Optional[InternalNode]) -> rlp.RLP:
def encode_internal_node(node: Optional[InternalNode]) -> rlp.Extended:
"""
Encodes a Merkle Trie node into its RLP form. The RLP will then be
serialized into a `Bytes` and hashed unless it is less that 32 bytes
Expand All @@ -121,10 +120,10 @@ def encode_internal_node(node: Optional[InternalNode]) -> rlp.RLP:
Returns
-------
encoded : `rlp.RLP`
encoded : `rlp.Extended`
The node encoded as RLP.
"""
unencoded: rlp.RLP
unencoded: rlp.Extended
if node is None:
unencoded = b""
elif isinstance(node, LeafNode):
Expand Down Expand Up @@ -159,7 +158,7 @@ def encode_node(node: Node, storage_root: Optional[Bytes] = None) -> Bytes:
assert storage_root is not None
return encode_account(node, storage_root)
elif isinstance(node, (LegacyTransaction, Receipt, Withdrawal, U256)):
return rlp.encode(cast(rlp.RLP, node))
return rlp.encode(node)
elif isinstance(node, Bytes):
return node
else:
Expand Down
4 changes: 2 additions & 2 deletions src/ethereum/constantinople/fork.py
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,7 @@ def generate_header_hash_for_pow(header: Header) -> Hash32:
hash : `Hash32`
The PoW valid rlp hash of the passed in header.
"""
header_data_without_pow_artefacts = [
header_data_without_pow_artefacts = (
header.parent_hash,
header.ommers_hash,
header.coinbase,
Expand All @@ -267,7 +267,7 @@ def generate_header_hash_for_pow(header: Header) -> Hash32:
header.gas_used,
header.timestamp,
header.extra_data,
]
)

return rlp.rlp_hash(header_data_without_pow_artefacts)

Expand Down
Loading

0 comments on commit fcd1275

Please sign in to comment.