-
Notifications
You must be signed in to change notification settings - Fork 66
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1426 from onflow/jribbink/migrate-command
Add `flow migrate state` command
- Loading branch information
Showing
5 changed files
with
250 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,143 @@ | ||
/* | ||
* Flow CLI | ||
* | ||
* Copyright 2019 Dapper Labs, Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package migrate | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
|
||
"github.com/onflow/cadence/runtime/common" | ||
"github.com/onflow/flow-emulator/storage/migration" | ||
emulatorMigrate "github.com/onflow/flow-emulator/storage/migration" | ||
"github.com/onflow/flow-emulator/storage/sqlite" | ||
"github.com/onflow/flow-go-sdk" | ||
"github.com/onflow/flow-go/cmd/util/ledger/migrations" | ||
"github.com/onflow/flow-go/cmd/util/ledger/reporters" | ||
"github.com/onflow/flowkit/v2" | ||
"github.com/onflow/flowkit/v2/config" | ||
"github.com/onflow/flowkit/v2/output" | ||
"github.com/rs/zerolog" | ||
"github.com/spf13/cobra" | ||
|
||
"github.com/onflow/flow-cli/internal/command" | ||
) | ||
|
||
var stateFlags struct { | ||
Contracts []string `default:"" flag:"contracts" info:"contract names to migrate"` | ||
SaveReport string `default:"" flag:"save-report" info:"save migration report to a given directory if provided"` | ||
DBPath string `default:"./flowdb" flag:"db-path" info:"path to the sqlite database file"` | ||
} | ||
|
||
var stateCommand = &command.Command{ | ||
Cmd: &cobra.Command{ | ||
Use: "state", | ||
Short: "Migrate the state of a SQLite Flow Emulator database", | ||
Example: `flow migrate state`, | ||
Args: cobra.MinimumNArgs(0), | ||
}, | ||
Flags: &stateFlags, | ||
RunS: migrateState, | ||
} | ||
|
||
func migrateState( | ||
args []string, | ||
globalFlags command.GlobalFlags, | ||
_ output.Logger, | ||
flow flowkit.Services, | ||
state *flowkit.State, | ||
) (command.Result, error) { | ||
if globalFlags.Network != config.EmulatorNetwork.Name { | ||
return nil, fmt.Errorf("state migration is only supported for the emulator network") | ||
} | ||
|
||
contracts, err := resolveStagedContracts(state, stateFlags.Contracts) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to resolve staged contracts: %w", err) | ||
} | ||
|
||
store, err := sqlite.New(stateFlags.DBPath) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to open database: %w", err) | ||
} | ||
|
||
logger := zerolog.New(zerolog.ConsoleWriter{Out: os.Stderr}).With().Timestamp().Logger() | ||
|
||
// Create a report writer factory if a report path is provided | ||
var rwf reporters.ReportWriterFactory | ||
if stateFlags.SaveReport != "" { | ||
rwf = reporters.NewReportFileWriterFactory(stateFlags.SaveReport, logger) | ||
} else { | ||
rwf = &migration.NOOPReportWriterFactory{} | ||
} | ||
|
||
err = emulatorMigrate.MigrateCadence1( | ||
store, | ||
migrations.EVMContractChangeNone, | ||
migrations.BurnerContractChangeDeploy, | ||
contracts, | ||
rwf, | ||
logger, | ||
) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to migrate database: %w", err) | ||
} | ||
|
||
return nil, nil | ||
} | ||
|
||
func resolveStagedContracts(state *flowkit.State, contractNames []string) ([]migrations.StagedContract, error) { | ||
contracts := make([]migrations.StagedContract, len(contractNames)) | ||
|
||
for i, contractName := range contractNames { | ||
// First try to get contract address from aliases | ||
contract, err := state.Contracts().ByName(contractName) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to get contract by name: %w", err) | ||
} | ||
|
||
var address flow.Address | ||
alias := contract.Aliases.ByNetwork(config.EmulatorNetwork.Name) | ||
if alias != nil { | ||
address = alias.Address | ||
} | ||
|
||
code, err := state.ReadFile(contract.Location) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to read contract file: %w", err) | ||
} | ||
|
||
// If contract is not aliased, try to get address by deployment account | ||
if address == flow.EmptyAddress { | ||
address, err = getAddressByContractName(state, contractName, config.EmulatorNetwork) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to get address by contract name: %w", err) | ||
} | ||
} | ||
|
||
contracts[i] = migrations.StagedContract{ | ||
Contract: migrations.Contract{ | ||
Name: contractName, | ||
Code: code, | ||
}, | ||
Address: common.Address(address), | ||
} | ||
} | ||
|
||
return contracts, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
/* | ||
* Flow CLI | ||
* | ||
* Copyright 2019 Dapper Labs, Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package migrate | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/onflow/cadence/runtime/common" | ||
"github.com/onflow/flow-go-sdk" | ||
"github.com/onflow/flow-go/cmd/util/ledger/migrations" | ||
"github.com/onflow/flowkit/v2/config" | ||
"github.com/onflow/flowkit/v2/tests" | ||
"github.com/stretchr/testify/assert" | ||
|
||
"github.com/onflow/flow-cli/internal/util" | ||
) | ||
|
||
func Test_MigrateState(t *testing.T) { | ||
_, state, _ := util.TestMocks(t) | ||
|
||
testContractAliased := tests.ContractSimple | ||
testContractDeployed := tests.ContractA | ||
|
||
t.Run("resolves staged contracts by name", func(t *testing.T) { | ||
// Add an aliased contract to state | ||
state.Contracts().AddOrUpdate( | ||
config.Contract{ | ||
Name: testContractAliased.Name, | ||
Location: testContractAliased.Filename, | ||
Aliases: config.Aliases{ | ||
{ | ||
Network: "emulator", | ||
Address: flow.HexToAddress("0x1"), | ||
}, | ||
}, | ||
}, | ||
) | ||
|
||
state.Contracts().AddOrUpdate( | ||
config.Contract{ | ||
Name: testContractDeployed.Name, | ||
Location: testContractDeployed.Filename, | ||
}, | ||
) | ||
|
||
// Add deployment to state | ||
state.Deployments().AddOrUpdate( | ||
config.Deployment{ | ||
Network: "emulator", | ||
Account: "emulator-account", | ||
Contracts: []config.ContractDeployment{ | ||
{ | ||
Name: testContractDeployed.Name, | ||
}, | ||
}, | ||
}, | ||
) | ||
|
||
account, err := state.EmulatorServiceAccount() | ||
assert.NoError(t, err) | ||
|
||
contracts, err := resolveStagedContracts( | ||
state, | ||
[]string{testContractAliased.Name, testContractDeployed.Name}, | ||
) | ||
assert.NoError(t, err) | ||
|
||
assert.Equal(t, []migrations.StagedContract{ | ||
{ | ||
Contract: migrations.Contract{ | ||
Name: testContractAliased.Name, | ||
Code: testContractAliased.Source, | ||
}, | ||
Address: common.Address(flow.HexToAddress("0x1")), | ||
}, | ||
{ | ||
Contract: migrations.Contract{ | ||
Name: testContractDeployed.Name, | ||
Code: testContractDeployed.Source, | ||
}, | ||
Address: common.Address(account.Address), | ||
}, | ||
}, contracts) | ||
}) | ||
} |