Skip to content

Commit

Permalink
Merge pull request #6422 from onflow/bastian/evm-storage-account-crea…
Browse files Browse the repository at this point in the history
…tion-migration

[Cadence 1.0 migration] Add EVM storage account creation migration
  • Loading branch information
turbolent authored Aug 30, 2024
2 parents 9708618 + 68d88f1 commit 1a9e9d5
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 0 deletions.
62 changes: 62 additions & 0 deletions cmd/util/ledger/migrations/account.go
Original file line number Diff line number Diff line change
@@ -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
}
}
15 changes: 15 additions & 0 deletions cmd/util/ledger/migrations/cadence.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 1a9e9d5

Please sign in to comment.