forked from celo-org/celo-blockchain
/
node.go
233 lines (200 loc) · 6.06 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
package cluster
import (
"context"
"encoding/json"
"fmt"
"io/ioutil"
"log"
"math/big"
"net"
"os"
"os/exec"
"path"
"strconv"
"strings"
"github.com/aaronwinter/celo-blockchain/crypto"
"github.com/aaronwinter/celo-blockchain/internal/fileutils"
"github.com/aaronwinter/celo-blockchain/mycelo/env"
"github.com/aaronwinter/celo-blockchain/p2p/enode"
"github.com/aaronwinter/celo-blockchain/accounts/keystore"
"github.com/aaronwinter/celo-blockchain/common"
)
// NodeConfig represents the configuration of a celo-blockchain node runner
type NodeConfig struct {
GethPath string
ExtraFlags string
ChainID *big.Int
Number int
Account env.Account
TxFeeRecipientAccount env.Account
OtherAccounts []env.Account
Datadir string
}
// RPCPort is the rpc port this node will use
func (nc *NodeConfig) RPCPort() int64 {
return int64(8545 + nc.Number)
}
// NodePort is the node port this node will use
func (nc *NodeConfig) NodePort() int64 {
return int64(30303 + nc.Number)
}
// Node represents a Node runner
type Node struct {
*NodeConfig
}
// NewNode creates a node runner
func NewNode(cfg *NodeConfig) *Node {
return &Node{
NodeConfig: cfg,
}
}
// SetStaticNodes configures static nodes to be used on the node
func (n *Node) SetStaticNodes(enodeUrls ...string) error {
var staticNodesRaw []byte
var err error
if staticNodesRaw, err = json.Marshal(enodeUrls); err != nil {
return fmt.Errorf("Can't serialize static nodes: %w", err)
}
//nolint:gosec
if err = ioutil.WriteFile(n.staticNodesFile(), staticNodesRaw, 0644); err != nil {
return fmt.Errorf("Can't serialize static nodes: %w", err)
}
return nil
}
// EnodeURL returns the enode url used by the node
func (n *Node) EnodeURL() (string, error) {
nodekey, err := crypto.LoadECDSA(n.keyFile())
if err != nil {
return "", err
}
ip := net.IP{127, 0, 0, 1}
en := enode.NewV4(&nodekey.PublicKey, ip, int(n.NodePort()), int(n.NodePort()))
return en.URLv4(), nil
}
// AccountAddresses retrieves the list of accounts currently configured in the node
func (n *Node) AccountAddresses() []common.Address {
ks := keystore.NewKeyStore(path.Join(n.Datadir, "keystore"), keystore.LightScryptN, keystore.LightScryptP)
addresses := make([]common.Address, 0)
for _, acc := range ks.Accounts() {
addresses = append(addresses, acc.Address)
}
return addresses
}
// Init will run `geth init` on the node along other initialization procedures
// that need to happen before we run the node
func (n *Node) Init(GenesisJSON string) error {
if fileutils.FileExists(n.Datadir) {
os.RemoveAll(n.Datadir)
}
os.MkdirAll(n.Datadir, os.ModePerm)
// Write password file
if err := ioutil.WriteFile(n.pwdFile(), []byte{}, os.ModePerm); err != nil {
return err
}
// Run geth init
if out, err := n.runSync("init", GenesisJSON); err != nil {
os.Stderr.Write(out)
return err
}
// Generate nodekey file (enode private key)
if err := n.generateNodeKey(); err != nil {
return err
}
// Add Accounts
ks := keystore.NewKeyStore(path.Join(n.Datadir, "keystore"), keystore.LightScryptN, keystore.LightScryptP)
if _, err := ks.ImportECDSA(n.Account.PrivateKey, ""); err != nil {
return err
}
for _, acc := range n.OtherAccounts {
if _, err := ks.ImportECDSA(acc.PrivateKey, ""); err != nil {
return err
}
}
return nil
}
func (n *Node) generateNodeKey() error {
nodeKey, err := crypto.GenerateKey()
if err != nil {
return err
}
if err = crypto.SaveECDSA(n.keyFile(), nodeKey); err != nil {
return err
}
return nil
}
// Run will run the node
func (n *Node) Run(ctx context.Context) error {
var addressToUnlock string
for _, addr := range n.AccountAddresses() {
addressToUnlock += "," + addr.Hex()
}
args := []string{
"--datadir", n.Datadir,
"--verbosity", "4",
"--networkid", n.ChainID.String(),
"--syncmode", "full",
"--mine",
"--allow-insecure-unlock",
"--nodiscover",
"--nat", "extip:127.0.0.1",
"--port", strconv.FormatInt(n.NodePort(), 10),
"--rpc",
"--rpcaddr", "127.0.0.1",
"--rpcport", strconv.FormatInt(n.RPCPort(), 10),
"--rpcapi", "eth,net,web3,debug,admin,personal,istanbul,txpool",
// "--nodiscover", "--nousb ",
"--unlock", addressToUnlock,
"--password", n.pwdFile(),
}
// Once we're sure we won't run v1.2.x and older, can get rid of this check
// and just use the new options
helpBytes, _ := exec.Command(n.GethPath, "--help").Output() // #nosec G204
useTxFeeRecipient := strings.Contains(string(helpBytes), "miner.validator")
if useTxFeeRecipient {
args = append(args,
"--miner.validator", n.Account.Address.Hex(),
"--tx-fee-recipient", n.TxFeeRecipientAccount.Address.Hex(),
)
} else {
args = append(args,
"--etherbase", n.Account.Address.Hex(),
)
}
if n.ExtraFlags != "" {
args = append(args, strings.Fields(n.ExtraFlags)...)
}
cmd := exec.Command(n.GethPath, args...) // #nosec G204
log.Println(n.GethPath, strings.Join(args, " "))
logfile, err := os.OpenFile(n.logFile(), os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
return err
}
defer logfile.Close()
cmd.Stderr = logfile
cmd.Stdout = os.Stdout
// cmd.Stderr = os.Stderr
if err := cmd.Start(); err != nil {
return err
}
// rpc, err := rpc.Dial(fmt.Sprintf("http://localhost:%d", n.RPCPort()))
// if err != nil {
// return err
// }
// rpc.CallContext(ctx, nil, "personal_unlock", )
go func() {
<-ctx.Done()
if err := cmd.Process.Signal(os.Interrupt); err != nil {
log.Fatal("Failed to send interrupt signal to geth cmd")
}
}()
return cmd.Wait()
}
func (n *Node) pwdFile() string { return path.Join(n.Datadir, "password") }
func (n *Node) logFile() string { return path.Join(n.Datadir, "geth.log") }
func (n *Node) keyFile() string { return path.Join(n.Datadir, "celo/nodekey") }
func (n *Node) staticNodesFile() string { return path.Join(n.Datadir, "/celo/static-nodes.json") }
func (n *Node) runSync(args ...string) ([]byte, error) {
args = append([]string{"--datadir", n.Datadir}, args...)
cmd := exec.Command(n.GethPath, args...) // #nosec G204
return cmd.CombinedOutput()
}