Skip to content

Commit

Permalink
Merge pull request #6315 from onflow/bastian/fix-issue-storage-cap-co…
Browse files Browse the repository at this point in the history
…n-migration

[Cadence 1.0 Migration] Merge and commit changes after issuing cap cons
  • Loading branch information
SupunS authored Aug 10, 2024
2 parents a53b9b8 + 31f8c09 commit 263f1c4
Show file tree
Hide file tree
Showing 2 changed files with 109 additions and 2 deletions.
30 changes: 29 additions & 1 deletion cmd/util/ledger/migrations/cadence.go
Original file line number Diff line number Diff line change
Expand Up @@ -328,15 +328,43 @@ func (m *IssueStorageCapConMigration) MigrateAccount(
m.verboseErrorOutput,
)

inter := migrationRuntime.Interpreter

capcons.IssueAccountCapabilities(
migrationRuntime.Interpreter,
inter,
reporter,
address,
accountCapabilities,
handler,
m.mapping,
)

err = migrationRuntime.Storage.NondeterministicCommit(inter, false)
if err != nil {
return fmt.Errorf("failed to commit changes: %w", err)
}

// finalize the transaction
result, err := migrationRuntime.TransactionState.FinalizeMainTransaction()
if err != nil {
return fmt.Errorf("failed to finalize main transaction: %w", err)
}

// Merge the changes into the registers
expectedAddresses := map[flow.Address]struct{}{
flow.Address(address): {},
}

err = registers.ApplyChanges(
accountRegisters,
result.WriteSet,
expectedAddresses,
m.log,
)
if err != nil {
return fmt.Errorf("failed to apply changes: %w", err)
}

return nil
}

Expand Down
81 changes: 80 additions & 1 deletion cmd/util/ledger/migrations/cadence_values_migration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"testing"

_ "github.com/glebarez/go-sqlite"
"github.com/onflow/cadence"
migrations2 "github.com/onflow/cadence/migrations"
"github.com/rs/zerolog"
"github.com/stretchr/testify/assert"
Expand All @@ -21,6 +22,8 @@ import (
"github.com/onflow/flow-go/cmd/util/ledger/reporters"
"github.com/onflow/flow-go/cmd/util/ledger/util"
"github.com/onflow/flow-go/cmd/util/ledger/util/registers"
"github.com/onflow/flow-go/engine/execution/computation"
"github.com/onflow/flow-go/fvm"
"github.com/onflow/flow-go/fvm/environment"
"github.com/onflow/flow-go/fvm/systemcontracts"
"github.com/onflow/flow-go/model/flow"
Expand Down Expand Up @@ -2134,7 +2137,7 @@ func TestCadenceValueMigrationEntry_MarshalJSON(t *testing.T) {
)
}

func TestCapabilityMigration(t *testing.T) {
func TestStoragePathCapabilityMigration(t *testing.T) {
t.Parallel()

rwf := &testReportWriterFactory{}
Expand Down Expand Up @@ -2330,6 +2333,50 @@ func TestCapabilityMigration(t *testing.T) {
},
issueStorageCapConReporter.entries,
)

// Check account A

_, err = runScript(
chainID,
registersByAccount,
fmt.Sprintf(
//language=Cadence
`
access(all)
fun main() {
let storage = getAuthAccount<auth(Storage) &Account>(%s).storage
let fooCap = storage.copy<Capability>(from: /storage/fooCap)!
let barCap = storage.copy<Capability>(from: /storage/barCap)!
assert(fooCap.id == 3)
assert(barCap.id == 0)
}
`,
addressA.HexWithPrefix(),
),
)
require.NoError(t, err)

// Check account B

_, err = runScript(
chainID,
registersByAccount,
fmt.Sprintf(
//language=Cadence
`
access(all)
fun main() {
let capabilities = getAuthAccount<auth(Capabilities) &Account>(%s).capabilities.storage
let fooCapCons = capabilities.getControllers(forPath: /storage/foo)
assert(fooCapCons.length == 1)
assert(fooCapCons[0].capabilityID == 3)
}
`,
addressB.HexWithPrefix(),
),
)
require.NoError(t, err)

}

func TestStorageCapConIssuedEntry_MarshalJSON(t *testing.T) {
Expand Down Expand Up @@ -2399,3 +2446,35 @@ func TestStorageCapConsMissingBorrowTypeEntry_MarshalJSON(t *testing.T) {
string(actual),
)
}

func runScript(chainID flow.ChainID, registersByAccount *registers.ByAccount, script string) (cadence.Value, error) {
options := computation.DefaultFVMOptions(chainID, false, false)
options = append(options,
fvm.WithContractDeploymentRestricted(false),
fvm.WithContractRemovalRestricted(false),
fvm.WithAuthorizationChecksEnabled(false),
fvm.WithSequenceNumberCheckAndIncrementEnabled(false),
fvm.WithTransactionFeesEnabled(false))
ctx := fvm.NewContext(options...)

storageSnapshot := registers.StorageSnapshot{
Registers: registersByAccount,
}

vm := fvm.NewVirtualMachine()

_, res, err := vm.Run(
ctx,
fvm.Script([]byte(script)),
storageSnapshot,
)
if err != nil {
return nil, fmt.Errorf("failed to run transaction: %w", err)
}

if res.Err != nil {
return nil, fmt.Errorf("transaction failed: %w", res.Err)
}

return res.Value, nil
}

0 comments on commit 263f1c4

Please sign in to comment.