From 29b51ab6cfa1e1f1378745ac2deb177abcc0d66c Mon Sep 17 00:00:00 2001 From: Hans Moog <3293976+hmoog@users.noreply.github.com> Date: Thu, 2 May 2024 12:33:50 +0200 Subject: [PATCH] Fix: Too greedy cut-off condition in accounts getter --- .../engine/accounts/accountsledger/manager.go | 6 ------ .../sybilprotectionv1/sybilprotection.go | 2 +- pkg/requesthandler/accounts.go | 19 +++++++++++++------ 3 files changed, 14 insertions(+), 13 deletions(-) diff --git a/pkg/protocol/engine/accounts/accountsledger/manager.go b/pkg/protocol/engine/accounts/accountsledger/manager.go index 833f6d445..c4a45d6bd 100644 --- a/pkg/protocol/engine/accounts/accountsledger/manager.go +++ b/pkg/protocol/engine/accounts/accountsledger/manager.go @@ -200,12 +200,6 @@ func (m *Manager) Account(accountID iotago.AccountID, targetSlot iotago.SlotInde } func (m *Manager) account(accountID iotago.AccountID, targetSlot iotago.SlotIndex) (accountData *accounts.AccountData, exists bool, err error) { - // if m.latestCommittedSlot < maxCommittableAge we should have all history - maxCommittableAge := m.apiProvider.APIForSlot(targetSlot).ProtocolParameters().MaxCommittableAge() - if m.latestCommittedSlot >= maxCommittableAge && targetSlot+maxCommittableAge < m.latestCommittedSlot { - return nil, false, ierrors.Errorf("can't calculate account, target slot index older than allowed (%d<%d)", targetSlot, m.latestCommittedSlot-maxCommittableAge) - } - if targetSlot > m.latestCommittedSlot { return nil, false, ierrors.Errorf("can't retrieve account, slot %d is not committed yet, latest committed slot: %d", targetSlot, m.latestCommittedSlot) } diff --git a/pkg/protocol/sybilprotection/sybilprotectionv1/sybilprotection.go b/pkg/protocol/sybilprotection/sybilprotectionv1/sybilprotection.go index 2a2d078d6..d5fbe9bfa 100644 --- a/pkg/protocol/sybilprotection/sybilprotectionv1/sybilprotection.go +++ b/pkg/protocol/sybilprotection/sybilprotectionv1/sybilprotection.go @@ -298,7 +298,7 @@ func (o *SybilProtection) slotFinalized(slot iotago.SlotIndex) { // Otherwise, skip committee selection because it's too late and the committee has been reused. epochEndSlot := timeProvider.EpochEnd(epoch) if slot+apiForSlot.ProtocolParameters().EpochNearingThreshold() == epochEndSlot && - epochEndSlot > o.lastCommittedSlot+apiForSlot.ProtocolParameters().MaxCommittableAge() { + o.lastCommittedSlot < epochEndSlot-apiForSlot.ProtocolParameters().MaxCommittableAge() { newCommittee, err := o.selectNewCommittee(slot) if err != nil { panic(ierrors.Wrap(err, "error while selecting new committee")) diff --git a/pkg/requesthandler/accounts.go b/pkg/requesthandler/accounts.go index a8c56378c..c7989746e 100644 --- a/pkg/requesthandler/accounts.go +++ b/pkg/requesthandler/accounts.go @@ -27,19 +27,26 @@ func (r *RequestHandler) CongestionByAccountAddress(accountAddress *iotago.Accou } accountID := accountAddress.AccountID() - acc, exists, err := r.protocol.Engines.Main.Get().Ledger.Account(accountID, commitment.Slot()) + targetSlot := commitment.Slot() + maxCommittableAge := r.protocol.APIForSlot(targetSlot).ProtocolParameters().MaxCommittableAge() + latestCommittedSlot := r.protocol.Engines.Main.Get().SyncManager.LatestCommitment().Slot() + + if latestCommittedSlot >= maxCommittableAge && targetSlot+maxCommittableAge < latestCommittedSlot { + return nil, ierrors.Errorf("can't calculate account, target slot index older than allowed (%d<%d)", targetSlot, latestCommittedSlot-maxCommittableAge) + } + + acc, exists, err := r.protocol.Engines.Main.Get().Ledger.Account(accountID, targetSlot) if err != nil { return nil, ierrors.WithMessagef(echo.ErrInternalServerError, "failed to get account %s from the Ledger: %w", accountID.ToHex(), err) - } - if !exists { + } else if !exists { return nil, ierrors.WithMessagef(echo.ErrNotFound, "account %s not found", accountID.ToHex()) } blockIssuanceCredits := acc.Credits.Value // Apply decay to BIC if the value is positive if blockIssuanceCredits > 0 { - manaDecayProvider := r.APIProvider().APIForSlot(commitment.Slot()).ManaDecayProvider() - decayedBIC, err := manaDecayProvider.DecayManaBySlots(iotago.Mana(acc.Credits.Value), acc.Credits.UpdateSlot, commitment.Slot()) + manaDecayProvider := r.APIProvider().APIForSlot(targetSlot).ManaDecayProvider() + decayedBIC, err := manaDecayProvider.DecayManaBySlots(iotago.Mana(acc.Credits.Value), acc.Credits.UpdateSlot, targetSlot) if err != nil { return nil, ierrors.WithMessagef(echo.ErrInternalServerError, "failed to decay BIC for account %s: %w", accountID.ToHex(), err) } @@ -47,7 +54,7 @@ func (r *RequestHandler) CongestionByAccountAddress(accountAddress *iotago.Accou } return &api.CongestionResponse{ - Slot: commitment.Slot(), + Slot: targetSlot, Ready: r.protocol.Engines.Main.Get().Scheduler.IsBlockIssuerReady(accountID, workScores...), ReferenceManaCost: commitment.ReferenceManaCost(), BlockIssuanceCredits: blockIssuanceCredits,