/
respond-challenges.go
126 lines (104 loc) · 2.94 KB
/
respond-challenges.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
package watchtower
import (
"fmt"
"github.com/Seb369888/poolsea-go/dao/trustednode"
"github.com/Seb369888/poolsea-go/rocketpool"
"github.com/Seb369888/poolsea-go/utils/eth"
"github.com/urfave/cli"
"github.com/Seb369888/smartnode/shared/services"
"github.com/Seb369888/smartnode/shared/services/config"
"github.com/Seb369888/smartnode/shared/services/state"
"github.com/Seb369888/smartnode/shared/services/wallet"
"github.com/Seb369888/smartnode/shared/utils/api"
"github.com/Seb369888/smartnode/shared/utils/log"
)
// Respond to challenges task
type respondChallenges struct {
c *cli.Context
log log.ColorLogger
cfg *config.RocketPoolConfig
w *wallet.Wallet
rp *rocketpool.RocketPool
m *state.NetworkStateManager
}
// Create respond to challenges task
func newRespondChallenges(c *cli.Context, logger log.ColorLogger, m *state.NetworkStateManager) (*respondChallenges, error) {
// Get services
cfg, err := services.GetConfig(c)
if err != nil {
return nil, err
}
w, err := services.GetWallet(c)
if err != nil {
return nil, err
}
rp, err := services.GetRocketPool(c)
if err != nil {
return nil, err
}
// Return task
return &respondChallenges{
c: c,
log: logger,
cfg: cfg,
w: w,
rp: rp,
m: m,
}, nil
}
// Respond to challenges
func (t *respondChallenges) run(isAtlasDeployed bool) error {
// Wait for eth client to sync
if err := services.WaitEthClientSynced(t.c, true); err != nil {
return err
}
// Get node account
nodeAccount, err := t.w.GetNodeAccount()
if err != nil {
return err
}
// Log
t.log.Println("Checking for challenges to respond to...")
// Check for active challenges
isChallenged, err := trustednode.GetMemberIsChallenged(t.rp, nodeAccount.Address, nil)
if err != nil {
return err
}
if !isChallenged {
return nil
}
// Log
t.log.Printlnf("Node %s has an active challenge against it, responding...", nodeAccount.Address.Hex())
// Get transactor
opts, err := t.w.GetNodeAccountTransactor()
if err != nil {
return err
}
// Get the gas limit
gasInfo, err := trustednode.EstimateDecideChallengeGas(t.rp, nodeAccount.Address, opts)
if err != nil {
return fmt.Errorf("Could not estimate the gas required to respond to the challenge: %w", err)
}
// Print the gas info
maxFee := eth.GweiToWei(getWatchtowerMaxFee(t.cfg))
if !api.PrintAndCheckGasInfo(gasInfo, false, 0, t.log, maxFee, 0) {
return nil
}
// Set the gas settings
opts.GasFeeCap = maxFee
opts.GasTipCap = eth.GweiToWei(getWatchtowerPrioFee(t.cfg))
opts.GasLimit = gasInfo.SafeGasLimit
// Respond to challenge
hash, err := trustednode.DecideChallenge(t.rp, nodeAccount.Address, opts)
if err != nil {
return err
}
// Print TX info and wait for it to be included in a block
err = api.PrintAndWaitForTransaction(t.cfg, hash, t.rp.Client, t.log)
if err != nil {
return err
}
// Log & return
t.log.Printlnf("Successfully responded to challenge against node %s.", nodeAccount.Address.Hex())
return nil
}