Skip to content

Commit

Permalink
Ledger: Move trie validation step to only export time (#172)
Browse files Browse the repository at this point in the history
  • Loading branch information
ramtinms authored Nov 25, 2020
1 parent 2a02081 commit ef54713
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 3 deletions.
19 changes: 19 additions & 0 deletions ledger/complete/ledger.go
Original file line number Diff line number Diff line change
Expand Up @@ -254,25 +254,38 @@ func (l *Ledger) ExportCheckpointAt(state ledger.State,
targetPathFinderVersion uint8,
outputFilePath string) (ledger.State, error) {

l.logger.Info().Msgf("Ledger is loaded, checkpoint Export has started for state %s, and %d migrations has been planed", state.String(), len(migrations))

// get trie
t, err := l.forest.GetTrie(ledger.RootHash(state))
if err != nil {
return nil, fmt.Errorf("cannot get try at the given state commitment: %w", err)
}

// TODO enable validity check of trie
// only check validity of the trie we are interested in
// l.logger.Info().Msg("Checking validity of the trie at the given state...")
// if !t.IsAValidTrie() {
// return nil, fmt.Errorf("trie is not valid: %w", err)
// }
// l.logger.Info().Msg("Trie is valid.")

// get all payloads
payloads := t.AllPayloads()
payloadSize := len(payloads)

// migrate payloads
for i, migrate := range migrations {
l.logger.Info().Msgf("migration %d is underway", i)

payloads, err = migrate(payloads)
if err != nil {
return nil, fmt.Errorf("error applying migration (%d): %w", i, err)
}
if payloadSize != len(payloads) {
l.logger.Warn().Int("migration_step", i).Int("expected_size", payloadSize).Int("outcome_size", len(payloads)).Msg("payload counts has changed during migration, make sure this is expected.")
}
l.logger.Info().Msgf("migration %d is done", i)
}

// run reporters
Expand All @@ -283,6 +296,8 @@ func (l *Ledger) ExportCheckpointAt(state ledger.State,
}
}

l.logger.Info().Msgf("constructing a new trie with migrated payloads (count: %d)...", len(payloads))

// get paths
paths, err := pathfinder.PathsFromPayloads(payloads, targetPathFinderVersion)
if err != nil {
Expand All @@ -299,6 +314,8 @@ func (l *Ledger) ExportCheckpointAt(state ledger.State,
return nil, fmt.Errorf("constructing updated trie failed: %w", err)
}

l.logger.Info().Msg("creating a checkpoint for the new trie")

writer, err := wal.CreateCheckpointWriterForFile(outputFilePath)
if err != nil {
return nil, fmt.Errorf("failed to create a checkpoint writer: %w", err)
Expand All @@ -309,6 +326,8 @@ func (l *Ledger) ExportCheckpointAt(state ledger.State,
return nil, fmt.Errorf("failed to flatten the trie: %w", err)
}

l.logger.Info().Msg("storing the checkpoint to the file")

err = wal.StoreCheckpoint(flatTrie.ToFlattenedForestWithASingleTrie(), writer)
if err != nil {
return nil, fmt.Errorf("failed to store the checkpoint: %w", err)
Expand Down
3 changes: 0 additions & 3 deletions ledger/complete/mtrie/flattener/forest.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,9 +126,6 @@ func RebuildTries(flatForest *FlattenedForest) ([]*trie.MTrie, error) {
if err != nil {
return nil, fmt.Errorf("restoring trie failed: %w", err)
}
if !mtrie.IsAValidTrie() {
return nil, fmt.Errorf("restoring trie failed: the constructed trie is not a valid trie")
}
if !bytes.Equal(storableTrie.RootHash, mtrie.RootHash()) {
return nil, fmt.Errorf("restoring trie failed: roothash doesn't match")
}
Expand Down

0 comments on commit ef54713

Please sign in to comment.