Skip to content
This repository has been archived by the owner on Jan 24, 2025. It is now read-only.

Commit

Permalink
Feat: more cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
hmoog committed Jan 19, 2024
1 parent 43719e7 commit 6d1533e
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 43 deletions.
4 changes: 3 additions & 1 deletion pkg/protocol/attestations.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,9 @@ func (a *Attestations) initRequester() (shutdown func()) {
a.protocol.Commitments.WithElements(func(commitment *Commitment) (shutdown func()) {
return commitment.RequestAttestations.WithNonEmptyValue(func(_ bool) (teardown func()) {
if commitment.CumulativeWeight.Get() == 0 {
commitment.IsAttested.Set(true)
a.workerPool.Submit(func() {
commitment.IsAttested.Set(true)
})

return nil
}
Expand Down
14 changes: 7 additions & 7 deletions pkg/protocol/chains.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,9 @@ func newChains(protocol *Protocol) *Chains {
protocol: protocol,
}

c.HeaviestClaimedCandidate = newHeaviestChainCandidate(c, (*Commitment).cumulativeWeightAddr)
c.HeaviestAttestedCandidate = newHeaviestChainCandidate(c, (*Commitment).cumulativeAttestedWeightAddr)
c.HeaviestVerifiedCandidate = newHeaviestChainCandidate(c, (*Commitment).cumulativeVerifiedWeightAddr)
c.HeaviestClaimedCandidate = newHeaviestChainCandidate(c, (*Commitment).cumulativeWeight)
c.HeaviestAttestedCandidate = newHeaviestChainCandidate(c, (*Commitment).cumulativeAttestedWeight)
c.HeaviestVerifiedCandidate = newHeaviestChainCandidate(c, (*Commitment).cumulativeVerifiedWeight)

shutdown := lo.Batch(
c.initLogger(protocol.NewChildLogger("Chains")),
Expand Down Expand Up @@ -83,10 +83,10 @@ func (c *Chains) initLogger(logger log.Logger) (shutdown func()) {
c.Logger = logger

return lo.Batch(
c.Main.LogUpdates(c, log.LevelInfo, "Main", (*Chain).LogName),
c.HeaviestClaimedCandidate.LogUpdates(c, log.LevelInfo, "HeaviestClaimedCandidate", (*Chain).LogName),
c.HeaviestAttestedCandidate.LogUpdates(c, log.LevelInfo, "HeaviestAttestedCandidate", (*Chain).LogName),
c.HeaviestVerifiedCandidate.LogUpdates(c, log.LevelInfo, "HeaviestVerifiedCandidate", (*Chain).LogName),
c.Main.LogUpdates(c, log.LevelTrace, "Main", (*Chain).LogName),
c.HeaviestClaimedCandidate.LogUpdates(c, log.LevelTrace, "HeaviestClaimedCandidate", (*Chain).LogName),
c.HeaviestAttestedCandidate.LogUpdates(c, log.LevelTrace, "HeaviestAttestedCandidate", (*Chain).LogName),
c.HeaviestVerifiedCandidate.LogUpdates(c, log.LevelTrace, "HeaviestVerifiedCandidate", (*Chain).LogName),

logger.UnsubscribeFromParentLogger,
)
Expand Down
28 changes: 16 additions & 12 deletions pkg/protocol/commitment.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,12 @@ type Commitment struct {
// and its parent).
Weight reactive.Variable[uint64]

// CumulativeWeight contains the cumulative weight of all Commitments up to this point.
CumulativeWeight reactive.Variable[uint64]

// AttestedWeight contains the weight of the Commitment that was attested by other nodes.
AttestedWeight reactive.Variable[uint64]

// CumulativeWeight contains the cumulative weight of all Commitments up to this point.
CumulativeWeight reactive.Variable[uint64]

// CumulativeAttestedWeight contains the cumulative weight of all attested Commitments up to this point.
CumulativeAttestedWeight reactive.Variable[uint64]

Expand Down Expand Up @@ -102,8 +102,8 @@ func newCommitment(commitments *Commitments, model *model.Commitment) *Commitmen
WarpSyncBlocks: reactive.NewVariable[bool](),
BlocksToWarpSync: reactive.NewVariable[ds.Set[iotago.BlockID]](),
Weight: reactive.NewVariable[uint64](),
CumulativeWeight: reactive.NewVariable[uint64](),
AttestedWeight: reactive.NewVariable[uint64](func(currentValue uint64, newValue uint64) uint64 { return max(currentValue, newValue) }),
CumulativeWeight: reactive.NewVariable[uint64](),
CumulativeAttestedWeight: reactive.NewVariable[uint64](),
CumulativeVerifiedWeight: reactive.NewVariable[uint64](),
IsRoot: reactive.NewEvent(),
Expand Down Expand Up @@ -136,6 +136,7 @@ func (c *Commitment) TargetEngine() *engine.Engine {
return nil
}

// Less returns true if this Commitment is smaller than the other Commitment (which is used as a tie-breaker).
func (c *Commitment) Less(other *Commitment) bool {
return other.Chain.Get().winsTieBreak(c.Chain.Get())
}
Expand Down Expand Up @@ -178,11 +179,11 @@ func (c *Commitment) initDerivedProperties() (shutdown func()) {
c.deriveCumulativeVerifiedWeight(),

c.Parent.WithNonEmptyValue(func(parent *Commitment) func() {
// the weight can be fixed as a one time operation (it only relies on static information)
//if parent.CumulativeWeight.Get() < c.CumulativeWeight.Get() {
c.Weight.Set(c.Commitment.CumulativeWeight() - parent.Commitment.CumulativeWeight())
c.CumulativeWeight.Set(c.Commitment.CumulativeWeight())
//}
// make sure malicious commitments can not create an overflow in the weights
if parent.CumulativeWeight.Get() < c.CumulativeWeight.Get() {
c.Weight.Set(c.Commitment.CumulativeWeight() - parent.Commitment.CumulativeWeight())
c.CumulativeWeight.Set(c.Commitment.CumulativeWeight())
}

return lo.Batch(
parent.deriveChildren(c),
Expand Down Expand Up @@ -323,14 +324,17 @@ func (c *Commitment) forceChain(targetChain *Chain) {
}
}

func (c *Commitment) cumulativeWeightAddr() reactive.Variable[uint64] {
// cumulativeWeight returns the Variable that contains the cumulative weight of this Commitment.
func (c *Commitment) cumulativeWeight() reactive.Variable[uint64] {
return c.CumulativeWeight
}

func (c *Commitment) cumulativeAttestedWeightAddr() reactive.Variable[uint64] {
// cumulativeAttestedWeight returns the Variable that contains the cumulative attested weight of this Commitment.
func (c *Commitment) cumulativeAttestedWeight() reactive.Variable[uint64] {
return c.CumulativeAttestedWeight
}

func (c *Commitment) cumulativeVerifiedWeightAddr() reactive.Variable[uint64] {
// cumulativeVerifiedWeight returns the Variable that contains the cumulative verified weight of this Commitment.
func (c *Commitment) cumulativeVerifiedWeight() reactive.Variable[uint64] {
return c.CumulativeVerifiedWeight
}
10 changes: 9 additions & 1 deletion pkg/protocol/commitments.go
Original file line number Diff line number Diff line change
Expand Up @@ -327,8 +327,16 @@ func (c *Commitments) processRequest(commitmentID iotago.CommitmentID, from peer
// processResponse processes the given commitment response.
func (c *Commitments) processResponse(commitment *model.Commitment, from peer.ID) {
c.workerPool.Submit(func() {
// make sure the main engine is available to process the response
mainEngine := c.protocol.Engines.Main.Get()
if mainEngine == nil {
c.LogDebug("main engine unavailable for response", "commitment", commitment.ID(), "fromPeer", from)

return
}

// verify the commitment's version corresponds to the protocol version for the slot.
if apiForSlot := c.protocol.APIForSlot(commitment.Slot()); apiForSlot.Version() != commitment.Commitment().ProtocolVersion {
if apiForSlot := mainEngine.APIForSlot(commitment.Slot()); apiForSlot.Version() != commitment.Commitment().ProtocolVersion {
c.LogDebug("received commitment with invalid protocol version", "commitment", commitment.ID(), "version", commitment.Commitment().ProtocolVersion, "expectedVersion", apiForSlot.Version(), "fromPeer", from)

return
Expand Down
22 changes: 0 additions & 22 deletions pkg/protocol/engine/blockdag/inmemoryblockdag/blockdag.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@ package inmemoryblockdag
import (
"sync/atomic"

"github.com/iotaledger/hive.go/ds"
"github.com/iotaledger/hive.go/ierrors"
"github.com/iotaledger/hive.go/lo"
"github.com/iotaledger/hive.go/log"
"github.com/iotaledger/hive.go/runtime/event"
"github.com/iotaledger/hive.go/runtime/module"
Expand Down Expand Up @@ -86,15 +84,7 @@ func (b *BlockDAG) setupBlock(block *blocks.Block) {
var unsolidParentsCount atomic.Int32
unsolidParentsCount.Store(int32(len(block.Parents())))

unsolidParents := ds.NewSet[iotago.BlockID]()

block.ForEachParent(func(parent iotago.Parent) {
if unsolidParents.Add(parent.ID) {
b.LogTrace("unsolid parents", "blockID", block.ID(), "parents", unsolidParents.ToSlice())
}

b.LogTrace("waiting for parent", "parent", parent.ID, "blockID", block.ID())

parentBlock, exists := b.blockCache.Block(parent.ID)
if !exists {
b.errorHandler(ierrors.Errorf("failed to setup block %s, parent %s is missing", block.ID(), parent.ID))
Expand All @@ -103,12 +93,6 @@ func (b *BlockDAG) setupBlock(block *blocks.Block) {
}

parentBlock.Solid().OnUpdateOnce(func(_ bool, _ bool) {
b.LogTrace("parent solid", "parent", parent.ID, "blockID", block.ID())

if unsolidParents.Delete(parent.ID) {
b.LogTrace("unsolid parents", "blockID", block.ID(), "parents", unsolidParents.ToSlice())
}

if unsolidParentsCount.Add(-1) == 0 {
if block.SetSolid() {
b.events.BlockSolid.Trigger(block)
Expand Down Expand Up @@ -153,13 +137,9 @@ func (b *BlockDAG) Attach(data *model.Block) (block *blocks.Block, wasAttached b
if b.uncommittedSlotBlocks.AddWithFunc(block.SlotCommitmentID(), block, func() bool {
return block.SlotCommitmentID().Slot() > b.latestCommitmentFunc().Commitment().Slot
}) {
b.LogError("unsolid commitment", "blockID", block.ID(), "commitmentID", block.SlotCommitmentID(), "latestCommitment", lo.PanicOnErr(b.latestCommitmentFunc().Commitment().ID()))

return
}

b.LogTrace("block attached", "blockID", block.ID())

b.events.BlockAttached.Trigger(block)

b.setupBlock(block)
Expand All @@ -173,8 +153,6 @@ func (b *BlockDAG) Attach(data *model.Block) (block *blocks.Block, wasAttached b
// creating it.
func (b *BlockDAG) GetOrRequestBlock(blockID iotago.BlockID) (block *blocks.Block, requested bool) {
return b.blockCache.GetOrCreate(blockID, func() (newBlock *blocks.Block) {
b.LogTrace("block missing", "blockID", blockID)

newBlock = blocks.NewMissingBlock(blockID)
b.events.BlockMissing.Trigger(newBlock)

Expand Down

0 comments on commit 6d1533e

Please sign in to comment.