forked from hyperledger/fabric
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_helper.go
236 lines (208 loc) · 8.28 KB
/
test_helper.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
/*
Copyright IBM Corp. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/
package testutil
import (
"testing"
"github.com/golang/protobuf/proto"
"github.com/hyperledger/fabric/common/configtx/test"
"github.com/hyperledger/fabric/common/util"
lutils "github.com/hyperledger/fabric/core/ledger/util"
"github.com/hyperledger/fabric/protos/common"
pb "github.com/hyperledger/fabric/protos/peer"
ptestutils "github.com/hyperledger/fabric/protos/testutils"
"github.com/hyperledger/fabric/protos/utils"
"github.com/stretchr/testify/assert"
)
//BlockGenerator generates a series of blocks for testing
type BlockGenerator struct {
blockNum uint64
previousHash []byte
signTxs bool
t *testing.T
}
type TxDetails struct {
TxID string
ChaincodeName, ChaincodeVersion string
SimulationResults []byte
}
type BlockDetails struct {
BlockNum uint64
PreviousHash []byte
Txs []*TxDetails
}
// NewBlockGenerator instantiates new BlockGenerator for testing
func NewBlockGenerator(t *testing.T, ledgerID string, signTxs bool) (*BlockGenerator, *common.Block) {
gb, err := test.MakeGenesisBlock(ledgerID)
assert.NoError(t, err)
gb.Metadata.Metadata[common.BlockMetadataIndex_TRANSACTIONS_FILTER] = lutils.NewTxValidationFlagsSetValue(len(gb.Data.Data), pb.TxValidationCode_VALID)
return &BlockGenerator{1, gb.GetHeader().Hash(), signTxs, t}, gb
}
// NextBlock constructs next block in sequence that includes a number of transactions - one per simulationResults
func (bg *BlockGenerator) NextBlock(simulationResults [][]byte) *common.Block {
block := ConstructBlock(bg.t, bg.blockNum, bg.previousHash, simulationResults, bg.signTxs)
bg.blockNum++
bg.previousHash = block.Header.Hash()
return block
}
// NextBlockWithTxid constructs next block in sequence that includes a number of transactions - one per simulationResults
func (bg *BlockGenerator) NextBlockWithTxid(simulationResults [][]byte, txids []string) *common.Block {
// Length of simulationResults should be same as the length of txids.
if len(simulationResults) != len(txids) {
return nil
}
block := ConstructBlockWithTxid(bg.t, bg.blockNum, bg.previousHash, simulationResults, txids, bg.signTxs)
bg.blockNum++
bg.previousHash = block.Header.Hash()
return block
}
// NextTestBlock constructs next block in sequence block with 'numTx' number of transactions for testing
func (bg *BlockGenerator) NextTestBlock(numTx int, txSize int) *common.Block {
simulationResults := [][]byte{}
for i := 0; i < numTx; i++ {
simulationResults = append(simulationResults, ConstructRandomBytes(bg.t, txSize))
}
return bg.NextBlock(simulationResults)
}
// NextTestBlocks constructs 'numBlocks' number of blocks for testing
func (bg *BlockGenerator) NextTestBlocks(numBlocks int) []*common.Block {
blocks := []*common.Block{}
numTx := 10
for i := 0; i < numBlocks; i++ {
block := bg.NextTestBlock(numTx, 100)
block.Metadata.Metadata[common.BlockMetadataIndex_TRANSACTIONS_FILTER] = lutils.NewTxValidationFlagsSetValue(numTx, pb.TxValidationCode_VALID)
blocks = append(blocks, block)
}
return blocks
}
// ConstructTransaction constructs a transaction for testing
func ConstructTransaction(_ *testing.T, simulationResults []byte, txid string, sign bool) (*common.Envelope, string, error) {
return ConstructTransactionFromTxDetails(
&TxDetails{
ChaincodeName: "foo",
ChaincodeVersion: "v1",
TxID: txid,
SimulationResults: simulationResults,
},
sign,
)
}
func ConstructTransactionFromTxDetails(txDetails *TxDetails, sign bool) (*common.Envelope, string, error) {
ccid := &pb.ChaincodeID{
Name: txDetails.ChaincodeName,
Version: txDetails.ChaincodeVersion,
}
var txEnv *common.Envelope
var err error
var txID string
if sign {
txEnv, txID, err = ptestutils.ConstructSignedTxEnvWithDefaultSigner(util.GetTestChainID(), ccid, nil, txDetails.SimulationResults, txDetails.TxID, nil, nil)
} else {
txEnv, txID, err = ptestutils.ConstructUnsignedTxEnv(util.GetTestChainID(), ccid, nil, txDetails.SimulationResults, txDetails.TxID, nil, nil)
}
return txEnv, txID, err
}
func ConstructBlockFromBlockDetails(t *testing.T, blockDetails *BlockDetails, sign bool) *common.Block {
var envs []*common.Envelope
for _, txDetails := range blockDetails.Txs {
env, _, err := ConstructTransactionFromTxDetails(txDetails, sign)
if err != nil {
t.Fatalf("ConstructTestTransaction failed, err %s", err)
}
envs = append(envs, env)
}
return NewBlock(envs, blockDetails.BlockNum, blockDetails.PreviousHash)
}
func ConstructBlockWithTxid(t *testing.T, blockNum uint64, previousHash []byte, simulationResults [][]byte, txids []string, sign bool) *common.Block {
envs := []*common.Envelope{}
for i := 0; i < len(simulationResults); i++ {
env, _, err := ConstructTransaction(t, simulationResults[i], txids[i], sign)
if err != nil {
t.Fatalf("ConstructTestTransaction failed, err %s", err)
}
envs = append(envs, env)
}
return NewBlock(envs, blockNum, previousHash)
}
// ConstructBlock constructs a single block
func ConstructBlock(t *testing.T, blockNum uint64, previousHash []byte, simulationResults [][]byte, sign bool) *common.Block {
envs := []*common.Envelope{}
for i := 0; i < len(simulationResults); i++ {
env, _, err := ConstructTransaction(t, simulationResults[i], "", sign)
if err != nil {
t.Fatalf("ConstructTestTransaction failed, err %s", err)
}
envs = append(envs, env)
}
return NewBlock(envs, blockNum, previousHash)
}
//ConstructTestBlock constructs a single block with random contents
func ConstructTestBlock(t *testing.T, blockNum uint64, numTx int, txSize int) *common.Block {
simulationResults := [][]byte{}
for i := 0; i < numTx; i++ {
simulationResults = append(simulationResults, ConstructRandomBytes(t, txSize))
}
return ConstructBlock(t, blockNum, ConstructRandomBytes(t, 32), simulationResults, false)
}
// ConstructTestBlocks returns a series of blocks starting with blockNum=0.
// The first block in the returned array is a config tx block that represents a genesis block
// Except the genesis block, the size of each of the block would be the same.
func ConstructTestBlocks(t *testing.T, numBlocks int) []*common.Block {
bg, gb := NewBlockGenerator(t, util.GetTestChainID(), false)
blocks := []*common.Block{}
if numBlocks != 0 {
blocks = append(blocks, gb)
}
return append(blocks, bg.NextTestBlocks(numBlocks-1)...)
}
// ConstructBytesProposalResponsePayload constructs a ProposalResponse byte with given chaincode version and simulationResults for testing
func ConstructBytesProposalResponsePayload(version string, simulationResults []byte) ([]byte, error) {
ccid := &pb.ChaincodeID{
Name: "foo",
Version: version,
}
return ptestutils.ConstructBytesProposalResponsePayload(util.GetTestChainID(), ccid, nil, simulationResults)
}
func NewBlock(env []*common.Envelope, blockNum uint64, previousHash []byte) *common.Block {
block := common.NewBlock(blockNum, previousHash)
for i := 0; i < len(env); i++ {
txEnvBytes, _ := proto.Marshal(env[i])
block.Data.Data = append(block.Data.Data, txEnvBytes)
}
block.Header.DataHash = block.Data.Hash()
utils.InitBlockMetadata(block)
block.Metadata.Metadata[common.BlockMetadataIndex_TRANSACTIONS_FILTER] = lutils.NewTxValidationFlagsSetValue(len(env), pb.TxValidationCode_VALID)
return block
}
func SetTxID(t *testing.T, block *common.Block, txNum int, txID string) {
envelopeBytes := block.Data.Data[txNum]
envelope, err := utils.UnmarshalEnvelope(envelopeBytes)
if err != nil {
t.Fatalf("error unmarshaling envelope: %s", err)
}
payload, err := utils.GetPayload(envelope)
if err != nil {
t.Fatalf("error getting payload from envelope: %s", err)
}
channelHeader, err := utils.UnmarshalChannelHeader(payload.Header.ChannelHeader)
if err != nil {
t.Fatalf("error unmarshaling channel header: %s", err)
}
channelHeader.TxId = txID
channelHeaderBytes, err := proto.Marshal(channelHeader)
if err != nil {
t.Fatalf("error marshaling channel header: %s", err)
}
payload.Header.ChannelHeader = channelHeaderBytes
payloadBytes, err := proto.Marshal(payload)
if err != nil {
t.Fatalf("error marshaling payload: %s", err)
}
envelope.Payload = payloadBytes
envelopeBytes, err = proto.Marshal(envelope)
if err != nil {
t.Fatalf("error marshaling envelope: %s", err)
}
block.Data.Data[txNum] = envelopeBytes
}