Skip to content

Commit

Permalink
Merge pull request #6430 from onflow/fxamacker/support-evm-registers-…
Browse files Browse the repository at this point in the history
…in-check-storage

Add support for checking EVM registers in `check-storage` command
  • Loading branch information
fxamacker authored Sep 10, 2024
2 parents 426f640 + a1a3c33 commit 72ac1d1
Show file tree
Hide file tree
Showing 3 changed files with 674 additions and 22 deletions.
45 changes: 23 additions & 22 deletions cmd/util/cmd/check-storage/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package check_storage

import (
"context"
"slices"

"github.com/rs/zerolog/log"
"github.com/spf13/cobra"
Expand All @@ -14,6 +13,7 @@ import (
"github.com/onflow/flow-go/cmd/util/ledger/reporters"
"github.com/onflow/flow-go/cmd/util/ledger/util"
"github.com/onflow/flow-go/cmd/util/ledger/util/registers"
"github.com/onflow/flow-go/fvm/evm/emulator/state"
"github.com/onflow/flow-go/fvm/systemcontracts"
"github.com/onflow/flow-go/ledger"
"github.com/onflow/flow-go/model/flow"
Expand All @@ -29,6 +29,14 @@ var (
flagNWorker int
)

var (
evmAccount flow.Address
evmStorageIDKeys = []string{
state.AccountsStorageIDKey,
state.CodesStorageIDKey,
}
)

var Cmd = &cobra.Command{
Use: "check-storage",
Short: "Check storage health",
Expand Down Expand Up @@ -102,13 +110,8 @@ func run(*cobra.Command, []string) {
log.Fatal().Msg("--state-commitment must be provided when --state is provided")
}

// For now, skip EVM storage account since a different decoder is needed for decoding EVM registers.

systemContracts := systemcontracts.SystemContractsForChain(chainID)

acctsToSkip := []string{
flow.AddressToRegisterOwner(systemContracts.EVMStorage.Address),
}
// Get EVM account by chain
evmAccount = systemcontracts.SystemContractsForChain(chainID).EVMStorage.Address

// Create report in JSONL format
rw := reporters.NewReportFileWriterFactoryWithFormat(flagOutputDirectory, log.Logger, reporters.ReportFormatJSONL).
Expand Down Expand Up @@ -161,13 +164,13 @@ func run(*cobra.Command, []string) {
len(payloads),
)

failedAccountAddresses, err := checkStorageHealth(registersByAccount, flagNWorker, rw, acctsToSkip)
failedAccountAddresses, err := checkStorageHealth(registersByAccount, flagNWorker, rw)
if err != nil {
log.Fatal().Err(err).Msg("failed to check storage health")
}

if len(failedAccountAddresses) == 0 {
log.Info().Msgf("All %d accounts are health", accountCount)
log.Info().Msgf("All %d accounts are healthy", accountCount)
return
}

Expand All @@ -188,7 +191,6 @@ func checkStorageHealth(
registersByAccount *registers.ByAccount,
nWorkers int,
rw reporters.ReportWriter,
acctsToSkip []string,
) (failedAccountAddresses []string, err error) {

accountCount := registersByAccount.AccountCount()
Expand All @@ -211,10 +213,6 @@ func checkStorageHealth(
func(accountRegisters *registers.AccountRegisters) error {
defer logAccount(1)

if slices.Contains(acctsToSkip, accountRegisters.Owner()) {
return nil
}

accountStorageIssues := checkAccountStorageHealth(accountRegisters, nWorkers)

if len(accountStorageIssues) > 0 {
Expand Down Expand Up @@ -281,9 +279,6 @@ func checkStorageHealth(

err = registersByAccount.ForEachAccount(
func(accountRegisters *registers.AccountRegisters) error {
if slices.Contains(acctsToSkip, accountRegisters.Owner()) {
return nil
}
jobs <- job{accountRegisters: accountRegisters}
return nil
})
Expand Down Expand Up @@ -318,6 +313,10 @@ func checkAccountStorageHealth(accountRegisters *registers.AccountRegisters, nWo
}}
}

if isEVMAccount(address) {
return checkEVMAccountStorageHealth(address, accountRegisters)
}

var issues []accountStorageIssue

// Check atree storage health
Expand All @@ -331,7 +330,7 @@ func checkAccountStorageHealth(accountRegisters *registers.AccountRegisters, nWo
issues,
accountStorageIssue{
Address: address.Hex(),
Kind: storageErrorKindString[atreeStorageErrorKind],
Kind: storageErrorKindString[cadenceAtreeStorageErrorKind],
Msg: err.Error(),
})
}
Expand All @@ -345,12 +344,14 @@ type storageErrorKind int

const (
otherErrorKind storageErrorKind = iota
atreeStorageErrorKind
cadenceAtreeStorageErrorKind
evmAtreeStorageErrorKind
)

var storageErrorKindString = map[storageErrorKind]string{
otherErrorKind: "error_check_storage_failed",
atreeStorageErrorKind: "error_atree_storage",
otherErrorKind: "error_check_storage_failed",
cadenceAtreeStorageErrorKind: "error_cadence_atree_storage",
evmAtreeStorageErrorKind: "error_evm_atree_storage",
}

type accountStorageIssue struct {
Expand Down
Loading

0 comments on commit 72ac1d1

Please sign in to comment.