From 2a4cef2d77dc6084659a20294ab5408c95b4a6ed Mon Sep 17 00:00:00 2001 From: Mark Spanbroek Date: Mon, 16 Dec 2024 10:48:52 +0100 Subject: [PATCH] serialize signed block --- codexvalidator/blocks.nim | 7 ++++--- codexvalidator/blocks/blck.nim | 8 +++++++- codexvalidator/blocks/blockhash.nim | 15 --------------- codexvalidator/blocks/hashing.nim | 18 ++++++++++++++++++ codexvalidator/blocks/serialization.nim | 19 +++++++++++++++++++ .../blocks/testSerialization.nim | 11 +++++++++++ 6 files changed, 59 insertions(+), 19 deletions(-) delete mode 100644 codexvalidator/blocks/blockhash.nim create mode 100644 codexvalidator/blocks/hashing.nim diff --git a/codexvalidator/blocks.nim b/codexvalidator/blocks.nim index 00c2aab..bed76b4 100644 --- a/codexvalidator/blocks.nim +++ b/codexvalidator/blocks.nim @@ -2,12 +2,13 @@ import ./blocks/blockid import ./blocks/blck export blockid -export blck +export blck except hash import ./blocks/serialization export serialization.toBytes -import ./blocks/blockhash +import ./blocks/hashing -export blockhash.id +export hashing.hash +export hashing.id diff --git a/codexvalidator/blocks/blck.nim b/codexvalidator/blocks/blck.nim index e3a87cc..4efb5a4 100644 --- a/codexvalidator/blocks/blck.nim +++ b/codexvalidator/blocks/blck.nim @@ -12,4 +12,10 @@ type Block* = ref object round*: uint64 parents*: seq[BlockId] transactions*: seq[Transaction] - hash*: ?Hash + hash: ?Hash + +func `hash=`*(blck: Block, hash: Hash) = + blck.hash = some hash + +func hash*(blck: Block): ?Hash = + blck.hash diff --git a/codexvalidator/blocks/blockhash.nim b/codexvalidator/blocks/blockhash.nim deleted file mode 100644 index ebdec38..0000000 --- a/codexvalidator/blocks/blockhash.nim +++ /dev/null @@ -1,15 +0,0 @@ -import ../basics -import ./blck -import ./blockid -import ./serialization -import ../hashing - -func id*(blck: Block): BlockId = - without var hash =? blck.hash: - hash = Hash.hash(blck.toBytes()) - blck.hash = some hash - BlockId.init( - blck.author, - blck.round, - hash - ) diff --git a/codexvalidator/blocks/hashing.nim b/codexvalidator/blocks/hashing.nim new file mode 100644 index 0000000..fe3d0b4 --- /dev/null +++ b/codexvalidator/blocks/hashing.nim @@ -0,0 +1,18 @@ +import ../basics +import ../hashing +import ./blck +import ./blockid +import ./serialization + +func hash*(b: Block): Hash = + without var hash =? blck.hash(b): + hash = Hash.hash(b.toBytes()) + b.hash = hash + hash + +func id*(b: Block): BlockId = + BlockId.init( + b.author, + b.round, + hash(b) + ) diff --git a/codexvalidator/blocks/serialization.nim b/codexvalidator/blocks/serialization.nim index 58bea1d..ee5215c 100644 --- a/codexvalidator/blocks/serialization.nim +++ b/codexvalidator/blocks/serialization.nim @@ -3,6 +3,7 @@ import ../basics import ../hashing import ../transaction import ../transaction/serialization +import ../signatures import ./blockid import ./blck @@ -39,3 +40,21 @@ func init*(_: type BlockMessage, blck: Block): BlockMessage = func toBytes*(blck: Block): seq[byte] = Protobuf.encode(BlockMessage.init(blck)) + +type SignedBlockMessage* {.proto3.} = object + blck* {.fieldNumber: 1.}: BlockMessage + signer* {.fieldNumber: 2.}: seq[byte] + signature* {.fieldNumber: 3.}: seq[byte] + +func init*( + _: type SignedBlockMessage, + signed: Signed[Block] +): SignedBlockMessage = + SignedBlockMessage( + blck: BlockMessage.init(signed.value), + signer: signed.signer.toBytes(), + signature: signed.signature.toBytes() + ) + +func toBytes*(signed: Signed[Block]): seq[byte] = + Protobuf.encode(SignedBlockMessage.init(signed)) diff --git a/tests/codexvalidator/blocks/testSerialization.nim b/tests/codexvalidator/blocks/testSerialization.nim index a4f2102..2d735b2 100644 --- a/tests/codexvalidator/blocks/testSerialization.nim +++ b/tests/codexvalidator/blocks/testSerialization.nim @@ -3,6 +3,7 @@ import codexvalidator/blocks import codexvalidator/blocks/serialization import codexvalidator/transaction/serialization import codexvalidator/hashing +import codexvalidator/signatures suite "Block serialization": @@ -26,3 +27,13 @@ suite "Block serialization": check protobuf.round == blck.round check protobuf.parents == blck.parents.mapIt(BlockIdMessage.init(it)) check protobuf.transactions == blck.transactions.mapIt(TransactionMessage.init(it)) + + test "serializes a signed block with protobuf": + let blck = Block.example + let identity = Identity.example + let signed = Signed.sign(identity, blck) + let serialized = signed.toBytes() + let protobuf = Protobuf.decode(serialized, SignedBlockMessage) + check protobuf.blck == BlockMessage.init(blck) + check protobuf.signer == signed.signer.toBytes() + check protobuf.signature == signed.signature.toBytes()