Skip to content
This repository was archived by the owner on Jan 24, 2025. It is now read-only.

Commit 40256f0

Browse files
committed
Properly track newly created accounts
1 parent d30796c commit 40256f0

File tree

1 file changed

+22
-16
lines changed

1 file changed

+22
-16
lines changed

pkg/protocol/engine/ledger/ledger/ledger.go

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ func (l *Ledger) CommitSlot(slot iotago.SlotIndex) (stateRoot iotago.Identifier,
169169
// account changes at UTXO level without needing to worry about multiple spenders of the same account in the same slot,
170170
// we only care about the initial account output to be consumed and the final account output to be created.
171171
// output side
172-
createdAccounts, consumedAccounts, destroyedAccounts, err := l.processCreatedAndConsumedAccountOutputs(stateDiff, accountDiffs)
172+
createdAccountOutputs, consumedAccountOutputs, createdAccounts, destroyedAccounts, err := l.processCreatedAndConsumedAccountOutputs(stateDiff, accountDiffs)
173173
if err != nil {
174174
return iotago.Identifier{}, iotago.Identifier{}, iotago.Identifier{}, nil, nil, nil, ierrors.Wrapf(err, "failed to process outputs consumed and created in slot %d", slot)
175175
}
@@ -181,7 +181,8 @@ func (l *Ledger) CommitSlot(slot iotago.SlotIndex) (stateRoot iotago.Identifier,
181181

182182
return nil
183183
})
184-
l.prepareAccountDiffs(accountDiffs, slot, consumedAccounts, createdAccounts)
184+
185+
l.prepareAccountDiffs(accountDiffs, slot, consumedAccountOutputs, createdAccountOutputs)
185186

186187
// Commit the changes
187188
// Update the UTXO ledger
@@ -206,15 +207,17 @@ func (l *Ledger) CommitSlot(slot iotago.SlotIndex) (stateRoot iotago.Identifier,
206207
}
207208

