-
Notifications
You must be signed in to change notification settings - Fork 47
/
testutil.go
93 lines (80 loc) · 2.57 KB
/
testutil.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
// This Source Code Form is subject to the terms of the MIT License.
// If a copy of the MIT License was not distributed with this
// file, you can obtain one at https://opensource.org/licenses/MIT.
//
// Copyright (c) DUSK NETWORK. All rights reserved.
package selection
import (
"time"
"github.com/dusk-network/dusk-blockchain/pkg/config/genesis"
"github.com/dusk-network/dusk-blockchain/pkg/core/consensus"
"github.com/dusk-network/dusk-blockchain/pkg/core/consensus/blockgenerator/candidate"
"github.com/dusk-network/dusk-blockchain/pkg/core/consensus/header"
"github.com/dusk-network/dusk-blockchain/pkg/core/consensus/key"
"github.com/dusk-network/dusk-blockchain/pkg/core/consensus/user"
"github.com/dusk-network/dusk-blockchain/pkg/p2p/wire/message"
crypto "github.com/dusk-network/dusk-crypto/hash"
)
// ProvisionerNr is the default amount of Provisioners utilized in the
// selection tests. This nr is just used to create the RoundUpdate and bares no
// importance in the selection step.
var ProvisionerNr = 10
// Helper for reducing selection test boilerplate.
type Helper struct {
*consensus.Emitter
Round uint64
Step uint8
scoreToSpawn int
P *user.Provisioners
}
// NewHelper creates a Helper.
func NewHelper(scoreToSpawn int) *Helper {
p, provisionersKeys := consensus.MockProvisioners(ProvisionerNr)
emitter := consensus.MockEmitter(time.Second)
emitter.Keys = provisionersKeys[0]
hlp := &Helper{
Emitter: emitter,
Round: uint64(1),
Step: uint8(1),
scoreToSpawn: scoreToSpawn,
P: p,
}
genHlp := &candidate.Helper{
ThisSender: emitter.Keys.BLSPubKey,
ProvisionersKeys: provisionersKeys,
P: p,
Nr: ProvisionerNr,
Emitter: emitter,
}
genHlp.MockRPCCalls(provisionersKeys)
return hlp
}
// RoundUpdate mocks a round update with the Round and Step embedded in the
// Helper.
func (h *Helper) RoundUpdate() consensus.RoundUpdate {
hash, _ := crypto.RandEntropy(32)
seed, _ := crypto.RandEntropy(32)
return consensus.RoundUpdate{
Round: h.Round,
Hash: hash,
Seed: seed,
P: *h.P,
}
}
// Spawn a number of score events.
func (h *Helper) Spawn() []message.NewBlock {
evs := make([]message.NewBlock, 0, h.scoreToSpawn)
for i := 0; i < h.scoreToSpawn; i++ {
hash, _ := crypto.RandEntropy(32)
keys := key.NewRandKeys()
hdr := header.Header{
Round: h.Round,
Step: h.Step,
PubKeyBLS: keys.BLSPubKey,
BlockHash: hash,
}
genesis := genesis.Decode()
evs = append(evs, message.MockNewBlock(hdr, *genesis))
}
return evs
}