Skip to content

Commit

Permalink
Add a dependency check for cadecne imports
Browse files Browse the repository at this point in the history
  • Loading branch information
janezpodhostnik committed May 14, 2024
1 parent eb44a7d commit c999af0
Show file tree
Hide file tree
Showing 10 changed files with 503 additions and 0 deletions.
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
45 changes: 45 additions & 0 deletions fvm/environment/system_contracts.go
Original file line number Diff line number Diff line change
Expand Up @@ -279,3 +279,48 @@ func (sys *SystemContracts) AccountsStorageCapacity(
},
)
}

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(
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{},
),
},
},
[]cadence.Value{
cadence.NewArray(dependenciesAddresses),
cadence.NewArray(dependenciesNames),
cadence.NewArray(authorizersAddresses),
},
)
}
Loading

0 comments on commit c999af0

Please sign in to comment.