From a8122cb27c53eabbd49772921c899dad11496962 Mon Sep 17 00:00:00 2001 From: Mark Spanbroek Date: Tue, 26 Nov 2024 10:59:04 +0100 Subject: [PATCH] primary proposer for a round --- mysticeti.nim | 1 + mysticeti/validator.nim | 3 +++ mysticeti/validator/round.nim | 3 +++ tests/mysticeti/validator/testRound.nim | 13 +++++++++++++ tests/mysticeti/validator/testValidatorNetwork.nim | 11 +++++++++++ 5 files changed, 31 insertions(+) diff --git a/mysticeti.nim b/mysticeti.nim index 224ece3..5022fb8 100644 --- a/mysticeti.nim +++ b/mysticeti.nim @@ -10,6 +10,7 @@ export validator.new export validator.identifier export validator.membership export validator.round +export validator.primaryProposer export validator.nextRound export validator.propose export validator.check diff --git a/mysticeti/validator.nim b/mysticeti/validator.nim index dafba3e..14af208 100644 --- a/mysticeti/validator.nim +++ b/mysticeti/validator.nim @@ -33,6 +33,9 @@ func membership*(validator: Validator): CommitteeMember = func round*(validator: Validator): uint64 = validator.rounds.latest.number +func primaryProposer*(validator: Validator): CommitteeMember = + validator.rounds.latest.primaryProposer + func nextRound*(validator: Validator) = validator.rounds.addNewRound() diff --git a/mysticeti/validator/round.nim b/mysticeti/validator/round.nim index 8738375..d70d03b 100644 --- a/mysticeti/validator/round.nim +++ b/mysticeti/validator/round.nim @@ -32,6 +32,9 @@ func next*(round: Round): auto = func `[]`*(round: Round, member: CommitteeMember): auto = round.slots[int(member)] +func primaryProposer*(round: Round): CommitteeMember = + CommitteeMember((round.number mod round.slots.len.uint64).int) + iterator proposers*(round: Round): CommitteeMember = let length = round.slots.len let offset = (round.number mod length.uint64).int diff --git a/tests/mysticeti/validator/testRound.nim b/tests/mysticeti/validator/testRound.nim index 7b45124..05a1066 100644 --- a/tests/mysticeti/validator/testRound.nim +++ b/tests/mysticeti/validator/testRound.nim @@ -120,6 +120,19 @@ suite "Validator Round": check third.previous == none Round check third.next == none Round + test "primary proposer rotates on a round-robin schedule": + var round: Round + round = Round.new(0, 4) + check round.primaryProposer == CommitteeMember(0) + round = Round.new(1, 4) + check round.primaryProposer == CommitteeMember(1) + round = Round.new(2, 4) + check round.primaryProposer == CommitteeMember(2) + round = Round.new(3, 4) + check round.primaryProposer == CommitteeMember(3) + round = Round.new(4, 4) + check round.primaryProposer == CommitteeMember(0) + test "proposers are ordered round-robin for each round": var round: Round round = Round.new(0, 4) diff --git a/tests/mysticeti/validator/testValidatorNetwork.nim b/tests/mysticeti/validator/testValidatorNetwork.nim index 6fdd05b..2f3bcca 100644 --- a/tests/mysticeti/validator/testValidatorNetwork.nim +++ b/tests/mysticeti/validator/testValidatorNetwork.nim @@ -19,6 +19,17 @@ suite "Validator Network": setup: simulator = NetworkSimulator.init() + test "primary proposer rotates on a round-robin schedule": + check simulator.validators.allIt(it.primaryProposer == CommitteeMember(0)) + simulator.nextRound() + check simulator.validators.allIt(it.primaryProposer == CommitteeMember(1)) + simulator.nextRound() + check simulator.validators.allIt(it.primaryProposer == CommitteeMember(2)) + simulator.nextRound() + check simulator.validators.allIt(it.primaryProposer == CommitteeMember(3)) + simulator.nextRound() + check simulator.validators.allIt(it.primaryProposer == CommitteeMember(0)) + test "validators include blocks from previous round as parents": let previous = !simulator.exchangeProposals() simulator.nextRound()