reference blocks by author, round and hash

This commit is contained in:
Mark Spanbroek 2024-09-18 15:41:03 +02:00
parent 67f9f1e149
commit 4f8c280c7b
4 changed files with 23 additions and 15 deletions

View File

@ -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

View File

@ -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]

View File

@ -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,

View File

@ -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