Skip to content

Commit

Permalink
Merge pull request #6459 from onflow/leo/util-ensure-checkpoint-exists
Browse files Browse the repository at this point in the history
[Util] Check checkpoint file exists when running state extraction
  • Loading branch information
zhangchiqing authored Sep 12, 2024
2 parents 09153da + eb9e25c commit 50e5c3a
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 32 deletions.
58 changes: 30 additions & 28 deletions cmd/util/cmd/execution-state-extract/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -346,6 +342,12 @@ func run(*cobra.Command, []string) {
hex.EncodeToString(stateCommitment[:]),
flagExecutionStateDir,
)

err := ensureCheckpointFileExist(flagExecutionStateDir)
if err != nil {
log.Error().Err(err).Msgf("cannot ensure checkpoint file exist in folder %v", flagExecutionStateDir)
}

}

var outputMsg string
Expand Down Expand Up @@ -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-state-dir flag", dir)
}
16 changes: 12 additions & 4 deletions cmd/util/ledger/util/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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-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-state-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)
}

Expand Down

0 comments on commit 50e5c3a

Please sign in to comment.