Skip to content

Commit df33ab8

Browse files
authored
Merge pull request #3619 from onflow/bastian/3135-move-crypto-contract-onchain
2 parents 0b1fc2b + 7f1b6fb commit df33ab8

File tree

15 files changed

+225
-919
lines changed

15 files changed

+225
-919
lines changed

.github/workflows/ci.yml

-6
Original file line numberDiff line numberDiff line change
@@ -39,18 +39,12 @@ jobs:
3939
go-version: ${{ env.GO_VERSION }}
4040
cache: true
4141

42-
- name: Install Flow CLI
43-
run: sh -ci "$(curl -fsSL https://raw.githubusercontent.com/onflow/flow-cli/master/install.sh)"
44-
4542
- name: Build
4643
run: make -j8 build
4744

4845
- name: Test
4946
run: make ci
5047

51-
- name: Cadence Testing Framework
52-
run: cd stdlib/contracts && flow test --cover --covercode="contracts" crypto_test.cdc
53-
5448
- name: Upload coverage report
5549
uses: codecov/codecov-action@v2
5650
with:

cmd/cmd.go

-7
Original file line numberDiff line numberDiff line change
@@ -97,13 +97,6 @@ func DefaultCheckerConfig(
9797
importedLocation common.Location,
9898
_ ast.Range,
9999
) (sema.Import, error) {
100-
if importedLocation == stdlib.CryptoCheckerLocation {
101-
cryptoChecker := stdlib.CryptoChecker()
102-
return sema.ElaborationImport{
103-
Elaboration: cryptoChecker.Elaboration,
104-
}, nil
105-
}
106-
107100
stringLocation, ok := importedLocation.(common.StringLocation)
108101
if !ok {
109102
return nil, &sema.CheckerError{

runtime/environment.go

+73-117
Original file line numberDiff line numberDiff line change
@@ -674,40 +674,29 @@ func (e *interpreterEnvironment) resolveImport(
674674
importRange ast.Range,
675675
) (sema.Import, error) {
676676

677-
var elaboration *sema.Elaboration
678-
switch importedLocation {
679-
case stdlib.CryptoCheckerLocation:
680-
cryptoChecker := stdlib.CryptoChecker()
681-
elaboration = cryptoChecker.Elaboration
682-
683-
default:
684-
685-
// Check for cyclic imports
686-
if e.checkedImports[importedLocation] {
687-
return nil, &sema.CyclicImportsError{
688-
Location: importedLocation,
689-
Range: importRange,
690-
}
691-
} else {
692-
e.checkedImports[importedLocation] = true
693-
defer delete(e.checkedImports, importedLocation)
694-
}
695-
696-
const getAndSetProgram = true
697-
program, err := e.GetProgram(
698-
importedLocation,
699-
getAndSetProgram,
700-
e.checkedImports,
701-
)
702-
if err != nil {
703-
return nil, err
677+
// Check for cyclic imports
678+
if e.checkedImports[importedLocation] {
679+
return nil, &sema.CyclicImportsError{
680+
Location: importedLocation,
681+
Range: importRange,
704682
}
683+
} else {
684+
e.checkedImports[importedLocation] = true
685+
defer delete(e.checkedImports, importedLocation)
686+
}
705687

706-
elaboration = program.Elaboration
688+
const getAndSetProgram = true
689+
program, err := e.GetProgram(
690+
importedLocation,
691+
getAndSetProgram,
692+
e.checkedImports,
693+
)
694+
if err != nil {
695+
return nil, err
707696
}
708697

709698
return sema.ElaborationImport{
710-
Elaboration: elaboration,
699+
Elaboration: program.Elaboration,
711700
}, nil
712701
}
713702

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

1009-
switch location {
1010-
case stdlib.CryptoCheckerLocation:
1011-
return nil
998+
switch compositeKind {
999+
case common.CompositeKindContract:
1000+
var address Address
10121001

1013-
default:
1014-
switch compositeKind {
1015-
case common.CompositeKindContract:
1016-
var address Address
1017-
1018-
switch location := location.(type) {
1019-
case common.AddressLocation:
1020-
address = location.Address
1021-
default:
1022-
return nil
1023-
}
1002+
switch location := location.(type) {
1003+
case common.AddressLocation:
1004+
address = location.Address
1005+
default:
1006+
return nil
1007+
}
10241008

1025-
addressValue := interpreter.NewAddressValue(
1026-
inter,
1027-
address,
1028-
)
1009+
addressValue := interpreter.NewAddressValue(
1010+
inter,
1011+
address,
1012+
)
10291013

1030-
return map[string]interpreter.Value{
1031-
sema.ContractAccountFieldName: stdlib.NewAccountReferenceValue(
1032-
inter,
1033-
e,
1034-
addressValue,
1035-
interpreter.FullyEntitledAccountAccess,
1036-
interpreter.EmptyLocationRange,
1037-
),
1038-
}
1014+
return map[string]interpreter.Value{
1015+
sema.ContractAccountFieldName: stdlib.NewAccountReferenceValue(
1016+
inter,
1017+
e,
1018+
addressValue,
1019+
interpreter.FullyEntitledAccountAccess,
1020+
interpreter.EmptyLocationRange,
1021+
),
10391022
}
10401023
}
10411024

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

1049-
switch location {
1050-
case stdlib.CryptoCheckerLocation:
1051-
cryptoChecker := stdlib.CryptoChecker()
1052-
program := interpreter.ProgramFromChecker(cryptoChecker)
1053-
subInterpreter, err := inter.NewSubInterpreter(program, location)
1054-
if err != nil {
1055-
panic(err)
1056-
}
1057-
return interpreter.InterpreterImport{
1058-
Interpreter: subInterpreter,
1059-
}
1060-
1061-
default:
1062-
const getAndSetProgram = true
1063-
program, err := e.GetProgram(
1064-
location,
1065-
getAndSetProgram,
1066-
importResolutionResults{},
1067-
)
1068-
if err != nil {
1069-
panic(err)
1070-
}
1032+
const getAndSetProgram = true
1033+
program, err := e.GetProgram(
1034+
location,
1035+
getAndSetProgram,
1036+
importResolutionResults{},
1037+
)
1038+
if err != nil {
1039+
panic(err)
1040+
}
10711041

1072-
subInterpreter, err := inter.NewSubInterpreter(program, location)
1073-
if err != nil {
1074-
panic(err)
1075-
}
1076-
return interpreter.InterpreterImport{
1077-
Interpreter: subInterpreter,
1078-
}
1042+
subInterpreter, err := inter.NewSubInterpreter(program, location)
1043+
if err != nil {
1044+
panic(err)
1045+
}
1046+
return interpreter.InterpreterImport{
1047+
Interpreter: subInterpreter,
10791048
}
10801049
}
10811050
}
@@ -1123,45 +1092,32 @@ func (e *interpreterEnvironment) newCompositeValueFunctionsHandler() interpreter
11231092
func (e *interpreterEnvironment) loadContract(
11241093
inter *interpreter.Interpreter,
11251094
compositeType *sema.CompositeType,
1126-
constructorGenerator func(common.Address) *interpreter.HostFunctionValue,
1127-
invocationRange ast.Range,
1095+
_ func(common.Address) *interpreter.HostFunctionValue,
1096+
_ ast.Range,
11281097
) *interpreter.CompositeValue {
11291098

1130-
switch compositeType.Location {
1131-
case stdlib.CryptoCheckerLocation:
1132-
contract, err := stdlib.NewCryptoContract(
1133-
inter,
1134-
constructorGenerator(common.ZeroAddress),
1135-
invocationRange,
1136-
)
1137-
if err != nil {
1138-
panic(err)
1139-
}
1140-
return contract
1141-
1142-
default:
1143-
1144-
var storedValue interpreter.Value
1145-
1146-
switch location := compositeType.Location.(type) {
1099+
var contractValue interpreter.Value
11471100

1148-
case common.AddressLocation:
1149-
storageMap := e.storage.GetStorageMap(
1150-
location.Address,
1151-
StorageDomainContract,
1152-
false,
1101+
location := compositeType.Location
1102+
if addressLocation, ok := location.(common.AddressLocation); ok {
1103+
storageMap := e.storage.GetStorageMap(
1104+
addressLocation.Address,
1105+
StorageDomainContract,
1106+
false,
1107+
)
1108+
if storageMap != nil {
1109+
contractValue = storageMap.ReadValue(
1110+
inter,
1111+
interpreter.StringStorageMapKey(addressLocation.Name),
11531112
)
1154-
if storageMap != nil {
1155-
storedValue = storageMap.ReadValue(inter, interpreter.StringStorageMapKey(location.Name))
1156-
}
1157-
}
1158-
1159-
if storedValue == nil {
1160-
panic(errors.NewDefaultUserError("failed to load contract: %s", compositeType.Location))
11611113
}
1114+
}
11621115

1163-
return storedValue.(*interpreter.CompositeValue)
1116+
if contractValue == nil {
1117+
panic(errors.NewDefaultUserError("failed to load contract: %s", location))
11641118
}
1119+
1120+
return contractValue.(*interpreter.CompositeValue)
11651121
}
11661122

11671123
func (e *interpreterEnvironment) newOnFunctionInvocationHandler() func(_ *interpreter.Interpreter) {

0 commit comments

Comments
 (0)