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

Commit 35f346b

Browse files
authored
Merge pull request #956 from iotaledger/fix/accounts
Fix: OutputID empty after rollback
2 parents b28efc9 + 1c68499 commit 35f346b

File tree

3 files changed

+32
-13
lines changed

3 files changed

+32
-13
lines changed

pkg/protocol/engine/accounts/accounts.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package accounts
22

33
import (
44
"io"
5+
"strconv"
56

67
"github.com/iotaledger/hive.go/ierrors"
78
"github.com/iotaledger/hive.go/runtime/options"
@@ -172,7 +173,7 @@ func (a *AccountData) String() string {
172173
stringify.NewStructField("Credits", a.Credits),
173174
stringify.NewStructField("ExpirySlot", uint32(a.ExpirySlot)),
174175
stringify.NewStructField("OutputID", a.OutputID),
175-
stringify.NewStructField("BlockIssuerKeys", a.BlockIssuerKeys),
176+
stringify.NewStructField("BlockIssuerKeys", func() string { return strconv.Itoa(a.BlockIssuerKeys.Size()) }()),
176177
stringify.NewStructField("ValidatorStake", uint64(a.ValidatorStake)),
177178
stringify.NewStructField("DelegationStake", uint64(a.DelegationStake)),
178179
stringify.NewStructField("FixedCost", uint64(a.FixedCost)),

pkg/protocol/engine/accounts/accountsledger/manager.go

+21-2
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,11 @@ import (
99
"github.com/iotaledger/hive.go/ierrors"
1010
"github.com/iotaledger/hive.go/kvstore"
1111
"github.com/iotaledger/hive.go/lo"
12+
"github.com/iotaledger/hive.go/log"
1213
"github.com/iotaledger/hive.go/runtime/module"
1314
"github.com/iotaledger/hive.go/runtime/options"
1415
"github.com/iotaledger/hive.go/runtime/syncutils"
16+
"github.com/iotaledger/hive.go/stringify"
1517
"github.com/iotaledger/iota-core/pkg/model"
1618
"github.com/iotaledger/iota-core/pkg/protocol/engine/accounts"
1719
"github.com/iotaledger/iota-core/pkg/protocol/engine/blocks"
@@ -127,7 +129,22 @@ func (m *Manager) AccountsTreeRoot() iotago.Identifier {
127129
m.mutex.RLock()
128130
defer m.mutex.RUnlock()
129131

130-
return m.accountsTree.Root()
132+
root := m.accountsTree.Root()
133+
134+
if m.LogLevel() == log.LevelTrace {
135+
m.LogTrace("retrieving accounts tree", "root", root, "accounts", func() string {
136+
accountsInTree := stringify.NewStructBuilder("Accounts")
137+
_ = m.accountsTree.Stream(func(accountID iotago.AccountID, accountData *accounts.AccountData) error {
138+
accountsInTree.AddField(stringify.NewStructField(accountID.String(), accountData))
139+
140+
return nil
141+
})
142+
143+
return accountsInTree.String()
144+
}())
145+
}
146+
147+
return root
131148
}
132149

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

405422
// update the output ID of the account if it was changed
406-
accountData.OutputID = diffChange.PreviousOutputID
423+
if diffChange.PreviousOutputID != iotago.EmptyOutputID {
424+
accountData.OutputID = diffChange.PreviousOutputID
425+
}
407426

408427
accountData.AddBlockIssuerKeys(diffChange.BlockIssuerKeysRemoved...)
409428
accountData.RemoveBlockIssuerKey(diffChange.BlockIssuerKeysAdded...)

pkg/protocol/engine/accounts/accountsledger/snapshot.go

+9-10
Original file line numberDiff line numberDiff line change
@@ -78,27 +78,26 @@ func (m *Manager) Export(writer io.WriteSeeker, targetIndex iotago.SlotIndex) er
7878
func (m *Manager) exportAccountTree(writer io.WriteSeeker, targetIndex iotago.SlotIndex) (int, error) {
7979
var accountCount int
8080

81-
if err := m.accountsTree.Stream(func(accountID iotago.AccountID, accountData *accounts.AccountData) error {
82-
m.LogDebug("Exporting account", "accountID", accountID, "outputID", accountData.OutputID, "credits.value", accountData.Credits.Value, "credits.updateSlot", accountData.Credits.UpdateSlot)
83-
84-
wasCreatedAfterTargetSlot, _, err := m.rollbackAccountTo(accountData, targetIndex)
81+
if err := m.accountsTree.Stream(func(id iotago.AccountID, account *accounts.AccountData) error {
82+
wasCreatedAfterTargetSlot, _, err := m.rollbackAccountTo(account, targetIndex)
8583
if err != nil {
86-
return ierrors.Wrapf(err, "unable to rollback account %s", accountID)
84+
return ierrors.Wrapf(err, "unable to rollback account %s", id)
8785
}
8886

89-
m.LogDebug("Exporting account after rollback", "accountID", accountID, "outputID", accountData.OutputID, "credits.value", accountData.Credits.Value, "credits.updateSlot", accountData.Credits.UpdateSlot)
90-
9187
// Account was created after the target slot, so we don't need to export it.
9288
if wasCreatedAfterTargetSlot {
93-
m.LogDebug("Exporting account was created after target slot", "accountID", accountID, "targetSlot", targetIndex)
89+
m.LogTrace("account was created after target slot", "id", id, "targetSlot", targetIndex)
90+
9491
return nil
9592
}
9693

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

99+
m.LogTrace("exported account", "id", id, "account", account)
100+
102101
return nil
103102
}); err != nil {
104103
return 0, ierrors.Wrap(err, "error in streaming account tree")

0 commit comments

Comments
 (0)