Skip to content

Commit

Permalink
Merge pull request #5484 from onflow/janez/add-logs-to-merge
Browse files Browse the repository at this point in the history
  • Loading branch information
turbolent committed Feb 29, 2024
2 parents 547cf64 + c90a1cd commit cf232f0
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 4 deletions.
1 change: 1 addition & 0 deletions cmd/util/ledger/migrations/cadence_values_migration.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,7 @@ func (m *CadenceBaseMigrator) MigrateAccount(
return MergeRegisterChanges(
migrationRuntime.Snapshot.Payloads,
result.WriteSet,
flow.ConvertAddress(address),
m.log,
)
}
Expand Down
2 changes: 2 additions & 0 deletions cmd/util/ledger/migrations/deploy_migration.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ func NewTransactionBasedMigration(
return MergeRegisterChanges(
snapshot.Payloads,
executionSnapshot.WriteSet,
// we expect more than one address to change here
flow.EmptyAddress,
logger,
)
}
Expand Down
33 changes: 29 additions & 4 deletions cmd/util/ledger/migrations/merge.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,33 @@ import (
func MergeRegisterChanges(
originalPayloads map[flow.RegisterID]*ledger.Payload,
changes map[flow.RegisterID]flow.RegisterValue,
expectedAddress flow.Address,
logger zerolog.Logger,
) ([]*ledger.Payload, error) {

newPayloads := make([]*ledger.Payload, 0, len(originalPayloads))

// Add all new payloads.
for id, value := range changes {
delete(originalPayloads, id)
if len(value) == 0 {
continue
}

if expectedAddress != flow.EmptyAddress {
ownerAddress := flow.BytesToAddress([]byte(id.Owner))

if ownerAddress != expectedAddress {
// something was changed that does not belong to this account. Log it.
logger.Error().
Str("key", id.String()).
Str("owner_address", ownerAddress.Hex()).
Str("account", expectedAddress.Hex()).
Hex("value", value).
Msg("key is part of the change set, but is for a different account")
}
}

key := convert.RegisterIDToLedgerKey(id)
newPayloads = append(newPayloads, ledger.NewPayload(key, value))
}
Expand All @@ -33,10 +50,18 @@ func MergeRegisterChanges(
continue
}

// If the payload had changed, then it has been added earlier.
// So skip old payload.
if _, contains := changes[id]; contains {
continue
if expectedAddress != flow.EmptyAddress {
ownerAddress := flow.BytesToAddress([]byte(id.Owner))

if ownerAddress != expectedAddress {
// something was changed that does not belong to this account. Log it.
logger.Error().
Str("key", id.String()).
Str("owner_address", ownerAddress.Hex()).
Str("account", expectedAddress.Hex()).
Hex("value", value.Value()).
Msg("key is part of the original set, but is for a different account")
}
}

newPayloads = append(newPayloads, value)
Expand Down

0 comments on commit cf232f0

Please sign in to comment.