-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathnode.go
484 lines (402 loc) · 13.9 KB
/
node.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
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
package main
import (
"context"
"crypto/ecdsa"
"encoding/json"
"fmt"
"log"
"math/big"
"os"
"strings"
"time"
"github.com/ethereum/go-ethereum"
"github.com/ethereum/go-ethereum/accounts/abi"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/ethclient"
"github.com/joho/godotenv"
libp2p "github.com/libp2p/go-libp2p"
"github.com/libp2p/go-libp2p/core"
"github.com/libp2p/go-libp2p/core/network"
"github.com/libp2p/go-libp2p/core/peer"
"github.com/libp2p/go-libp2p/core/peerstore"
"github.com/multiformats/go-multiaddr"
"github.com/tokamak-network/DRB-node/logger"
"github.com/tokamak-network/DRB-node/transactions"
"github.com/tokamak-network/DRB-node/utils"
)
const abiFilePath = "contract/abi/Commit2RevealDRB.json"
type RegistrationRequest struct {
EOAAddress string `json:"eoa_address"`
Signature []byte `json:"signature"`
PeerID string `json:"peer_id"`
}
func init() {
if err := godotenv.Load(); err != nil {
log.Println("No .env file found")
}
}
func main() {
logger.InitLogger()
defer logger.CloseLogger()
nodeType := os.Getenv("NODE_TYPE") // Expecting 'leader' or 'regular'
switch nodeType {
case "leader":
runLeaderNode()
case "regular":
runRegularNode()
default:
log.Fatal("NODE_TYPE must be set to either 'leader' or 'regular'")
}
}
// Leader Node
func runLeaderNode() {
port := os.Getenv("LEADER_PORT")
if port == "" {
log.Fatal("LEADER_PORT not set in environment variables.")
}
h, err := libp2p.New(
libp2p.DefaultTransports,
libp2p.ListenAddrStrings(fmt.Sprintf("/ip4/0.0.0.0/tcp/%s", port)),
)
if err != nil {
log.Fatalf("Failed to create libp2p host: %v", err)
}
defer h.Close()
h.SetStreamHandler("/register", func(s network.Stream) {
handleRegistrationRequest(s)
})
log.Printf("Leader node is running on addresses: %s\n", h.Addrs())
log.Printf("Leader node PeerID: %s\n", h.ID().String())
select {}
}
func handleRegistrationRequest(s network.Stream) {
defer s.Close()
var req RegistrationRequest
if err := json.NewDecoder(s).Decode(&req); err != nil {
log.Printf("Failed to decode registration request: %v", err)
return
}
if !verifySignature(req) {
log.Printf("Failed to verify signature for PeerID: %s", req.PeerID)
return
}
log.Printf("Verified registration for PeerID: %s", req.PeerID)
client, err := ethclient.Dial(os.Getenv("ETH_RPC_URL"))
if err != nil {
log.Fatalf("Failed to connect to Ethereum client: %v", err)
}
contractAddress := common.HexToAddress(os.Getenv("CONTRACT_ADDRESS"))
parsedABI, err := loadContractABI(abiFilePath)
if err != nil {
log.Fatalf("Failed to load contract ABI: %v", err)
}
operatorAddress := common.HexToAddress(req.EOAAddress)
activatedOperatorsResult, err := callSmartContract(client, parsedABI, "getActivatedOperators", contractAddress)
if err != nil {
log.Printf("Failed to call getActivatedOperators: %v", err)
return
}
activatedOperators := activatedOperatorsResult.([]common.Address)
isActivated := false
for _, operator := range activatedOperators {
if operator == operatorAddress {
isActivated = true
break
}
}
if isActivated {
log.Println("Operator is already activated.")
return
}
depositAmountResult, err := callSmartContract(client, parsedABI, "s_depositAmount", contractAddress, operatorAddress)
if err != nil {
log.Printf("Failed to call s_depositAmount: %v", err)
return
}
depositAmount := depositAmountResult.(*big.Int)
activationThresholdResult, err := callSmartContract(client, parsedABI, "s_activationThreshold", contractAddress)
if err != nil {
log.Printf("Failed to call s_activationThreshold: %v", err)
return
}
activationThreshold := activationThresholdResult.(*big.Int)
if depositAmount.Cmp(activationThreshold) < 0 {
log.Printf("Deposit amount is insufficient. Deposit: %s, Threshold: %s", depositAmount, activationThreshold)
return
}
// Prepare for transaction execution using ExecuteTransaction from transactions package
privateKeyHex := os.Getenv("LEADER_PRIVATE_KEY")
privateKey, err := crypto.HexToECDSA(privateKeyHex)
if err != nil {
log.Fatalf("Failed to decode leader private key: %v", err)
}
clientUtils := &utils.Client{
Client: client,
ContractAddress: contractAddress,
PrivateKey: privateKey,
ContractABI: parsedABI,
}
_, _, err = transactions.ExecuteTransaction(
context.Background(),
clientUtils,
"activate",
big.NewInt(0),
operatorAddress,
)
if err != nil {
log.Printf("Failed to activate operator: %v", err)
return
}
log.Println("Operator activated successfully.")
}
// Verify the signature of a registration request
func verifySignature(req RegistrationRequest) bool {
hash := crypto.Keccak256Hash([]byte(req.EOAAddress))
pubKey, err := crypto.SigToPub(hash.Bytes(), req.Signature)
if err != nil {
log.Printf("Error recovering public key: %v", err)
return false
}
recoveredAddress := crypto.PubkeyToAddress(*pubKey).Hex()
return recoveredAddress == req.EOAAddress
}
func runRegularNode() {
ctx := context.Background()
h, err := libp2p.New(libp2p.ListenAddrStrings(fmt.Sprintf("/ip4/0.0.0.0/tcp/%s", os.Getenv("PORT"))))
if err != nil {
log.Fatalf("Failed to create libp2p host: %v", err)
}
defer h.Close()
leaderIP := os.Getenv("LEADER_IP")
leaderPort := os.Getenv("LEADER_PORT")
leaderPeerID := os.Getenv("LEADER_PEER_ID")
privateKeyHex := os.Getenv("EOA_PRIVATE_KEY")
if privateKeyHex == "" {
log.Fatal("EOA_PRIVATE_KEY is not set in the environment variables")
}
privateKey, err := crypto.HexToECDSA(privateKeyHex)
if err != nil {
log.Fatalf("Failed to decode Ethereum private key: %v", err)
}
eoaAddress := crypto.PubkeyToAddress(privateKey.PublicKey).Hex()
log.Printf("EOA Address: %s", eoaAddress)
leaderAddrString := fmt.Sprintf("/ip4/%s/tcp/%s/p2p/%s", leaderIP, leaderPort, leaderPeerID)
log.Printf("Leader multiaddress: %s", leaderAddrString)
leaderAddr, err := multiaddr.NewMultiaddr(leaderAddrString)
if err != nil {
log.Fatalf("Failed to parse leader multiaddress: %v", err)
}
leaderInfo, err := peer.AddrInfoFromP2pAddr(leaderAddr)
if err != nil {
log.Fatalf("Failed to create peer info from leader multiaddress: %v", err)
}
h.Peerstore().AddAddrs(leaderInfo.ID, leaderInfo.Addrs, peerstore.PermanentAddrTTL)
client, err := ethclient.Dial(os.Getenv("ETH_RPC_URL"))
if err != nil {
log.Fatalf("Failed to connect to Ethereum client: %v", err)
}
contractAddress := common.HexToAddress(os.Getenv("CONTRACT_ADDRESS"))
parsedABI, err := loadContractABI(abiFilePath)
if err != nil {
log.Fatalf("Failed to load contract ABI: %v", err)
}
clientUtils := &utils.Client{
Client: client,
ContractAddress: contractAddress,
PrivateKey: privateKey,
ContractABI: parsedABI,
}
for {
// Check activation status
isActivated := checkActivationStatus(clientUtils, eoaAddress)
if isActivated {
log.Println("Node is activated. No further action required.")
time.Sleep(30 * time.Second)
continue
}
log.Println("Node is not activated. Checking deposit amount...")
// Check and ensure deposit is sufficient
depositSufficient, err := checkDepositAmount(clientUtils, eoaAddress)
if err != nil {
log.Printf("Error checking deposit amount: %v", err)
time.Sleep(30 * time.Second)
continue
}
if !depositSufficient {
log.Println("Deposit insufficient. Initiating deposit transaction...")
txSent, err := depositAndCheckActivation(ctx, eoaAddress, privateKey)
if err != nil {
log.Printf("Error during deposit transaction: %v", err)
time.Sleep(30 * time.Second)
continue
}
if txSent {
log.Println("Deposit transaction sent. Waiting for confirmation...")
time.Sleep(30 * time.Second)
continue
}
}
// Send registration request to leader
log.Println("Deposit sufficient. Sending registration request to leader...")
sendRegistrationRequestToLeader(ctx, h, leaderInfo.ID, eoaAddress, privateKey)
// Wait before rechecking activation status
time.Sleep(30 * time.Second)
}
}
func checkActivationStatus(client *utils.Client, eoaAddress string) bool {
activatedOperatorsResult, err := callSmartContract(client.Client, client.ContractABI, "getActivatedOperators", client.ContractAddress)
if err != nil {
log.Printf("Failed to call getActivatedOperators: %v", err)
return false
}
activatedOperators := activatedOperatorsResult.([]common.Address)
for _, operator := range activatedOperators {
if operator.Hex() == eoaAddress {
return true
}
}
return false
}
func checkDepositAmount(client *utils.Client, eoaAddress string) (bool, error) {
// Fetch deposit amount
depositAmountResult, err := callSmartContract(client.Client, client.ContractABI, "s_depositAmount", client.ContractAddress, common.HexToAddress(eoaAddress))
if err != nil {
return false, fmt.Errorf("failed to call s_depositAmount: %v", err)
}
depositAmount := depositAmountResult.(*big.Int)
// Fetch activation threshold
activationThresholdResult, err := callSmartContract(client.Client, client.ContractABI, "s_activationThreshold", client.ContractAddress)
if err != nil {
return false, fmt.Errorf("failed to call s_activationThreshold: %v", err)
}
activationThreshold := activationThresholdResult.(*big.Int)
log.Printf("Deposit amount: %s, Activation threshold: %s", depositAmount.String(), activationThreshold.String())
// Check if deposit is sufficient
if depositAmount.Cmp(activationThreshold) >= 0 {
return true, nil
}
return false, nil
}
func depositAndCheckActivation(ctx context.Context, eoaAddress string, privateKey *ecdsa.PrivateKey) (bool, error) {
client, err := ethclient.Dial(os.Getenv("ETH_RPC_URL"))
if err != nil {
return false, fmt.Errorf("failed to connect to Ethereum client: %v", err)
}
contractAddress := common.HexToAddress(os.Getenv("CONTRACT_ADDRESS"))
parsedABI, err := loadContractABI(abiFilePath)
if err != nil {
return false, fmt.Errorf("failed to load contract ABI: %v", err)
}
// Fetch deposit amount
depositAmountResult, err := callSmartContract(client, parsedABI, "s_depositAmount", contractAddress, common.HexToAddress(eoaAddress))
if err != nil {
return false, fmt.Errorf("failed to call s_depositAmount: %v", err)
}
depositAmount := depositAmountResult.(*big.Int)
// Fetch activation threshold
activationThresholdResult, err := callSmartContract(client, parsedABI, "s_activationThreshold", contractAddress)
if err != nil {
return false, fmt.Errorf("failed to call s_activationThreshold: %v", err)
}
activationThreshold := activationThresholdResult.(*big.Int)
// If deposit is insufficient, we calculate the remaining amount and proceed with the deposit
if depositAmount.Cmp(activationThreshold) < 0 {
remaining := new(big.Int).Sub(activationThreshold, depositAmount)
log.Printf("Deposit insufficient. Adding remaining: %s", remaining.String())
// Check account balance
balance, err := client.BalanceAt(ctx, common.HexToAddress(eoaAddress), nil)
if err != nil {
return false, fmt.Errorf("failed to fetch account balance: %v", err)
}
log.Printf("Account balance: %s", balance.String())
if balance.Cmp(remaining) < 0 {
return false, fmt.Errorf("insufficient balance: required %s, available %s", remaining.String(), balance.String())
}
// Create and send deposit transaction
_, _, err = transactions.ExecuteTransaction(
ctx,
&utils.Client{
Client: client,
ContractAddress: contractAddress,
PrivateKey: privateKey,
ContractABI: parsedABI,
},
"deposit",
remaining,
)
if err != nil {
return false, fmt.Errorf("failed to send deposit transaction: %v", err)
}
log.Println("Deposit transaction sent.")
return true, nil
}
log.Println("Deposit amount is sufficient. No additional deposit required.")
return false, nil
}
func sendRegistrationRequestToLeader(ctx context.Context, h core.Host, leaderID peer.ID, eoaAddress string, privateKey *ecdsa.PrivateKey) {
req := RegistrationRequest{
EOAAddress: eoaAddress,
Signature: signData(eoaAddress, privateKey),
PeerID: h.ID().String(),
}
s, err := h.NewStream(ctx, leaderID, "/register")
if err != nil {
log.Printf("Failed to create stream to leader: %v", err)
h.Peerstore().AddAddrs(leaderID, h.Peerstore().Addrs(leaderID), peerstore.PermanentAddrTTL)
return
}
defer s.Close()
if err := json.NewEncoder(s).Encode(req); err != nil {
log.Printf("Failed to send registration request: %v", err)
} else {
log.Println("Registration request sent to leader.")
}
}
// Helper Functions
func signData(data string, privateKey *ecdsa.PrivateKey) []byte {
hash := crypto.Keccak256Hash([]byte(data))
signature, err := crypto.Sign(hash.Bytes(), privateKey)
if err != nil {
log.Fatalf("Failed to sign data: %v", err)
}
return signature
}
func loadContractABI(filename string) (abi.ABI, error) {
abiBytes, err := os.ReadFile(filename)
if err != nil {
return abi.ABI{}, fmt.Errorf("failed to read ABI file: %v", err)
}
var abiObject struct {
ABI json.RawMessage `json:"abi"`
}
err = json.Unmarshal(abiBytes, &abiObject)
if err != nil {
return abi.ABI{}, fmt.Errorf("failed to unmarshal ABI JSON: %v", err)
}
parsedABI, err := abi.JSON(strings.NewReader(string(abiObject.ABI)))
if err != nil {
return abi.ABI{}, fmt.Errorf("failed to parse contract ABI: %v", err)
}
return parsedABI, nil
}
// Smart contract call helper function
func callSmartContract(client *ethclient.Client, parsedABI abi.ABI, method string, contractAddress common.Address, params ...interface{}) (interface{}, error) {
data, err := parsedABI.Pack(method, params...)
if err != nil {
return nil, fmt.Errorf("failed to pack data for %s: %v", method, err)
}
result, err := client.CallContract(context.Background(), ethereum.CallMsg{
To: &contractAddress,
Data: data,
}, nil)
if err != nil {
return nil, fmt.Errorf("failed to call contract method %s: %v", method, err)
}
var unpackedResult interface{}
err = parsedABI.UnpackIntoInterface(&unpackedResult, method, result)
if err != nil {
return nil, fmt.Errorf("failed to unpack result for %s: %v", method, err)
}
return unpackedResult, nil
}