introduce Round.find()

This commit is contained in:
Mark Spanbroek 2024-10-17 15:05:02 +02:00
parent 2dede094c8
commit 70f61d1ef6
2 changed files with 28 additions and 11 deletions

View File

@ -115,7 +115,7 @@ func round(validator: Validator, number: uint64): auto =
return some round
func status*(validator: Validator, blck: Block): ?SlotStatus =
if round =? round(validator, blck.round):
if round =? validator.rounds.first.find(blck.round):
some round[blck.author].status
else:
none SlotStatus
@ -124,7 +124,7 @@ func status*(validator: Validator, proposal: SignedBlock): ?SlotStatus =
validator.status(proposal.blck)
func findAnchor(validator: Validator, round: Round): auto =
var next = round.next.?next.?next
var next = round.find(round.number + 3)
while current =? next:
for member in validator.committee.ordered(current.number):
let slot = current[member]
@ -133,11 +133,8 @@ func findAnchor(validator: Validator, round: Round): auto =
next = current.next
func searchBackwards(round: Round, blockId: BlockId): auto =
var current = round
while current.number > blockId.round and previous =? current.previous:
current = previous
if current.number == blockId.round:
let slot = current[blockId.author]
if found =? round.find(blockId.round):
let slot = found[blockId.author]
for proposal in slot.proposals:
let blck = proposal.blck
if blck.id == blockId:

View File

@ -6,7 +6,7 @@ type
first*, last*: Round[Hashing]
Round*[Hashing] = ref object
number: uint64
previous*, next*: ?Round[Hashing]
previous, next: ?Round[Hashing]
slots*: seq[ProposerSlot[Hashing]]
func remove*(rounds: var Rounds, round: Round) =
@ -24,12 +24,32 @@ func new*(T: type Round, number: uint64, slots: int): T =
let slots = newSeqWith(slots, Slot.new())
T(number: number, slots: slots)
func previous*(round: Round): auto =
round.previous
func next*(round: Round): auto =
round.next
func number*(round: Round): uint64 =
round.number
func find*(round: Round, number: uint64): ?Round =
var current = round
while true:
if current.number == number:
return some current
elif current.number < number:
without next =? current.next:
return none Round
current = next
else:
without previous =? current.previous:
return none Round
current = previous
func createNext*(round: Round): auto =
assert round.next.isNone
let next = Round.new(round.number + 1, round.slots.len)
next.previous = some round
round.next = some next
next
func number*(round: Round): uint64 =
round.number