Skip to content

Commit

Permalink
Merge pull request #5204 from onflow/fxamacker/atree-migration-cadenc…
Browse files Browse the repository at this point in the history
…e-value-validation

Replace hash-based validation of migrated Cadence values to use `Equal()`
  • Loading branch information
fxamacker authored Jan 9, 2024
2 parents 07f8723 + d56df32 commit 00cdc35
Show file tree
Hide file tree
Showing 6 changed files with 914 additions and 18 deletions.
35 changes: 26 additions & 9 deletions cmd/util/cmd/execution-state-extract/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,17 @@ import (
)

var (
flagExecutionStateDir string
flagOutputDir string
flagBlockHash string
flagStateCommitment string
flagDatadir string
flagChain string
flagNoMigration bool
flagNoReport bool
flagNWorker int
flagExecutionStateDir string
flagOutputDir string
flagBlockHash string
flagStateCommitment string
flagDatadir string
flagChain string
flagNWorker int
flagNoMigration bool
flagNoReport bool
flagValidateMigration bool
flagLogVerboseValidationError bool
)

var Cmd = &cobra.Command{
Expand Down Expand Up @@ -59,6 +61,13 @@ func init() {
"don't report the state")

Cmd.Flags().IntVar(&flagNWorker, "n-migrate-worker", 10, "number of workers to migrate payload concurrently")

Cmd.Flags().BoolVar(&flagValidateMigration, "validate", false,
"validate migrated Cadence values (atree migration)")

Cmd.Flags().BoolVar(&flagLogVerboseValidationError, "log-verbose-validation-error", false,
"log entire Cadence values on validation error (atree migration)")

}

func run(*cobra.Command, []string) {
Expand Down Expand Up @@ -135,6 +144,14 @@ func run(*cobra.Command, []string) {
log.Warn().Msgf("--no-migration flag is deprecated")
}

if flagValidateMigration {
log.Warn().Msgf("atree migration validation flag is enabled and will increase duration of migration")
}

if flagLogVerboseValidationError {
log.Warn().Msgf("atree migration has verbose validation error logging enabled which may increase size of log")
}

err := extractExecutionState(
flagExecutionStateDir,
stateCommitment,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,19 +85,18 @@ func extractExecutionState(

rwf := reporters.NewReportFileWriterFactory(dir, log)

cadenceDataValidation := migrators.NewCadenceDataValidationMigrations(rwf, nWorker)

var migrations = []ledger.Migration{
migrators.CreateAccountBasedMigration(
log,
nWorker,
[]migrators.AccountBasedMigration{
cadenceDataValidation.PreMigration(),
migrators.NewAtreeRegisterMigrator(
rwf,
flagValidateMigration,
flagLogVerboseValidationError,
),

&migrators.DeduplicateContractNamesMigration{},
cadenceDataValidation.PostMigration(),

// This will fix storage used discrepancies caused by the
// DeduplicateContractNamesMigration.
Expand Down
21 changes: 17 additions & 4 deletions cmd/util/ledger/migrations/atree_register_migration.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,22 +35,28 @@ type AtreeRegisterMigrator struct {
rwf reporters.ReportWriterFactory

nWorkers int

validateMigratedValues bool
logVerboseValidationError bool
}

var _ AccountBasedMigration = (*AtreeRegisterMigrator)(nil)
var _ io.Closer = (*AtreeRegisterMigrator)(nil)

func NewAtreeRegisterMigrator(
rwf reporters.ReportWriterFactory,
validateMigratedValues bool,
logVerboseValidationError bool,
) *AtreeRegisterMigrator {

sampler := util2.NewTimedSampler(30 * time.Second)

migrator := &AtreeRegisterMigrator{
sampler: sampler,

rwf: rwf,
rw: rwf.ReportWriter("atree-register-migrator"),
sampler: sampler,
rwf: rwf,
rw: rwf.ReportWriter("atree-register-migrator"),
validateMigratedValues: validateMigratedValues,
logVerboseValidationError: logVerboseValidationError,
}

return migrator
Expand Down Expand Up @@ -108,6 +114,13 @@ func (m *AtreeRegisterMigrator) MigrateAccount(
return nil, err
}

if m.validateMigratedValues {
err = validateCadenceValues(address, oldPayloads, newPayloads, m.log, m.logVerboseValidationError)
if err != nil {
return nil, err
}
}

newLen := len(newPayloads)

if newLen > originalLen {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func TestAtreeRegisterMigration(t *testing.T) {
migrations.CreateAccountBasedMigration(log, 2,
[]migrations.AccountBasedMigration{
validation.PreMigration(),
migrations.NewAtreeRegisterMigrator(reporters.NewReportFileWriterFactory(dir, log)),
migrations.NewAtreeRegisterMigrator(reporters.NewReportFileWriterFactory(dir, log), true, false),
validation.PostMigration(),
},
),
Expand Down
Loading

0 comments on commit 00cdc35

Please sign in to comment.