diff --git a/mysticeti/validator.nim b/mysticeti/validator.nim index fb0afef..b10b3d8 100644 --- a/mysticeti/validator.nim +++ b/mysticeti/validator.nim @@ -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: diff --git a/mysticeti/validator/rounds.nim b/mysticeti/validator/rounds.nim index 3d6f947..aa34e51 100644 --- a/mysticeti/validator/rounds.nim +++ b/mysticeti/validator/rounds.nim @@ -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