github.com/onflow/flow-go@v0.35.7-crescendo-preview.23-atree-inlining/cmd/util/ledger/migrations/utils.go (about)

     1  package migrations
     2  
     3  import (
     4  	"github.com/onflow/atree"
     5  	"github.com/onflow/cadence/runtime"
     6  	"github.com/onflow/cadence/runtime/common"
     7  	"github.com/onflow/cadence/runtime/stdlib"
     8  
     9  	"github.com/onflow/flow-go/cmd/util/ledger/util/registers"
    10  	"github.com/onflow/flow-go/model/flow"
    11  )
    12  
    13  type RegistersMigration func(registersByAccount *registers.ByAccount) error
    14  
    15  var allStorageMapDomains = []string{
    16  	common.PathDomainStorage.Identifier(),
    17  	common.PathDomainPrivate.Identifier(),
    18  	common.PathDomainPublic.Identifier(),
    19  	runtime.StorageDomainContract,
    20  	stdlib.InboxStorageDomain,
    21  	stdlib.CapabilityControllerStorageDomain,
    22  }
    23  
    24  var allStorageMapDomainsSet = map[string]struct{}{}
    25  
    26  func init() {
    27  	for _, domain := range allStorageMapDomains {
    28  		allStorageMapDomainsSet[domain] = struct{}{}
    29  	}
    30  }
    31  
    32  func getSlabIDsFromRegisters(registers registers.Registers) ([]atree.SlabID, error) {
    33  	storageIDs := make([]atree.SlabID, 0, registers.Count())
    34  
    35  	err := registers.ForEach(func(owner string, key string, value []byte) error {
    36  
    37  		if !flow.IsSlabIndexKey(key) {
    38  			return nil
    39  		}
    40  
    41  		slabID := atree.NewSlabID(
    42  			atree.Address([]byte(owner)),
    43  			atree.SlabIndex([]byte(key[1:])),
    44  		)
    45  
    46  		storageIDs = append(storageIDs, slabID)
    47  
    48  		return nil
    49  	})
    50  	if err != nil {
    51  		return nil, err
    52  	}
    53  
    54  	return storageIDs, nil
    55  }
    56  
    57  func loadAtreeSlabsInStorage(
    58  	storage *runtime.Storage,
    59  	registers registers.Registers,
    60  	nWorkers int,
    61  ) error {
    62  
    63  	storageIDs, err := getSlabIDsFromRegisters(registers)
    64  	if err != nil {
    65  		return err
    66  	}
    67  
    68  	return storage.PersistentSlabStorage.BatchPreload(storageIDs, nWorkers)
    69  }
    70  
    71  func checkStorageHealth(
    72  	address common.Address,
    73  	storage *runtime.Storage,
    74  	registers registers.Registers,
    75  	nWorkers int,
    76  ) error {
    77  
    78  	err := loadAtreeSlabsInStorage(storage, registers, nWorkers)
    79  	if err != nil {
    80  		return err
    81  	}
    82  
    83  	for _, domain := range allStorageMapDomains {
    84  		_ = storage.GetStorageMap(address, domain, false)
    85  	}
    86  
    87  	return storage.CheckHealth()
    88  }