-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathrandom_test.go
271 lines (234 loc) · 7.41 KB
/
random_test.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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
// Copyright (c) 2018 The MATRIX Authors
// Distributed under the MIT software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php
package random
import (
"testing"
"github.com/MatrixAINetwork/go-matrix/consensus/ethash"
"github.com/MatrixAINetwork/go-matrix/core"
"github.com/MatrixAINetwork/go-matrix/params"
. "github.com/smartystreets/goconvey/convey"
"fmt"
"math/big"
"time"
"bou.ke/monkey"
"github.com/MatrixAINetwork/go-matrix/common"
"github.com/MatrixAINetwork/go-matrix/log"
"github.com/MatrixAINetwork/go-matrix/mc"
"github.com/MatrixAINetwork/go-matrix/random/commonsupport"
"github.com/MatrixAINetwork/go-matrix/random/electionseed"
)
func Monkey_NeedVote() *monkey.PatchGuard {
return monkey.Patch(electionseed.NeedVote, func(uint64) bool {
return true
})
}
func Monkey_GetKeyTransInfo() *monkey.PatchGuard {
return monkey.Patch(commonsupport.GetKeyTransInfo, func(uint64, string) map[common.Address][]byte {
ans := make(map[common.Address][]byte, 0)
return ans
})
}
func NewBlockChain(n int) *core.BlockChain {
_, bc, err := core.NewCanonical(ethash.NewFaker(), n, true)
if err != nil {
fmt.Println("生成blockchain失败")
}
return bc
}
type TestEth struct {
}
func (self *TestEth) BlockChain() *core.BlockChain {
return NewBlockChain(321)
}
func TestUnit0(t *testing.T) {
//子服务的注册
//因为子服务的注册时放在init出,所以执行永远在test文件执行之前,所以,不能在这里该参数,会晚
}
func TestUnit1(t *testing.T) {
}
func TestUnit2(t *testing.T) {
log.InitLog(3)
mapConfig := make(map[string]string)
mapConfig["electionseed"] = "Minhash&Key"
///mapConfig["everyblockseed"]="Nonce&Address&Coinbase"
params.RandomConfig = mapConfig
testEth := &TestEth{}
random, err := New(testEth)
Convey("初始化", t, func() {
So(err, ShouldBeNil)
})
random.Stop()
//time.Sleep(100*time.Second)
}
func TestUnit3(t *testing.T) {
log.InitLog(3)
mapConfig := make(map[string]string)
mapConfig["electionseed"] = "Minhash&Key"
mapConfig["everyblockseed"] = "Nonce&Address&Coinbase"
params.RandomConfig = mapConfig
testEth := &TestEth{}
random, err := New(testEth)
Convey("初始化", t, func() {
So(err, ShouldBeNil)
})
random.Stop()
//time.Sleep(100*time.Second)
}
func TestUnit4(t *testing.T) {
log.InitLog(3)
mapConfig := make(map[string]string)
mapConfig["ss"] = "sss"
mapConfig["cc"] = "ccc"
mapConfig["ff"] = "fff"
params.RandomConfig = mapConfig
testEth := &TestEth{}
random, err := New(testEth)
Convey("初始化", t, func() {
So(err, ShouldBeNil)
})
random.Stop()
}
func TestUnit5(t *testing.T) {
log.InitLog(3)
mapConfig := make(map[string]string)
mapConfig["electionseed"] = "Minhash&Key"
///mapConfig["everyblockseed"]="Nonce&Address&Coinbase"
params.RandomConfig = mapConfig
testEth := &TestEth{}
random, err := New(testEth)
Convey("初始化", t, func() {
So(err, ShouldBeNil)
})
SendNum := 0
go func() {
for {
time.Sleep(2 * time.Second)
mc.PublishEvent(mc.CA_RoleUpdated, &mc.RoleUpdatedMsg{BlockNum: uint64(0), Leader: common.BigToAddress(big.NewInt(100)), Role: common.RoleMiner})
if SendNum == 3 {
fmt.Println("SendNum", SendNum)
random.Stop()
}
SendNum++
}
}()
time.Sleep(100 * time.Second)
}
//RandomServiceName = []string{"electionseed", "everyblockseed", "everybroadcastseed"}
func TestUnit6(t *testing.T) {
log.InitLog(3)
mapConfig := make(map[string]string)
mapConfig["electionseed"] = "1"
///mapConfig["everyblockseed"]="Nonce&Address&Coinbase"
params.RandomConfig = mapConfig
testEth := &TestEth{}
_, err := New(testEth)
fmt.Println(err)
Convey("初始化", t, func() {
needVote := Monkey_NeedVote()
defer needVote.Unpatch()
for i := 95; i < 1000; i += 100 {
time.Sleep(2 * time.Second)
mc.PublishEvent(mc.CA_RoleUpdated, &mc.RoleUpdatedMsg{BlockNum: uint64(i), Leader: common.BigToAddress(big.NewInt(100)), Role: common.RoleMiner})
}
})
time.Sleep(100 * time.Second)
}
//RandomServiceName = []string{"electionseed", "everyblockseed", "everybroadcastseed"}
func TestUnit7(t *testing.T) {
log.InitLog(3)
mapConfig := make(map[string]string)
mapConfig["everyblockseed"] = "2"
///mapConfig["everyblockseed"]="Nonce&Address&Coinbase"
params.RandomConfig = mapConfig
testEth := &TestEth{}
_, err := New(testEth)
fmt.Println(err)
Convey("初始化", t, func() {
needVote := Monkey_NeedVote()
defer needVote.Unpatch()
for i := 90; i < 101; i++ {
time.Sleep(2 * time.Second)
mc.PublishEvent(mc.CA_RoleUpdated, &mc.RoleUpdatedMsg{BlockNum: uint64(i), Leader: common.BigToAddress(big.NewInt(100)), Role: common.RoleMiner})
}
})
time.Sleep(100 * time.Second)
}
func TestUnit8(t *testing.T) {
log.InitLog(3)
mapConfig := make(map[string]string)
mapConfig["everybroadcastseed"] = "2"
params.RandomConfig = mapConfig
testEth := &TestEth{}
_, err := New(testEth)
fmt.Println(err)
for i := 90; i <= 101; i++ {
time.Sleep(2 * time.Second)
mc.PublishEvent(mc.CA_RoleUpdated, &mc.RoleUpdatedMsg{BlockNum: uint64(i), Leader: common.BigToAddress(big.NewInt(100)), Role: common.RoleMiner})
}
time.Sleep(100 * time.Second)
}
func TestUnit9(t *testing.T) {
log.InitLog(3)
mapConfig := make(map[string]string)
mapConfig["electionseed"] = "Minhash&Key"
mapConfig["everybroadcastseed"] = "2"
mapConfig["everyblockseed"] = "2"
params.RandomConfig = mapConfig
testEth := &TestEth{}
_, err := New(testEth)
fmt.Println(err)
Convey("初始化", t, func() {
needVote := Monkey_NeedVote()
defer needVote.Unpatch()
for i := 90; i <= 101; i++ {
time.Sleep(2 * time.Second)
mc.PublishEvent(mc.CA_RoleUpdated, &mc.RoleUpdatedMsg{BlockNum: uint64(i), Leader: common.BigToAddress(big.NewInt(100)), Role: common.RoleMiner})
}
})
time.Sleep(100 * time.Second)
}
func TestUnit10(t *testing.T) {
//log.InitLog(3)
mapConfig := make(map[string]string)
mapConfig["electionseed"] = "Minhash&Key"
mapConfig["everybroadcastseed"] = "2"
mapConfig["everyblockseed"] = "2"
params.RandomConfig = mapConfig
testEth := &TestEth{}
_, err := New(testEth)
fmt.Println(err)
fmt.Println(testEth.BlockChain().GetBlockByNumber(0).Header().Leader)
fmt.Println(time.Now())
ans := testEth.BlockChain().GetBlockByNumber(1).Hash().Big()
fmt.Println(time.Now())
ansi := 1
fmt.Println(time.Now())
for i := 1; i <= 100; i++ {
temp := testEth.BlockChain().GetBlockByNumber(uint64(i)).Hash().Big()
if ans.Cmp(temp) >= 0 {
ans = temp
ansi = i
}
}
fmt.Println(time.Now())
fmt.Println("---ans,", ans, "ansi", ansi)
Convey("获取选举种子数据", t, func() {
Getkey := Monkey_GetKeyTransInfo()
defer Getkey.Unpatch()
ans, err := GetRandom(100, "electionseed")
fmt.Println("electionseed-100-------", ans, "err", err, "uint64", ans.Uint64())
})
fmt.Println("nonce", testEth.BlockChain().GetBlockByNumber(34).Header().Nonce, "leader", testEth.BlockChain().GetBlockByNumber(34).Header().Leader)
Convey("获取每个块一个的随机数", t, func() {
ans, err := GetRandom(34, "everyblockseed")
fmt.Println("everyblockseed-34------", ans, "err", err)
})
Convey("每个广播区块一个的随机数", t, func() {
Getkey := Monkey_GetKeyTransInfo()
defer Getkey.Unpatch()
fmt.Println("nonce", testEth.BlockChain().GetBlockByNumber(100).Header().Nonce.Uint64(), "leader", testEth.BlockChain().GetBlockByNumber(100).Header().Leader)
ans, err := GetRandom(100, "everybroadcastseed")
fmt.Println("everybroadcastseed-100---", ans, "err", err)
})
}