Skip to content

Commit 6a254ed

Browse files
authored
Merge pull request #6571 from onflow/bastian/move-cadence-crypto-contract-onchain
Update to Cadence v1.2.1, Move Cadence `Crypto` contract on-chain
2 parents 14c5eee + 27f56e0 commit 6a254ed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

48 files changed

+438
-279
lines changed

cmd/bootstrap/utils/key_generation.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313

1414
sdk "github.com/onflow/flow-go-sdk"
1515
sdkcrypto "github.com/onflow/flow-go-sdk/crypto"
16+
1617
"github.com/onflow/flow-go/fvm/systemcontracts"
1718
"github.com/onflow/flow-go/model/bootstrap"
1819
model "github.com/onflow/flow-go/model/bootstrap"

cmd/collection/main.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99

1010
client "github.com/onflow/flow-go-sdk/access/grpc"
1111
sdkcrypto "github.com/onflow/flow-go-sdk/crypto"
12+
1213
"github.com/onflow/flow-go/admin/commands"
1314
collectionCommands "github.com/onflow/flow-go/admin/commands/collection"
1415
storageCommands "github.com/onflow/flow-go/admin/commands/storage"

cmd/consensus/main.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212

1313
client "github.com/onflow/flow-go-sdk/access/grpc"
1414
"github.com/onflow/flow-go-sdk/crypto"
15+
1516
"github.com/onflow/flow-go/cmd"
1617
"github.com/onflow/flow-go/cmd/util/cmd/common"
1718
"github.com/onflow/flow-go/consensus"

