Skip to content

Commit

Permalink
Code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
muXxer committed Apr 29, 2024
1 parent 3a47266 commit e1c87fd
Show file tree
Hide file tree
Showing 21 changed files with 126 additions and 117 deletions.
2 changes: 1 addition & 1 deletion components/prometheus/metrics_accounts.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ var AccountMetrics = collector.NewCollection(accountNamespace,
collector.WithPruningDelay(10*time.Minute),
collector.WithInitFunc(func() {
deps.Protocol.Events.Engine.BlockGadget.BlockAccepted.Hook(func(block *blocks.Block) {
accountData, exists, _ := deps.Protocol.Engines.Main.Get().Ledger.Account(block.ProtocolBlock().Header.IssuerID, deps.Protocol.Engines.Main.Get().SyncManager.LatestCommitment().Slot())
accountData, exists, _ := deps.Protocol.Engines.Main.Get().Ledger.Account(block.IssuerID(), deps.Protocol.Engines.Main.Get().SyncManager.LatestCommitment().Slot())
if exists {
deps.Collector.Update(accountNamespace, credits, float64(accountData.Credits.Value), accountData.ID.String())
}
Expand Down
44 changes: 20 additions & 24 deletions components/prometheus/metrics_scheduler.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,20 +35,20 @@ var SchedulerMetrics = collector.NewCollection(schedulerNamespace,
collector.WithHelp("Current size of each node's queue (in work units)."),
collector.WithInitFunc(func() {
deps.Protocol.Events.Engine.Scheduler.BlockEnqueued.Hook(func(block *blocks.Block) {
deps.Collector.Update(schedulerNamespace, queueSizePerNodeWork, float64(deps.Protocol.Engines.Main.Get().Scheduler.IssuerQueueWork(block.ProtocolBlock().Header.IssuerID)), block.ProtocolBlock().Header.IssuerID.String())
deps.Collector.Update(schedulerNamespace, queueSizePerNodeWork, float64(deps.Protocol.Engines.Main.Get().Scheduler.IssuerQueueWork(block.IssuerID())), block.IssuerID().String())

}, event.WithWorkerPool(Component.WorkerPool))

deps.Protocol.Events.Engine.Scheduler.BlockSkipped.Hook(func(block *blocks.Block) {
deps.Collector.Update(schedulerNamespace, queueSizePerNodeWork, float64(deps.Protocol.Engines.Main.Get().Scheduler.IssuerQueueWork(block.ProtocolBlock().Header.IssuerID)), block.ProtocolBlock().Header.IssuerID.String())
deps.Collector.Update(schedulerNamespace, queueSizePerNodeWork, float64(deps.Protocol.Engines.Main.Get().Scheduler.IssuerQueueWork(block.IssuerID())), block.IssuerID().String())
}, event.WithWorkerPool(Component.WorkerPool))

deps.Protocol.Events.Engine.Scheduler.BlockDropped.Hook(func(block *blocks.Block, _ error) {
deps.Collector.Update(schedulerNamespace, queueSizePerNodeWork, float64(deps.Protocol.Engines.Main.Get().Scheduler.IssuerQueueWork(block.ProtocolBlock().Header.IssuerID)), block.ProtocolBlock().Header.IssuerID.String())
deps.Collector.Update(schedulerNamespace, queueSizePerNodeWork, float64(deps.Protocol.Engines.Main.Get().Scheduler.IssuerQueueWork(block.IssuerID())), block.IssuerID().String())
}, event.WithWorkerPool(Component.WorkerPool))

deps.Protocol.Events.Engine.Scheduler.BlockScheduled.Hook(func(block *blocks.Block) {
deps.Collector.Update(schedulerNamespace, queueSizePerNodeWork, float64(deps.Protocol.Engines.Main.Get().Scheduler.IssuerQueueWork(block.ProtocolBlock().Header.IssuerID)), block.ProtocolBlock().Header.IssuerID.String())
deps.Collector.Update(schedulerNamespace, queueSizePerNodeWork, float64(deps.Protocol.Engines.Main.Get().Scheduler.IssuerQueueWork(block.IssuerID())), block.IssuerID().String())
}, event.WithWorkerPool(Component.WorkerPool))
}),
)),
Expand All @@ -59,26 +59,26 @@ var SchedulerMetrics = collector.NewCollection(schedulerNamespace,
collector.WithHelp("Current size of each node's queue (as block count)."),
collector.WithInitFunc(func() {
deps.Protocol.Events.Engine.Scheduler.BlockEnqueued.Hook(func(block *blocks.Block) {
if _, isBasic := block.BasicBlock(); isBasic {
deps.Collector.Update(schedulerNamespace, queueSizePerNodeCount, float64(deps.Protocol.Engines.Main.Get().Scheduler.IssuerQueueBlockCount(block.ProtocolBlock().Header.IssuerID)), block.ProtocolBlock().Header.IssuerID.String())
if block.IsBasicBlock() {
deps.Collector.Update(schedulerNamespace, queueSizePerNodeCount, float64(deps.Protocol.Engines.Main.Get().Scheduler.IssuerQueueBlockCount(block.IssuerID())), block.IssuerID().String())
}
}, event.WithWorkerPool(Component.WorkerPool))

deps.Protocol.Events.Engine.Scheduler.BlockSkipped.Hook(func(block *blocks.Block) {
if _, isBasic := block.BasicBlock(); isBasic {
deps.Collector.Update(schedulerNamespace, queueSizePerNodeCount, float64(deps.Protocol.Engines.Main.Get().Scheduler.IssuerQueueBlockCount(block.ProtocolBlock().Header.IssuerID)), block.ProtocolBlock().Header.IssuerID.String())
if block.IsBasicBlock() {
deps.Collector.Update(schedulerNamespace, queueSizePerNodeCount, float64(deps.Protocol.Engines.Main.Get().Scheduler.IssuerQueueBlockCount(block.IssuerID())), block.IssuerID().String())
}
}, event.WithWorkerPool(Component.WorkerPool))

deps.Protocol.Events.Engine.Scheduler.BlockDropped.Hook(func(block *blocks.Block, _ error) {
if _, isBasic := block.BasicBlock(); isBasic {
deps.Collector.Update(schedulerNamespace, queueSizePerNodeCount, float64(deps.Protocol.Engines.Main.Get().Scheduler.IssuerQueueBlockCount(block.ProtocolBlock().Header.IssuerID)), block.ProtocolBlock().Header.IssuerID.String())
if block.IsBasicBlock() {
deps.Collector.Update(schedulerNamespace, queueSizePerNodeCount, float64(deps.Protocol.Engines.Main.Get().Scheduler.IssuerQueueBlockCount(block.IssuerID())), block.IssuerID().String())
}
}, event.WithWorkerPool(Component.WorkerPool))

deps.Protocol.Events.Engine.Scheduler.BlockScheduled.Hook(func(block *blocks.Block) {
if _, isBasic := block.BasicBlock(); isBasic {
deps.Collector.Update(schedulerNamespace, queueSizePerNodeCount, float64(deps.Protocol.Engines.Main.Get().Scheduler.IssuerQueueBlockCount(block.ProtocolBlock().Header.IssuerID)), block.ProtocolBlock().Header.IssuerID.String())
if block.IsBasicBlock() {
deps.Collector.Update(schedulerNamespace, queueSizePerNodeCount, float64(deps.Protocol.Engines.Main.Get().Scheduler.IssuerQueueBlockCount(block.IssuerID())), block.IssuerID().String())
}
}, event.WithWorkerPool(Component.WorkerPool))
}),
Expand All @@ -90,26 +90,26 @@ var SchedulerMetrics = collector.NewCollection(schedulerNamespace,
collector.WithHelp("Current number of validation blocks in each validator's queue."),
collector.WithInitFunc(func() {
deps.Protocol.Events.Engine.Scheduler.BlockEnqueued.Hook(func(block *blocks.Block) {
if _, isValidation := block.ValidationBlock(); isValidation {
deps.Collector.Update(schedulerNamespace, validatorQueueSizePerNodeCount, float64(deps.Protocol.Engines.Main.Get().Scheduler.ValidatorQueueBlockCount(block.ProtocolBlock().Header.IssuerID)), block.ProtocolBlock().Header.IssuerID.String())
if block.IsValidationBlock() {
deps.Collector.Update(schedulerNamespace, validatorQueueSizePerNodeCount, float64(deps.Protocol.Engines.Main.Get().Scheduler.ValidatorQueueBlockCount(block.IssuerID())), block.IssuerID().String())
}
}, event.WithWorkerPool(Component.WorkerPool))

deps.Protocol.Events.Engine.Scheduler.BlockSkipped.Hook(func(block *blocks.Block) {
if _, isValidation := block.ValidationBlock(); isValidation {
deps.Collector.Update(schedulerNamespace, validatorQueueSizePerNodeCount, float64(deps.Protocol.Engines.Main.Get().Scheduler.ValidatorQueueBlockCount(block.ProtocolBlock().Header.IssuerID)), block.ProtocolBlock().Header.IssuerID.String())
if block.IsValidationBlock() {
deps.Collector.Update(schedulerNamespace, validatorQueueSizePerNodeCount, float64(deps.Protocol.Engines.Main.Get().Scheduler.ValidatorQueueBlockCount(block.IssuerID())), block.IssuerID().String())
}
}, event.WithWorkerPool(Component.WorkerPool))

deps.Protocol.Events.Engine.Scheduler.BlockDropped.Hook(func(block *blocks.Block, _ error) {
if _, isValidation := block.ValidationBlock(); isValidation {
deps.Collector.Update(schedulerNamespace, validatorQueueSizePerNodeCount, float64(deps.Protocol.Engines.Main.Get().Scheduler.ValidatorQueueBlockCount(block.ProtocolBlock().Header.IssuerID)), block.ProtocolBlock().Header.IssuerID.String())
if block.IsValidationBlock() {
deps.Collector.Update(schedulerNamespace, validatorQueueSizePerNodeCount, float64(deps.Protocol.Engines.Main.Get().Scheduler.ValidatorQueueBlockCount(block.IssuerID())), block.IssuerID().String())
}
}, event.WithWorkerPool(Component.WorkerPool))

deps.Protocol.Events.Engine.Scheduler.BlockScheduled.Hook(func(block *blocks.Block) {
if _, isValidation := block.ValidationBlock(); isValidation {
deps.Collector.Update(schedulerNamespace, validatorQueueSizePerNodeCount, float64(deps.Protocol.Engines.Main.Get().Scheduler.ValidatorQueueBlockCount(block.ProtocolBlock().Header.IssuerID)), block.ProtocolBlock().Header.IssuerID.String())
if block.IsValidationBlock() {
deps.Collector.Update(schedulerNamespace, validatorQueueSizePerNodeCount, float64(deps.Protocol.Engines.Main.Get().Scheduler.ValidatorQueueBlockCount(block.IssuerID())), block.IssuerID().String())
}
}, event.WithWorkerPool(Component.WorkerPool))
}),
Expand All @@ -121,22 +121,18 @@ var SchedulerMetrics = collector.NewCollection(schedulerNamespace,
collector.WithInitFunc(func() {
deps.Protocol.Events.Engine.Scheduler.BlockEnqueued.Hook(func(_ *blocks.Block) {
deps.Collector.Increment(schedulerNamespace, schedulerProcessedBlocks, enqueuedBlockLabel)

}, event.WithWorkerPool(Component.WorkerPool))

deps.Protocol.Events.Engine.Scheduler.BlockDropped.Hook(func(_ *blocks.Block, _ error) {
deps.Collector.Increment(schedulerNamespace, schedulerProcessedBlocks, droppedBlockLabel)

}, event.WithWorkerPool(Component.WorkerPool))

deps.Protocol.Events.Engine.Scheduler.BlockSkipped.Hook(func(_ *blocks.Block) {
deps.Collector.Increment(schedulerNamespace, schedulerProcessedBlocks, skippedBlockLabel)

}, event.WithWorkerPool(Component.WorkerPool))

deps.Protocol.Events.Engine.Scheduler.BlockScheduled.Hook(func(_ *blocks.Block) {
deps.Collector.Increment(schedulerNamespace, schedulerProcessedBlocks, scheduledBlockLabel)

}, event.WithWorkerPool(Component.WorkerPool))
}),
)),
Expand Down
10 changes: 5 additions & 5 deletions pkg/protocol/engine/accounts/accountsledger/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ func (m *Manager) TrackBlock(block *blocks.Block) {
if validationBlock, isValidationBlock := block.ValidationBlock(); isValidationBlock {
newSignaledBlock := model.NewSignaledBlock(block.ID(), block.ProtocolBlock(), validationBlock)

m.latestSupportedVersionSignals.Get(block.ID().Slot(), true).Compute(block.ProtocolBlock().Header.IssuerID, func(currentValue *model.SignaledBlock, exists bool) *model.SignaledBlock {
m.latestSupportedVersionSignals.Get(block.ID().Slot(), true).Compute(block.IssuerID(), func(currentValue *model.SignaledBlock, exists bool) *model.SignaledBlock {
if !exists {
return newSignaledBlock
}
Expand Down Expand Up @@ -481,10 +481,10 @@ func (m *Manager) computeBlockBurnsForSlot(slot iotago.SlotIndex, rmc iotago.Man
if !blockLoaded {
return nil, ierrors.Errorf("cannot apply the new diff, block %s not found in the block cache", blockID)
}
if _, isBasicBlock := block.BasicBlock(); isBasicBlock {
burns[block.ProtocolBlock().Header.IssuerID] += iotago.Mana(block.WorkScore()) * rmc
} else if _, isValidationBlock := block.ValidationBlock(); isValidationBlock {
validationBlockCount[block.ProtocolBlock().Header.IssuerID]++
if block.IsBasicBlock() {
burns[block.IssuerID()] += iotago.Mana(block.WorkScore()) * rmc
} else if block.IsValidationBlock() {
validationBlockCount[block.IssuerID()]++
}
}
validationBlocksPerSlot := int(apiForSlot.ProtocolParameters().ValidationBlocksPerSlot())
Expand Down
6 changes: 3 additions & 3 deletions pkg/protocol/engine/attestation/slotattestation/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ func (m *Manager) GetMap(slot iotago.SlotIndex) (ads.Map[iotago.Identifier, iota
// AddAttestationFromValidationBlock adds an attestation from a block to the future attestations (beyond the attestation window).
func (m *Manager) AddAttestationFromValidationBlock(block *blocks.Block) error {
// Only track validation blocks.
if _, isValidationBlock := block.ValidationBlock(); !isValidationBlock {
if !block.IsValidationBlock() {
return nil
}

Expand All @@ -150,7 +150,7 @@ func (m *Manager) AddAttestationFromValidationBlock(block *blocks.Block) error {
return ierrors.Errorf("committee for slot %d does not exist", block.ID().Slot())
}
// Only track attestations of active committee members.
if _, exists := committee.GetSeat(block.ProtocolBlock().Header.IssuerID); !exists {
if _, exists := committee.GetSeat(block.IssuerID()); !exists {
return nil
}

Expand All @@ -165,7 +165,7 @@ func (m *Manager) AddAttestationFromValidationBlock(block *blocks.Block) error {
newAttestation := iotago.NewAttestation(m.apiProvider.APIForSlot(block.ID().Slot()), block.ProtocolBlock())

// We keep only the latest attestation for each committee member.
m.futureAttestations.Get(block.ID().Slot(), true).Compute(block.ProtocolBlock().Header.IssuerID, func(currentValue *iotago.Attestation, exists bool) *iotago.Attestation {
m.futureAttestations.Get(block.ID().Slot(), true).Compute(block.IssuerID(), func(currentValue *iotago.Attestation, exists bool) *iotago.Attestation {
if !exists {
return newAttestation
}
Expand Down
12 changes: 12 additions & 0 deletions pkg/protocol/engine/blocks/block.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,10 @@ func (b *Block) ForEachParent(consumer func(parent iotago.Parent)) {
b.modelBlock.ProtocolBlock().ForEachParent(consumer)
}

func (b *Block) IssuerID() iotago.AccountID {
return b.modelBlock.ProtocolBlock().Header.IssuerID
}

func (b *Block) IsRootBlock() bool {
return b.rootBlock != nil
}
Expand All @@ -189,6 +193,10 @@ func (b *Block) SignedTransaction() (tx *iotago.SignedTransaction, hasTransactio
return b.modelBlock.SignedTransaction()
}

func (b *Block) IsBasicBlock() bool {
return lo.Return2(b.BasicBlock())
}

func (b *Block) BasicBlock() (basicBlock *iotago.BasicBlockBody, isBasicBlock bool) {
if b.modelBlock == nil {
return nil, false
Expand All @@ -197,6 +205,10 @@ func (b *Block) BasicBlock() (basicBlock *iotago.BasicBlockBody, isBasicBlock bo
return b.modelBlock.BasicBlock()
}

func (b *Block) IsValidationBlock() bool {
return lo.Return2(b.ValidationBlock())
}

func (b *Block) ValidationBlock() (validationBlock *iotago.ValidationBlockBody, isValidationBlock bool) {
if b.modelBlock == nil {
return nil, false
Expand Down
Loading

0 comments on commit e1c87fd

Please sign in to comment.