-
Notifications
You must be signed in to change notification settings - Fork 47
/
mock.go
68 lines (55 loc) · 1.92 KB
/
mock.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
// 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 candidate
import (
"context"
"github.com/dusk-network/dusk-blockchain/pkg/core/consensus"
"github.com/dusk-network/dusk-blockchain/pkg/core/consensus/header"
"github.com/dusk-network/dusk-blockchain/pkg/core/data/block"
"github.com/dusk-network/dusk-blockchain/pkg/core/data/ipc/transactions"
"github.com/dusk-network/dusk-blockchain/pkg/p2p/wire/message"
crypto "github.com/dusk-network/dusk-crypto/hash"
)
// MockCandidate ...
func (m *mock) MockCandidate(hdr header.Header, previousBlock []byte) block.Block {
if previousBlock == nil {
previousBlock, _ = crypto.RandEntropy(32)
}
seed, _ := crypto.RandEntropy(32)
b, err := m.GenerateBlock(context.Background(), hdr.Round, seed, previousBlock, 0)
if err != nil {
panic(err)
}
return *b
}
type mock struct {
*generator
}
func (m *mock) GenerateCandidateMessage(ctx context.Context, r consensus.RoundUpdate, step uint8) (*message.NewBlock, error) {
hdr := header.Header{
PubKeyBLS: make([]byte, 96),
Round: r.Round,
Step: step,
BlockHash: make([]byte, 32),
}
cand := m.MockCandidate(hdr, nil)
mockNewBlock := message.MockNewBlock(hdr, cand)
return &mockNewBlock, nil
}
// Mock the candidate generator.
func Mock(e *consensus.Emitter) Generator {
fn := func(ctx context.Context, txs []transactions.ContractCall, bh uint64, gasLimit uint64, generator []byte) ([]transactions.ContractCall, []byte, error) {
if len(txs) == 0 {
// Function simulates Rusk ExecuteStateTransition.
// That said, it should always provide a Distribute transaction
txs = make([]transactions.ContractCall, 0)
}
return txs, make([]byte, 32), nil
}
return &mock{
generator: New(e, fn).(*generator),
}
}