Skip to content

Commit

Permalink
add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
turbolent committed Aug 10, 2024
1 parent c8d1209 commit 31f8c09
Showing 1 changed file with 80 additions and 1 deletion.
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 31f8c09

Please sign in to comment.