Skip to content

Commit

Permalink
Merge pull request onflow#6509 from onflow/auto-update-onflow-cadence…
Browse files Browse the repository at this point in the history
…-v1.0.0

Update to Cadence v1.0.0
  • Loading branch information
turbolent authored Sep 27, 2024
2 parents c6decd3 + 68f60a0 commit 79d1fe6
Show file tree
Hide file tree
Showing 11 changed files with 262 additions and 33 deletions.
5 changes: 5 additions & 0 deletions cmd/util/cmd/generate-authorization-fixes/cmd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,11 @@ var _ reporters.ReportWriter = &testReportWriter{}
func TestGenerateAuthorizationFixes(t *testing.T) {
t.Parallel()

// This test no longer works because publishing authorized capabilities is no longer allowed.
// The migration and test are kept for historical reasons.

t.Skip()

const chainID = flow.Emulator
chain := chainID.Chain()

Expand Down
20 changes: 5 additions & 15 deletions cmd/util/ledger/migrations/fix_authorizations_migration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,31 +9,21 @@ import (
jsoncdc "github.com/onflow/cadence/encoding/json"
"github.com/onflow/cadence/runtime/common"
"github.com/onflow/cadence/runtime/interpreter"
"github.com/onflow/cadence/runtime/sema"
"github.com/rs/zerolog"
"github.com/stretchr/testify/require"

"github.com/onflow/flow-go/cmd/util/ledger/util/registers"
"github.com/onflow/flow-go/model/flow"
)

func newEntitlementSetAuthorizationFromTypeIDs(
typeIDs []common.TypeID,
setKind sema.EntitlementSetKind,
) interpreter.EntitlementSetAuthorization {
return interpreter.NewEntitlementSetAuthorization(
nil,
func() []common.TypeID {
return typeIDs
},
len(typeIDs),
setKind,
)
}

func TestFixAuthorizationsMigration(t *testing.T) {
t.Parallel()

// This test no longer works because publishing authorized capabilities is no longer allowed.
// The migration and test are kept for historical reasons.

t.Skip()

const chainID = flow.Emulator
chain := chainID.Chain()

Expand Down
35 changes: 35 additions & 0 deletions fvm/environment/facade_env.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"github.com/onflow/cadence/runtime/ast"
"github.com/onflow/cadence/runtime/common"
"github.com/onflow/cadence/runtime/interpreter"
"github.com/onflow/cadence/runtime/sema"

"github.com/onflow/flow-go/fvm/storage"
"github.com/onflow/flow-go/fvm/storage/snapshot"
Expand Down Expand Up @@ -343,3 +344,37 @@ func (env *facadeEnvironment) RecoverProgram(program *ast.Program, location comm
location,
)
}

func (env *facadeEnvironment) ValidateAccountCapabilitiesGet(
_ *interpreter.Interpreter,
_ interpreter.LocationRange,
_ interpreter.AddressValue,
_ interpreter.PathValue,
wantedBorrowType *sema.ReferenceType,
_ *sema.ReferenceType,
) (bool, error) {
_, hasEntitlements := wantedBorrowType.Authorization.(sema.EntitlementSetAccess)
if hasEntitlements {
// TODO: maybe abort
//return false, interpreter.GetCapabilityError{
// LocationRange: locationRange,
//}
return false, nil
}
return true, nil
}

func (env *facadeEnvironment) ValidateAccountCapabilitiesPublish(
_ *interpreter.Interpreter,
_ interpreter.LocationRange,
_ interpreter.AddressValue,
_ interpreter.PathValue,
capabilityBorrowType *interpreter.ReferenceStaticType,
) (bool, error) {
_, isEntitledCapability := capabilityBorrowType.Authorization.(interpreter.EntitlementSetAuthorization)
if isEntitledCapability {
// TODO: maybe abort
return false, nil
}
return true, nil
}
56 changes: 56 additions & 0 deletions fvm/environment/mock/environment.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

143 changes: 143 additions & 0 deletions fvm/fvm_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package fvm_test

import (
"context"
"crypto/rand"
"encoding/hex"
"fmt"
Expand All @@ -19,6 +20,8 @@ import (
"github.com/onflow/cadence/runtime"
"github.com/onflow/cadence/runtime/common"
cadenceErrors "github.com/onflow/cadence/runtime/errors"
"github.com/onflow/cadence/runtime/interpreter"
"github.com/onflow/cadence/runtime/sema"
"github.com/onflow/cadence/runtime/tests/utils"
"github.com/onflow/crypto"
"github.com/stretchr/testify/assert"
Expand All @@ -39,6 +42,7 @@ import (
"github.com/onflow/flow-go/fvm/storage/snapshot/mock"
"github.com/onflow/flow-go/fvm/storage/testutils"
"github.com/onflow/flow-go/fvm/systemcontracts"
"github.com/onflow/flow-go/fvm/tracing"
"github.com/onflow/flow-go/model/flow"
"github.com/onflow/flow-go/utils/unittest"
)
Expand Down Expand Up @@ -3086,3 +3090,142 @@ func TestEVM(t *testing.T) {
}),
)
}

