forked from nspcc-dev/neofs-node
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinitialize.go
227 lines (190 loc) · 5.93 KB
/
initialize.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
package morph
import (
"errors"
"fmt"
"io/ioutil"
"path"
"time"
"github.com/nspcc-dev/neo-go/pkg/core/transaction"
"github.com/nspcc-dev/neo-go/pkg/crypto/keys"
"github.com/nspcc-dev/neo-go/pkg/rpc/client"
"github.com/nspcc-dev/neo-go/pkg/smartcontract/trigger"
"github.com/nspcc-dev/neo-go/pkg/util"
"github.com/nspcc-dev/neo-go/pkg/wallet"
"github.com/nspcc-dev/neofs-node/cmd/neofs-adm/internal/modules/config"
"github.com/nspcc-dev/neofs-node/pkg/innerring"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
type initializeContext struct {
Client *client.Client
// CommitteeAcc is used for retrieving committee address and verification script.
CommitteeAcc *wallet.Account
// ConsensusAcc is used for retrieving committee address and verification script.
ConsensusAcc *wallet.Account
Wallets []*wallet.Wallet
Hashes []util.Uint256
WaitDuration time.Duration
PollInterval time.Duration
Contracts map[string]*contractState
Command *cobra.Command
}
func initializeSideChainCmd(cmd *cobra.Command, args []string) error {
// contract path is not part of the config
contractsPath, err := cmd.Flags().GetString(contractsInitFlag)
if err != nil {
return err
}
initCtx, err := newInitializeContext(cmd, viper.GetViper())
if err != nil {
return fmt.Errorf("initialization error: %w", err)
}
// 1. Transfer funds to committee accounts.
cmd.Println("Stage 1: transfer GAS to alphabet nodes.")
if err := initCtx.transferFunds(); err != nil {
return err
}
cmd.Println("Stage 2: set notary and alphabet nodes in designate contract.")
if err := initCtx.setNotaryAndAlphabetNodes(); err != nil {
return err
}
// 3. Deploy NNS contract.
cmd.Println("Stage 3: deploy NNS contract.")
if err := initCtx.deployNNS(); err != nil {
return err
}
// 4. Deploy NeoFS contracts.
cmd.Println("Stage 4: deploy NeoFS contracts.")
if err := initCtx.deployContracts(); err != nil {
return err
}
cmd.Println("Stage 4.1: Transfer GAS to proxy contract.")
if err := initCtx.transferGASToProxy(); err != nil {
return err
}
cmd.Println("Stage 5: register candidates.")
if err := initCtx.registerCandidates(); err != nil {
return err
}
cmd.Println("Stage 6: transfer NEO to alphabet contracts.")
if err := initCtx.transferNEOToAlphabetContracts(); err != nil {
return err
}
cmd.Println("Stage 7: set addresses in NNS.")
if err := initCtx.setNNS(); err != nil {
return err
}
cmd.Println("endpoint:", viper.GetString(endpointFlag))
cmd.Println("alphabet-wallets:", viper.GetString(alphabetWalletsFlag))
cmd.Println("contracts:", contractsPath)
cmd.Println("epoch-duration:", viper.GetUint(epochDurationInitFlag))
cmd.Println("max-object-size:", viper.GetUint(maxObjectSizeInitFlag))
return nil
}
func newInitializeContext(cmd *cobra.Command, v *viper.Viper) (*initializeContext, error) {
walletDir := v.GetString(alphabetWalletsFlag)
wallets, err := openAlphabetWallets(walletDir)
if err != nil {
return nil, err
}
c, err := getN3Client(v)
if err != nil {
return nil, fmt.Errorf("can't create N3 client: %w", err)
}
committeeAcc, err := getWalletAccount(wallets[0], committeeAccountName)
if err != nil {
return nil, fmt.Errorf("can't find committee account: %w", err)
}
consensusAcc, err := getWalletAccount(wallets[0], consensusAccountName)
if err != nil {
return nil, fmt.Errorf("can't find consensus account: %w", err)
}
initCtx := &initializeContext{
Client: c,
ConsensusAcc: consensusAcc,
CommitteeAcc: committeeAcc,
Wallets: wallets,
WaitDuration: time.Second * 30,
PollInterval: time.Second,
Command: cmd,
Contracts: make(map[string]*contractState),
}
return initCtx, nil
}
func openAlphabetWallets(walletDir string) ([]*wallet.Wallet, error) {
walletFiles, err := ioutil.ReadDir(walletDir)
if err != nil {
return nil, fmt.Errorf("can't read alphabet wallets dir: %w", err)
}
size := len(walletFiles)
if size == 0 {
return nil, errors.New("alphabet wallets dir is empty (run `generate-alphabet` command first)")
}
wallets := make([]*wallet.Wallet, size)
for i := 0; i < size; i++ {
p := path.Join(walletDir, innerring.GlagoliticLetter(i).String()+".json")
w, err := wallet.NewWalletFromFile(p)
if err != nil {
return nil, fmt.Errorf("can't open wallet: %w", err)
}
password, err := config.AlphabetPassword(viper.GetViper(), i)
if err != nil {
return nil, fmt.Errorf("can't fetch password: %w", err)
}
for i := range w.Accounts {
if err := w.Accounts[i].Decrypt(password, keys.NEP2ScryptParams()); err != nil {
return nil, fmt.Errorf("can't unlock wallet: %w", err)
}
}
wallets[i] = w
}
return wallets, nil
}
func (c *initializeContext) awaitTx() error {
c.Command.Println("Waiting for transactions to persist...")
tick := time.NewTicker(c.PollInterval)
defer tick.Stop()
timer := time.NewTimer(c.WaitDuration)
defer timer.Stop()
at := trigger.Application
loop:
for i := range c.Hashes {
_, err := c.Client.GetApplicationLog(c.Hashes[i], &at)
if err == nil {
continue loop
}
for {
select {
case <-tick.C:
_, err := c.Client.GetApplicationLog(c.Hashes[i], &at)
if err == nil {
continue loop
}
case <-timer.C:
return errors.New("timeout while waiting for transaction to persist")
}
}
}
return nil
}
func (c *initializeContext) sendCommitteeTx(script []byte, sysFee int64) error {
tx, err := c.Client.CreateTxFromScript(script, c.CommitteeAcc, sysFee, 0, []client.SignerAccount{{
Signer: transaction.Signer{
Account: c.CommitteeAcc.Contract.ScriptHash(),
Scopes: transaction.CalledByEntry,
},
Account: c.CommitteeAcc,
}})
if err != nil {
return fmt.Errorf("can't create tx: %w", err)
}
return c.multiSignAndSend(tx, committeeAccountName)
}
func getWalletAccount(w *wallet.Wallet, typ string) (*wallet.Account, error) {
for i := range w.Accounts {
if w.Accounts[i].Label == typ {
return w.Accounts[i], nil
}
}
return nil, fmt.Errorf("account for '%s' not found", typ)
}