diff --git a/test/python/test_instructions.py b/test/python/test_instructions.py index 58c7ff24..5a3f27e3 100644 --- a/test/python/test_instructions.py +++ b/test/python/test_instructions.py @@ -14,7 +14,6 @@ from utils.message import ( Preattestation, Attestation, - Fitness, BlockHeader, Block, DEFAULT_CHAIN_ID @@ -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) @@ -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) @@ -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) diff --git a/test/python/utils/message.py b/test/python/utils/message.py index 612d0809..91c0a001 100644 --- a/test/python/utils/message.py +++ b/test/python/utils/message.py @@ -100,35 +100,26 @@ 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) @@ -136,34 +127,39 @@ def raw(self) -> bytes: 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) @@ -171,6 +167,19 @@ def raw(self) -> bytes: 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.""" @@ -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)