-
Notifications
You must be signed in to change notification settings - Fork 178
/
epochs.go
237 lines (206 loc) · 7.71 KB
/
epochs.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
package blueprints
import (
_ "embed"
"encoding/hex"
"fmt"
"github.com/onflow/cadence"
jsoncdc "github.com/onflow/cadence/encoding/json"
"github.com/onflow/flow-core-contracts/lib/go/templates"
flowsdk "github.com/onflow/flow-go-sdk"
sdktemplates "github.com/onflow/flow-go-sdk/templates"
"github.com/onflow/flow-go/model/bootstrap"
"github.com/onflow/flow-go/model/flow"
"github.com/onflow/flow-go/module/epochs"
)
//go:embed scripts/deployIDTableStakingTransactionTemplate.cdc
var deployIDTableStakingTransactionTemplate string
//go:embed scripts/deployEpochTransactionTemplate.cdc
var deployEpochTransactionTemplate string
//go:embed scripts/setupAccountTemplate.cdc
var setupAccountTemplate string
//go:embed scripts/fundAccountTemplate.cdc
var fundAccountTemplate string
//go:embed scripts/deployLockedTokensTemplate.cdc
var deployLockedTokensTemplate string
// DeployEpochTransaction returns the transaction body for the deploy epoch transaction
func DeployEpochTransaction(service flow.Address, contract []byte, epochConfig epochs.EpochConfig) *flow.TransactionBody {
return flow.NewTransactionBody().
SetScript([]byte(
templates.ReplaceAddresses(
deployEpochTransactionTemplate,
templates.Environment{
QuorumCertificateAddress: service.Hex(),
},
),
)).
AddArgument(jsoncdc.MustEncode(cadence.String(hex.EncodeToString(contract)))).
AddArgument(jsoncdc.MustEncode(epochConfig.CurrentEpochCounter)).
AddArgument(jsoncdc.MustEncode(epochConfig.NumViewsInEpoch)).
AddArgument(jsoncdc.MustEncode(epochConfig.NumViewsInStakingAuction)).
AddArgument(jsoncdc.MustEncode(epochConfig.NumViewsInDKGPhase)).
AddArgument(jsoncdc.MustEncode(epochConfig.NumCollectorClusters)).
AddArgument(jsoncdc.MustEncode(epochConfig.FLOWsupplyIncreasePercentage)).
AddArgument(jsoncdc.MustEncode(epochConfig.RandomSource)).
AddArgument(epochs.EncodeClusterAssignments(epochConfig.CollectorClusters)).
AddAuthorizer(service)
}
// SetupAccountTransaction returns the transaction body for the setup account transaction
func SetupAccountTransaction(
fungibleToken flow.Address,
flowToken flow.Address,
accountAddress flow.Address,
) *flow.TransactionBody {
return flow.NewTransactionBody().
SetScript([]byte(
templates.ReplaceAddresses(
setupAccountTemplate,
templates.Environment{
FungibleTokenAddress: fungibleToken.Hex(),
FlowTokenAddress: flowToken.Hex(),
},
),
)).
AddAuthorizer(accountAddress)
}
// DeployIDTableStakingTransaction returns the transaction body for the deploy id table staking transaction
func DeployIDTableStakingTransaction(service flow.Address, contract []byte, epochTokenPayout cadence.UFix64, rewardCut cadence.UFix64) *flow.TransactionBody {
return flow.NewTransactionBody().
SetScript([]byte(deployIDTableStakingTransactionTemplate)).
AddArgument(jsoncdc.MustEncode(cadence.String(hex.EncodeToString(contract)))).
AddArgument(jsoncdc.MustEncode(epochTokenPayout)).
AddArgument(jsoncdc.MustEncode(rewardCut)).
AddAuthorizer(service)
}
// FundAccountTransaction returns the transaction body for the fund account transaction
func FundAccountTransaction(
service flow.Address,
fungibleToken flow.Address,
flowToken flow.Address,
nodeAddress flow.Address,
) *flow.TransactionBody {
cdcAmount, err := cadence.NewUFix64(fmt.Sprintf("%d.0", 2_000_000))
if err != nil {
panic(err)
}
return flow.NewTransactionBody().
SetScript([]byte(templates.ReplaceAddresses(
fundAccountTemplate,
templates.Environment{
FungibleTokenAddress: fungibleToken.Hex(),
FlowTokenAddress: flowToken.Hex(),
},
))).
AddArgument(jsoncdc.MustEncode(cdcAmount)).
AddArgument(jsoncdc.MustEncode(cadence.NewAddress(nodeAddress))).
AddAuthorizer(service)
}
// DeployLockedTokensTransaction returns the transaction body for the deploy locked tokens transaction
func DeployLockedTokensTransaction(service flow.Address, contract []byte, publicKeys []cadence.Value) *flow.TransactionBody {
return flow.NewTransactionBody().
SetScript([]byte(
deployLockedTokensTemplate,
)).
AddArgument(jsoncdc.MustEncode(cadence.NewArray(publicKeys))).
AddArgument(jsoncdc.MustEncode(cadence.String(hex.EncodeToString(contract)))).
AddAuthorizer(service)
}
// RegisterNodeTransaction creates a new node struct object.
// Then, if the node is a collector node, creates a new account and adds a QC object to it
// If the node is a consensus node, it creates a new account and adds a DKG object to it
func RegisterNodeTransaction(
service flow.Address,
flowTokenAddress flow.Address,
nodeAddress flow.Address,
id *flow.Identity,
) *flow.TransactionBody {
env := templates.Environment{
FlowTokenAddress: flowTokenAddress.HexWithPrefix(),
IDTableAddress: service.HexWithPrefix(),
QuorumCertificateAddress: service.HexWithPrefix(),
DkgAddress: service.HexWithPrefix(),
EpochAddress: service.HexWithPrefix(),
}
// Use NetworkingKey as the public key of the machine account.
// We do this for tests/localnet but normally it should be a separate key.
accountKey := &flowsdk.AccountKey{
PublicKey: id.NetworkPubKey,
SigAlgo: id.NetworkPubKey.Algorithm(),
HashAlgo: bootstrap.DefaultMachineAccountHashAlgo,
Weight: 1000,
}
cadenceCryptoKey, err := sdktemplates.AccountKeyToCadenceCryptoKey(accountKey)
if err != nil {
panic(err)
}
cadencePublicKeys := cadence.NewArray(
[]cadence.Value{
cadenceCryptoKey,
},
)
cdcAmount, err := cadence.NewUFix64("1250000.0")
if err != nil {
panic(err)
}
cdcNodeID, err := cadence.NewString(id.NodeID.String())
if err != nil {
panic(err)
}
cdcAddress, err := cadence.NewString(id.Address)
if err != nil {
panic(err)
}
cdcNetworkPubKey, err := cadence.NewString(id.NetworkPubKey.String()[2:])
if err != nil {
panic(err)
}
cdcStakingPubKey, err := cadence.NewString(id.StakingPubKey.String()[2:])
if err != nil {
panic(err)
}
// register node
return flow.NewTransactionBody().
SetScript(templates.GenerateEpochRegisterNodeScript(env)).
AddArgument(jsoncdc.MustEncode(cdcNodeID)).
AddArgument(jsoncdc.MustEncode(cadence.NewUInt8(uint8(id.Role)))).
AddArgument(jsoncdc.MustEncode(cdcAddress)).
AddArgument(jsoncdc.MustEncode(cdcNetworkPubKey)).
AddArgument(jsoncdc.MustEncode(cdcStakingPubKey)).
AddArgument(jsoncdc.MustEncode(cdcAmount)).
AddArgument(jsoncdc.MustEncode(cadencePublicKeys)).
AddAuthorizer(nodeAddress)
}
// SetStakingAllowlistTransaction returns transaction body for set staking allowlist transaction
func SetStakingAllowlistTransaction(idTableStakingAddr flow.Address, allowedNodeIDs []flow.Identifier) *flow.TransactionBody {
env := templates.Environment{
IDTableAddress: idTableStakingAddr.HexWithPrefix(),
}
allowedNodesArg := SetStakingAllowlistTxArg(allowedNodeIDs)
return flow.NewTransactionBody().
SetScript(templates.GenerateSetApprovedNodesScript(env)).
AddArgument(jsoncdc.MustEncode(allowedNodesArg)).
AddAuthorizer(idTableStakingAddr)
}
// SetStakingAllowlistTxArg returns the transaction argument for setting the staking allow-list.
func SetStakingAllowlistTxArg(allowedNodeIDs []flow.Identifier) cadence.Value {
cdcDictEntries := make([]cadence.KeyValuePair, 0, len(allowedNodeIDs))
for _, id := range allowedNodeIDs {
cdcNodeID, err := cadence.NewString(id.String())
if err != nil {
panic(err)
}
kvPair := cadence.KeyValuePair{
Key: cdcNodeID,
Value: cadence.NewBool(true),
}
cdcDictEntries = append(cdcDictEntries, kvPair)
}
return cadence.NewDictionary(cdcDictEntries)
}
// BytesToCadenceArray converts byte slice to cadence array
func BytesToCadenceArray(b []byte) cadence.Array {
values := make([]cadence.Value, len(b))
for i, v := range b {
values[i] = cadence.NewUInt8(v)
}
return cadence.NewArray(values)
}