/
announce_node.go
235 lines (196 loc) · 7.12 KB
/
announce_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
// Copyright (c) 2022 Gobalsky Labs Limited
//
// Use of this software is governed by the Business Source License included
// in the LICENSE file and at https://www.mariadb.com/bsl11.
//
// Change Date: 18 months from the later of the date of the first publicly
// available Distribution of this version of the repository, and 25 June 2022.
//
// On the date above, in accordance with the Business Source License, use
// of this software will be governed by version 3 or later of the GNU General
// Public License.
package commands
import (
"context"
"errors"
"fmt"
"net"
"strconv"
"time"
"github.com/elysiumstation/fury/core/blockchain"
"github.com/elysiumstation/fury/core/blockchain/abci"
"github.com/elysiumstation/fury/core/config"
"github.com/elysiumstation/fury/core/nodewallets"
"github.com/elysiumstation/fury/core/txn"
"github.com/elysiumstation/fury/core/validators"
vgcrypto "github.com/elysiumstation/fury/libs/crypto"
vgjson "github.com/elysiumstation/fury/libs/json"
"github.com/elysiumstation/fury/logging"
"github.com/elysiumstation/fury/paths"
api "github.com/elysiumstation/fury/protos/fury/api/v1"
commandspb "github.com/elysiumstation/fury/protos/fury/commands/v1"
"google.golang.org/grpc"
"github.com/jessevdk/go-flags"
)
type AnnounceNodeCmd struct {
config.FuryHomeFlag
config.OutputFlag
config.Passphrase `long:"passphrase-file"`
InfoURL string `short:"i" long:"info-url" required:"true" description:"An url to the website / information about this validator"`
Country string `short:"c" long:"country" required:"true" description:"The country from which the validator is operating"`
Name string `short:"n" long:"name" required:"true" description:"The name of this validator"`
AvatarURL string `short:"a" long:"avatar-url" required:"true" description:"A link to an avatar picture for this validator"`
FromEpoch uint64 `short:"f" long:"from-epoch" required:"true" description:"The epoch from which this validator should be ready to validate blocks" `
SubmitterAddress string `short:"s" long:"submitter-address" description:"Ethereum address to use as a submitter to contract changes" `
}
var announceNodeCmd AnnounceNodeCmd
func (opts *AnnounceNodeCmd) Execute(_ []string) error {
log := logging.NewLoggerFromConfig(logging.NewDefaultConfig())
defer log.AtExit()
output, err := opts.GetOutput()
if err != nil {
return err
}
registryPass, err := opts.Get("node wallet", false)
if err != nil {
return err
}
furyPaths := paths.New(opts.FuryHome)
_, conf, err := config.EnsureNodeConfig(furyPaths)
if err != nil {
return err
}
if !conf.IsValidator() {
return errors.New("node is not a validator")
}
nodeWallets, err := nodewallets.GetNodeWallets(conf.NodeWallet, furyPaths, registryPass)
if err != nil {
return fmt.Errorf("couldn't get node wallets: %w", err)
}
// ensure the nodewallet is setup properly, if not we could not complete the command
if err := nodeWallets.Verify(); err != nil {
return fmt.Errorf("node wallet misconfigured: %w", err)
}
cmd := commandspb.AnnounceNode{
Id: nodeWallets.Fury.ID().Hex(),
FuryPubKey: nodeWallets.Fury.PubKey().Hex(),
FuryPubKeyIndex: nodeWallets.Fury.Index(),
ChainPubKey: nodeWallets.Tendermint.Pubkey,
EthereumAddress: vgcrypto.EthereumChecksumAddress(nodeWallets.Ethereum.PubKey().Hex()),
FromEpoch: opts.FromEpoch,
InfoUrl: opts.InfoURL,
Name: opts.Name,
AvatarUrl: opts.AvatarURL,
Country: opts.Country,
SubmitterAddress: opts.SubmitterAddress,
}
if len(cmd.SubmitterAddress) != 0 {
cmd.SubmitterAddress = vgcrypto.EthereumChecksumAddress(cmd.SubmitterAddress)
}
if err := validators.SignAnnounceNode(
&cmd, nodeWallets.Fury, nodeWallets.Ethereum,
); err != nil {
return err
}
// now we are OK, send the command
commander, blockData, cfunc, err := getNodeWalletCommander(log, registryPass, furyPaths)
if err != nil {
return fmt.Errorf("failed to get commander: %w", err)
}
defer cfunc()
tid := vgcrypto.RandomHash()
powNonce, _, err := vgcrypto.PoW(blockData.Hash, tid, uint(blockData.SpamPowDifficulty), vgcrypto.Sha3)
if err != nil {
return fmt.Errorf("failed to get commander: %w", err)
}
pow := &commandspb.ProofOfWork{
Tid: tid,
Nonce: powNonce,
}
var txHash string
ch := make(chan struct{})
commander.CommandWithPoW(
context.Background(),
txn.AnnounceNodeCommand,
&cmd,
func(h string, e error) {
txHash, err = h, e
close(ch)
}, nil, pow)
<-ch
if err != nil {
return err
}
if output.IsHuman() {
fmt.Printf("node successfully announced.\ntxHash: %s\nfury signature: %v\nethereum signature: %v\n",
txHash,
cmd.FurySignature.Value,
cmd.EthereumSignature.Value,
)
} else if output.IsJSON() {
return vgjson.Print(struct {
TxHash string `json:"txHash"`
EthereumSignature string `json:"ethereumSignature"`
FurySignature string `json:"furySignature"`
}{
TxHash: txHash,
EthereumSignature: cmd.EthereumSignature.Value,
FurySignature: cmd.FurySignature.Value,
})
}
return nil
}
func AnnounceNode(ctx context.Context, parser *flags.Parser) error {
announceNodeCmd = AnnounceNodeCmd{}
var (
short = "Announce a node as a potential validator to the network"
long = "Announce a node as a potential validator to the network"
)
_, err := parser.AddCommand("announce_node", short, long, &announceNodeCmd)
return err
}
func getNodeWalletCommander(log *logging.Logger, registryPass string, furyPaths paths.Paths) (*nodewallets.Commander, *api.LastBlockHeightResponse, context.CancelFunc, error) {
_, cfg, err := config.EnsureNodeConfig(furyPaths)
if err != nil {
return nil, nil, nil, err
}
furyWallet, err := nodewallets.GetFuryWallet(furyPaths, registryPass)
if err != nil {
return nil, nil, nil, fmt.Errorf("couldn't get Fury node wallet: %w", err)
}
abciClient, err := abci.NewClient(cfg.Blockchain.Tendermint.RPCAddr)
if err != nil {
return nil, nil, nil, fmt.Errorf("couldn't initialise ABCI client: %w", err)
}
coreClient, err := getCoreClient(
net.JoinHostPort(cfg.API.IP, strconv.Itoa(cfg.API.Port)))
if err != nil {
return nil, nil, nil, fmt.Errorf("couldn't connect to node: %w", err)
}
ctx, cancel := timeoutContext()
resp, err := coreClient.LastBlockHeight(ctx, &api.LastBlockHeightRequest{})
if err != nil {
return nil, nil, nil, fmt.Errorf("couldn't get last block height: %w", err)
}
commander, err := nodewallets.NewCommander(cfg.NodeWallet, log, blockchain.NewClientWithImpl(abciClient), furyWallet, heightProvider{height: resp.Height})
if err != nil {
return nil, nil, nil, fmt.Errorf("couldn't initialise node wallet commander: %w", err)
}
return commander, resp, cancel, nil
}
type heightProvider struct {
height uint64
}
func (h heightProvider) Height() uint64 {
return h.height
}
func getCoreClient(address string) (api.CoreServiceClient, error) {
tdconn, err := grpc.Dial(address, grpc.WithInsecure())
if err != nil {
return nil, err
}
return api.NewCoreServiceClient(tdconn), nil
}
func timeoutContext() (context.Context, func()) {
return context.WithTimeout(context.Background(), 5*time.Second)
}