func TestAccountCapabilitiesGetEntitledRejection(t *testing.T) {

// Note: This cannot be tested anymore using a transaction,
// because publish method also aborts when trying to publish an entitled capability.
// Therefore, test the functionality of the `ValidateAccountCapabilitiesGet` function.

t.Run("entitled capability", func(t *testing.T) {

env := environment.NewScriptEnv(
context.TODO(),
tracing.NewMockTracerSpan(),
environment.DefaultEnvironmentParams(),
nil,
)

valid, err := env.ValidateAccountCapabilitiesGet(
nil,
interpreter.EmptyLocationRange,
interpreter.AddressValue(common.ZeroAddress),
interpreter.NewUnmeteredPathValue(common.PathDomainPublic, "dummy_value"),
sema.NewReferenceType(
nil,
sema.NewEntitlementSetAccess(
[]*sema.EntitlementType{
sema.MutateType,
},
sema.Conjunction,
),
sema.IntType,
),
nil,
)
assert.NoError(t, err)
assert.False(t, valid)
})

t.Run("non-entitled capability", func(t *testing.T) {

env := environment.NewScriptEnv(
context.TODO(),
tracing.NewMockTracerSpan(),
environment.DefaultEnvironmentParams(),
nil,
)

valid, err := env.ValidateAccountCapabilitiesGet(
nil,
interpreter.EmptyLocationRange,
interpreter.AddressValue(common.ZeroAddress),
interpreter.NewUnmeteredPathValue(common.PathDomainPublic, "dummy_value"),
sema.NewReferenceType(
nil,
sema.UnauthorizedAccess,
sema.IntType,
),
nil,
)
assert.NoError(t, err)
assert.True(t, valid)
})
}

