Skip to content

Commit

Permalink
Trigger AccountCreated event when accounts are visible in the ledger.…
Browse files Browse the repository at this point in the history
… Hook up AccountDestroyed event.
  • Loading branch information
piotrm50 committed Jun 4, 2024
1 parent d9077af commit 896ed8a
Showing 1 changed file with 13 additions and 2 deletions.
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

0 comments on commit 896ed8a

Please sign in to comment.