Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a dependency check for cadence imports #5908

Merged
merged 4 commits into from
May 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions cmd/scaffold.go
Original file line number Diff line number Diff line change
Expand Up @@ -1547,6 +1547,13 @@ func (fnb *FlowNodeBuilder) initFvmOptions() {
fvm.WithContractDeploymentRestricted(false),
)
}
// temporarily enable dependency check for testnet
if fnb.RootChainID == flow.Testnet {
vmOpts = append(vmOpts,
fvm.WithDependencyCheckEnabled(true),
)
}

fnb.FvmOptions = vmOpts
}

Expand Down
9 changes: 9 additions & 0 deletions fvm/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ type Context struct {
// limits and set them to MaxUint64, effectively disabling these limits.
DisableMemoryAndInteractionLimits bool
EVMEnabled bool
DependencyCheckEnabled bool
ComputationLimit uint64
MemoryLimit uint64
MaxStateKeySize uint64
Expand Down Expand Up @@ -193,6 +194,14 @@ func WithServiceEventCollectionEnabled() Option {
}
}

// WithDependencyCheckEnabled enables or disables the dependency check.
func WithDependencyCheckEnabled(enabled bool) Option {
return func(ctx Context) Context {
ctx.DependencyCheckEnabled = enabled
return ctx
}
}

// WithBlocks sets the block storage provider for a virtual machine context.
//
// The VM uses the block storage provider to provide historical block information to
Expand Down
6 changes: 6 additions & 0 deletions fvm/environment/env.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@ package environment
import (
"github.com/onflow/cadence"
"github.com/onflow/cadence/runtime"
"github.com/onflow/cadence/runtime/common"
"github.com/rs/zerolog"
otelTrace "go.opentelemetry.io/otel/trace"

reusableRuntime "github.com/onflow/flow-go/fvm/runtime"
"github.com/onflow/flow-go/fvm/storage/derived"
"github.com/onflow/flow-go/fvm/tracing"
"github.com/onflow/flow-go/model/flow"
"github.com/onflow/flow-go/module/trace"
Expand Down Expand Up @@ -85,6 +87,10 @@ type Environment interface {
// Reset resets all stateful environment modules (e.g., ContractUpdater,
// EventEmitter) to initial state.
Reset()

GetProgramDependencies() (derived.ProgramDependencies, error)

CheckDependencies(dependencies []common.AddressLocation, auths []flow.Address) (cadence.Value, error)
}

type EnvironmentParams struct {
Expand Down
52 changes: 52 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.

8 changes: 8 additions & 0 deletions fvm/environment/programs.go
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,14 @@ func (programs *Programs) DecodeArgument(
return v, err
}

func (programs *Programs) GetProgramDependencies() (derived.ProgramDependencies, error) {
top, err := programs.dependencyStack.top()
if err != nil {
return derived.ProgramDependencies{}, err
}
return top, nil
}

func (programs *Programs) cacheHit() {
programs.metrics.RuntimeTransactionProgramsCacheHit()
}
Expand Down
47 changes: 47 additions & 0 deletions fvm/environment/system_contracts.go
Original file line number Diff line number Diff line change
Expand Up @@ -279,3 +279,50 @@ func (sys *SystemContracts) AccountsStorageCapacity(
},
)
}

var checkDependenciesSpec = ContractFunctionSpec{
AddressFromChain: ServiceAddress,
LocationName: systemcontracts.ContractNameServiceAccount,
FunctionName: systemcontracts.ContractServiceAccountFunction_checkDependencies,
ArgumentTypes: []sema.Type{
sema.NewVariableSizedType(
nil,
&sema.AddressType{},
),
sema.NewVariableSizedType(
nil,
sema.StringType,
),
sema.NewVariableSizedType(
nil,
&sema.AddressType{},
),
},
}

func (sys *SystemContracts) CheckDependencies(
dependencies []common.AddressLocation,
authorizers []flow.Address,
) (cadence.Value, error) {

dependenciesAddresses := make([]cadence.Value, len(dependencies))
dependenciesNames := make([]cadence.Value, len(dependencies))
for i, dep := range dependencies {
dependenciesAddresses[i] = cadence.BytesToAddress(dep.Address.Bytes())
dependenciesNames[i] = cadence.String(dep.Name)
}

authorizersAddresses := make([]cadence.Value, len(authorizers))
for i, auth := range authorizers {
authorizersAddresses[i] = cadence.BytesToAddress(auth.Bytes())
}

return sys.Invoke(
checkDependenciesSpec,
[]cadence.Value{
cadence.NewArray(dependenciesAddresses),
cadence.NewArray(dependenciesNames),
cadence.NewArray(authorizersAddresses),
},
)
}
Loading
Loading