diff --git a/mysticeti.nim b/mysticeti.nim index f5f5711..9072720 100644 --- a/mysticeti.nim +++ b/mysticeti.nim @@ -15,10 +15,11 @@ import ./mysticeti/blocks export blocks.Transaction export blocks.Block +export blocks.BlockId export blocks.author export blocks.round export blocks.parents -export blocks.blockHash +export blocks.id export blocks.SignedBlock export blocks.blck export blocks.signer diff --git a/mysticeti/blocks.nim b/mysticeti/blocks.nim index d78dd5a..1217f29 100644 --- a/mysticeti/blocks.nim +++ b/mysticeti/blocks.nim @@ -7,27 +7,31 @@ type Block*[Signing, Hashing] = object author: Identifier[Signing] round: uint64 - parents: seq[Hash[Hashing]] + parents: seq[BlockId[Signing, Hashing]] transactions: seq[Transaction] + BlockId*[Signing, Hashing] = object + author: Identifier[Signing] + round: uint64 + hash: Hash[Hashing] func new*( _: type Block, author: Identifier, round: uint64, - parents: seq[Hash], + parents: seq[BlockId], transactions: seq[Transaction] ): auto = - Block[Identifier.Signing, Hash.Hashing]( + Block[BlockId.Signing, BlockId.Hashing]( author: author, round: round, parents: parents, transactions: transactions ) -func author*(blck: Block): auto = +func author*(blck: Block | BlockId): auto = blck.author -func round*(blck: Block): uint64 = +func round*(blck: Block | BlockId): uint64 = blck.round func parents*(blck: Block): auto = @@ -36,8 +40,12 @@ func parents*(blck: Block): auto = func toBytes(blck: Block): seq[byte] = cast[seq[byte]]($blck) # TODO: proper serialization -func blockHash*(blck: Block): auto = - Block.Hashing.hash(blck.toBytes) +func id*(blck: Block): auto = + BlockId[Block.Signing, Block.Hashing]( + author: blck.author, + round: blck.round, + hash: Block.Hashing.hash(blck.toBytes) + ) type SignedBlock*[Signing, Hashing] = object blck: Block[Signing, Hashing] diff --git a/mysticeti/validator.nim b/mysticeti/validator.nim index c697030..44229db 100644 --- a/mysticeti/validator.nim +++ b/mysticeti/validator.nim @@ -1,6 +1,5 @@ import ./basics import ./signing -import ./hashing import ./blocks type @@ -48,10 +47,10 @@ func nextRound*(validator: Validator) = proc propose*(validator: Validator, transactions: seq[Transaction]): auto = assert validator.identifier notin validator.round.slots - var parents: seq[Hash[Validator.Hashing]] + var parents: seq[BlockId[Validator.Signing, Validator.Hashing]] if previous =? validator.round.previous: for id in previous.slots.keys: - parents.add(previous.slots[id].proposal.blockHash) + parents.add(previous.slots[id].proposal.id) let blck = Block.new( author = validator.identifier, round = validator.round.number, diff --git a/tests/testValidator.nim b/tests/testValidator.nim index e718d38..cb9e1af 100644 --- a/tests/testValidator.nim +++ b/tests/testValidator.nim @@ -64,7 +64,7 @@ suite "Validator": validator1.receive(proposal4) validator1.nextRound() let proposal5 = validator1.propose(seq[Transaction].example) - check proposal1.blck.blockHash in proposal5.blck.parents - check proposal2.blck.blockHash in proposal5.blck.parents - check proposal3.blck.blockHash in proposal5.blck.parents - check proposal4.blck.blockHash in proposal5.blck.parents + check proposal1.blck.id in proposal5.blck.parents + check proposal2.blck.id in proposal5.blck.parents + check proposal3.blck.id in proposal5.blck.parents + check proposal4.blck.id in proposal5.blck.parents