208209
// Update the mana manager's cache
209-
if err = l.manaManager.ApplyDiff(slot, destroyedAccounts, createdAccounts, accountDiffs); err != nil {
210+
if err = l.manaManager.ApplyDiff(slot, destroyedAccounts, createdAccountOutputs, accountDiffs); err != nil {
210211
return iotago.Identifier{}, iotago.Identifier{}, iotago.Identifier{}, nil, nil, nil, ierrors.Wrapf(err, "failed to apply diff to mana manager for slot %d", slot)
211212
}
212213

213214
// Created account event need to be triggered only after the AccountLedger and UTXO ledger are updated,
214215
// so that components (like Scheduler) that listen to the event can access the consistent account state.
215-
for accountID := range createdAccounts {
216+
_ = createdAccounts.ForEach(func(accountID iotago.AccountID) error {
216217
l.events.AccountCreated.Trigger(accountID)
217-
}
218+
219+
return nil
220+
})
218221

219222
// Mark each transaction as committed so the mempool can evict it
220223
stateDiff.ExecutedTransactions().ForEach(func(_ iotago.TransactionID, tx mempool.TransactionMetadata) bool {
@@ -509,9 +512,10 @@ func (l *Ledger) prepareAccountDiffs(accountDiffs map[iotago.AccountID]*model.Ac
509512
}
510513
}
511514

512-
func (l *Ledger) processCreatedAndConsumedAccountOutputs(stateDiff mempool.StateDiff, accountDiffs map[iotago.AccountID]*model.AccountDiff) (createdAccounts map[iotago.AccountID]*utxoledger.Output, consumedAccounts map[iotago.AccountID]*utxoledger.Output, destroyedAccounts ds.Set[iotago.AccountID], err error) {
513-
createdAccounts = make(map[iotago.AccountID]*utxoledger.Output)
514-
consumedAccounts = make(map[iotago.AccountID]*utxoledger.Output)
515+
func (l *Ledger) processCreatedAndConsumedAccountOutputs(stateDiff mempool.StateDiff, accountDiffs map[iotago.AccountID]*model.AccountDiff) (createdAccountOutputs map[iotago.AccountID]*utxoledger.Output, consumedAccountOutputs map[iotago.AccountID]*utxoledger.Output, createdAccounts ds.Set[iotago.AccountID], destroyedAccounts ds.Set[iotago.AccountID], err error) {
516+
createdAccountOutputs = make(map[iotago.AccountID]*utxoledger.Output)
517+
consumedAccountOutputs = make(map[iotago.AccountID]*utxoledger.Output)
518+
createdAccounts = ds.NewSet[iotago.AccountID]()
515519
destroyedAccounts = ds.NewSet[iotago.AccountID]()
516520

517521
newAccountDelegation := make(map[iotago.ChainID]*iotago.DelegationOutput)
@@ -536,9 +540,10 @@ func (l *Ledger) processCreatedAndConsumedAccountOutputs(stateDiff mempool.State
536540
accountID := createdAccount.AccountID
537541
if accountID.Empty() {
538542
accountID = iotago.AccountIDFromOutputID(createdOutput.OutputID())
543+
createdAccounts.Add(accountID)
539544
}
540545

541-
createdAccounts[accountID] = createdOutput
546+
createdAccountOutputs[accountID] = createdOutput
542547
case iotago.OutputDelegation:
543548
delegationOutput, _ := createdOutput.Output().(*iotago.DelegationOutput)
544549
delegationID := delegationOutput.DelegationID
@@ -554,15 +559,16 @@ func (l *Ledger) processCreatedAndConsumedAccountOutputs(stateDiff mempool.State
554559
// if a basic output is sent to an implicit account creation address, we need to create the account
555560
if createdOutput.Output().UnlockConditionSet().Address().Address.Type() == iotago.AddressImplicitAccountCreation {
556561
accountID := iotago.AccountIDFromOutputID(createdOutput.OutputID())
557-
createdAccounts[accountID] = createdOutput
562+
createdAccounts.Add(accountID)
563+
createdAccountOutputs[accountID] = createdOutput
558564
}
559565
}
560566

561567
return true
562568
})
563569

564570
if err != nil {
565-
return nil, nil, nil, ierrors.Wrap(err, "error while processing created states")
571+
return nil, nil, nil, nil, ierrors.Wrap(err, "error while processing created states")
566572
}
567573

568574
// input side
@@ -585,10 +591,10 @@ func (l *Ledger) processCreatedAndConsumedAccountOutputs(stateDiff mempool.State
585591
if consumedAccount.FeatureSet().BlockIssuer() == nil && consumedAccount.FeatureSet().Staking() == nil {
586592
return true
587593
}
588-
consumedAccounts[accountID] = spentOutput
594+
consumedAccountOutputs[accountID] = spentOutput
589595

590596
// if we have consumed accounts that are not created in the same slot, we need to track them as destroyed
591-
if _, exists := createdAccounts[accountID]; !exists {
597+
if _, exists := createdAccountOutputs[accountID]; !exists {
592598
destroyedAccounts.Add(accountID)
593599
}
594600

@@ -606,7 +612,7 @@ func (l *Ledger) processCreatedAndConsumedAccountOutputs(stateDiff mempool.State
606612
// if a basic output (implicit account) is consumed, get the accountID as hash of the output ID.
607613
if spentOutput.Output().UnlockConditionSet().Address().Address.Type() == iotago.AddressImplicitAccountCreation {
608614
accountID := iotago.AccountIDFromOutputID(spentOutput.OutputID())
609-
consumedAccounts[accountID] = spentOutput
615+
consumedAccountOutputs[accountID] = spentOutput
610616
}
611617
}
612618

@@ -620,10 +626,10 @@ func (l *Ledger) processCreatedAndConsumedAccountOutputs(stateDiff mempool.State
620626
}
621627

622628
if err != nil {
623-
return nil, nil, nil, ierrors.Wrap(err, "error while processing created states")
629+
return nil, nil, nil, nil, ierrors.Wrap(err, "error while processing created states")
624630
}
625631

626-
return createdAccounts, consumedAccounts, destroyedAccounts, nil
632+
return createdAccountOutputs, consumedAccountOutputs, createdAccounts, destroyedAccounts, nil
627633
}
628634

629635
func (l *Ledger) processStateDiffTransactions(stateDiff mempool.StateDiff) (spents utxoledger.Spents, outputs utxoledger.Outputs, accountDiffs map[iotago.AccountID]*model.AccountDiff, err error) {

0 commit comments

Comments
 (0)