From a5275dd86fd870611f302812776c5904fbd53b65 Mon Sep 17 00:00:00 2001 From: Janez Podhostnik Date: Tue, 16 Jan 2024 17:31:55 +0100 Subject: [PATCH] remove cricket moments references from atree migration --- .../migrations/atree_register_migration.go | 71 +- .../migrations/cadence_data_validation.go | 18 +- cmd/util/ledger/migrations/cricket_moments.go | 732 ------------------ .../ledger/migrations/migrator_runtime.go | 122 --- 4 files changed, 6 insertions(+), 937 deletions(-) delete mode 100644 cmd/util/ledger/migrations/cricket_moments.go diff --git a/cmd/util/ledger/migrations/atree_register_migration.go b/cmd/util/ledger/migrations/atree_register_migration.go index 82407952abe..db84e6ce066 100644 --- a/cmd/util/ledger/migrations/atree_register_migration.go +++ b/cmd/util/ledger/migrations/atree_register_migration.go @@ -135,15 +135,6 @@ func (m *AtreeRegisterMigrator) MigrateAccount( }) } - if address == cricketMomentsAddress { - // extra logging for cricket moments - m.log.Info(). - Str("address", address.Hex()). - Int("originalLen", originalLen). - Int("newLen", newLen). - Msgf("done migrating cricketMomentsAddress") - } - return newPayloads, nil } @@ -160,32 +151,17 @@ func (m *AtreeRegisterMigrator) migrateAccountStorage( } } - if mr.Address == cricketMomentsAddress { - m.log.Info().Msg("Committing storage domain changes") - } - - // commit the storage changes - // TODO: for cricket moments `commitNewStorageMaps` already happened potentially - // try switching directly to s.PersistentSlabStorage.FastCommit(runtime.NumCPU()) err := mr.Storage.Commit(mr.Interpreter, false) if err != nil { return nil, fmt.Errorf("failed to commit storage: %w", err) } - if mr.Address == cricketMomentsAddress { - m.log.Info().Msg("Finalizing storage domain changes transaction") - } - // finalize the transaction result, err := mr.TransactionState.FinalizeMainTransaction() if err != nil { return nil, fmt.Errorf("failed to finalize main transaction: %w", err) } - if mr.Address == cricketMomentsAddress { - m.log.Info().Msg("Storage domain changes transaction finalized") - } - return result.WriteSet, nil } @@ -230,7 +206,7 @@ func (m *AtreeRegisterMigrator) convertStorageDomain( return fmt.Errorf("failed to read value for key %s: %w", key, err) } - value, err = m.cloneValue(mr, domain, key, value) + value, err = m.cloneValue(mr, value) if err != nil { return fmt.Errorf("failed to clone value for key %s: %w", key, err) @@ -276,14 +252,6 @@ func (m *AtreeRegisterMigrator) validateChangesAndCreateNewRegisters( var statePayload *ledger.Payload progressLog := func(int) {} - if mr.Address == cricketMomentsAddress { - progressLog = util2.LogProgress(m.log, - util2.DefaultLogProgressConfig( - "applying changes", - len(changes), - )) - } - for id, value := range changes { progressLog(1) // delete all values that were changed from the original payloads so that we can @@ -324,13 +292,7 @@ func (m *AtreeRegisterMigrator) validateChangesAndCreateNewRegisters( // add all values that were not changed if len(originalPayloads) > 0 { - if mr.Address == cricketMomentsAddress { - progressLog = util2.LogProgress(m.log, - util2.DefaultLogProgressConfig( - "checking unchanged registers", - len(originalPayloads)), - ) - } + for id, value := range originalPayloads { progressLog(1) @@ -366,13 +328,6 @@ func (m *AtreeRegisterMigrator) validateChangesAndCreateNewRegisters( continue } - if mr.Address == cricketMomentsAddress { - // to be sure, copy all cricket moments keys - newPayloads = append(newPayloads, value) - m.log.Info().Msgf("copying cricket moments key %s", id) - continue - } - m.rw.Write(migrationProblem{ Address: mr.Address.Hex(), Key: id.String(), @@ -423,31 +378,9 @@ func (m *AtreeRegisterMigrator) validateChangesAndCreateNewRegisters( func (m *AtreeRegisterMigrator) cloneValue( mr *migratorRuntime, - domain string, - key interpreter.StorageMapKey, value interpreter.Value, ) (interpreter.Value, error) { - if isCricketMomentsShardedCollection(mr, value) { - m.log.Info().Msg("migrating CricketMomentsShardedCollection") - value, err := cloneCricketMomentsShardedCollection( - m.log, - m.nWorkers, - mr, - domain, - key, - value, - ) - - if err != nil { - m.log.Info().Err(err).Msg("failed to clone value") - return nil, err - } - - m.log.Info().Msg("done migrating CricketMomentsShardedCollection") - return value, nil - } - err := capturePanic(func() { // force the value to be read entirely value = value.Clone(mr.Interpreter) diff --git a/cmd/util/ledger/migrations/cadence_data_validation.go b/cmd/util/ledger/migrations/cadence_data_validation.go index a5da1d6d4ed..a1276fa4753 100644 --- a/cmd/util/ledger/migrations/cadence_data_validation.go +++ b/cmd/util/ledger/migrations/cadence_data_validation.go @@ -106,7 +106,7 @@ func (m *preMigration) MigrateAccount( payloads []*ledger.Payload, ) ([]*ledger.Payload, error) { - accountHash, err := m.v.hashAccountCadenceValues(m.log, address, payloads) + accountHash, err := m.v.hashAccountCadenceValues(address, payloads) if err != nil { m.log.Info(). Err(err). @@ -166,7 +166,7 @@ func (m *postMigration) MigrateAccount( address common.Address, payloads []*ledger.Payload, ) ([]*ledger.Payload, error) { - newHash, err := m.v.hashAccountCadenceValues(m.log, address, payloads) + newHash, err := m.v.hashAccountCadenceValues(address, payloads) if err != nil { m.log.Info(). Err(err). @@ -212,7 +212,6 @@ func (m *postMigration) MigrateAccount( } func (m *CadenceDataValidationMigrations) hashAccountCadenceValues( - log zerolog.Logger, address common.Address, payloads []*ledger.Payload, ) ([]byte, error) { @@ -223,7 +222,7 @@ func (m *CadenceDataValidationMigrations) hashAccountCadenceValues( } for _, domain := range domains { - domainHash, err := m.hashDomainCadenceValues(log, mr, domain) + domainHash, err := m.hashDomainCadenceValues(mr, domain) if err != nil { return nil, fmt.Errorf("failed to hash storage domain %s : %w", domain, err) } @@ -237,7 +236,6 @@ func (m *CadenceDataValidationMigrations) hashAccountCadenceValues( } func (m *CadenceDataValidationMigrations) hashDomainCadenceValues( - log zerolog.Logger, mr *migratorRuntime, domain string, ) ([]byte, error) { @@ -263,7 +261,7 @@ func (m *CadenceDataValidationMigrations) hashDomainCadenceValues( break } - h, err := m.recursiveString(log, mr, domain, interpreter.StringStorageMapKey(key.(interpreter.StringAtreeValue)), value, hasher) + h, err := m.recursiveString(value, hasher) if err != nil { return nil, fmt.Errorf("failed to convert value to string: %w", err) } @@ -277,17 +275,9 @@ func (m *CadenceDataValidationMigrations) hashDomainCadenceValues( } func (m *CadenceDataValidationMigrations) recursiveString( - log zerolog.Logger, - mr *migratorRuntime, - domain string, - key interpreter.StorageMapKey, value interpreter.Value, hasher hash.Hasher, ) ([]byte, error) { - if isCricketMomentsShardedCollection(mr, value) { - log.Info().Msg("recursive string hash for cricket moments sharded collection") - return recursiveStringShardedCollection(log, m.nWorkers, mr, domain, key, value) - } var s string err := capturePanic( diff --git a/cmd/util/ledger/migrations/cricket_moments.go b/cmd/util/ledger/migrations/cricket_moments.go deleted file mode 100644 index 5fc7d90f541..00000000000 --- a/cmd/util/ledger/migrations/cricket_moments.go +++ /dev/null @@ -1,732 +0,0 @@ -package migrations - -import ( - "context" - "fmt" - "sort" - "sync" - - "github.com/rs/zerolog" - - "github.com/onflow/cadence/runtime/interpreter" - - "github.com/onflow/flow-go/cmd/util/ledger/util" - util2 "github.com/onflow/flow-go/module/util" -) - -var cricketMomentsAddress = mustHexToAddress("4eded0de73020ca5") - -//var cricketMomentsShardedCollectionType = "A.4eded0de73020ca5.CricketMomentsShardedCollection.ShardedCollection" - -func isCricketMomentsShardedCollection( - mr *migratorRuntime, - value interpreter.Value, -) bool { - return false - - //if mr.Address != cricketMomentsAddress { - // return false - //} - // - //compositeValue, ok := value.(*interpreter.CompositeValue) - //if !ok { - // return false - //} - // - //return string(compositeValue.TypeID()) == cricketMomentsShardedCollectionType -} - -func getCricketMomentsShardedCollectionNFTCount( - mr *migratorRuntime, - shardedCollectionMap *interpreter.DictionaryValue, -) (int, error) { - // count all values so we can track progress - count := 0 - shardedCollectionMapIterator := shardedCollectionMap.Iterator() - for { - key := shardedCollectionMapIterator.NextKey(nil) - if key == nil { - break - } - - ownedNFTs, err := getNftCollection(mr.Interpreter, key, shardedCollectionMap) - if err != nil { - return 0, err - } - - count += ownedNFTs.Count() - } - return count, nil -} - -func getShardedCollectionMap(mr *migratorRuntime, value interpreter.Value) (*interpreter.DictionaryValue, error) { - shardedCollectionResource, ok := value.(*interpreter.CompositeValue) - if !ok { - return nil, fmt.Errorf("expected *interpreter.CompositeValue, got %T", value) - } - shardedCollectionMapField := shardedCollectionResource.GetField( - mr.Interpreter, - interpreter.EmptyLocationRange, - "collections", - ) - if shardedCollectionMapField == nil { - return nil, fmt.Errorf("expected collections field") - } - shardedCollectionMap, ok := shardedCollectionMapField.(*interpreter.DictionaryValue) - if !ok { - return nil, fmt.Errorf("expected collections to be *interpreter.DictionaryValue, got %T", shardedCollectionMapField) - } - return shardedCollectionMap, nil -} - -func getNftCollection( - inter *interpreter.Interpreter, - outerKey interpreter.Value, - shardedCollectionMap *interpreter.DictionaryValue, -) (*interpreter.DictionaryValue, error) { - value := shardedCollectionMap.GetKey( - inter, - interpreter.EmptyLocationRange, - outerKey, - ) - - if value == nil { - return nil, fmt.Errorf("expected value for key %s", outerKey) - } - - someCollection, ok := value.(*interpreter.SomeValue) - if !ok { - return nil, fmt.Errorf("expected collection to be *interpreter.SomeValue, got %T", value) - } - - collection, ok := someCollection.InnerValue( - inter, - interpreter.EmptyLocationRange).(*interpreter.CompositeValue) - if !ok { - return nil, fmt.Errorf("expected inner collection to be *interpreter.CompositeValue, got %T", value) - } - - ownedNFTsRaw := collection.GetField( - inter, - interpreter.EmptyLocationRange, - "ownedNFTs", - ) - if ownedNFTsRaw == nil { - return nil, fmt.Errorf("expected ownedNFTs field") - } - ownedNFTs, ok := ownedNFTsRaw.(*interpreter.DictionaryValue) - if !ok { - return nil, fmt.Errorf("expected ownedNFTs to be *interpreter.DictionaryValue, got %T", ownedNFTsRaw) - } - - return ownedNFTs, nil -} - -type cricketKeyPair struct { - shardedCollectionKey interpreter.Value - nftCollectionKey interpreter.Value -} - -func cloneCricketMomentsShardedCollection( - log zerolog.Logger, - nWorkers int, - mr *migratorRuntime, - domain string, - key interpreter.StorageMapKey, - value interpreter.Value, -) (interpreter.Value, error) { - return nil, nil - - //shardedCollectionMap, err := getShardedCollectionMap(mr, value) - //if err != nil { - // return nil, err - //} - //count, err := getCricketMomentsShardedCollectionNFTCount( - // mr, - // shardedCollectionMap, - //) - //if err != nil { - // return nil, err - //} - // - //ctx, c := context.WithCancelCause(context.Background()) - //cancel := func(err error) { - // if err != nil { - // log.Info().Err(err).Msg("canceling context") - // } - // c(err) - //} - //defer cancel(nil) - // - //type valueWithKeys struct { - // cricketKeyPair - // value interpreter.Value - //} - // - //keyPairChan := make(chan cricketKeyPair, count) - //clonedValues := make([]valueWithKeys, 0, count) - // - //interpreters, _, err := mr.ChildInterpreters(log, 1, flow.ConvertAddress(mr.Address)) - //if err != nil { - // return nil, err - //} - //inter := interpreters[0] - //inter.SharedState.Config.InvalidatedResourceValidationEnabled = false - // - //// worker for dispatching values to clone - //go func() { - // defer close(keyPairChan) - // - // storageMap := mr.GetReadOnlyStorage().GetStorageMap(mr.Address, domain, false) - // storageMapValue := storageMap.ReadValue(&util.NopMemoryGauge{}, key) - // scm, err := getShardedCollectionMap(mr, storageMapValue) - // if err != nil { - // cancel(err) - // return - // } - // - // shardedCollectionMapIterator := scm.Iterator() - // for { - // select { - // case <-ctx.Done(): - // return - // default: - // } - // - // outerKey := shardedCollectionMapIterator.NextKey(nil) - // if outerKey == nil { - // break - // } - // - // ownedNFTs, err := getNftCollection(inter, outerKey, scm) - // if err != nil { - // cancel(err) - // return - // } - // - // ownedNFTsIterator := ownedNFTs.Iterator() - // for { - // select { - // case <-ctx.Done(): - // return - // default: - // } - // - // innerKey := ownedNFTsIterator.NextKey(nil) - // if innerKey == nil { - // break - // } - // - // keyPairChan <- cricketKeyPair{ - // nftCollectionKey: innerKey, - // shardedCollectionKey: outerKey, - // } - // } - // } - //}() - // - //interpreters, closeInterpreters, err := mr.ChildInterpreters(log, nWorkers, flow.ConvertAddress(mr.Address)) - //if err != nil { - // return nil, err - //} - // - //// workers for cloning values - //wg := sync.WaitGroup{} - //wg.Add(nWorkers) - //progressLog := util2.LogProgress(log, "cloning cricket moments", count) - //for i := 0; i < nWorkers; i++ { - // go func(i int) { - // defer wg.Done() - // inter := interpreters[i] - // - // storageMap := mr.GetReadOnlyStorage().GetStorageMap(mr.Address, domain, false) - // storageMapValue := storageMap.ReadValue(&util.NopMemoryGauge{}, key) - // scm, err := getShardedCollectionMap(mr, storageMapValue) - // if err != nil { - // cancel(err) - // return - // } - // - // inter.SharedState.Config.InvalidatedResourceValidationEnabled = false - // - // for { - // select { - // case <-ctx.Done(): - // return - // case keyPair, ok := <-keyPairChan: - // if !ok { - // return - // } - // - // ownedNFTs, err := getNftCollection( - // inter, - // keyPair.shardedCollectionKey, - // scm, - // ) - // if err != nil { - // cancel(err) - // return - // } - // - // value, ok := ownedNFTs.Get( - // inter, - // interpreter.EmptyLocationRange, - // keyPair.nftCollectionKey, - // ) - // if !ok { - // cancel(fmt.Errorf("expected value for key %s", keyPair.nftCollectionKey)) - // return - // } - // - // var newValue interpreter.Value - // err = capturePanic(func() { - // newValue = value.Clone(inter) - // }) - // if err != nil { - // cancel(err) - // return - // } - // if newValue == nil { - // cancel(fmt.Errorf("failed to clone value")) - // return - // } - // - // clonedValues = append(clonedValues, - // valueWithKeys{ - // cricketKeyPair: keyPair, - // value: newValue, - // }, - // ) - // - // // TODO: potentially remove here as well - // - // progressLog(1) - // } - // } - // }(i) - //} - //// only after all values have been cloned, can they be set back - //wg.Wait() - // - //if ctx.Err() != nil { - // return nil, fmt.Errorf("context error when cloning values: %w", ctx.Err()) - //} - // - //// close all child interpreters - //err = closeInterpreters() - //if err != nil { - // return nil, err - //} - // - //progressLog = util2.LogProgress(log, "removing cricket moments", len(clonedValues)) - //for _, clonedValue := range clonedValues { - // ownedNFTs, err := getNftCollection( - // mr.Interpreter, - // clonedValue.shardedCollectionKey, - // shardedCollectionMap, - // ) - // - // if err != nil { - // return nil, fmt.Errorf("failed to get nft collection: %w", err) - // } - // - // // TODO: move remove inside the copy loop - // err = capturePanic(func() { - // // TODO: remove is fairly slow, consider customizing it, to remove some - // // unnecessary checks - // ownedNFTs.Remove( - // mr.Interpreter, - // interpreter.EmptyLocationRange, - // clonedValue.nftCollectionKey, - // ) - // }) - // if err != nil { - // return nil, fmt.Errorf("failed to set key: %w", err) - // } - // progressLog(1) - //} - // - //log.Info().Msg("cloning empty cricket moments sharded collection") - //value = value.Clone(mr.Interpreter) - //log.Info().Msg("cloned empty cricket moments sharded collection") - //shardedCollectionMap, err = getShardedCollectionMap(mr, value) - //if err != nil { - // return nil, err - //} - // - //// this is fairly short no need to optimise - //progressLog = util2.LogProgress(log, "setting cloned cricket moments", len(clonedValues)) - //for _, clonedValue := range clonedValues { - // ownedNFTs, err := getNftCollection( - // mr.Interpreter, - // clonedValue.shardedCollectionKey, - // shardedCollectionMap, - // ) - // - // if err != nil { - // return nil, fmt.Errorf("failed to get nft collection: %w", err) - // } - // - // err = capturePanic(func() { - // ownedNFTs.UnsafeInsert( - // mr.Interpreter, - // interpreter.EmptyLocationRange, - // clonedValue.nftCollectionKey, - // clonedValue.value, - // ) - // }) - // if err != nil { - // return nil, fmt.Errorf("failed to set key: %w", err) - // } - // progressLog(1) - //} - // - //return value, nil -} - -type hashWithKeys struct { - key uint64 - hash []byte -} - -func recursiveStringShardedCollection( - log zerolog.Logger, - nWorkers int, - mr *migratorRuntime, - domain string, - key interpreter.StorageMapKey, - value interpreter.Value, -) ([]byte, error) { - // hash all values - hashes, err := hashAllNftsInAllCollections( - log, - nWorkers, - mr, - domain, - key, - value, - ) - - if err != nil { - return nil, fmt.Errorf("failed to hash all values: %w", err) - } - - return consolidateAllHashes( - log, - nWorkers, - hashes, - ) -} - -func hashAllNftsInAllCollections( - log zerolog.Logger, - nWorkers int, - mr *migratorRuntime, - domain string, - key interpreter.StorageMapKey, - value interpreter.Value, -) (map[uint64]sortableHashes, error) { - - shardedCollectionMap, err := getShardedCollectionMap(mr, value) - if err != nil { - return nil, err - } - count, err := getCricketMomentsShardedCollectionNFTCount( - mr, - shardedCollectionMap) - if err != nil { - return nil, err - } - progressLog := util2.LogProgress(log, - util2.DefaultLogProgressConfig( - "hashing cricket moments", - count, - )) - - ctx, c := context.WithCancelCause(context.Background()) - cancel := func(err error) { - if err != nil { - log.Info().Err(err).Msg("canceling context") - } - c(err) - } - defer cancel(nil) - - keyPairChan := make(chan cricketKeyPair, count) - hashChan := make(chan hashWithKeys, count) - hashes := make(map[uint64]sortableHashes) - wg := sync.WaitGroup{} - wg.Add(nWorkers) - done := make(chan struct{}) - - // workers for hashing - for i := 0; i < nWorkers; i++ { - go func() { - defer wg.Done() - - storageMap := mr.GetReadOnlyStorage().GetStorageMap(mr.Address, domain, false) - storageMapValue := storageMap.ReadValue(&util.NopMemoryGauge{}, key) - - hashNFTWorker(log, progressLog, ctx, cancel, mr, storageMapValue, keyPairChan, hashChan) - }() - } - - // worker for collecting hashes - go func() { - defer close(done) - defer log.Info().Msg("finished collecting hashes") - for { - select { - case <-ctx.Done(): - return - case hashed, ok := <-hashChan: - if !ok { - return - } - _, ok = hashes[hashed.key] - if !ok { - hashes[hashed.key] = make(sortableHashes, 0, 1_000_000) - } - hashes[hashed.key] = append(hashes[hashed.key], hashed.hash) - } - } - }() - - // worker for dispatching values to hash - go func() { - defer close(keyPairChan) - - shardedCollectionMapIterator := shardedCollectionMap.Iterator() - for { - select { - case <-ctx.Done(): - return - default: - } - - outerKey := shardedCollectionMapIterator.NextKey(nil) - if outerKey == nil { - break - } - - ownedNFTs, err := getNftCollection(mr.Interpreter, outerKey, shardedCollectionMap) - if err != nil { - cancel(err) - return - } - - ownedNFTsIterator := ownedNFTs.Iterator() - for { - select { - case <-ctx.Done(): - return - default: - } - - innerKey := ownedNFTsIterator.NextKey(nil) - if innerKey == nil { - break - } - - keyPairChan <- cricketKeyPair{ - nftCollectionKey: innerKey, - shardedCollectionKey: outerKey, - } - } - } - }() - - wg.Wait() - log.Info().Msg("finished hashing values") - close(hashChan) - <-done - - if ctx.Err() != nil { - log.Info().Err(ctx.Err()).Msg("context error when hashing individual values") - return nil, ctx.Err() - } - return hashes, nil -} - -func consolidateAllHashes( - log zerolog.Logger, - nWorkers int, - hashes map[uint64]sortableHashes, -) ([]byte, error) { - - log.Info().Msg("starting consolidateAllHashes") - defer log.Info().Msg("done with consolidateAllHashes") - - ctx, c := context.WithCancelCause(context.Background()) - cancel := func(err error) { - if err != nil { - log.Info().Err(err).Msg("canceling context") - } - c(err) - } - defer cancel(nil) - - hashCollectionChan := make(chan uint64, nWorkers) - hashedCollectionChan := make(chan []byte) - finalHashes := make(sortableHashes, 0, 10_000_000) - wg := sync.WaitGroup{} - wg.Add(nWorkers) - done := make(chan struct{}) - - for i := 0; i < nWorkers; i++ { - go func(i int) { - defer wg.Done() - hasher := newHasher() - - for { - select { - case <-ctx.Done(): - return - case i, ok := <-hashCollectionChan: - if !ok { - return - } - l := hashes[i] - - h, err := l.SortAndHash(hasher) - if err != nil { - cancel(fmt.Errorf("failed to write hash: %w", err)) - return - } - - hashedCollectionChan <- h - } - } - }(i) - } - - go func() { - defer close(done) - defer log.Info().Msg("finished collecting final hashes") - - for { - select { - case <-ctx.Done(): - return - case h, ok := <-hashedCollectionChan: - if !ok { - return - } - finalHashes = append(finalHashes, h) - } - } - }() - - go func() { - defer close(hashCollectionChan) - defer log.Info().Msg("finished dispatching final hashes") - - for k := range hashes { - select { - case <-ctx.Done(): - return - case hashCollectionChan <- k: - } - } - }() - - wg.Wait() - close(hashedCollectionChan) - <-done - - if ctx.Err() != nil { - log.Info().Err(ctx.Err()).Msg("context error when hashing values together") - return nil, ctx.Err() - } - - sort.Sort(finalHashes) - - for _, h := range finalHashes { - log.Info(). - Hex("hash", h). - Msg("hashed collection") - } - - hasher := newHasher() - h, err := finalHashes.SortAndHash(hasher) - if err != nil { - return nil, fmt.Errorf("failed to write hash: %w", err) - } - - return h, nil -} - -func hashNFTWorker( - log zerolog.Logger, - progress util2.LogProgressFunc, - ctx context.Context, - cancel context.CancelCauseFunc, - mr *migratorRuntime, - storageMapValue interpreter.Value, - keyPairChan <-chan cricketKeyPair, - hashChan chan<- hashWithKeys, -) { - hasher := newHasher() - - shardedCollectionMap, err := getShardedCollectionMap(mr, storageMapValue) - if err != nil { - cancel(err) - return - } - - printValue := 10 - - for { - select { - case <-ctx.Done(): - return - case keyPair, ok := <-keyPairChan: - if !ok { - return - } - s := "" - - ownedNFTs, err := getNftCollection( - mr.Interpreter, - keyPair.shardedCollectionKey, - shardedCollectionMap, - ) - if err != nil { - cancel(err) - return - } - - value := ownedNFTs.GetKey( - mr.Interpreter, - interpreter.EmptyLocationRange, - keyPair.nftCollectionKey, - ) - - err = capturePanic(func() { - s = value.RecursiveString(interpreter.SeenReferences{}) - }) - if err != nil { - cancel(err) - return - } - hash := hasher.ComputeHash([]byte(s)) - hasher.Reset() - - uintKey := uint64(keyPair.shardedCollectionKey.(interpreter.UInt64Value)) - - if printValue > 0 && uintKey == 0 { - printValue-- - log.Info(). - Hex("hash", hash). - Str("rs", s). - Msg("recursive string hash for cricket moments sharded collection") - } - - hashChan <- hashWithKeys{ - key: uintKey, - hash: hash, - } - progress(1) - } - } -} diff --git a/cmd/util/ledger/migrations/migrator_runtime.go b/cmd/util/ledger/migrations/migrator_runtime.go index 38a86019b56..7157e705e7a 100644 --- a/cmd/util/ledger/migrations/migrator_runtime.go +++ b/cmd/util/ledger/migrations/migrator_runtime.go @@ -2,11 +2,7 @@ package migrations import ( "fmt" - "sync" - "github.com/rs/zerolog" - - "github.com/onflow/atree" "github.com/onflow/cadence/runtime" "github.com/onflow/cadence/runtime/common" "github.com/onflow/cadence/runtime/interpreter" @@ -15,8 +11,6 @@ import ( "github.com/onflow/flow-go/fvm/environment" "github.com/onflow/flow-go/fvm/storage/state" "github.com/onflow/flow-go/ledger" - "github.com/onflow/flow-go/model/flow" - util2 "github.com/onflow/flow-go/module/util" ) // migratorRuntime is a runtime that can be used to run a migration on a single account @@ -88,119 +82,3 @@ type migratorRuntime struct { func (mr *migratorRuntime) GetReadOnlyStorage() *runtime.Storage { return runtime.NewStorage(util.NewPayloadsReadonlyLedger(mr.Snapshot), util.NopMemoryGauge{}) } - -func (mr *migratorRuntime) ChildInterpreters(log zerolog.Logger, n int, address flow.Address) ([]*interpreter.Interpreter, func() error, error) { - - interpreters := make([]*interpreter.Interpreter, n) - storages := make([]*runtime.Storage, n) - - //id := flow.AccountStatusRegisterID(address) - //statusBytes, err := mr.Accounts.GetValue([]byte(id.Owner), []byte(id.Key)) - //if err != nil { - // return nil, nil, fmt.Errorf( - // "failed to load account status for the account (%s): %w", - // address.String(), - // err) - //} - //accountStatus, err := environment.AccountStatusFromBytes(statusBytes) - //if err != nil { - // return nil, nil, fmt.Errorf( - // "failed to parse account status for the account (%s): %w", - // address.String(), - // err) - //} - // - //index := accountStatus.StorageIndex() - //index = index.Next() - - //// create a channel that will generate storage indexes on demand - //storageIndexChan := make(chan atree.StorageIndex) - //stopStorageIndexChan := make(chan struct{}) - //go func() { - // for { - // select { - // case <-stopStorageIndexChan: - // return - // case storageIndexChan <- index: - // index = index.Next() - // } - // } - //}() - - mu := sync.Mutex{} - - for i := 0; i < n; i++ { - ledger := util.NewPayloadsReadonlyLedger(mr.Snapshot) - ledger.AllocateStorageIndexFunc = func(owner []byte) (atree.StorageIndex, error) { - mu.Lock() - defer mu.Unlock() - - return mr.Accounts.AllocateStorageIndex(owner) - - } - ledger.SetValueFunc = func(owner, key, value []byte) (err error) { - return mr.Accounts.SetValue(owner, key, value) - } - - storage := runtime.NewStorage(ledger, util.NopMemoryGauge{}) - - ri := &util.MigrationRuntimeInterface{} - - env := runtime.NewBaseInterpreterEnvironment(runtime.Config{ - AccountLinkingEnabled: true, - // Attachments are enabled everywhere except for Mainnet - AttachmentsEnabled: true, - // Capability Controllers are enabled everywhere except for Mainnet - CapabilityControllersEnabled: true, - }) - - env.Configure( - ri, - runtime.NewCodesAndPrograms(), - storage, - runtime.NewCoverageReport(), - ) - - inter, err := interpreter.NewInterpreter( - nil, - nil, - env.InterpreterConfig) - if err != nil { - return nil, nil, err - } - - interpreters[i] = inter - storages[i] = storage - } - - closeInterpreters := func() error { - progressLog := util2.LogProgress( - log, - util2.DefaultLogProgressConfig( - "closing child interpreters", - len(interpreters), - )) - //index := <-storageIndexChan - //close(stopStorageIndexChan) - //accountStatus.SetStorageIndex(index) - //err := mr.Accounts.SetValue([]byte(id.Owner), []byte(id.Key), accountStatus.ToBytes()) - //if err != nil { - // return fmt.Errorf( - // "failed to save account status for the account (%s): %w", - // address.String(), - // err) - //} - - for i, storage := range storages { - err := storage.Commit(interpreters[i], false) - if err != nil { - return fmt.Errorf("failed to commit storage: %w", err) - } - progressLog(1) - } - - return nil - } - - return interpreters, closeInterpreters, nil -}