Skip to content

Commit

Permalink
Merge pull request #954 from iotaledger/fix/accounts-early-return
Browse files Browse the repository at this point in the history
Fix: Too greedy cut-off condition in accounts getter
  • Loading branch information
alexsporn authored May 2, 2024
2 parents 06d43dc + 29b51ab commit 5690ffe
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 13 deletions.
6 changes: 0 additions & 6 deletions pkg/protocol/engine/accounts/accountsledger/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"))
Expand Down
19 changes: 13 additions & 6 deletions pkg/requesthandler/accounts.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,27 +27,34 @@ 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)
}
blockIssuanceCredits = iotago.BlockIssuanceCredits(decayedBIC)
}

return &api.CongestionResponse{
Slot: commitment.Slot(),
Slot: targetSlot,
Ready: r.protocol.Engines.Main.Get().Scheduler.IsBlockIssuerReady(accountID, workScores...),
ReferenceManaCost: commitment.ReferenceManaCost(),
BlockIssuanceCredits: blockIssuanceCredits,
Expand Down

0 comments on commit 5690ffe

Please sign in to comment.