Skip to content

Commit

Permalink
add a migration which creates the EVM storage account in the Emulator…
Browse files Browse the repository at this point in the history
… state
  • Loading branch information
turbolent committed Aug 29, 2024
1 parent 9708618 commit 68d88f1
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 68d88f1

Please sign in to comment.