introduce slot abstraction

This commit is contained in:
Mark Spanbroek 2024-09-17 14:04:30 +02:00
parent 075786a165
commit 0102e17efb

View File

@ -10,9 +10,9 @@ type
Round[Signing, Hashing] = ref object
number: uint64
previous: ?Round[Signing, Hashing]
proposals: Table[Identifier[Signing], seq[Proposal[Signing, Hashing]]]
Proposal[Signing, Hashing] = object
blck: Block[Signing, Hashing]
slots: Table[Identifier[Signing], ProposerSlot[Signing, Hashing]]
ProposerSlot[Signing, Hashing] = object
proposal: Block[Signing, Hashing]
status: ProposalStatus
ProposalStatus* = enum
undecided
@ -27,8 +27,8 @@ proc new*(T: type Validator): T =
func new*(_: type Round, number: uint64, previous: Round): auto =
Round(number: number, previous: some previous)
func init*(_: type Proposal, blck: Block): auto =
Proposal[Block.Signing, Block.Hashing](blck: blck)
func init(_: type ProposerSlot, proposal: Block): auto =
ProposerSlot[Block.Signing, Block.Hashing](proposal: proposal)
func identifier*(validator: Validator): auto =
validator.identity.identifier
@ -41,24 +41,22 @@ func nextRound*(validator: Validator) =
validator.round = Round.new(previous.number + 1, previous)
proc propose*(validator: Validator, transactions: seq[Transaction]): auto =
assert validator.identifier notin validator.round.proposals
assert validator.identifier notin validator.round.slots
var parents: seq[Hash[Validator.Hashing]]
if previous =? validator.round.previous:
for id in previous.proposals.keys:
parents.add(previous.proposals[id][0].blck.blockHash)
for id in previous.slots.keys:
parents.add(previous.slots[id].proposal.blockHash)
let blck = Block.new(
author = validator.identifier,
round = validator.round.number,
parents = parents,
transactions = transactions
)
let proposal = Proposal.init(blck)
validator.round.proposals[validator.identifier] = @[proposal]
validator.round.slots[validator.identifier] = ProposerSlot.init(blck)
validator.identity.sign(blck)
func receive*(validator: Validator, signed: SignedBlock) =
let proposal = Proposal.init(signed.blck)
validator.round.proposals[signed.blck.author] = @[proposal]
validator.round.slots[signed.blck.author] = ProposerSlot.init(signed.blck)
func round(validator: Validator, number: uint64): auto =
var round = validator.round
@ -68,9 +66,9 @@ func round(validator: Validator, number: uint64): auto =
return some round
func status*(validator: Validator, blck: Block): ?ProposalStatus =
if round =? round(validator, blck.round) and blck.author in round.proposals:
let proposal = round.proposals[blck.author][0]
some proposal.status
if round =? round(validator, blck.round) and blck.author in round.slots:
let slot = round.slots[blck.author]
some slot.status
else:
none ProposalStatus