Skip to content

Commit

Permalink
Tests: use forge rather than inheriting Operation/Block from Message
Browse files Browse the repository at this point in the history
this avoids adding inconsistent fields to operations
  • Loading branch information
spalmer25 committed Jan 29, 2024
1 parent 859344a commit 6363000
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 42 deletions.
10 changes: 3 additions & 7 deletions test/python/test_instructions.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
from utils.message import (
Preattestation,
Attestation,
Fitness,
BlockHeader,
Block,
DEFAULT_CHAIN_ID
Expand Down Expand Up @@ -329,7 +328,7 @@ def test_sign_preattestation(
test_hwm),
navigate=lambda: navigator.navigate(instructions))

preattestation = Preattestation(chain_id=main_chain_id)
preattestation = Preattestation().forge(chain_id=main_chain_id)

if with_hash:
signature = client.sign_message(account, preattestation)
Expand Down Expand Up @@ -365,7 +364,7 @@ def test_sign_attestation(
test_hwm),
navigate=lambda: navigator.navigate(instructions))

attestation = Attestation(chain_id=main_chain_id)
attestation = Attestation().forge(chain_id=main_chain_id)

if with_hash:
signature = client.sign_message(account, attestation)
Expand Down Expand Up @@ -401,10 +400,7 @@ def test_sign_block(
test_hwm),
navigate=lambda: navigator.navigate(instructions))

block = Block(
chain_id=main_chain_id,
header=BlockHeader(level=1)
)
block = Block(header=BlockHeader(level=1)).forge(chain_id=main_chain_id)

if with_hash:
signature = client.sign_message(account, block)
Expand Down
79 changes: 44 additions & 35 deletions test/python/utils/message.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,77 +100,86 @@ class OperationTag(IntEnum):
# Context_hash.zero
DEFAULT_CONTEXT_HASH = "CoUeJrcPBj3T3iJL3PY4jZHnmZa5rRZ87VQPdSBNBcwZRMWJGh9j"

class Preattestation(Message):
class Preattestation:
"""Class representing a preattestation."""

chain_id: str
branch: str
slot: int
op_level: int
op_round: int
block_payload_hash: str

def __init__(self,
chain_id: str = DEFAULT_CHAIN_ID,
branch: str = DEFAULT_BLOCK_HASH,
slot: int = 0,
op_level: int = 0,
op_round: int = 0,
block_payload_hash: str = DEFAULT_BLOCK_PAYLOAD_HASH):
self.chain_id = chain_id
self.branch = branch
self.slot = slot
self.op_level = op_level
self.op_round = op_round
self.block_payload_hash = block_payload_hash

def raw(self) -> bytes:
def __bytes__(self) -> bytes:
raw = b''
raw += forge_int_fixed(MagicByte.TENDERBAKE_PREENDORSEMENT, 1)
raw += forge.forge_base58(self.chain_id)
raw += forge.forge_base58(self.branch)
raw += forge_int_fixed(OperationTag.PREATTESTATION, 1)
raw += forge.forge_int16(self.slot)
raw += forge.forge_int32(self.op_level)
raw += forge.forge_int32(self.op_round)
raw += forge.forge_base58(self.block_payload_hash)
return raw

class Attestation(Message):
def forge(self,
chain_id: str = DEFAULT_CHAIN_ID,
branch: str = DEFAULT_BLOCK_HASH) -> Message:
"""Forge the preattestation."""
raw_operation = \
forge.forge_base58(branch) + \
bytes(self)
watermark = \
forge_int_fixed(MagicByte.TENDERBAKE_PREENDORSEMENT, 1) + \
forge.forge_base58(chain_id)
raw = watermark + raw_operation
return RawMessage(raw)

class Attestation:
"""Class representing an attestation."""
chain_id: str
branch: str

slot: int
op_level: int
op_round: int
block_payload_hash: str

def __init__(self,
chain_id: str = DEFAULT_CHAIN_ID,
branch: str = DEFAULT_BLOCK_HASH,
slot: int = 0,
op_level: int = 0,
op_round: int = 0,
block_payload_hash: str = DEFAULT_BLOCK_PAYLOAD_HASH):
self.chain_id = chain_id
self.branch = branch
self.slot = slot
self.op_level = op_level
self.op_round = op_round
self.block_payload_hash = block_payload_hash

def raw(self) -> bytes:
def __bytes__(self) -> bytes:
raw = b''
raw += forge_int_fixed(MagicByte.TENDERBAKE_ENDORSEMENT, 1)
raw += forge.forge_base58(self.chain_id)
raw += forge.forge_base58(self.branch)
raw += forge_int_fixed(OperationTag.ATTESTATION, 1)
raw += forge.forge_int16(self.slot)
raw += forge.forge_int32(self.op_level)
raw += forge.forge_int32(self.op_round)
raw += forge.forge_base58(self.block_payload_hash)
return raw

def forge(self,
chain_id: str = DEFAULT_CHAIN_ID,
branch: str = DEFAULT_BLOCK_HASH) -> Message:
"""Forge the attestation."""
raw_operation = \
forge.forge_base58(branch) + \
bytes(self)
watermark = \
forge_int_fixed(MagicByte.TENDERBAKE_ENDORSEMENT, 1) + \
forge.forge_base58(chain_id)
raw = watermark + raw_operation
return RawMessage(raw)

class Fitness:
"""Class representing a fitness."""

Expand Down Expand Up @@ -246,26 +255,26 @@ def __bytes__(self) -> bytes:
raw += forge.forge_base58(self.context)
return raw

class Block(Message):
class Block:
"""Class representing a block."""

chain_id: str
header: BlockHeader
content: bytes

def __init__(self,
chain_id: str = DEFAULT_CHAIN_ID,
header: BlockHeader = BlockHeader(),
content: Union[str, bytes] = b''):
self.chain_id = chain_id
self.header = header
self.content = content if isinstance(content, bytes) else \
self.header = header
self.content = content if isinstance(content, bytes) else \
bytes.fromhex(content)

def raw(self) -> bytes:
raw = b''
raw += forge_int_fixed(MagicByte.TENDERBAKE_BLOCK, 1)
raw += forge.forge_base58(self.chain_id)
raw += bytes(self.header)
raw += self.content
return raw
def __bytes__(self) -> bytes:
return bytes(self.header) + self.content

def forge(self, chain_id: str = DEFAULT_CHAIN_ID) -> Message:
"""Forge the block."""
watermark = \
forge_int_fixed(MagicByte.TENDERBAKE_BLOCK, 1) + \
forge.forge_base58(chain_id)
raw = watermark + bytes(self)
return RawMessage(raw)

0 comments on commit 6363000

Please sign in to comment.