-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsimplechainbuilder.go
149 lines (130 loc) · 4.23 KB
/
simplechainbuilder.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
package coinharness
import (
"fmt"
"github.com/jfixby/pin"
"github.com/jfixby/pin/commandline"
"strconv"
"strings"
)
// DeploySimpleChain defines harness setup sequence for this package:
// 1. obtains a new mining wallet address
// 2. restart harness node and wallet with the new mining address
// 3. builds a new chain with the target number of mature outputs
// receiving the mining reward to the test wallet
// 4. syncs wallet to the tip of the chain
func DeploySimpleChain(testSetup *ChainWithMatureOutputsSpawner, h *Harness) {
pin.AssertNotEmpty("harness name", h.Name)
fmt.Println("Deploying Harness[" + h.Name + "]")
createFlag := testSetup.CreateTempWallet
// launch a fresh h (assumes h working dir is empty)
{
args := &launchArguments{
DebugNodeOutput: testSetup.DebugNodeOutput,
DebugWalletOutput: testSetup.DebugWalletOutput,
NodeExtraArguments: testSetup.NodeStartExtraArguments,
}
if createFlag {
args.WalletExtraArguments = make(map[string]interface{})
args.WalletExtraArguments["createtemp"] = commandline.NoArgumentValue
}
launchHarnessSequence(h, args)
}
// Get a new address from the WalletTestServer
// to be set with node --miningaddr
var address Address
var err error
{
for {
address, err = h.Wallet.NewAddress(DefaultAccountName)
if err != nil {
pin.D("address", address)
pin.D("error", err)
pin.Sleep(1000)
} else {
break
}
}
//pin.CheckTestSetupMalfunction(err)
h.MiningAddress = address
pin.AssertNotNil("MiningAddress", h.MiningAddress)
pin.AssertNotEmpty("MiningAddress", h.MiningAddress.String())
fmt.Println("Mining address: " + h.MiningAddress.String())
}
// restart the h with the new argument
{
shutdownHarnessSequence(h)
args := &launchArguments{
DebugNodeOutput: testSetup.DebugNodeOutput,
DebugWalletOutput: testSetup.DebugWalletOutput,
NodeExtraArguments: testSetup.NodeStartExtraArguments,
}
if createFlag {
args.WalletExtraArguments = make(map[string]interface{})
args.WalletExtraArguments["createtemp"] = commandline.NoArgumentValue
}
launchHarnessSequence(h, args)
}
{
if testSetup.NumMatureOutputs > 0 {
numToGenerate := int64(testSetup.ActiveNet.CoinbaseMaturity()) + testSetup.NumMatureOutputs
err := GenerateTestChain(numToGenerate, h.NodeRPCClient())
pin.CheckTestSetupMalfunction(err)
}
// wait for the WalletTestServer to sync up to the current height
_, H, e := h.NodeRPCClient().GetBestBlock()
pin.CheckTestSetupMalfunction(e)
h.Wallet.Sync(H)
}
fmt.Println("Harness[" + h.Name + "] is ready")
}
// local struct to bundle launchHarnessSequence function arguments
type launchArguments struct {
DebugNodeOutput bool
DebugWalletOutput bool
MiningAddress Address
NodeExtraArguments map[string]interface{}
WalletExtraArguments map[string]interface{}
}
// launchHarnessSequence
func launchHarnessSequence(h *Harness, args *launchArguments) {
node := h.Node
wallet := h.Wallet
sargs := &StartNodeArgs{
DebugOutput: args.DebugNodeOutput,
MiningAddress: h.MiningAddress,
ExtraArguments: args.NodeExtraArguments,
}
node.Start(sargs)
rpcConfig := node.RPCConnectionConfig()
walletLaunchArguments := &TestWalletStartArgs{
NodeRPCCertFile: node.CertFile(),
DebugOutput: args.DebugWalletOutput,
MaxSecondsToWaitOnLaunch: 90,
NodeRPCConfig: rpcConfig,
ExtraArguments: args.WalletExtraArguments,
}
// wait for the WalletTestServer to sync up to the current height
_, _, e := h.NodeRPCClient().GetBestBlock()
pin.CheckTestSetupMalfunction(e)
wallet.Start(walletLaunchArguments)
}
// shutdownHarnessSequence reverses the launchHarnessSequence
func shutdownHarnessSequence(harness *Harness) {
harness.Wallet.Stop()
harness.Node.Stop()
}
// ExtractSeedSaltFromHarnessName tries to split harness name string
// at `.`-character and parse the second part as a uint32 number.
// Otherwise returns default value.
func ExtractSeedSaltFromHarnessName(harnessName string) uint32 {
parts := strings.Split(harnessName, ".")
if len(parts) != 2 {
// no salt specified, return default value
return 0
}
seedString := parts[1]
tmp, err := strconv.Atoi(seedString)
seedNonce := uint32(tmp)
pin.CheckTestSetupMalfunction(err)
return seedNonce
}