Skip to content

Commit

Permalink
primary proposer for a round
Browse files Browse the repository at this point in the history
  • Loading branch information
markspanbroek committed Nov 26, 2024
1 parent 7218991 commit a8122cb
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 0 deletions.
1 change: 1 addition & 0 deletions mysticeti.nim
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 3 additions & 0 deletions mysticeti/validator.nim
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down
3 changes: 3 additions & 0 deletions mysticeti/validator/round.nim
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
13 changes: 13 additions & 0 deletions tests/mysticeti/validator/testRound.nim
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
11 changes: 11 additions & 0 deletions tests/mysticeti/validator/testValidatorNetwork.nim
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down

0 comments on commit a8122cb

Please sign in to comment.