diff --git a/cmd/util/ledger/migrations/account.go b/cmd/util/ledger/migrations/account.go new file mode 100644 index 00000000000..be070b31e10 --- /dev/null +++ b/cmd/util/ledger/migrations/account.go @@ -0,0 +1,62 @@ +package migrations + +import ( + "fmt" + + "github.com/rs/zerolog" + + "github.com/onflow/flow-go/cmd/util/ledger/util/registers" + "github.com/onflow/flow-go/model/flow" +) + +func NewAccountCreationMigration( + address flow.Address, + logger zerolog.Logger, +) RegistersMigration { + + return func(registersByAccount *registers.ByAccount) error { + + migrationRuntime := NewBasicMigrationRuntime(registersByAccount) + + // Check if the account already exists + exists, err := migrationRuntime.Accounts.Exists(address) + if err != nil { + return fmt.Errorf( + "failed to check if account %s exists: %w", + address, + err, + ) + } + + // If the account already exists, do nothing + if exists { + logger.Info().Msgf("account %s already exists", address) + return nil + } + + // Create the account + err = migrationRuntime.Accounts.Create(nil, address) + if err != nil { + return fmt.Errorf( + "failed to create account %s: %w", + address, + err, + ) + } + + logger.Info().Msgf("created account %s", address) + + // Commit the changes to the migrated registers + err = migrationRuntime.Commit( + map[flow.Address]struct{}{ + address: {}, + }, + logger, + ) + if err != nil { + return fmt.Errorf("failed to commit account creation: %w", err) + } + + return nil + } +} diff --git a/cmd/util/ledger/migrations/cadence.go b/cmd/util/ledger/migrations/cadence.go index e4f39665c7a..2cc5c9154ed 100644 --- a/cmd/util/ledger/migrations/cadence.go +++ b/cmd/util/ledger/migrations/cadence.go @@ -834,6 +834,21 @@ func NewCadence1Migrations( Migrate: NewEVMSetupMigration(opts.ChainID, log), }, ) + if opts.ChainID == flow.Emulator { + + // In the Emulator the EVM storage account needs to be created + + systemContracts := systemcontracts.SystemContractsForChain(opts.ChainID) + evmStorageAddress := systemContracts.EVMStorage.Address + + migs = append( + migs, + NamedMigration{ + Name: "evm-storage-account-creation-migration", + Migrate: NewAccountCreationMigration(evmStorageAddress, log), + }, + ) + } } return migs