Skip to content

Commit

Permalink
Merge pull request #956 from iotaledger/fix/accounts
Browse files Browse the repository at this point in the history
Fix: OutputID empty after rollback
  • Loading branch information
alexsporn authored May 3, 2024
2 parents b28efc9 + 1c68499 commit 35f346b
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 13 deletions.
3 changes: 2 additions & 1 deletion pkg/protocol/engine/accounts/accounts.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package accounts

import (
"io"
"strconv"

"github.com/iotaledger/hive.go/ierrors"
"github.com/iotaledger/hive.go/runtime/options"
Expand Down Expand Up @@ -172,7 +173,7 @@ func (a *AccountData) String() string {
stringify.NewStructField("Credits", a.Credits),
stringify.NewStructField("ExpirySlot", uint32(a.ExpirySlot)),
stringify.NewStructField("OutputID", a.OutputID),
stringify.NewStructField("BlockIssuerKeys", a.BlockIssuerKeys),
stringify.NewStructField("BlockIssuerKeys", func() string { return strconv.Itoa(a.BlockIssuerKeys.Size()) }()),
stringify.NewStructField("ValidatorStake", uint64(a.ValidatorStake)),
stringify.NewStructField("DelegationStake", uint64(a.DelegationStake)),
stringify.NewStructField("FixedCost", uint64(a.FixedCost)),
Expand Down
23 changes: 21 additions & 2 deletions pkg/protocol/engine/accounts/accountsledger/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,11 @@ import (
"github.com/iotaledger/hive.go/ierrors"
"github.com/iotaledger/hive.go/kvstore"
"github.com/iotaledger/hive.go/lo"
"github.com/iotaledger/hive.go/log"
"github.com/iotaledger/hive.go/runtime/module"
"github.com/iotaledger/hive.go/runtime/options"
"github.com/iotaledger/hive.go/runtime/syncutils"
"github.com/iotaledger/hive.go/stringify"
"github.com/iotaledger/iota-core/pkg/model"
"github.com/iotaledger/iota-core/pkg/protocol/engine/accounts"
"github.com/iotaledger/iota-core/pkg/protocol/engine/blocks"
Expand Down Expand Up @@ -127,7 +129,22 @@ func (m *Manager) AccountsTreeRoot() iotago.Identifier {
m.mutex.RLock()
defer m.mutex.RUnlock()

return m.accountsTree.Root()
root := m.accountsTree.Root()

if m.LogLevel() == log.LevelTrace {
m.LogTrace("retrieving accounts tree", "root", root, "accounts", func() string {
accountsInTree := stringify.NewStructBuilder("Accounts")
_ = m.accountsTree.Stream(func(accountID iotago.AccountID, accountData *accounts.AccountData) error {
accountsInTree.AddField(stringify.NewStructField(accountID.String(), accountData))

return nil
})

return accountsInTree.String()
}())
}

return root
}

// ApplyDiff applies the given accountDiff to the Account tree.
Expand Down Expand Up @@ -403,7 +420,9 @@ func (m *Manager) rollbackAccountTo(accountData *accounts.AccountData, targetSlo
}

// update the output ID of the account if it was changed
accountData.OutputID = diffChange.PreviousOutputID
if diffChange.PreviousOutputID != iotago.EmptyOutputID {
accountData.OutputID = diffChange.PreviousOutputID
}

accountData.AddBlockIssuerKeys(diffChange.BlockIssuerKeysRemoved...)
accountData.RemoveBlockIssuerKey(diffChange.BlockIssuerKeysAdded...)
Expand Down
19 changes: 9 additions & 10 deletions pkg/protocol/engine/accounts/accountsledger/snapshot.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,27 +78,26 @@ func (m *Manager) Export(writer io.WriteSeeker, targetIndex iotago.SlotIndex) er
func (m *Manager) exportAccountTree(writer io.WriteSeeker, targetIndex iotago.SlotIndex) (int, error) {
var accountCount int

if err := m.accountsTree.Stream(func(accountID iotago.AccountID, accountData *accounts.AccountData) error {
m.LogDebug("Exporting account", "accountID", accountID, "outputID", accountData.OutputID, "credits.value", accountData.Credits.Value, "credits.updateSlot", accountData.Credits.UpdateSlot)

wasCreatedAfterTargetSlot, _, err := m.rollbackAccountTo(accountData, targetIndex)
if err := m.accountsTree.Stream(func(id iotago.AccountID, account *accounts.AccountData) error {
wasCreatedAfterTargetSlot, _, err := m.rollbackAccountTo(account, targetIndex)
if err != nil {
return ierrors.Wrapf(err, "unable to rollback account %s", accountID)
return ierrors.Wrapf(err, "unable to rollback account %s", id)
}

m.LogDebug("Exporting account after rollback", "accountID", accountID, "outputID", accountData.OutputID, "credits.value", accountData.Credits.Value, "credits.updateSlot", accountData.Credits.UpdateSlot)

// Account was created after the target slot, so we don't need to export it.
if wasCreatedAfterTargetSlot {
m.LogDebug("Exporting account was created after target slot", "accountID", accountID, "targetSlot", targetIndex)
m.LogTrace("account was created after target slot", "id", id, "targetSlot", targetIndex)

return nil
}

if err := stream.WriteObject(writer, accountData, (*accounts.AccountData).Bytes); err != nil {
return ierrors.Wrapf(err, "unable to write account %s", accountID)
if err = stream.WriteObject(writer, account, (*accounts.AccountData).Bytes); err != nil {
return ierrors.Wrapf(err, "unable to write account %s", id)
}
accountCount++

m.LogTrace("exported account", "id", id, "account", account)

return nil
}); err != nil {
return 0, ierrors.Wrap(err, "error in streaming account tree")
Expand Down

0 comments on commit 35f346b

Please sign in to comment.