-
Notifications
You must be signed in to change notification settings - Fork 180
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add migration to fix refs to non-existent registers
This commit adds --fix-testnet-slabs-with-broken-references flag to the migration program. It uses a feature from onflow/atree#388 to fix 10 testnet registers affecting 9 testnet accounts. The 9 testnet accounts are hardcoded in this commit to prevent accidentally applying this fix to any other accounts. In testnet, broken references seem to have resulted from a bug that was fixed 2 years ago by onflow/cadence#1565. A broken reference is a `StorageID` referencing a non-existent register. So far, only 10 registers in testnet (none on mainnet) were found to contain broken references.
- Loading branch information
Showing
4 changed files
with
388 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
191 changes: 191 additions & 0 deletions
191
cmd/util/ledger/migrations/fix_broken_data_migration.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,191 @@ | ||
package migrations | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
"github.com/rs/zerolog" | ||
|
||
"github.com/onflow/atree" | ||
|
||
"github.com/onflow/cadence/runtime/common" | ||
|
||
"github.com/onflow/flow-go/cmd/util/ledger/reporters" | ||
"github.com/onflow/flow-go/ledger" | ||
"github.com/onflow/flow-go/ledger/common/convert" | ||
"github.com/onflow/flow-go/model/flow" | ||
) | ||
|
||
type FixSlabsWithBrokenReferencesMigration struct { | ||
log zerolog.Logger | ||
rw reporters.ReportWriter | ||
accountsToFix map[common.Address]string | ||
nWorkers int | ||
} | ||
|
||
var _ AccountBasedMigration = &FixSlabsWithBrokenReferencesMigration{} | ||
|
||
const fixSlabsWithBrokenReferencesName = "fix-slabs-with-broken-references" | ||
|
||
func NewFixBrokenReferencesInSlabsMigration( | ||
rwf reporters.ReportWriterFactory, | ||
accountsToFix map[common.Address]string, | ||
) *FixSlabsWithBrokenReferencesMigration { | ||
return &FixSlabsWithBrokenReferencesMigration{ | ||
rw: rwf.ReportWriter(fixSlabsWithBrokenReferencesName), | ||
accountsToFix: accountsToFix, | ||
} | ||
} | ||
|
||
func (m *FixSlabsWithBrokenReferencesMigration) InitMigration( | ||
log zerolog.Logger, | ||
_ []*ledger.Payload, | ||
nWorkers int, | ||
) error { | ||
m.log = log. | ||
With(). | ||
Str("migration", fixSlabsWithBrokenReferencesName). | ||
Logger() | ||
m.nWorkers = nWorkers | ||
|
||
return nil | ||
} | ||
|
||
func (m *FixSlabsWithBrokenReferencesMigration) MigrateAccount( | ||
_ context.Context, | ||
address common.Address, | ||
oldPayloads []*ledger.Payload, | ||
) ( | ||
newPayloads []*ledger.Payload, | ||
err error, | ||
) { | ||
|
||
if _, exist := m.accountsToFix[address]; !exist { | ||
return oldPayloads, nil | ||
} | ||
|
||
migrationRuntime, err := NewAtreeRegisterMigratorRuntime(address, oldPayloads) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to create cadence runtime: %w", err) | ||
} | ||
|
||
storage := migrationRuntime.Storage | ||
|
||
// Load all atree registers in storage | ||
for _, payload := range oldPayloads { | ||
registerID, _, err := convert.PayloadToRegister(payload) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to convert payload to register: %w", err) | ||
} | ||
|
||
if !registerID.IsSlabIndex() { | ||
continue | ||
} | ||
|
||
// Convert the register ID to a storage ID. | ||
slabID := atree.NewStorageID( | ||
atree.Address([]byte(registerID.Owner)), | ||
atree.StorageIndex([]byte(registerID.Key[1:]))) | ||
|
||
// Retrieve the slab. | ||
_, _, err = storage.Retrieve(slabID) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to retrieve slab %s: %w", slabID, err) | ||
} | ||
} | ||
|
||
// Fix broken references | ||
fixedStorageIDs, skippedStorageIDs, err := storage.FixLoadedBrokenReferences(func(old atree.Value) bool { | ||
// TODO: Cadence may need to export functions to check type info, etc. | ||
return true | ||
}) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
if len(skippedStorageIDs) > 0 { | ||
m.log.Warn(). | ||
Str("account", address.Hex()). | ||
Msgf("skipped slabs with broken references: %v", skippedStorageIDs) | ||
} | ||
|
||
if len(fixedStorageIDs) == 0 { | ||
m.log.Warn(). | ||
Str("account", address.Hex()). | ||
Msgf("did not fix any slabs with broken references") | ||
|
||
return oldPayloads, nil | ||
} | ||
|
||
m.log.Log(). | ||
Str("account", address.Hex()). | ||
Msgf("fixed slabs with broken references: %v", fixedStorageIDs) | ||
|
||
err = storage.FastCommit(m.nWorkers) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
// Finalize the transaction | ||
result, err := migrationRuntime.TransactionState.FinalizeMainTransaction() | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to finalize main transaction: %w", err) | ||
} | ||
|
||
// Merge the changes to the original payloads. | ||
expectedAddresses := map[flow.Address]struct{}{ | ||
flow.Address(address): {}, | ||
} | ||
|
||
newPayloads, err = migrationRuntime.Snapshot.ApplyChangesAndGetNewPayloads( | ||
result.WriteSet, | ||
expectedAddresses, | ||
m.log, | ||
) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
// Log fixed payloads | ||
fixedPayloads := make([]*ledger.Payload, 0, len(fixedStorageIDs)) | ||
for _, payload := range newPayloads { | ||
registerID, _, err := convert.PayloadToRegister(payload) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to convert payload to register: %w", err) | ||
} | ||
|
||
if !registerID.IsSlabIndex() { | ||
continue | ||
} | ||
|
||
storageID := atree.NewStorageID( | ||
atree.Address([]byte(registerID.Owner)), | ||
atree.StorageIndex([]byte(registerID.Key[1:])), | ||
) | ||
|
||
if _, ok := fixedStorageIDs[storageID]; ok { | ||
fixedPayloads = append(fixedPayloads, payload) | ||
} | ||
} | ||
|
||
m.rw.Write(fixedSlabsWithBrokenReferences{ | ||
Account: address, | ||
Payloads: fixedPayloads, | ||
Msg: m.accountsToFix[address], | ||
}) | ||
|
||
return newPayloads, nil | ||
} | ||
|
||
func (m *FixSlabsWithBrokenReferencesMigration) Close() error { | ||
// close the report writer so it flushes to file | ||
m.rw.Close() | ||
|
||
return nil | ||
} | ||
|
||
type fixedSlabsWithBrokenReferences struct { | ||
Account common.Address `json:"account"` | ||
Payloads []*ledger.Payload `json:"payloads"` | ||
Msg string `json:"msg"` | ||
} |
Oops, something went wrong.