-
Notifications
You must be signed in to change notification settings - Fork 175
/
system_contracts.go
414 lines (344 loc) · 14.5 KB
/
system_contracts.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
// Package systemcontracts stores canonical address locations for all system
// smart contracts and service events.
//
// System contracts are special smart contracts controlled by the service account,
// a Flow account with special privileges to administer the network.
//
// Service events are special events defined within system contracts which
// are included within execution receipts and processed by the consensus committee
// to enable message-passing to the protocol state.
//
// For transient networks, all system contracts can be deployed to the service
// account. For long-lived networks, system contracts are spread across several
// accounts for historical reasons.
package systemcontracts
import (
"fmt"
"github.com/onflow/flow-core-contracts/lib/go/templates"
"github.com/onflow/flow-go/model/flow"
)
const (
// Unqualified names of system smart contracts (not including address prefix)
ContractNameEpoch = "FlowEpoch"
ContractNameIDTableStaking = "FlowIDTableStaking"
ContractNameClusterQC = "FlowClusterQC"
ContractNameDKG = "FlowDKG"
ContractNameServiceAccount = "FlowServiceAccount"
ContractNameFlowFees = "FlowFees"
ContractNameStorageFees = "FlowStorageFees"
ContractNameNodeVersionBeacon = "NodeVersionBeacon"
ContractNameRandomBeaconHistory = "RandomBeaconHistory"
ContractNameFungibleToken = "FungibleToken"
ContractNameFlowToken = "FlowToken"
ContractNameNonFungibleToken = "NonFungibleToken"
ContractNameMetadataViews = "MetadataViews"
ContractNameViewResolver = "ViewResolver"
ContractNameEVM = "EVM"
// AccountNameEVMStorage is not a contract, but a special account that is used to store EVM state
AccountNameEVMStorage = "EVMStorageAccount"
// Unqualified names of service events (not including address prefix or contract name)
EventNameEpochSetup = "EpochSetup"
EventNameEpochCommit = "EpochCommit"
EventNameVersionBeacon = "VersionBeacon"
// Unqualified names of service event contract functions (not including address prefix or contract name)
ContractServiceAccountFunction_setupNewAccount = "setupNewAccount"
ContractServiceAccountFunction_defaultTokenBalance = "defaultTokenBalance"
ContractServiceAccountFunction_deductTransactionFee = "deductTransactionFee"
ContractServiceAccountFunction_verifyPayersBalanceForTransactionExecution = "verifyPayersBalanceForTransactionExecution"
ContractStorageFeesFunction_calculateAccountCapacity = "calculateAccountCapacity"
ContractStorageFeesFunction_getAccountsCapacityForTransactionStorageCheck = "getAccountsCapacityForTransactionStorageCheck"
ContractStorageFeesFunction_defaultTokenAvailableBalance = "defaultTokenAvailableBalance"
// These are the account indexes of system contracts as deployed by the default bootstrapping.
// On long-running networks some of these contracts might have been deployed after bootstrapping,
// and therefore might not be at these indexes.
FungibleTokenAccountIndex = 2
FlowTokenAccountIndex = 3
FlowFeesAccountIndex = 4
EVMStorageAccountIndex = 5
// LastSystemAccountIndex is the last index of a system accounts.
// Other addresses will be created after this one.
LastSystemAccountIndex = EVMStorageAccountIndex
)
// Well-known addresses for system contracts on long-running networks.
// For now, all system contracts tracked by this package are deployed to the same
// address (per chain) as the staking contract.
//
// Ref: https://docs.onflow.org/core-contracts/staking-contract-reference/
var (
// stakingContractAddressMainnet is the address of the FlowIDTableStaking contract on Mainnet
stakingContractAddressMainnet = flow.HexToAddress("8624b52f9ddcd04a")
// stakingContractAddressTestnet is the address of the FlowIDTableStaking contract on Testnet
stakingContractAddressTestnet = flow.HexToAddress("9eca2b38b18b5dfe")
// nftTokenAddressTestnet is the address of the NonFungibleToken contract on Testnet
nftTokenAddressMainnet = flow.HexToAddress("1d7e57aa55817448")
// nftTokenAddressTestnet is the address of the NonFungibleToken contract on Testnet
nftTokenAddressTestnet = flow.HexToAddress("631e88ae7f1d7c20")
// evmStorageAddressTestnet is the address of the EVM state storage contract on Testnet
evmStorageAddressTestnet = flow.HexToAddress("1a54ed2be7552821")
// evmStorageAddressMainnet is the address of the EVM state storage contract on Mainnet
evmStorageAddressMainnet = flow.HexToAddress("d421a63faae318f9")
)
// SystemContract represents a system contract on a particular chain.
type SystemContract struct {
Address flow.Address
Name string
}
// SystemAccount represents an address used by the system.
type SystemAccount struct {
Address flow.Address
Name string
}
// ServiceEvent represents a service event on a particular chain.
type ServiceEvent struct {
Address flow.Address
ContractName string
Name string
}
// QualifiedIdentifier returns the Cadence qualified identifier of the service
// event, which includes the contract name and the event type name.
func (se ServiceEvent) QualifiedIdentifier() string {
return fmt.Sprintf("%s.%s", se.ContractName, se.Name)
}
// EventType returns the full event type identifier, including the address, the
// contract name, and the event type name.
func (se ServiceEvent) EventType() flow.EventType {
return flow.EventType(fmt.Sprintf("A.%s.%s.%s", se.Address, se.ContractName, se.Name))
}
// SystemContracts is a container for all system contracts on a particular chain.
type SystemContracts struct {
// epoch related contracts
Epoch SystemContract
IDTableStaking SystemContract
ClusterQC SystemContract
DKG SystemContract
// service account related contracts
FlowServiceAccount SystemContract
NodeVersionBeacon SystemContract
RandomBeaconHistory SystemContract
FlowStorageFees SystemContract
// token related contracts
FlowFees SystemContract
FlowToken SystemContract
FungibleToken SystemContract
FungibleTokenMetadataViews SystemContract
// NFT related contracts
NonFungibleToken SystemContract
MetadataViews SystemContract
ViewResolver SystemContract
// EVM related contracts
EVMContract SystemContract
EVMStorage SystemAccount
}
// AsTemplateEnv returns a template environment with all system contracts filled in.
// This is useful for generating Cadence code from templates.
func (c SystemContracts) AsTemplateEnv() templates.Environment {
return templates.Environment{
EpochAddress: c.Epoch.Address.Hex(),
IDTableAddress: c.IDTableStaking.Address.Hex(),
QuorumCertificateAddress: c.ClusterQC.Address.Hex(),
DkgAddress: c.DKG.Address.Hex(),
ServiceAccountAddress: c.FlowServiceAccount.Address.Hex(),
NodeVersionBeaconAddress: c.NodeVersionBeacon.Address.Hex(),
RandomBeaconHistoryAddress: c.RandomBeaconHistory.Address.Hex(),
StorageFeesAddress: c.FlowStorageFees.Address.Hex(),
FlowFeesAddress: c.FlowFees.Address.Hex(),
FlowTokenAddress: c.FlowToken.Address.Hex(),
FungibleTokenAddress: c.FungibleToken.Address.Hex(),
FungibleTokenMetadataViewsAddress: c.FungibleToken.Address.Hex(),
NonFungibleTokenAddress: c.NonFungibleToken.Address.Hex(),
MetadataViewsAddress: c.MetadataViews.Address.Hex(),
ViewResolverAddress: c.ViewResolver.Address.Hex(),
FungibleTokenSwitchboardAddress: c.FungibleToken.Address.Hex(),
}
}
// All returns all system contracts as a slice.
func (c SystemContracts) All() []SystemContract {
return []SystemContract{
c.Epoch,
c.IDTableStaking,
c.ClusterQC,
c.DKG,
c.FlowServiceAccount,
c.NodeVersionBeacon,
c.RandomBeaconHistory,
c.FlowStorageFees,
c.FlowFees,
c.FlowToken,
c.FungibleToken,
c.NonFungibleToken,
c.MetadataViews,
c.ViewResolver,
c.EVMContract,
// EVMStorage is not included here, since it is not a contract
}
}
// ServiceEvents is a container for all service events on a particular chain.
type ServiceEvents struct {
EpochSetup ServiceEvent
EpochCommit ServiceEvent
VersionBeacon ServiceEvent
}
// All returns all service events as a slice.
func (se ServiceEvents) All() []ServiceEvent {
return []ServiceEvent{
se.EpochSetup,
se.EpochCommit,
se.VersionBeacon,
}
}
// SystemContractsForChain returns the system contract configuration for the given chain.
// Panics if the chain is unknown.
func SystemContractsForChain(chainID flow.ChainID) *SystemContracts {
contracts, ok := systemContractsForChain[chainID]
if !ok {
// this is a panic, since it can only happen if the code is wrong
panic(fmt.Sprintf("unknown chain: %s", chainID))
}
return contracts
}
var systemContractsForChain = map[flow.ChainID]*SystemContracts{}
// ServiceEventsForChain returns the service event confirmation for the given chain.
// Panics if the chain is unknown.
func ServiceEventsForChain(chainID flow.ChainID) *ServiceEvents {
events, ok := serviceEventsForChain[chainID]
if !ok {
// this is a panic, since it can only happen if the code is wrong
panic(fmt.Sprintf("unknown chain: %s", chainID))
}
return events
}
var serviceEventsForChain = map[flow.ChainID]*ServiceEvents{}
var contractAddressFunc = map[string]func(id flow.ChainID) flow.Address{}
func init() {
serviceAddressFunc := func(chain flow.ChainID) flow.Address {
return chain.Chain().ServiceAddress()
}
// epoch contracts are deployed on a separate account on mainnet and testnet
epochAddressFunc := func(chain flow.ChainID) flow.Address {
switch chain {
case flow.Mainnet:
return stakingContractAddressMainnet
case flow.Testnet:
return stakingContractAddressTestnet
default:
return chain.Chain().ServiceAddress()
}
}
// some contracts are always at an address with a a predetermined index
nthAddressFunc := func(index uint64) func(chain flow.ChainID) flow.Address {
return func(chain flow.ChainID) flow.Address {
address, err := chain.Chain().AddressAtIndex(index)
if err != nil {
// this can only happen if the code is wrong
panic(fmt.Sprintf("failed to get %d address: %v", FlowFeesAccountIndex, err))
}
return address
}
}
nftTokenAddressFunc := func(chain flow.ChainID) flow.Address {
switch chain {
case flow.Mainnet:
return nftTokenAddressMainnet
case flow.Testnet:
return nftTokenAddressTestnet
default:
return chain.Chain().ServiceAddress()
}
}
evmStorageEVMFunc := func(chain flow.ChainID) flow.Address {
switch chain {
case flow.Mainnet:
return evmStorageAddressMainnet
case flow.Testnet:
return evmStorageAddressTestnet
default:
return nthAddressFunc(EVMStorageAccountIndex)(chain)
}
}
contractAddressFunc = map[string]func(id flow.ChainID) flow.Address{
ContractNameIDTableStaking: epochAddressFunc,
ContractNameEpoch: epochAddressFunc,
ContractNameClusterQC: epochAddressFunc,
ContractNameDKG: epochAddressFunc,
ContractNameNodeVersionBeacon: serviceAddressFunc,
ContractNameRandomBeaconHistory: serviceAddressFunc,
ContractNameServiceAccount: serviceAddressFunc,
ContractNameStorageFees: serviceAddressFunc,
ContractNameFlowFees: nthAddressFunc(FlowFeesAccountIndex),
ContractNameFungibleToken: nthAddressFunc(FungibleTokenAccountIndex),
ContractNameFlowToken: nthAddressFunc(FlowTokenAccountIndex),
ContractNameNonFungibleToken: nftTokenAddressFunc,
ContractNameMetadataViews: nftTokenAddressFunc,
ContractNameViewResolver: nftTokenAddressFunc,
ContractNameEVM: serviceAddressFunc,
AccountNameEVMStorage: evmStorageEVMFunc,
}
getSystemContractsForChain := func(chainID flow.ChainID) *SystemContracts {
addressOfContract := func(name string) SystemContract {
addressFunc, ok := contractAddressFunc[name]
if !ok {
// this is a panic, since it can only happen if the code is wrong
panic(fmt.Sprintf("unknown system contract name: %s", name))
}
return SystemContract{
Address: addressFunc(chainID),
Name: name,
}
}
addressOfAccount := func(name string) SystemAccount {
addressFunc, ok := contractAddressFunc[name]
if !ok {
// this is a panic, since it can only happen if the code is wrong
panic(fmt.Sprintf("unknown system account name: %s", name))
}
return SystemAccount{
Address: addressFunc(chainID),
Name: name,
}
}
contracts := &SystemContracts{
Epoch: addressOfContract(ContractNameEpoch),
IDTableStaking: addressOfContract(ContractNameIDTableStaking),
ClusterQC: addressOfContract(ContractNameClusterQC),
DKG: addressOfContract(ContractNameDKG),
FlowServiceAccount: addressOfContract(ContractNameServiceAccount),
NodeVersionBeacon: addressOfContract(ContractNameNodeVersionBeacon),
RandomBeaconHistory: addressOfContract(ContractNameRandomBeaconHistory),
FlowStorageFees: addressOfContract(ContractNameStorageFees),
FlowFees: addressOfContract(ContractNameFlowFees),
FlowToken: addressOfContract(ContractNameFlowToken),
FungibleToken: addressOfContract(ContractNameFungibleToken),
NonFungibleToken: addressOfContract(ContractNameNonFungibleToken),
MetadataViews: addressOfContract(ContractNameMetadataViews),
ViewResolver: addressOfContract(ContractNameViewResolver),
EVMContract: addressOfContract(ContractNameEVM),
EVMStorage: addressOfAccount(AccountNameEVMStorage),
}
return contracts
}
getServiceEventsForChain := func(chainID flow.ChainID) *ServiceEvents {
event := func(contractName, eventName string) ServiceEvent {
addressFunc, ok := contractAddressFunc[contractName]
if !ok {
// this is a panic, since it can only happen if the code is wrong
panic(fmt.Sprintf("unknown system contract name: %s", contractName))
}
return ServiceEvent{
Address: addressFunc(chainID),
ContractName: contractName,
Name: eventName,
}
}
events := &ServiceEvents{
EpochSetup: event(ContractNameEpoch, EventNameEpochSetup),
EpochCommit: event(ContractNameEpoch, EventNameEpochCommit),
VersionBeacon: event(ContractNameNodeVersionBeacon, EventNameVersionBeacon),
}
return events
}
// pre-populate the system contracts and service events for all chains for fast access
for _, chain := range flow.AllChainIDs() {
serviceEventsForChain[chain] = getServiceEventsForChain(chain)
systemContractsForChain[chain] = getSystemContractsForChain(chain)
}
}