Skip to content

Commit

Permalink
Merge pull request #891 from iotaledger/fix/slow-witnesses
Browse files Browse the repository at this point in the history
Fix witness and ratifier reading
  • Loading branch information
alexsporn authored Mar 28, 2024
2 parents 2d7ab67 + 3ece0d1 commit 672bea9
Show file tree
Hide file tree
Showing 7 changed files with 25 additions and 30 deletions.
2 changes: 1 addition & 1 deletion components/debugapi/debug_models.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ func BlockMetadataResponseFromBlock(block *blocks.Block) *BlockMetadataResponse
Accepted: block.IsAccepted(),
PreConfirmed: block.IsPreConfirmed(),
Confirmed: block.IsConfirmed(),
Witnesses: lo.Map(block.Witnesses(), func(seatIndex account.SeatIndex) string { return fmt.Sprintf("%d", seatIndex) }),
Witnesses: lo.Map(block.Witnesses().ToSlice(), func(seatIndex account.SeatIndex) string { return fmt.Sprintf("%d", seatIndex) }),
SpenderIDs: block.SpenderIDs().ToSlice(),
PayloadSpenderIDs: block.PayloadSpenderIDs().ToSlice(),
String: block.String(),
Expand Down
12 changes: 6 additions & 6 deletions pkg/protocol/engine/blocks/block.go
Original file line number Diff line number Diff line change
Expand Up @@ -367,11 +367,11 @@ func (b *Block) WitnessCount() int {
return b.witnesses.Size()
}

