Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix AccountDestroyed and AccountCreated events. #981

Merged
merged 4 commits into from
Jun 4, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 4 additions & 9 deletions pkg/protocol/engine/congestioncontrol/scheduler/drr/scheduler.go
Original file line number Diff line number Diff line change
Expand Up @@ -474,8 +474,7 @@ func (s *Scheduler) selectBasicBlockWithoutLocking() {
issuerID := queue.IssuerID()

if _, err := s.incrementDeficit(issuerID, rounds, slot); err != nil {
s.errorHandler(ierrors.Wrapf(err, "failed to increment deficit for issuerID %s in slot %d", issuerID, slot))
s.removeIssuer(issuerID, err)
s.LogPanic(ierrors.Wrapf(err, "failed to increment deficit for issuerID %s in slot %d", issuerID, slot).Error())
}

return true
Expand All @@ -493,8 +492,7 @@ func (s *Scheduler) selectBasicBlockWithoutLocking() {
issuerID := q.IssuerID()
newDeficit, err := s.incrementDeficit(issuerID, 1, slot)
if err != nil {
s.errorHandler(ierrors.Wrapf(err, "failed to increment deficit for issuerID %s in slot %d", issuerID, slot))
s.removeIssuer(issuerID, err)
s.LogPanic(ierrors.Wrapf(err, "failed to increment deficit for issuerID %s in slot %d", issuerID, slot).Error())

return
}
Expand Down Expand Up @@ -559,10 +557,7 @@ func (s *Scheduler) selectIssuer(slot iotago.SlotIndex) (Deficit, *IssuerQueue)
// calculate how many rounds we need to skip to accumulate enough deficit.
quantum, err := s.quantumFunc(issuerID, slot)
if err != nil {
s.errorHandler(ierrors.Wrapf(err, "failed to retrieve quantum for issuerID %s in slot %d during issuer selection", issuerID, slot))

// if quantum, can't be retrieved, we need to remove this issuer.
s.removeIssuer(issuerID, err)
s.LogPanic(ierrors.Wrapf(err, "failed to retrieve quantum for issuerID %s in slot %d during issuer selection", issuerID, slot).Error())

break
}
Expand Down Expand Up @@ -658,7 +653,7 @@ func (s *Scheduler) updateDeficit(accountID iotago.AccountID, delta Deficit) (De
})

if updateErr != nil {
s.removeIssuer(accountID, updateErr)
s.LogPanic(updateErr.Error())

return 0, updateErr
}
Expand Down
15 changes: 13 additions & 2 deletions pkg/protocol/engine/ledger/ledger/ledger.go
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,13 @@ func (l *Ledger) CommitSlot(slot iotago.SlotIndex) (stateRoot iotago.Identifier,
return iotago.Identifier{}, iotago.Identifier{}, iotago.Identifier{}, nil, nil, nil, ierrors.Wrapf(err, "failed to process outputs consumed and created in slot %d", slot)
}

// Trigger AccountDestroyed event before accounts are removed from the ledgers so that components (like Scheduler),
// that listen to that event always can access consistent view of the account.
_ = destroyedAccounts.ForEach(func(accountID iotago.AccountID) error {
l.events.AccountDestroyed.Trigger(accountID)

return nil
})
l.prepareAccountDiffs(accountDiffs, slot, consumedAccounts, createdAccounts)

// Commit the changes
Expand Down Expand Up @@ -203,6 +210,12 @@ func (l *Ledger) CommitSlot(slot iotago.SlotIndex) (stateRoot iotago.Identifier,
return iotago.Identifier{}, iotago.Identifier{}, iotago.Identifier{}, nil, nil, nil, ierrors.Wrapf(err, "failed to apply diff to mana manager for slot %d", slot)
}

// Created account event need to be triggered only after the AccountLedger and UTXO ledger are updated,
// so that components (like Scheduler) that listen to the event can access the consistent account state.
for accountID := range createdAccounts {
l.events.AccountCreated.Trigger(accountID)
}

// Mark each transaction as committed so the mempool can evict it
stateDiff.ExecutedTransactions().ForEach(func(_ iotago.TransactionID, tx mempool.TransactionMetadata) bool {
tx.Commit()
Expand Down Expand Up @@ -523,7 +536,6 @@ func (l *Ledger) processCreatedAndConsumedAccountOutputs(stateDiff mempool.State
accountID := createdAccount.AccountID
if accountID.Empty() {
accountID = iotago.AccountIDFromOutputID(createdOutput.OutputID())
l.events.AccountCreated.Trigger(accountID)
}

createdAccounts[accountID] = createdOutput
Expand All @@ -542,7 +554,6 @@ func (l *Ledger) processCreatedAndConsumedAccountOutputs(stateDiff mempool.State
// if a basic output is sent to an implicit account creation address, we need to create the account
if createdOutput.Output().UnlockConditionSet().Address().Address.Type() == iotago.AddressImplicitAccountCreation {
accountID := iotago.AccountIDFromOutputID(createdOutput.OutputID())
l.events.AccountCreated.Trigger(accountID)
createdAccounts[accountID] = createdOutput
}
}
Expand Down
Loading