func TestAccountCapabilitiesPublishEntitledRejection(t *testing.T) {

t.Run("entitled capability", newVMTest().
run(func(
t *testing.T,
vm fvm.VM,
chain flow.Chain,
ctx fvm.Context,
snapshotTree snapshot.SnapshotTree,
) {

serviceAddress := chain.ServiceAddress()
txBody := flow.NewTransactionBody().
SetScript([]byte(`
transaction {
prepare(signer: auth(Capabilities, Storage) &Account) {
signer.storage.save(42, to: /storage/number)
let cap = signer.capabilities.storage.issue<auth(Insert) &Int>(/storage/number)
signer.capabilities.publish(cap, at: /public/number)
}
}
`)).
AddAuthorizer(serviceAddress).
SetProposalKey(serviceAddress, 0, 0).
SetPayer(serviceAddress)

err := testutil.SignTransactionAsServiceAccount(txBody, 0, chain)
require.NoError(t, err)

_, output, err := vm.Run(
ctx,
fvm.Transaction(txBody, 0),
snapshotTree)

require.NoError(t, err)
require.ErrorAs(t, output.Err, &interpreter.EntitledCapabilityPublishingError{})
}),
)

t.Run("non entitled capability", newVMTest().
run(func(
t *testing.T,
vm fvm.VM,
chain flow.Chain,
ctx fvm.Context,
snapshotTree snapshot.SnapshotTree,
) {

serviceAddress := chain.ServiceAddress()
txBody := flow.NewTransactionBody().
SetScript([]byte(`
transaction {
prepare(signer: auth(Capabilities, Storage) &Account) {
signer.storage.save(42, to: /storage/number)
let cap = signer.capabilities.storage.issue<&Int>(/storage/number)
signer.capabilities.publish(cap, at: /public/number)
}
}
`)).
AddAuthorizer(serviceAddress).
SetProposalKey(serviceAddress, 0, 0).
SetPayer(serviceAddress)

err := testutil.SignTransactionAsServiceAccount(txBody, 0, chain)
require.NoError(t, err)

_, output, err := vm.Run(
ctx,
fvm.Transaction(txBody, 0),
snapshotTree)

require.NoError(t, err)
require.NoError(t, output.Err)
}),
)
}
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,12 @@ require (
github.com/multiformats/go-multiaddr-dns v0.3.1
github.com/multiformats/go-multihash v0.2.3
github.com/onflow/atree v0.8.0-rc.6
github.com/onflow/cadence v1.0.0-preview.52
github.com/onflow/cadence v1.0.0
github.com/onflow/crypto v0.25.2
github.com/onflow/flow v0.3.4
github.com/onflow/flow-core-contracts/lib/go/contracts v1.3.1
github.com/onflow/flow-core-contracts/lib/go/templates v1.3.1
github.com/onflow/flow-go-sdk v1.0.0-preview.55
github.com/onflow/flow-go-sdk v1.0.0
github.com/onflow/flow/protobuf/go/flow v0.4.7
github.com/pbnjay/memory v0.0.0-20210728143218-7b4eea64cf58
github.com/pierrec/lz4 v2.6.1+incompatible
Expand Down
8 changes: 4 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -2171,8 +2171,8 @@ github.com/onflow/atree v0.8.0-rc.6/go.mod h1:yccR+LR7xc1Jdic0mrjocbHvUD7lnVvg8/
github.com/onflow/boxo v0.0.0-20240201202436-f2477b92f483 h1:LpiQhTAfM9CAmNVEs0n//cBBgCg+vJSiIxTHYUklZ84=
github.com/onflow/boxo v0.0.0-20240201202436-f2477b92f483/go.mod h1:pIZgTWdm3k3pLF9Uq6MB8JEcW07UDwNJjlXW1HELW80=
github.com/onflow/cadence v1.0.0-M3/go.mod h1:odXGZZ/wGNA5mwT8bC9v8u8EXACHllB2ABSZK65TGL8=
github.com/onflow/cadence v1.0.0-preview.52 h1:hZ92e6lL2+PQa3C1i5jJh0zZYFdW89+X1MS0Bkd6Ayo=
github.com/onflow/cadence v1.0.0-preview.52/go.mod h1:7wvvecnAZtYOspLOS3Lh+FuAmMeSrXhAWiycC3kQ1UU=
github.com/onflow/cadence v1.0.0 h1:bvT75F2LZJvDCBmmajAv7QLISK6Qp30FAKcSwqNNH+o=
github.com/onflow/cadence v1.0.0/go.mod h1:7wvvecnAZtYOspLOS3Lh+FuAmMeSrXhAWiycC3kQ1UU=
github.com/onflow/crypto v0.25.0/go.mod h1:C8FbaX0x8y+FxWjbkHy0Q4EASCDR9bSPWZqlpCLYyVI=
github.com/onflow/crypto v0.25.2 h1:GjHunqVt+vPcdqhxxhAXiMIF3YiLX7gTuTR5O+VG2ns=
github.com/onflow/crypto v0.25.2/go.mod h1:fY7eLqUdMKV8EGOw301unP8h7PvLVy8/6gVR++/g0BY=
Expand All @@ -2187,8 +2187,8 @@ github.com/onflow/flow-ft/lib/go/contracts v1.0.0/go.mod h1:PwsL8fC81cjnUnTfmyL/
github.com/onflow/flow-ft/lib/go/templates v1.0.0 h1:6cMS/lUJJ17HjKBfMO/eh0GGvnpElPgBXx7h5aoWJhs=
github.com/onflow/flow-ft/lib/go/templates v1.0.0/go.mod h1:uQ8XFqmMK2jxyBSVrmyuwdWjTEb+6zGjRYotfDJ5pAE=
github.com/onflow/flow-go-sdk v1.0.0-M1/go.mod h1:TDW0MNuCs4SvqYRUzkbRnRmHQL1h4X8wURsCw9P9beo=
github.com/onflow/flow-go-sdk v1.0.0-preview.55 h1:tUM8K7GcWltM0YSzei/g2Gq4z3BwGFTdpq2QwvB6ubk=
github.com/onflow/flow-go-sdk v1.0.0-preview.55/go.mod h1:rBRNboXaTprn7M0MeO6/R1bxNpctbrx66I2FLp0V6fM=
github.com/onflow/flow-go-sdk v1.0.0 h1:Ha4fQm1MMKsyaqMkQLCN3rA/yaQKG6DGwiIfx06j40c=
github.com/onflow/flow-go-sdk v1.0.0/go.mod h1:iZkW2IWieVUZKK06mQCxpjJzPDgS0VtGpTaP/rKu6J4=
github.com/onflow/flow-nft/lib/go/contracts v1.2.1 h1:woAAS5z651sDpi7ihAHll8NvRS9uFXIXkL6xR+bKFZY=
github.com/onflow/flow-nft/lib/go/contracts v1.2.1/go.mod h1:2gpbza+uzs1k7x31hkpBPlggIRkI53Suo0n2AyA2HcE=
github.com/onflow/flow-nft/lib/go/templates v1.2.0 h1:JSQyh9rg0RC+D1930BiRXN8lrtMs+ubVMK6aQPon6Yc=
Expand Down
4 changes: 2 additions & 2 deletions insecure/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -203,12 +203,12 @@ require (
github.com/nxadm/tail v1.4.8 // indirect
github.com/olekukonko/tablewriter v0.0.5 // indirect
github.com/onflow/atree v0.8.0-rc.6 // indirect
github.com/onflow/cadence v1.0.0-preview.52 // indirect
github.com/onflow/cadence v1.0.0 // indirect
github.com/onflow/flow-core-contracts/lib/go/contracts v1.3.1 // indirect
github.com/onflow/flow-core-contracts/lib/go/templates v1.3.1 // indirect
github.com/onflow/flow-ft/lib/go/contracts v1.0.0 // indirect
github.com/onflow/flow-ft/lib/go/templates v1.0.0 // indirect
github.com/onflow/flow-go-sdk v1.0.0-preview.55 // indirect
github.com/onflow/flow-go-sdk v1.0.0 // indirect
github.com/onflow/flow-nft/lib/go/contracts v1.2.1 // indirect
github.com/onflow/flow-nft/lib/go/templates v1.2.0 // indirect
github.com/onflow/flow/protobuf/go/flow v0.4.7 // indirect
Expand Down
8 changes: 4 additions & 4 deletions insecure/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -2159,8 +2159,8 @@ github.com/onflow/atree v0.6.1-0.20230711151834-86040b30171f/go.mod h1:xvP61FoOs
github.com/onflow/atree v0.8.0-rc.6 h1:GWgaylK24b5ta2Hq+TvyOF7X5tZLiLzMMn7lEt59fsA=
github.com/onflow/atree v0.8.0-rc.6/go.mod h1:yccR+LR7xc1Jdic0mrjocbHvUD7lnVvg8/Ct1AA5zBo=
github.com/onflow/cadence v1.0.0-M3/go.mod h1:odXGZZ/wGNA5mwT8bC9v8u8EXACHllB2ABSZK65TGL8=
github.com/onflow/cadence v1.0.0-preview.52 h1:hZ92e6lL2+PQa3C1i5jJh0zZYFdW89+X1MS0Bkd6Ayo=
github.com/onflow/cadence v1.0.0-preview.52/go.mod h1:7wvvecnAZtYOspLOS3Lh+FuAmMeSrXhAWiycC3kQ1UU=
github.com/onflow/cadence v1.0.0 h1:bvT75F2LZJvDCBmmajAv7QLISK6Qp30FAKcSwqNNH+o=
github.com/onflow/cadence v1.0.0/go.mod h1:7wvvecnAZtYOspLOS3Lh+FuAmMeSrXhAWiycC3kQ1UU=
github.com/onflow/crypto v0.25.0/go.mod h1:C8FbaX0x8y+FxWjbkHy0Q4EASCDR9bSPWZqlpCLYyVI=
github.com/onflow/crypto v0.25.2 h1:GjHunqVt+vPcdqhxxhAXiMIF3YiLX7gTuTR5O+VG2ns=
github.com/onflow/crypto v0.25.2/go.mod h1:fY7eLqUdMKV8EGOw301unP8h7PvLVy8/6gVR++/g0BY=
Expand All @@ -2173,8 +2173,8 @@ github.com/onflow/flow-ft/lib/go/contracts v1.0.0/go.mod h1:PwsL8fC81cjnUnTfmyL/
github.com/onflow/flow-ft/lib/go/templates v1.0.0 h1:6cMS/lUJJ17HjKBfMO/eh0GGvnpElPgBXx7h5aoWJhs=
github.com/onflow/flow-ft/lib/go/templates v1.0.0/go.mod h1:uQ8XFqmMK2jxyBSVrmyuwdWjTEb+6zGjRYotfDJ5pAE=
github.com/onflow/flow-go-sdk v1.0.0-M1/go.mod h1:TDW0MNuCs4SvqYRUzkbRnRmHQL1h4X8wURsCw9P9beo=
github.com/onflow/flow-go-sdk v1.0.0-preview.55 h1:tUM8K7GcWltM0YSzei/g2Gq4z3BwGFTdpq2QwvB6ubk=
github.com/onflow/flow-go-sdk v1.0.0-preview.55/go.mod h1:rBRNboXaTprn7M0MeO6/R1bxNpctbrx66I2FLp0V6fM=
github.com/onflow/flow-go-sdk v1.0.0 h1:Ha4fQm1MMKsyaqMkQLCN3rA/yaQKG6DGwiIfx06j40c=
github.com/onflow/flow-go-sdk v1.0.0/go.mod h1:iZkW2IWieVUZKK06mQCxpjJzPDgS0VtGpTaP/rKu6J4=
github.com/onflow/flow-nft/lib/go/contracts v1.2.1 h1:woAAS5z651sDpi7ihAHll8NvRS9uFXIXkL6xR+bKFZY=
github.com/onflow/flow-nft/lib/go/contracts v1.2.1/go.mod h1:2gpbza+uzs1k7x31hkpBPlggIRkI53Suo0n2AyA2HcE=
github.com/onflow/flow-nft/lib/go/templates v1.2.0 h1:JSQyh9rg0RC+D1930BiRXN8lrtMs+ubVMK6aQPon6Yc=
Expand Down
Loading

0 comments on commit 79d1fe6

Please sign in to comment.