Skip to content

Commit

Permalink
Merge pull request #3619 from onflow/bastian/3135-move-crypto-contrac…
Browse files Browse the repository at this point in the history
…t-onchain
  • Loading branch information
turbolent authored Oct 18, 2024
2 parents 0b1fc2b + 7f1b6fb commit df33ab8
Show file tree
Hide file tree
Showing 15 changed files with 225 additions and 919 deletions.
6 changes: 0 additions & 6 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -39,18 +39,12 @@ jobs:
go-version: ${{ env.GO_VERSION }}
cache: true

- name: Install Flow CLI
run: sh -ci "$(curl -fsSL https://raw.githubusercontent.com/onflow/flow-cli/master/install.sh)"

- name: Build
run: make -j8 build

- name: Test
run: make ci

- name: Cadence Testing Framework
run: cd stdlib/contracts && flow test --cover --covercode="contracts" crypto_test.cdc

- name: Upload coverage report
uses: codecov/codecov-action@v2
with:
Expand Down
7 changes: 0 additions & 7 deletions cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,13 +97,6 @@ func DefaultCheckerConfig(
importedLocation common.Location,
_ ast.Range,
) (sema.Import, error) {
if importedLocation == stdlib.CryptoCheckerLocation {
cryptoChecker := stdlib.CryptoChecker()
return sema.ElaborationImport{
Elaboration: cryptoChecker.Elaboration,
}, nil
}

stringLocation, ok := importedLocation.(common.StringLocation)
if !ok {
return nil, &sema.CheckerError{
Expand Down
190 changes: 73 additions & 117 deletions runtime/environment.go
Original file line number Diff line number Diff line change
Expand Up @@ -674,40 +674,29 @@ func (e *interpreterEnvironment) resolveImport(
importRange ast.Range,
) (sema.Import, error) {

var elaboration *sema.Elaboration
switch importedLocation {
case stdlib.CryptoCheckerLocation:
cryptoChecker := stdlib.CryptoChecker()
elaboration = cryptoChecker.Elaboration

default:

// Check for cyclic imports
if e.checkedImports[importedLocation] {
return nil, &sema.CyclicImportsError{
Location: importedLocation,
Range: importRange,
}
} else {
e.checkedImports[importedLocation] = true
defer delete(e.checkedImports, importedLocation)
}

const getAndSetProgram = true
program, err := e.GetProgram(
importedLocation,
getAndSetProgram,
e.checkedImports,
)
if err != nil {
return nil, err
// Check for cyclic imports
if e.checkedImports[importedLocation] {
return nil, &sema.CyclicImportsError{
Location: importedLocation,
Range: importRange,
}
} else {
e.checkedImports[importedLocation] = true
defer delete(e.checkedImports, importedLocation)
}

elaboration = program.Elaboration
const getAndSetProgram = true
program, err := e.GetProgram(
importedLocation,
getAndSetProgram,
e.checkedImports,
)
if err != nil {
return nil, err
}

return sema.ElaborationImport{
Elaboration: elaboration,
Elaboration: program.Elaboration,
}, nil
}

Expand Down Expand Up @@ -1006,36 +995,30 @@ func (e *interpreterEnvironment) newInjectedCompositeFieldsHandler() interpreter
compositeKind common.CompositeKind,
) map[string]interpreter.Value {

switch location {
case stdlib.CryptoCheckerLocation:
return nil
switch compositeKind {
case common.CompositeKindContract:
var address Address

default:
switch compositeKind {
case common.CompositeKindContract:
var address Address

switch location := location.(type) {
case common.AddressLocation:
address = location.Address
default:
return nil
}
switch location := location.(type) {
case common.AddressLocation:
address = location.Address
default:
return nil
}

addressValue := interpreter.NewAddressValue(
inter,
address,
)
addressValue := interpreter.NewAddressValue(
inter,
address,
)

return map[string]interpreter.Value{
sema.ContractAccountFieldName: stdlib.NewAccountReferenceValue(
inter,
e,
addressValue,
interpreter.FullyEntitledAccountAccess,
interpreter.EmptyLocationRange,
),
}
return map[string]interpreter.Value{
sema.ContractAccountFieldName: stdlib.NewAccountReferenceValue(
inter,
e,
addressValue,
interpreter.FullyEntitledAccountAccess,
interpreter.EmptyLocationRange,
),
}
}

Expand All @@ -1046,36 +1029,22 @@ func (e *interpreterEnvironment) newInjectedCompositeFieldsHandler() interpreter
func (e *interpreterEnvironment) newImportLocationHandler() interpreter.ImportLocationHandlerFunc {
return func(inter *interpreter.Interpreter, location common.Location) interpreter.Import {

switch location {
case stdlib.CryptoCheckerLocation:
cryptoChecker := stdlib.CryptoChecker()
program := interpreter.ProgramFromChecker(cryptoChecker)
subInterpreter, err := inter.NewSubInterpreter(program, location)
if err != nil {
panic(err)
}
return interpreter.InterpreterImport{
Interpreter: subInterpreter,
}

default:
const getAndSetProgram = true
program, err := e.GetProgram(
location,
getAndSetProgram,
importResolutionResults{},
)
if err != nil {
panic(err)
}
const getAndSetProgram = true
program, err := e.GetProgram(
location,
getAndSetProgram,
importResolutionResults{},
)
if err != nil {
panic(err)
}

subInterpreter, err := inter.NewSubInterpreter(program, location)
if err != nil {
panic(err)
}
return interpreter.InterpreterImport{
Interpreter: subInterpreter,
}
subInterpreter, err := inter.NewSubInterpreter(program, location)
if err != nil {
panic(err)
}
return interpreter.InterpreterImport{
Interpreter: subInterpreter,
}
}
}
Expand Down Expand Up @@ -1123,45 +1092,32 @@ func (e *interpreterEnvironment) newCompositeValueFunctionsHandler() interpreter
func (e *interpreterEnvironment) loadContract(
inter *interpreter.Interpreter,
compositeType *sema.CompositeType,
constructorGenerator func(common.Address) *interpreter.HostFunctionValue,
invocationRange ast.Range,
_ func(common.Address) *interpreter.HostFunctionValue,
_ ast.Range,
) *interpreter.CompositeValue {

switch compositeType.Location {
case stdlib.CryptoCheckerLocation:
contract, err := stdlib.NewCryptoContract(
inter,
constructorGenerator(common.ZeroAddress),
invocationRange,
)
if err != nil {
panic(err)
}
return contract

default:

var storedValue interpreter.Value

switch location := compositeType.Location.(type) {
var contractValue interpreter.Value

case common.AddressLocation:
storageMap := e.storage.GetStorageMap(
location.Address,
StorageDomainContract,
false,
location := compositeType.Location
if addressLocation, ok := location.(common.AddressLocation); ok {
storageMap := e.storage.GetStorageMap(
addressLocation.Address,
StorageDomainContract,
false,
)
if storageMap != nil {
contractValue = storageMap.ReadValue(
inter,
interpreter.StringStorageMapKey(addressLocation.Name),
)
if storageMap != nil {
storedValue = storageMap.ReadValue(inter, interpreter.StringStorageMapKey(location.Name))
}
}

if storedValue == nil {
panic(errors.NewDefaultUserError("failed to load contract: %s", compositeType.Location))
}
}

return storedValue.(*interpreter.CompositeValue)
if contractValue == nil {
panic(errors.NewDefaultUserError("failed to load contract: %s", location))
}

return contractValue.(*interpreter.CompositeValue)
}

func (e *interpreterEnvironment) newOnFunctionInvocationHandler() func(_ *interpreter.Interpreter) {
Expand Down
Loading

0 comments on commit df33ab8

Please sign in to comment.