From 6b74c0b16eb60cb85f257ddd707c5ada7505e35b Mon Sep 17 00:00:00 2001 From: "Leo Zhang (zhangchiqing)" Date: Wed, 11 Sep 2024 14:27:06 -0700 Subject: [PATCH 1/3] check checkpoint file exists when running state extraction, and improve error message --- cmd/util/cmd/execution-state-extract/cmd.go | 58 +++++++++++---------- cmd/util/ledger/util/state.go | 16 ++++-- 2 files changed, 42 insertions(+), 32 deletions(-) diff --git a/cmd/util/cmd/execution-state-extract/cmd.go b/cmd/util/cmd/execution-state-extract/cmd.go index 2196b554c93..52ef72bb097 100644 --- a/cmd/util/cmd/execution-state-extract/cmd.go +++ b/cmd/util/cmd/execution-state-extract/cmd.go @@ -16,6 +16,7 @@ import ( "github.com/onflow/flow-go/cmd/util/ledger/migrations" "github.com/onflow/flow-go/cmd/util/ledger/reporters" "github.com/onflow/flow-go/cmd/util/ledger/util" + "github.com/onflow/flow-go/ledger/complete/wal" "github.com/onflow/flow-go/model/bootstrap" "github.com/onflow/flow-go/model/flow" "github.com/onflow/flow-go/module/metrics" @@ -297,11 +298,6 @@ func run(*cobra.Command, []string) { } } - // err := ensureCheckpointFileExist(flagExecutionStateDir) - // if err != nil { - // log.Fatal().Err(err).Msgf("cannot ensure checkpoint file exist in folder %v", flagExecutionStateDir) - // } - chain := flow.ChainID(flagChain).Chain() if flagNoReport { @@ -346,6 +342,12 @@ func run(*cobra.Command, []string) { hex.EncodeToString(stateCommitment[:]), flagExecutionStateDir, ) + + err := ensureCheckpointFileExist(flagExecutionStateDir) + if err != nil { + log.Fatal().Err(err).Msgf("cannot ensure checkpoint file exist in folder %v", flagExecutionStateDir) + } + } var outputMsg string @@ -484,26 +486,26 @@ func run(*cobra.Command, []string) { ) } -// func ensureCheckpointFileExist(dir string) error { -// checkpoints, err := wal.Checkpoints(dir) -// if err != nil { -// return fmt.Errorf("could not find checkpoint files: %v", err) -// } -// -// if len(checkpoints) != 0 { -// log.Info().Msgf("found checkpoint %v files: %v", len(checkpoints), checkpoints) -// return nil -// } -// -// has, err := wal.HasRootCheckpoint(dir) -// if err != nil { -// return fmt.Errorf("could not check has root checkpoint: %w", err) -// } -// -// if has { -// log.Info().Msg("found root checkpoint file") -// return nil -// } -// -// return fmt.Errorf("no checkpoint file was found, no root checkpoint file was found") -// } +func ensureCheckpointFileExist(dir string) error { + checkpoints, err := wal.Checkpoints(dir) + if err != nil { + return fmt.Errorf("could not find checkpoint files: %v", err) + } + + if len(checkpoints) != 0 { + log.Info().Msgf("found checkpoint %v files: %v", len(checkpoints), checkpoints) + return nil + } + + has, err := wal.HasRootCheckpoint(dir) + if err != nil { + return fmt.Errorf("could not check has root checkpoint: %w", err) + } + + if has { + log.Info().Msg("found root checkpoint file") + return nil + } + + return fmt.Errorf("no checkpoint file was found, no root checkpoint file was found in %v, check the --execution-data-dir flag", dir) +} diff --git a/cmd/util/ledger/util/state.go b/cmd/util/ledger/util/state.go index 9e56a461985..7f42c04813f 100644 --- a/cmd/util/ledger/util/state.go +++ b/cmd/util/ledger/util/state.go @@ -11,6 +11,7 @@ import ( "github.com/onflow/flow-go/ledger" "github.com/onflow/flow-go/ledger/common/pathfinder" "github.com/onflow/flow-go/ledger/complete" + mtrie "github.com/onflow/flow-go/ledger/complete/mtrie/trie" "github.com/onflow/flow-go/ledger/complete/wal" "github.com/onflow/flow-go/model/flow" "github.com/onflow/flow-go/module/metrics" @@ -78,10 +79,17 @@ func ReadTrie(dir string, targetHash flow.StateCommitment) ([]*ledger.Payload, e trie, err := led.Trie(ledger.RootHash(state)) if err != nil { - s, _ := led.MostRecentTouchedState() - log.Info(). - Str("hash", s.String()). - Msgf("Most recently touched state") + s, err2 := led.MostRecentTouchedState() + if err2 != nil { + log.Error().Err(err2). + Msgf("cannot get most recently touched state in %v, check the --execution-data-dir flag", dir) + } else if s == ledger.State(mtrie.NewEmptyMTrie().RootHash()) { + log.Error().Msgf("cannot find any trie in folder %v. check the --execution-data-dir flag", dir) + } else { + log.Info(). + Str("hash", s.String()). + Msgf("Most recently touched state") + } return nil, fmt.Errorf("cannot get trie at the given state commitment: %w", err) } From 24d820dc8b69a6ae965428c20d394afc3c7cd536 Mon Sep 17 00:00:00 2001 From: "Leo Zhang (zhangchiqing)" Date: Thu, 12 Sep 2024 08:27:02 -0700 Subject: [PATCH 2/3] use Error instead of Fatal --- cmd/util/cmd/execution-state-extract/cmd.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cmd/util/cmd/execution-state-extract/cmd.go b/cmd/util/cmd/execution-state-extract/cmd.go index 52ef72bb097..207ddd37846 100644 --- a/cmd/util/cmd/execution-state-extract/cmd.go +++ b/cmd/util/cmd/execution-state-extract/cmd.go @@ -345,7 +345,7 @@ func run(*cobra.Command, []string) { err := ensureCheckpointFileExist(flagExecutionStateDir) if err != nil { - log.Fatal().Err(err).Msgf("cannot ensure checkpoint file exist in folder %v", flagExecutionStateDir) + log.Error().Err(err).Msgf("cannot ensure checkpoint file exist in folder %v", flagExecutionStateDir) } } From eb9e25c581634db780901d47b54e1d2ea98e3b96 Mon Sep 17 00:00:00 2001 From: "Leo Zhang (zhangchiqing)" Date: Thu, 12 Sep 2024 12:57:04 -0700 Subject: [PATCH 3/3] fix the tag name in the error message --- cmd/util/cmd/execution-state-extract/cmd.go | 2 +- cmd/util/ledger/util/state.go | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/cmd/util/cmd/execution-state-extract/cmd.go b/cmd/util/cmd/execution-state-extract/cmd.go index 207ddd37846..9bfba99bad4 100644 --- a/cmd/util/cmd/execution-state-extract/cmd.go +++ b/cmd/util/cmd/execution-state-extract/cmd.go @@ -507,5 +507,5 @@ func ensureCheckpointFileExist(dir string) error { return nil } - return fmt.Errorf("no checkpoint file was found, no root checkpoint file was found in %v, check the --execution-data-dir flag", dir) + return fmt.Errorf("no checkpoint file was found, no root checkpoint file was found in %v, check the --execution-state-dir flag", dir) } diff --git a/cmd/util/ledger/util/state.go b/cmd/util/ledger/util/state.go index 7f42c04813f..049573a2362 100644 --- a/cmd/util/ledger/util/state.go +++ b/cmd/util/ledger/util/state.go @@ -82,9 +82,9 @@ func ReadTrie(dir string, targetHash flow.StateCommitment) ([]*ledger.Payload, e s, err2 := led.MostRecentTouchedState() if err2 != nil { log.Error().Err(err2). - Msgf("cannot get most recently touched state in %v, check the --execution-data-dir flag", dir) + Msgf("cannot get most recently touched state in %v, check the --execution-state-dir flag", dir) } else if s == ledger.State(mtrie.NewEmptyMTrie().RootHash()) { - log.Error().Msgf("cannot find any trie in folder %v. check the --execution-data-dir flag", dir) + log.Error().Msgf("cannot find any trie in folder %v. check the --execution-state-dir flag", dir) } else { log.Info(). Str("hash", s.String()).