-
Notifications
You must be signed in to change notification settings - Fork 361
/
block.go
149 lines (130 loc) · 3.78 KB
/
block.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 prottest
import (
"context"
"sync"
"testing"
"time"
"chain/crypto/ed25519"
"chain/protocol"
"chain/protocol/bc"
"chain/protocol/bc/legacy"
"chain/protocol/prottest/memstore"
"chain/protocol/state"
"chain/testutil"
)
var (
mutex sync.Mutex // protects the following
states = make(map[*protocol.Chain]*state.Snapshot)
blockPubkeys = make(map[*protocol.Chain][]ed25519.PublicKey)
blockPrivkeys = make(map[*protocol.Chain][]ed25519.PrivateKey)
)
type Option func(testing.TB, *config)
func WithStore(store protocol.Store) Option {
return func(_ testing.TB, conf *config) { conf.store = store }
}
func WithOutputIDs(outputIDs ...bc.Hash) Option {
return func(_ testing.TB, conf *config) {
for _, oid := range outputIDs {
conf.initialState.Tree.Insert(oid.Bytes())
}
}
}
func WithBlockSigners(quorum, n int) Option {
return func(tb testing.TB, conf *config) {
conf.quorum = quorum
for i := 0; i < n; i++ {
pubkey, privkey, err := ed25519.GenerateKey(nil)
if err != nil {
testutil.FatalErr(tb, err)
}
conf.pubkeys = append(conf.pubkeys, pubkey)
conf.privkeys = append(conf.privkeys, privkey)
}
}
}
type config struct {
store protocol.Store
initialState *state.Snapshot
pubkeys []ed25519.PublicKey
privkeys []ed25519.PrivateKey
quorum int
}
// NewChain makes a new Chain. By default it uses a memstore for
// storage and creates an initial block using a 0/0 multisig program.
// It commits the initial block before returning the Chain.
//
// Its defaults may be overridden by providing Options.
func NewChain(tb testing.TB, opts ...Option) *protocol.Chain {
conf := config{store: memstore.New(), initialState: state.Empty()}
for _, opt := range opts {
opt(tb, &conf)
}
ctx := context.Background()
b1, err := protocol.NewInitialBlock(conf.pubkeys, conf.quorum, time.Now())
if err != nil {
testutil.FatalErr(tb, err)
}
c, err := protocol.NewChain(ctx, b1.Hash(), conf.store, nil)
if err != nil {
testutil.FatalErr(tb, err)
}
c.MaxIssuanceWindow = 48 * time.Hour // TODO(tessr): consider adding MaxIssuanceWindow to NewChain
err = c.CommitAppliedBlock(ctx, b1, conf.initialState)
if err != nil {
testutil.FatalErr(tb, err)
}
// save block-signing keys in global state
mutex.Lock()
blockPubkeys[c] = conf.pubkeys
blockPrivkeys[c] = conf.privkeys
mutex.Unlock()
return c
}
// Initial returns the provided Chain's initial block.
func Initial(tb testing.TB, c *protocol.Chain) *legacy.Block {
ctx := context.Background()
b1, err := c.GetBlock(ctx, 1)
if err != nil {
testutil.FatalErr(tb, err)
}
return b1
}
// BlockKeyPairs returns the configured block-signing key-pairs
// for the provided Chain.
func BlockKeyPairs(c *protocol.Chain) ([]ed25519.PublicKey, []ed25519.PrivateKey) {
mutex.Lock()
defer mutex.Unlock()
return blockPubkeys[c], blockPrivkeys[c]
}
// MakeBlock makes a new block from txs, commits it, and returns it.
// It assumes c's consensus program requires 0 signatures.
// (This is true for chains returned by NewChain.)
// If c requires more than 0 signatures, MakeBlock will fail.
// MakeBlock always makes a block;
// if there are no transactions in txs,
// it makes an empty block.
func MakeBlock(tb testing.TB, c *protocol.Chain, txs []*legacy.Tx) *legacy.Block {
ctx := context.Background()
curBlock, err := c.GetBlock(ctx, c.Height())
if err != nil {
testutil.FatalErr(tb, err)
}
mutex.Lock()
curState := states[c]
mutex.Unlock()
if curState == nil {
curState = state.Empty()
}
nextBlock, nextState, err := c.GenerateBlock(ctx, curBlock, curState, time.Now(), txs)
if err != nil {
testutil.FatalErr(tb, err)
}
err = c.CommitAppliedBlock(ctx, nextBlock, nextState)
if err != nil {
testutil.FatalErr(tb, err)
}
mutex.Lock()
states[c] = nextState
mutex.Unlock()
return nextBlock
}