-
Notifications
You must be signed in to change notification settings - Fork 9
/
mineloop.go
144 lines (122 loc) · 4.2 KB
/
mineloop.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
package main
import (
"time"
"github.com/sedracoin/sedrad/go-secp256k1"
"github.com/sedracoin/sedrad/app/appmessage"
"github.com/sedracoin/sedrad/stability-tests/common"
"github.com/sedracoin/sedrad/stability-tests/common/rpc"
"github.com/sedracoin/sedrad/util"
"github.com/pkg/errors"
)
func mineLoop(syncerRPCClient, syncedRPCClient *rpc.Client) error {
miningAddr, err := generateAddress()
if err != nil {
return err
}
syncerBlockCountBefore, err := syncerRPCClient.GetBlockCount()
if err != nil {
return err
}
syncedBlockCountBefore, err := syncedRPCClient.GetBlockCount()
if err != nil {
return err
}
log.Infof("Starting to mine")
totalTime := time.Duration(0)
for i := uint64(0); i < activeConfig().NumberOfBlocks; i++ {
log.Infof("Mining block %d...", i+1)
err = mineBlock(syncerRPCClient.Address(), miningAddr)
if err != nil {
// Ignore error and instead check that the block count changed correctly.
// TODO: Fix the race condition in sedraminer so it won't panic (proper shutdown handler)
log.Warnf("mineBlock returned an err: %s", err)
}
start := time.Now()
const timeToPropagate = 60 * time.Second
select {
case <-syncedRPCClient.OnBlockAdded:
case <-time.After(timeToPropagate):
return errors.Errorf("block %d took more than %s to propagate", i+1, timeToPropagate)
}
totalTime += time.Since(start)
syncerResult, err := syncerRPCClient.GetBlockDAGInfo()
if err != nil {
return err
}
syncedResult, err := syncedRPCClient.GetBlockDAGInfo()
if err != nil {
return err
}
if !areTipsAreEqual(syncedResult, syncerResult) {
return errors.Errorf("syncer node has tips %s but synced node has tips %s", syncerResult.TipHashes, syncedResult.TipHashes)
}
}
const expectedAveragePropagationTime = time.Second
averagePropagationTime := totalTime / time.Duration(activeConfig().NumberOfBlocks)
if averagePropagationTime > expectedAveragePropagationTime {
return errors.Errorf("average block propagation time %s is higher than expected (%s)", averagePropagationTime, expectedAveragePropagationTime)
}
log.Infof("Finished to mine")
log.Infof("Getting syncer block count")
syncerBlockCountAfter, err := syncerRPCClient.GetBlockCount()
if err != nil {
return err
}
log.Infof("Getting syncee block count")
syncedBlockCountAfter, err := syncedRPCClient.GetBlockCount()
if err != nil {
return err
}
if syncerBlockCountAfter.BlockCount-syncerBlockCountBefore.BlockCount != activeConfig().NumberOfBlocks {
return errors.Errorf("Expected to mine %d blocks, instead mined: %d", activeConfig().NumberOfBlocks, syncerBlockCountAfter.BlockCount-syncerBlockCountBefore.BlockCount)
}
if syncedBlockCountAfter.BlockCount-syncedBlockCountBefore.BlockCount != activeConfig().NumberOfBlocks {
return errors.Errorf("Expected syncer to have %d new blocks, instead have: %d", activeConfig().NumberOfBlocks, syncedBlockCountAfter.BlockCount-syncedBlockCountBefore.BlockCount)
}
log.Infof("Finished the mine loop successfully")
return nil
}
func generateAddress() (util.Address, error) {
privateKey, err := secp256k1.GenerateSchnorrKeyPair()
if err != nil {
return nil, err
}
pubKey, err := privateKey.SchnorrPublicKey()
if err != nil {
return nil, err
}
pubKeySerialized, err := pubKey.Serialize()
if err != nil {
return nil, err
}
return util.NewAddressPublicKey(pubKeySerialized[:], activeConfig().ActiveNetParams.Prefix)
}
func areTipsAreEqual(resultA, resultB *appmessage.GetBlockDAGInfoResponseMessage) bool {
if len(resultA.TipHashes) != len(resultB.TipHashes) {
return false
}
tipsASet := make(map[string]struct{})
for _, tip := range resultA.TipHashes {
tipsASet[tip] = struct{}{}
}
for _, tip := range resultB.TipHashes {
if _, ok := tipsASet[tip]; !ok {
return false
}
}
return true
}
func mineBlock(syncerRPCAddress string, miningAddress util.Address) error {
sedraMinerCmd, err := common.StartCmd("MINER",
"sedraminer",
common.NetworkCliArgumentFromNetParams(activeConfig().NetParams()),
"-s", syncerRPCAddress,
"--mine-when-not-synced",
"--miningaddr", miningAddress.EncodeAddress(),
"--numblocks", "1",
)
if err != nil {
return err
}
return errors.Wrapf(sedraMinerCmd.Wait(), "error with command '%s'", sedraMinerCmd)
}