cmd/util/cmd/common/snapshot.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,10 @@ import (
99
"github.com/rs/zerolog"
1010
"github.com/sethvargo/go-retry"
1111

12+
"github.com/onflow/flow-go-sdk/access/grpc"
13+
1214
"github.com/onflow/flow-go/utils/logging"
1315

14-
"github.com/onflow/flow-go-sdk/access/grpc"
1516
"github.com/onflow/flow-go/model/flow"
1617
"github.com/onflow/flow-go/state/protocol"
1718
"github.com/onflow/flow-go/state/protocol/inmem"

cmd/util/ledger/migrations/account_based_migration_test.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package migrations
33
import (
44
"context"
55
"fmt"
6-
76
"testing"
87

98
"github.com/onflow/cadence/common"

cmd/util/ledger/migrations/migrator_runtime.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import (
1616
"github.com/onflow/flow-go/fvm/evm"
1717
evmStdlib "github.com/onflow/flow-go/fvm/evm/stdlib"
1818
"github.com/onflow/flow-go/fvm/storage/state"
19+
"github.com/onflow/flow-go/fvm/systemcontracts"
1920
"github.com/onflow/flow-go/model/flow"
2021
)
2122

@@ -111,8 +112,11 @@ func (c InterpreterMigrationRuntimeConfig) NewRuntimeInterface(
111112
}
112113
}
113114

115+
sc := systemcontracts.SystemContractsForChain(chainID)
116+
114117
return util.NewMigrationRuntimeInterface(
115118
chainID,
119+
common.Address(sc.Crypto.Address),
116120
getCodeFunc,
117121
getContractNames,
118122
getOrLoadProgram,

cmd/util/ledger/util/migration_runtime_interface.go

Lines changed: 9 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ type GerOrLoadProgramListenerFunc func(
3838
type MigrationRuntimeInterface struct {
3939
runtime.EmptyRuntimeInterface
4040
chainID flow.ChainID
41+
CryptoContractAddress common.Address
4142
GetContractCodeFunc GetContractCodeFunc
4243
GetContractNamesFunc GetContractNamesFunc
4344
GetOrLoadProgramFunc GetOrLoadProgramFunc
@@ -48,13 +49,15 @@ var _ runtime.Interface = &MigrationRuntimeInterface{}
4849

4950
func NewMigrationRuntimeInterface(
5051
chainID flow.ChainID,
52+
cryptoContractAddress common.Address,
5153
getCodeFunc GetContractCodeFunc,
5254
getContractNamesFunc GetContractNamesFunc,
5355
getOrLoadProgramFunc GetOrLoadProgramFunc,
5456
getOrLoadProgramListenerFunc GerOrLoadProgramListenerFunc,
5557
) *MigrationRuntimeInterface {
5658
return &MigrationRuntimeInterface{
5759
chainID: chainID,
60+
CryptoContractAddress: cryptoContractAddress,
5861
GetContractCodeFunc: getCodeFunc,
5962
GetContractNamesFunc: getContractNamesFunc,
6063
GetOrLoadProgramFunc: getOrLoadProgramFunc,
@@ -67,65 +70,12 @@ func (m *MigrationRuntimeInterface) ResolveLocation(
6770
location runtime.Location,
6871
) ([]runtime.ResolvedLocation, error) {
6972

70-
addressLocation, isAddress := location.(common.AddressLocation)
71-
72-
// if the location is not an address location, e.g. an identifier location (`import Crypto`),
73-
// then return a single resolved location which declares all identifiers.
74-
if !isAddress {
75-
return []runtime.ResolvedLocation{
76-
{
77-
Location: location,
78-
Identifiers: identifiers,
79-
},
80-
}, nil
81-
}
82-
83-
// if the location is an address,
84-
// and no specific identifiers where requested in the import statement,
85-
// then fetch all identifiers at this address
86-
if len(identifiers) == 0 {
87-
address := flow.Address(addressLocation.Address)
88-
89-
getContractNames := m.GetContractNamesFunc
90-
if getContractNames == nil {
91-
return nil, errors.New("GetContractNamesFunc missing")
92-
}
93-
94-
contractNames, err := getContractNames(address)
95-
if err != nil {
96-
return nil, fmt.Errorf("ResolveLocation failed: %w", err)
97-
}
98-
99-
// if there are no contractNames deployed,
100-
// then return no resolved locations
101-
if len(contractNames) == 0 {
102-
return nil, nil
103-
}
104-
105-
identifiers = make([]runtime.Identifier, len(contractNames))
106-
107-
for i := range identifiers {
108-
identifiers[i] = runtime.Identifier{
109-
Identifier: contractNames[i],
110-
}
111-
}
112-
}
113-
114-
// return one resolved location per identifier.
115-
// each resolved location is an address contract location
116-
resolvedLocations := make([]runtime.ResolvedLocation, len(identifiers))
117-
for i := range resolvedLocations {
118-
identifier := identifiers[i]
119-
resolvedLocations[i] = runtime.ResolvedLocation{
120-
Location: common.AddressLocation{
121-
Address: addressLocation.Address,
122-
Name: identifier.Identifier,
123-
},
124-
Identifiers: []runtime.Identifier{identifier},
125-
}
126-
}
127-
128-
return resolvedLocations, nil
73+
return environment.ResolveLocation(
74+
identifiers,
75+
location,
76+
m.GetContractNamesFunc,
77+
m.CryptoContractAddress,
78+
)
12979
}
13080

13181
func (m *MigrationRuntimeInterface) GetCode(location runtime.Location) ([]byte, error) {

consensus/hotstuff/verification/common.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
11
package verification
22

33
import (
4+
"encoding/binary"
45
"fmt"
56

67
"github.com/onflow/crypto"
78
"github.com/onflow/crypto/hash"
89

910
"github.com/onflow/flow-go/consensus/hotstuff/model"
1011
"github.com/onflow/flow-go/model/flow"
11-
12-
"encoding/binary"
1312
)
1413

1514
// MakeVoteMessage generates the message we have to sign in order to be able

engine/execution/state/bootstrap/bootstrap_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ func TestBootstrapLedger(t *testing.T) {
5353
}
5454

5555
func TestBootstrapLedger_ZeroTokenSupply(t *testing.T) {
56-
expectedStateCommitmentBytes, _ := hex.DecodeString("edc0ffd7e797e2383bc68b694645e6b62b0d996498bcff9b492bf4d426798ec5")
56+
expectedStateCommitmentBytes, _ := hex.DecodeString("543fa7112081094b66871692c5a7784f40a9e5cdb9cfda10d1d9b81653966409")
5757
expectedStateCommitment, err := flow.ToStateCommitment(expectedStateCommitmentBytes)
5858
require.NoError(t, err)
5959

fvm/bootstrap.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -351,6 +351,7 @@ func (b *bootstrapExecutor) Execute() error {
351351

352352
b.deployViewResolver(service, &env)
353353
b.deployBurner(service, &env)
354+
b.deployCrypto(service, &env)
354355

355356
err = expectAccounts(1)
356357
if err != nil {
@@ -523,6 +524,22 @@ func (b *bootstrapExecutor) deployBurner(deployTo flow.Address, env *templates.E
523524
panicOnMetaInvokeErrf("failed to deploy burner contract: %s", txError, err)
524525
}
525526

527+
func (b *bootstrapExecutor) deployCrypto(deployTo flow.Address, env *templates.Environment) {
528+
contract := contracts.Crypto()
529+
530+
txError, err := b.invokeMetaTransaction(
531+
b.ctx,
532+
Transaction(
533+
blueprints.DeployContractTransaction(
534+
deployTo,
535+
contract,
536+
"Crypto"),
537+
0),
538+
)
539+
env.CryptoAddress = deployTo.String()
540+
panicOnMetaInvokeErrf("failed to deploy crypto contract: %s", txError, err)
541+
}
542+
526543
func (b *bootstrapExecutor) deployMetadataViews(fungibleToken, nonFungibleToken flow.Address, env *templates.Environment) {
527544

528545
mvContract := contracts.MetadataViews(*env)

fvm/crypto/hash_test.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,9 @@ package crypto_test
22

33
import (
44
"crypto/rand"
5-
"testing"
6-
75
"crypto/sha256"
86
"crypto/sha512"
7+
"testing"
98

109
"github.com/onflow/crypto/hash"
1110
"github.com/stretchr/testify/assert"

fvm/environment/contract_reader.go

Lines changed: 42 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"github.com/onflow/cadence/ast"
77
"github.com/onflow/cadence/common"
88
"github.com/onflow/cadence/runtime"
9+
"github.com/onflow/cadence/stdlib"
910

1011
"github.com/onflow/flow-go/fvm/errors"
1112
"github.com/onflow/flow-go/fvm/tracing"
@@ -15,21 +16,23 @@ import (
1516

1617
// ContractReader provide read access to contracts.
1718
type ContractReader struct {
18-
tracer tracing.TracerSpan
19-
meter Meter
20-
21-
accounts Accounts
19+
tracer tracing.TracerSpan
20+
meter Meter
21+
accounts Accounts
22+
cryptoContractAddress common.Address
2223
}
2324

2425
func NewContractReader(
2526
tracer tracing.TracerSpan,
2627
meter Meter,
2728
accounts Accounts,
29+
cryptoContractAddress common.Address,
2830
) *ContractReader {
2931
return &ContractReader{
30-
tracer: tracer,
31-
meter: meter,
32-
accounts: accounts,
32+
tracer: tracer,
33+
meter: meter,
34+
accounts: accounts,
35+
cryptoContractAddress: cryptoContractAddress,
3336
}
3437
}
3538

@@ -69,12 +72,37 @@ func (reader *ContractReader) ResolveLocation(
6972
return nil, fmt.Errorf("resolve location failed: %w", err)
7073
}
7174

75+
return ResolveLocation(
76+
identifiers,
77+
location,
78+
reader.accounts.GetContractNames,
79+
reader.cryptoContractAddress,
80+
)
81+
}
82+
83+
func ResolveLocation(
84+
identifiers []ast.Identifier,
85+
location common.Location,
86+
getContractNames func(flow.Address) ([]string, error),
87+
cryptoContractAddress common.Address,
88+
) ([]runtime.ResolvedLocation, error) {
89+
7290
addressLocation, isAddress := location.(common.AddressLocation)
7391

7492
// if the location is not an address location, e.g. an identifier location
75-
// (`import Crypto`), then return a single resolved location which declares
76-
// all identifiers.
93+
// then return a single resolved location which declares all identifiers.
7794
if !isAddress {
95+
96+
// if the location is the Crypto contract,
97+
// translate it to the address of the Crypto contract on the chain
98+
99+
if location == stdlib.CryptoContractLocation {
100+
location = common.AddressLocation{
101+
Address: cryptoContractAddress,
102+
Name: string(stdlib.CryptoContractLocation),
103+
}
104+
}
105+
78106
return []runtime.ResolvedLocation{
79107
{
80108
Location: location,
@@ -87,9 +115,13 @@ func (reader *ContractReader) ResolveLocation(
87115
// and no specific identifiers where requested in the import statement,
88116
// then fetch all identifiers at this address
89117
if len(identifiers) == 0 {
118+
if getContractNames == nil {
119+
return nil, fmt.Errorf("no identifiers provided")
120+
}
121+
90122
address := flow.ConvertAddress(addressLocation.Address)
91123

92-
contractNames, err := reader.accounts.GetContractNames(address)
124+
contractNames, err := getContractNames(address)
93125
if err != nil {
94126
return nil, fmt.Errorf("resolving location failed: %w", err)
95127
}

fvm/environment/facade_env.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"github.com/onflow/flow-go/fvm/storage"
1212
"github.com/onflow/flow-go/fvm/storage/snapshot"
1313
"github.com/onflow/flow-go/fvm/storage/state"
14+
"github.com/onflow/flow-go/fvm/systemcontracts"
1415
"github.com/onflow/flow-go/fvm/tracing"
1516
)
1617

@@ -63,12 +64,15 @@ func newFacadeEnvironment(
6364
accounts := NewAccounts(txnState)
6465
logger := NewProgramLogger(tracer, params.ProgramLoggerParams)
6566
runtime := NewRuntime(params.RuntimeParams)
67+
chain := params.Chain
6668
systemContracts := NewSystemContracts(
67-
params.Chain,
69+
chain,
6870
tracer,
6971
logger,
7072
runtime)
7173

74+
sc := systemcontracts.SystemContractsForChain(chain.ChainID())
75+
7276
env := &facadeEnvironment{
7377
Runtime: runtime,
7478

@@ -130,6 +134,7 @@ func newFacadeEnvironment(
130134
tracer,
131135
meter,
132136
accounts,
137+
common.Address(sc.Crypto.Address),
133138
),
134139
ContractUpdater: NoContractUpdater{},
135140
Programs: NewPrograms(

0 commit comments

Comments
 (0)