From 6be358d1c16e2ce8375f833012d8717faa8c399b Mon Sep 17 00:00:00 2001 From: muXxer Date: Mon, 20 Nov 2023 19:01:15 +0100 Subject: [PATCH] Fix trigger of StateDiffApplied event before commitment is stored --- components/inx/server_utxo.go | 5 +++-- pkg/protocol/engine/ledger/events.go | 3 --- pkg/protocol/engine/ledger/ledger.go | 2 +- pkg/protocol/engine/ledger/ledger/ledger.go | 22 +++++++++---------- pkg/protocol/engine/notarization/events.go | 3 +++ .../notarization/slotnotarization/manager.go | 4 +++- 6 files changed, 20 insertions(+), 19 deletions(-) diff --git a/components/inx/server_utxo.go b/components/inx/server_utxo.go index 4cab44670..d1e87561e 100644 --- a/components/inx/server_utxo.go +++ b/components/inx/server_utxo.go @@ -12,6 +12,7 @@ import ( "github.com/iotaledger/hive.go/runtime/workerpool" inx "github.com/iotaledger/inx/go" "github.com/iotaledger/iota-core/pkg/protocol/engine/mempool" + "github.com/iotaledger/iota-core/pkg/protocol/engine/notarization" "github.com/iotaledger/iota-core/pkg/protocol/engine/utxoledger" iotago "github.com/iotaledger/iota.go/v4" ) @@ -322,8 +323,8 @@ func (s *Server) ListenToLedgerUpdates(req *inx.SlotRangeRequest, srv inx.INX_Li wp := workerpool.New("ListenToLedgerUpdates", workerpool.WithWorkerCount(workerCount)).Start() - unhook := deps.Protocol.Events.Engine.Ledger.StateDiffApplied.Hook(func(slot iotago.SlotIndex, newOutputs utxoledger.Outputs, newSpents utxoledger.Spents) { - done, err := handleRangedSend2(slot, newOutputs, newSpents, stream, catchUpFunc, sendFunc) + unhook := deps.Protocol.Events.Engine.Notarization.SlotCommitted.Hook(func(scd *notarization.SlotCommittedDetails) { + done, err := handleRangedSend2(scd.Commitment.Slot(), scd.OutputsCreated, scd.OutputsConsumed, stream, catchUpFunc, sendFunc) switch { case err != nil: innerErr = err diff --git a/pkg/protocol/engine/ledger/events.go b/pkg/protocol/engine/ledger/events.go index ad30d98c0..5549bfcc9 100644 --- a/pkg/protocol/engine/ledger/events.go +++ b/pkg/protocol/engine/ledger/events.go @@ -2,12 +2,10 @@ package ledger import ( "github.com/iotaledger/hive.go/runtime/event" - "github.com/iotaledger/iota-core/pkg/protocol/engine/utxoledger" iotago "github.com/iotaledger/iota.go/v4" ) type Events struct { - StateDiffApplied *event.Event3[iotago.SlotIndex, utxoledger.Outputs, utxoledger.Spents] AccountCreated *event.Event1[iotago.AccountID] AccountDestroyed *event.Event1[iotago.AccountID] @@ -17,7 +15,6 @@ type Events struct { // NewEvents contains the constructor of the Events object (it is generated by a generic factory). var NewEvents = event.CreateGroupConstructor(func() (newEvents *Events) { return &Events{ - StateDiffApplied: event.New3[iotago.SlotIndex, utxoledger.Outputs, utxoledger.Spents](), AccountCreated: event.New1[iotago.AccountID](), AccountDestroyed: event.New1[iotago.AccountID](), } diff --git a/pkg/protocol/engine/ledger/ledger.go b/pkg/protocol/engine/ledger/ledger.go index b038b9ca5..e0ff2baee 100644 --- a/pkg/protocol/engine/ledger/ledger.go +++ b/pkg/protocol/engine/ledger/ledger.go @@ -37,7 +37,7 @@ type Ledger interface { ManaManager() *mana.Manager RMCManager() *rmc.Manager - CommitSlot(slot iotago.SlotIndex) (stateRoot, mutationRoot, accountRoot iotago.Identifier, err error) + CommitSlot(slot iotago.SlotIndex) (stateRoot, mutationRoot, accountRoot iotago.Identifier, created utxoledger.Outputs, consumed utxoledger.Spents, err error) Import(reader io.ReadSeeker) error Export(writer io.WriteSeeker, targetSlot iotago.SlotIndex) error diff --git a/pkg/protocol/engine/ledger/ledger/ledger.go b/pkg/protocol/engine/ledger/ledger/ledger.go index 631e025e9..144b10325 100644 --- a/pkg/protocol/engine/ledger/ledger/ledger.go +++ b/pkg/protocol/engine/ledger/ledger/ledger.go @@ -138,10 +138,10 @@ func (l *Ledger) AttachTransaction(block *blocks.Block) (attachedTransaction mem return nil, false } -func (l *Ledger) CommitSlot(slot iotago.SlotIndex) (stateRoot iotago.Identifier, mutationRoot iotago.Identifier, accountRoot iotago.Identifier, err error) { +func (l *Ledger) CommitSlot(slot iotago.SlotIndex) (stateRoot iotago.Identifier, mutationRoot iotago.Identifier, accountRoot iotago.Identifier, created utxoledger.Outputs, consumed utxoledger.Spents, err error) { ledgerIndex, err := l.utxoLedger.ReadLedgerSlot() if err != nil { - return iotago.Identifier{}, iotago.Identifier{}, iotago.Identifier{}, err + return iotago.Identifier{}, iotago.Identifier{}, iotago.Identifier{}, nil, nil, err } if slot != ledgerIndex+1 { @@ -150,7 +150,7 @@ func (l *Ledger) CommitSlot(slot iotago.SlotIndex) (stateRoot iotago.Identifier, stateDiff, err := l.memPool.StateDiff(slot) if err != nil { - return iotago.Identifier{}, iotago.Identifier{}, iotago.Identifier{}, ierrors.Errorf("failed to retrieve state diff for slot %d: %w", slot, err) + return iotago.Identifier{}, iotago.Identifier{}, iotago.Identifier{}, nil, nil, ierrors.Errorf("failed to retrieve state diff for slot %d: %w", slot, err) } // collect outputs and allotments from the "uncompacted" stateDiff @@ -158,7 +158,7 @@ func (l *Ledger) CommitSlot(slot iotago.SlotIndex) (stateRoot iotago.Identifier, // and retrieve intermediate outputs to show to the user spends, outputs, accountDiffs, err := l.processStateDiffTransactions(stateDiff) if err != nil { - return iotago.Identifier{}, iotago.Identifier{}, iotago.Identifier{}, ierrors.Errorf("failed to process state diff transactions in slot %d: %w", slot, err) + return iotago.Identifier{}, iotago.Identifier{}, iotago.Identifier{}, nil, nil, ierrors.Errorf("failed to process state diff transactions in slot %d: %w", slot, err) } // Now we process the collected account changes, for that we consume the "compacted" state diff to get the overall @@ -167,7 +167,7 @@ func (l *Ledger) CommitSlot(slot iotago.SlotIndex) (stateRoot iotago.Identifier, // output side createdAccounts, consumedAccounts, destroyedAccounts, err := l.processCreatedAndConsumedAccountOutputs(stateDiff, accountDiffs) if err != nil { - return iotago.Identifier{}, iotago.Identifier{}, iotago.Identifier{}, ierrors.Errorf("failed to process outputs consumed and created in slot %d: %w", slot, err) + return iotago.Identifier{}, iotago.Identifier{}, iotago.Identifier{}, nil, nil, ierrors.Errorf("failed to process outputs consumed and created in slot %d: %w", slot, err) } l.prepareAccountDiffs(accountDiffs, slot, consumedAccounts, createdAccounts) @@ -175,7 +175,7 @@ func (l *Ledger) CommitSlot(slot iotago.SlotIndex) (stateRoot iotago.Identifier, // Commit the changes // Update the UTXO ledger if err = l.utxoLedger.ApplyDiff(slot, outputs, spends); err != nil { - return iotago.Identifier{}, iotago.Identifier{}, iotago.Identifier{}, ierrors.Errorf("failed to apply diff to UTXO ledger for slot %d: %w", slot, err) + return iotago.Identifier{}, iotago.Identifier{}, iotago.Identifier{}, nil, nil, ierrors.Errorf("failed to apply diff to UTXO ledger for slot %d: %w", slot, err) } // Update the Accounts ledger @@ -187,15 +187,15 @@ func (l *Ledger) CommitSlot(slot iotago.SlotIndex) (stateRoot iotago.Identifier, } rmcForSlot, err := l.rmcManager.RMC(rmcSlot) if err != nil { - return iotago.Identifier{}, iotago.Identifier{}, iotago.Identifier{}, ierrors.Errorf("ledger failed to get RMC for slot %d: %w", rmcSlot, err) + return iotago.Identifier{}, iotago.Identifier{}, iotago.Identifier{}, nil, nil, ierrors.Errorf("ledger failed to get RMC for slot %d: %w", rmcSlot, err) } if err = l.accountsLedger.ApplyDiff(slot, rmcForSlot, accountDiffs, destroyedAccounts); err != nil { - return iotago.Identifier{}, iotago.Identifier{}, iotago.Identifier{}, ierrors.Errorf("failed to apply diff to Accounts ledger for slot %d: %w", slot, err) + return iotago.Identifier{}, iotago.Identifier{}, iotago.Identifier{}, nil, nil, ierrors.Errorf("failed to apply diff to Accounts ledger for slot %d: %w", slot, err) } // Update the mana manager's cache if err = l.manaManager.ApplyDiff(slot, destroyedAccounts, createdAccounts, accountDiffs); err != nil { - return iotago.Identifier{}, iotago.Identifier{}, iotago.Identifier{}, ierrors.Errorf("failed to apply diff to mana manager for slot %d: %w", slot, err) + return iotago.Identifier{}, iotago.Identifier{}, iotago.Identifier{}, nil, nil, ierrors.Errorf("failed to apply diff to mana manager for slot %d: %w", slot, err) } // Mark each transaction as committed so the mempool can evict it @@ -204,9 +204,7 @@ func (l *Ledger) CommitSlot(slot iotago.SlotIndex) (stateRoot iotago.Identifier, return true }) - l.events.StateDiffApplied.Trigger(slot, outputs, spends) - - return l.utxoLedger.StateTreeRoot(), stateDiff.Mutations().Root(), l.accountsLedger.AccountsTreeRoot(), nil + return l.utxoLedger.StateTreeRoot(), stateDiff.Mutations().Root(), l.accountsLedger.AccountsTreeRoot(), outputs, spends, nil } func (l *Ledger) AddAccount(output *utxoledger.Output, blockIssuanceCredits iotago.BlockIssuanceCredits) error { diff --git a/pkg/protocol/engine/notarization/events.go b/pkg/protocol/engine/notarization/events.go index 082c75760..81aaa5e8a 100644 --- a/pkg/protocol/engine/notarization/events.go +++ b/pkg/protocol/engine/notarization/events.go @@ -4,6 +4,7 @@ import ( "github.com/iotaledger/hive.go/ads" "github.com/iotaledger/hive.go/runtime/event" "github.com/iotaledger/iota-core/pkg/model" + "github.com/iotaledger/iota-core/pkg/protocol/engine/utxoledger" iotago "github.com/iotaledger/iota.go/v4" ) @@ -28,4 +29,6 @@ type SlotCommittedDetails struct { Commitment *model.Commitment AcceptedBlocks ads.Set[iotago.Identifier, iotago.BlockID] ActiveValidatorsCount int + OutputsCreated utxoledger.Outputs + OutputsConsumed utxoledger.Spents } diff --git a/pkg/protocol/engine/notarization/slotnotarization/manager.go b/pkg/protocol/engine/notarization/slotnotarization/manager.go index 6dd1c36c5..0b033661b 100644 --- a/pkg/protocol/engine/notarization/slotnotarization/manager.go +++ b/pkg/protocol/engine/notarization/slotnotarization/manager.go @@ -192,7 +192,7 @@ func (m *Manager) createCommitment(slot iotago.SlotIndex) (*model.Commitment, er return nil, ierrors.Wrap(err, "failed to commit attestations") } - stateRoot, mutationRoot, accountRoot, err := m.ledger.CommitSlot(slot) + stateRoot, mutationRoot, accountRoot, created, consumed, err := m.ledger.CommitSlot(slot) if err != nil { return nil, ierrors.Wrap(err, "failed to commit ledger") } @@ -255,6 +255,8 @@ func (m *Manager) createCommitment(slot iotago.SlotIndex) (*model.Commitment, er Commitment: newModelCommitment, AcceptedBlocks: acceptedBlocks, ActiveValidatorsCount: 0, + OutputsCreated: created, + OutputsConsumed: consumed, }) if err = m.storage.Settings().SetLatestCommitment(newModelCommitment); err != nil {