forked from kaspanet/kaspad
-
Notifications
You must be signed in to change notification settings - Fork 0
/
node.go
203 lines (171 loc) · 5.21 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
package main
import (
"fmt"
"github.com/zoomy-network/zoomyd/domain/consensus"
"os/exec"
"strings"
"sync/atomic"
"syscall"
"time"
"github.com/pkg/errors"
"github.com/zoomy-network/zoomyd/app/appmessage"
"github.com/zoomy-network/zoomyd/stability-tests/common"
"github.com/zoomy-network/zoomyd/stability-tests/common/mine"
"github.com/zoomy-network/zoomyd/stability-tests/common/rpc"
"github.com/zoomy-network/zoomyd/util"
"github.com/zoomy-network/zoomyd/util/panics"
)
const (
syncerRPCAddress = "localhost:9000"
syncedRPCAddress = "localhost:9100"
syncerListen = "localhost:9001"
syncedListen = "localhost:9101"
)
func startNode(name string, rpcAddress, listen, connect, profilePort, dataDir string) (*exec.Cmd, func(), error) {
log.Infof("Data directory for %s is %s", name, dataDir)
args := []string{
"kaspad",
common.NetworkCliArgumentFromNetParams(activeConfig().NetParams()),
"--appdir", dataDir,
"--logdir", dataDir,
"--rpclisten", rpcAddress,
"--listen", listen,
"--profile", profilePort,
"--loglevel", "debug",
"--allow-submit-block-when-not-synced",
}
if connect != "" {
args = append(args, "--connect", connect)
}
if activeConfig().OverrideDAGParamsFile != "" {
args = append(args, "--override-dag-params-file", activeConfig().OverrideDAGParamsFile)
}
cmd, err := common.StartCmd(name,
args...,
)
if err != nil {
return nil, nil, err
}
var shutdown uint32
stopped := make(chan struct{})
spawn("startNode-cmd.Wait", func() {
err := cmd.Wait()
if err != nil {
if atomic.LoadUint32(&shutdown) == 0 {
panics.Exit(log, fmt.Sprintf("%s ( %s ) closed unexpectedly: %s", name, cmd, err))
}
if !strings.Contains(err.Error(), "signal: killed") {
panics.Exit(log, fmt.Sprintf("%s ( %s ) closed with an error: %s", name, cmd, err))
}
}
stopped <- struct{}{}
})
return cmd, func() {
atomic.StoreUint32(&shutdown, 1)
killWithSigkill(cmd, name)
const timeout = time.Second
select {
case <-stopped:
case <-time.After(timeout):
panics.Exit(log, fmt.Sprintf("%s couldn't be closed after %s", name, timeout))
}
}, nil
}
func killWithSigkill(cmd *exec.Cmd, commandName string) {
log.Error("SIGKILLED")
err := cmd.Process.Signal(syscall.SIGKILL)
if err != nil {
log.Criticalf("error sending SIGKILL to %s", commandName)
}
}
func setupNodeWithRPC(name, listen, rpcListen, connect, profilePort, dataDir string) (*rpc.Client, func(), error) {
_, teardown, err := startNode(name, rpcListen, listen, connect, profilePort, dataDir)
if err != nil {
return nil, nil, errors.Wrap(err, "error in startNode")
}
defer func() {
if r := recover(); r != nil {
teardown()
panic(r)
}
}()
log.Infof("Waiting for node %s to start...", name)
const initTime = 2 * time.Second
time.Sleep(initTime)
rpcClient, err := rpc.ConnectToRPC(&rpc.Config{
RPCServer: rpcListen,
}, activeConfig().NetParams())
if err != nil {
return nil, nil, errors.Wrap(err, "error connecting to JSON-RPC server")
}
return rpcClient, teardown, nil
}
func setupSyncee() (*rpc.Client, func(), error) {
const syncedProfilePort = "6061"
synceeDataDir, err := useDirOrCreateTemp(activeConfig().SynceeDataDirectory, "syncee-kaspad-data-dir")
if err != nil {
return nil, nil, err
}
return setupNodeWithRPC("SYNCEE", syncedListen, syncedRPCAddress, syncerListen, syncedProfilePort,
synceeDataDir)
}
func setupSyncer() (*rpc.Client, func(), error) {
const syncerProfilePort = "6062"
syncerDataDir, err := useDirOrCreateTemp(activeConfig().SyncerDataDirectory, "syncer-kaspad-data-dir")
if err != nil {
return nil, nil, err
}
rpcClient, teardown, err := setupNodeWithRPC("SYNCER", syncerListen, syncerRPCAddress, "",
syncerProfilePort, syncerDataDir)
if err != nil {
return nil, nil, err
}
defer func() {
if r := recover(); r != nil {
teardown()
panic(r)
}
}()
miningDataDir, err := useDirOrCreateTemp(activeConfig().MiningDataDirectory, "syncer-mining-data-dir")
if err != nil {
return nil, nil, err
}
err = mine.FromFile(cfg.DAGFile, &consensus.Config{Params: *activeConfig().NetParams()}, rpcClient, miningDataDir)
if err != nil {
return nil, nil, errors.Wrap(err, "error in mine.FromFile")
}
log.Info("Mining on top of syncer tips")
rejectReason, err := mineOnTips(rpcClient)
if err != nil {
panic(err)
}
if rejectReason != appmessage.RejectReasonNone {
panic(fmt.Sprintf("mined block rejected: %s", rejectReason))
}
return rpcClient, teardown, nil
}
func useDirOrCreateTemp(dataDir, tempName string) (string, error) {
if dataDir != "" {
return dataDir, nil
}
return common.TempDir(tempName)
}
func mineOnTips(client *rpc.Client) (appmessage.RejectReason, error) {
fakePublicKey := make([]byte, util.PublicKeySize)
addr, err := util.NewAddressPublicKey(fakePublicKey, activeConfig().NetParams().Prefix)
if err != nil {
return appmessage.RejectReasonNone, err
}
template, err := client.GetBlockTemplate(addr.String(), "")
if err != nil {
return appmessage.RejectReasonNone, err
}
domainBlock, err := appmessage.RPCBlockToDomainBlock(template.Block)
if err != nil {
return appmessage.RejectReasonNone, err
}
if !activeConfig().NetParams().SkipProofOfWork {
mine.SolveBlock(domainBlock)
}
return client.SubmitBlockAlsoIfNonDAA(domainBlock)
}