func (b *Block) Witnesses() []account.SeatIndex {
func (b *Block) Witnesses() ds.ReadableSet[account.SeatIndex] {
b.mutex.RLock()
defer b.mutex.RUnlock()

return b.witnesses.ToSlice()
return b.witnesses
}

func (b *Block) SpenderIDs() ds.Set[iotago.TransactionID] {
Expand Down Expand Up @@ -424,11 +424,11 @@ func (b *Block) AddAcceptanceRatifier(seat account.SeatIndex) (added bool) {
return b.acceptanceRatifiers.Add(seat)
}

func (b *Block) AcceptanceRatifiers() []account.SeatIndex {
func (b *Block) AcceptanceRatifiers() ds.ReadableSet[account.SeatIndex] {
b.mutex.RLock()
defer b.mutex.RUnlock()

return b.acceptanceRatifiers.ToSlice()
return b.acceptanceRatifiers
}

// Accepted returns a reactive variable that is true if the Block was accepted.
Expand Down Expand Up @@ -536,11 +536,11 @@ func (b *Block) AddConfirmationRatifier(seat account.SeatIndex) (added bool) {
return b.confirmationRatifiers.Add(seat)
}

func (b *Block) ConfirmationRatifiers() []account.SeatIndex {
func (b *Block) ConfirmationRatifiers() ds.ReadableSet[account.SeatIndex] {
b.mutex.RLock()
defer b.mutex.RUnlock()

return b.confirmationRatifiers.ToSlice()
return b.confirmationRatifiers
}

func (b *Block) IsConfirmed() bool {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ func (g *Gadget) trackAcceptanceRatifierWeight(votingBlock *blocks.Block) {
}

func (g *Gadget) shouldAccept(block *blocks.Block) bool {
blockSeats := len(block.AcceptanceRatifiers())
blockSeats := block.AcceptanceRatifiers().Size()
onlineCommitteeTotalSeats := g.seatManager.OnlineCommittee().Size()

return votes.IsThresholdReached(blockSeats, onlineCommitteeTotalSeats, g.optsAcceptanceThreshold)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ func (g *Gadget) trackConfirmationRatifierWeight(votingBlock *blocks.Block) {
}

func (g *Gadget) shouldConfirm(block *blocks.Block) bool {
blockSeats := len(block.ConfirmationRatifiers())
blockSeats := block.ConfirmationRatifiers().Size()
totalCommitteeSeats := g.seatManager.SeatCountInSlot(block.ID().Slot())

return votes.IsThresholdReached(blockSeats, totalCommitteeSeats, g.optsConfirmationThreshold)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package thresholdblockgadget

import (
"fmt"

"github.com/iotaledger/hive.go/ds"
"github.com/iotaledger/hive.go/ds/walker"
"github.com/iotaledger/hive.go/ierrors"
Expand Down Expand Up @@ -82,10 +80,6 @@ func (g *Gadget) propagate(initialBlockIDs iotago.BlockIDs, evaluateFunc func(bl
continue
}

if !block.IsBooked() {
panic(fmt.Sprintf("block %s is not booked", blockID))
}

if !evaluateFunc(block) {
continue
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,14 +96,13 @@ func (g *Gadget) TrackWitnessWeight(votingBlock *blocks.Block) {

func (g *Gadget) shouldPreAcceptAndPreConfirm(block *blocks.Block) (preAccept bool, preConfirm bool) {
committeeTotalSeats := g.seatManager.SeatCountInSlot(block.ID().Slot())
blockSeats := len(block.Witnesses())
blockSeats := block.Witnesses().Size()

onlineCommitteeTotalSeats := g.seatManager.OnlineCommittee().Size()
blockSeatsOnline := len(block.Witnesses())

if votes.IsThresholdReached(blockSeats, committeeTotalSeats, g.optsConfirmationThreshold) {
return true, true
} else if votes.IsThresholdReached(blockSeatsOnline, onlineCommitteeTotalSeats, g.optsAcceptanceThreshold) {
} else if votes.IsThresholdReached(blockSeats, onlineCommitteeTotalSeats, g.optsAcceptanceThreshold) {
return true, false
}

Expand Down
26 changes: 14 additions & 12 deletions pkg/testsuite/testsuite_issue_blocks.go
Original file line number Diff line number Diff line change
Expand Up @@ -221,21 +221,23 @@ func (t *TestSuite) IssueBlocksAtSlots(prefix string, slots []iotago.SlotIndex,
lastBlockRowIssued = lastRowInSlot

if waitForSlotsCommitted {
if slot > t.API.ProtocolParameters().MinCommittableAge() {
if useCommitmentAtMinCommittableAge {
// Make sure that all nodes create blocks throughout the slot that commit to the same commitment at slot-minCommittableAge-1.
commitmentSlot := slot - t.API.ProtocolParameters().MinCommittableAge()
t.AssertCommitmentSlotIndexExists(commitmentSlot, t.ClientsForNodes(nodes...)...)
for _, node := range nodes {
commitment, err := node.Protocol.Engines.Main.Get().Storage.Commitments().Load(commitmentSlot)
require.NoError(t.Testing, err)

issuingOptions[node.Name] = []options.Option[mock.BlockHeaderParams]{
mock.WithSlotCommitment(commitment.Commitment()),
}
if useCommitmentAtMinCommittableAge && slot > t.API.ProtocolParameters().MinCommittableAge() {
// Make sure that all nodes create blocks throughout the slot that commit to the same commitment at slot-minCommittableAge-1.
commitmentSlot := slot - t.API.ProtocolParameters().MinCommittableAge()
t.AssertCommitmentSlotIndexExists(commitmentSlot, t.ClientsForNodes(nodes...)...)
for _, node := range nodes {
commitment, err := node.Protocol.Engines.Main.Get().Storage.Commitments().Load(commitmentSlot)
require.NoError(t.Testing, err)

issuingOptions[node.Name] = []options.Option[mock.BlockHeaderParams]{
mock.WithSlotCommitment(commitment.Commitment()),
}
}
} else if slot > t.API.ProtocolParameters().MaxCommittableAge()+1 {
commitmentSlot := slot - t.API.ProtocolParameters().MaxCommittableAge() + 1
t.AssertCommitmentSlotIndexExists(commitmentSlot, t.ClientsForNodes(nodes...)...)
}

t.AssertBlocksExist(blocksInSlot, true, t.ClientsForNodes(nodes...)...)
}
}
Expand Down

0 comments on commit 672bea9

Please sign in to comment.