forked from FidelVe/gochain-local-decentralize
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
285 lines (260 loc) · 7.72 KB
/
index.js
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
const IconService = require("icon-sdk-js");
const SCORES = require("./src/scores");
const crypto = require("crypto");
const {
getIcxBalance,
getKeystore,
getPreps,
prepDetails,
registerPRepNodePublicKey,
registerPrep,
saveResult,
sendIcx,
setBond,
setBonderList,
setDelegation,
setStake,
sleep,
getTxResult
} = require("./src/utils");
const {
// CallBuilder,
IcxTransactionBuilder,
CallTransactionBuilder
} = IconService.default.IconBuilder;
const { hostname, nid } = SCORES.useNetwork;
const IconConverter = IconService.default.IconConverter;
const IconWallet = IconService.default.IconWallet;
const SignedTransaction = IconService.default.SignedTransaction;
const port = 9082;
const API_NODE = `http://${hostname}:${port}/api/v3`;
const httpProvider = new IconService.default.HttpProvider(API_NODE);
const iconService = new IconService.default(httpProvider);
const walletsPath = {
god: "./wallets/godWallet.json",
node0: "./wallets/ks0.json",
node1: "./wallets/ks1.json",
node2: "./wallets/ks2.json",
node3: "./wallets/ks3.json"
};
const godKs = getKeystore(walletsPath.god);
const node0Ks = getKeystore(walletsPath.node0);
const node1Ks = getKeystore(walletsPath.node1);
const node2Ks = getKeystore(walletsPath.node2);
const node3Ks = getKeystore(walletsPath.node3);
// load keystore files
// const godKsLoaded = IconWallet.loadKeystore(godKs, 'gochain');
const node0KsLoaded = IconWallet.loadKeystore(node0Ks, "gochain");
const node1KsLoaded = IconWallet.loadKeystore(node1Ks, "gochain");
const node2KsLoaded = IconWallet.loadKeystore(node2Ks, "gochain");
const node3KsLoaded = IconWallet.loadKeystore(node3Ks, "gochain");
const balance = 10000000;
const toStake = 9000000;
const toVote = 5000000;
const toBond = 4000000;
const bonderlist = [
node0KsLoaded.getAddress(),
node1KsLoaded.getAddress(),
node2KsLoaded.getAddress(),
node3KsLoaded.getAddress()
];
const lineBreak = "******************************************";
async function main() {
try {
// get preps
const preps = await getPreps(hostname, false, port);
console.log(preps);
// if no preps are registered in the network
// initiate decentralization process.
if (preps.preps.length === 0) {
// Step #1: send balance from god wallet to each node
await fundNode(node0Ks, balance);
console.log(lineBreak, balance);
await fundNode(node1Ks, balance);
console.log(lineBreak, balance);
await fundNode(node2Ks, balance);
console.log(lineBreak, balance);
await fundNode(node3Ks, balance);
console.log(lineBreak);
// Step #2: Registering preps
await registerNode(node0Ks);
console.log(lineBreak);
await registerNode(node1Ks);
console.log(lineBreak);
await registerNode(node2Ks);
console.log(lineBreak);
await registerNode(node3Ks);
console.log(lineBreak);
// Step #3: stake on each wallet
await stakeNode(node0Ks, toStake);
await stakeNode(node1Ks, toStake);
await stakeNode(node2Ks, toStake);
await stakeNode(node3Ks, toStake);
await sleep(5000);
// Step #4: vote on each wallet
await voteNode(node0Ks, toVote);
await voteNode(node1Ks, toVote);
await voteNode(node2Ks, toVote);
await voteNode(node3Ks, toVote);
await sleep(5000);
// Step #5: bond on each wallet
await bondNode(node0Ks, toBond);
await bondNode(node1Ks, toBond);
await bondNode(node2Ks, toBond);
await bondNode(node3Ks, toBond);
await sleep(5000);
// Step #5: bond on each wallet
await registerPubKey(node0Ks);
await registerPubKey(node1Ks);
await registerPubKey(node2Ks);
await registerPubKey(node3Ks);
await sleep(5000);
const a = await getPreps(hostname, false, port);
console.log(a);
saveResult(a);
} else {
// if preps are already registered in the network;
console.log("Preps are already registered in the network");
console.log(preps);
saveResult(preps);
}
} catch (err) {
console.log("Error running main script");
console.log(err);
}
}
main();
async function fundNode(nodeKs, balance) {
const walletLoaded = IconWallet.loadKeystore(nodeKs, "gochain");
console.log(
`sending balance from god wallet to node ${walletLoaded.getAddress()}`
);
const tx0 = await sendIcx(
walletLoaded.getAddress(),
godKs,
balance,
iconService,
nid,
IcxTransactionBuilder,
IconConverter,
IconWallet,
SignedTransaction
);
console.log(`Balance sent. TxHash: ${tx0}`);
await sleep(5000);
// fetch balance from chain
console.log("fetching node wallet balance from chain");
const tx0Balance = await getIcxBalance(
hostname,
false,
port,
walletLoaded.getAddress()
);
console.log(`Balance: ${tx0Balance}`);
}
async function registerNode(nodeKs) {
const walletLoaded = IconWallet.loadKeystore(nodeKs, "gochain");
const localPrepDetails = { ...prepDetails };
localPrepDetails.name = localPrepDetails.name + "-" + crypto.randomUUID();
localPrepDetails.nodeAddress = walletLoaded.getAddress();
console.log(`Registering Prep => ${localPrepDetails.name}`);
const prep = await registerPrep(
nodeKs,
localPrepDetails,
SCORES.mainnet.governance,
nid,
iconService,
CallTransactionBuilder,
IconConverter,
IconWallet,
SignedTransaction
);
console.log(`Registration result txHash: ${prep}`);
}
async function stakeNode(nodeKs, amount) {
const walletLoaded = IconWallet.loadKeystore(nodeKs, "gochain");
console.log(`Staking wallet Prep => ${walletLoaded.getAddress()}`);
const stake = await setStake(
amount,
nodeKs,
SCORES.mainnet.governance,
nid,
iconService,
CallTransactionBuilder,
IconConverter,
IconWallet,
SignedTransaction
);
console.log(`Staking result txHash: ${stake}`);
}
async function voteNode(nodeKs, amount) {
const walletLoaded = IconWallet.loadKeystore(nodeKs, "gochain");
console.log(`voting wallet Prep => ${walletLoaded.getAddress()}`);
const vote = await setDelegation(
amount,
nodeKs,
SCORES.mainnet.governance,
nid,
iconService,
CallTransactionBuilder,
IconConverter,
IconWallet,
SignedTransaction
);
console.log(`voting result txHash: ${vote}`);
}
async function bondNode(nodeKs, amount) {
const walletLoaded = IconWallet.loadKeystore(nodeKs, "gochain");
// setbonderlist
console.log("Setting bonderlist");
const bonderlistResult = await setBonderList(
nodeKs,
bonderlist,
SCORES.mainnet.governance,
nid,
iconService,
CallTransactionBuilder,
IconConverter,
IconWallet,
SignedTransaction
);
console.log(`Result of setting bonderlist. txHash ${bonderlistResult}`);
sleep(5000);
// setbond
console.log(`bonding wallet Prep => ${walletLoaded.getAddress()}`);
const bond = await setBond(
amount,
nodeKs,
SCORES.mainnet.governance,
nid,
iconService,
CallTransactionBuilder,
IconConverter,
IconWallet,
SignedTransaction
);
console.log(`bonding result txHash: ${bond}`);
}
async function registerPubKey(nodeKs) {
const walletLoaded = IconWallet.loadKeystore(nodeKs, "gochain");
const pubKey = walletLoaded.getPublicKey(true);
const address = walletLoaded.getAddress();
console.log(`PubKey: ${pubKey}`);
const txResult = await registerPRepNodePublicKey(
nodeKs,
pubKey,
address,
SCORES.mainnet.governance,
nid,
iconService,
CallTransactionBuilder,
IconConverter,
IconWallet,
SignedTransaction
);
console.log(`Result of pubKey registration. txHash ${txResult}`);
await sleep(5000);
const txResult2 = await getTxResult(txResult, hostname, false, port);
console.log("TxResult2");
console.log(txResult2);
}