test_helper.go 3.89 KB
Newer Older
manish's avatar
manish committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
/*
Copyright IBM Corp. 2016 All Rights Reserved.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

		 http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package testutil

import (
	"testing"

	"github.com/golang/protobuf/proto"
Jason Yellick's avatar
Jason Yellick committed
23
	"github.com/hyperledger/fabric/common/util"
24
	"github.com/hyperledger/fabric/protos/common"
25
	ptestutils "github.com/hyperledger/fabric/protos/testutils"
manish's avatar
manish committed
26
27
)

28
29
30
31
32
33
34
35
36
37
38
39
40
41
//BlockGenerator generates a series of blocks for testing
type BlockGenerator struct {
	blockNum     uint64
	previousHash []byte
	t            *testing.T
}

// NewBlockGenerator instantiates new BlockGenerator for testing
func NewBlockGenerator(t *testing.T) *BlockGenerator {
	return &BlockGenerator{1, []byte{}, t}
}

// NextBlock constructs next block in sequence that includes a number of transactions - one per simulationResults
func (bg *BlockGenerator) NextBlock(simulationResults [][]byte, sign bool) *common.Block {
42
	envs := []*common.Envelope{}
manish's avatar
manish committed
43
	for i := 0; i < len(simulationResults); i++ {
44
		env, _, err := ConstructTransaction(bg.t, simulationResults[i], sign)
45
		if err != nil {
46
			bg.t.Fatalf("ConstructTestTransaction failed, err %s", err)
47
48
		}
		envs = append(envs, env)
manish's avatar
manish committed
49
	}
50
51
52
53
54
55
56
57
58
59
60
61
62
	block := newBlock(envs, bg.blockNum, bg.previousHash)
	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, false)
manish's avatar
manish committed
63
64
}

65
66
67
// NextTestBlocks constructs 'numBlocks' number of blocks for testing
func (bg *BlockGenerator) NextTestBlocks(numBlocks int) []*common.Block {
	blocks := []*common.Block{}
manish's avatar
manish committed
68
	for i := 0; i < numBlocks; i++ {
69
		blocks = append(blocks, bg.NextTestBlock(10, 100))
manish's avatar
manish committed
70
71
72
73
	}
	return blocks
}

74
75
76
77
78
79
80
81
82
83
// ConstructBlock constructs a single block with blockNum=1
func ConstructBlock(t *testing.T, simulationResults [][]byte, sign bool) *common.Block {
	bg := NewBlockGenerator(t)
	return bg.NextBlock(simulationResults, sign)
}

// ConstructTestBlock constructs a single block with blocknum=1
func ConstructTestBlock(t *testing.T, numTx int, txSize int) *common.Block {
	bg := NewBlockGenerator(t)
	return bg.NextTestBlock(numTx, txSize)
manish's avatar
manish committed
84
85
}

86
87
88
89
90
91
92
93
// ConstructTestBlocks returns a series of blocks starting with blockNum=1
func ConstructTestBlocks(t *testing.T, numBlocks int) []*common.Block {
	bg := NewBlockGenerator(t)
	return bg.NextTestBlocks(numBlocks)
}

// ConstructTransaction constructs a transaction for testing
func ConstructTransaction(t *testing.T, simulationResults []byte, sign bool) (*common.Envelope, string, error) {
94
	ccName := "foo"
95
	//response := &pb.Response{Status: 200}
96
	txID := util.GenerateUUID()
97
98
	var txEnv *common.Envelope
	var err error
99
	if sign {
100
		txEnv, err = ptestutils.ConstructSingedTxEnvWithDefaultSigner(txID, util.GetTestChainID(), ccName, nil, simulationResults, nil, nil)
101
	} else {
102
		txEnv, err = ptestutils.ConstructUnsingedTxEnv(txID, util.GetTestChainID(), ccName, nil, simulationResults, nil, nil)
103
	}
104
	return txEnv, txID, err
manish's avatar
manish committed
105
106
}

107
108
func newBlock(env []*common.Envelope, blockNum uint64, previousHash []byte) *common.Block {
	block := common.NewBlock(blockNum, previousHash)
109
	for i := 0; i < len(env); i++ {
110
111
		txEnvBytes, _ := proto.Marshal(env[i])
		block.Data.Data = append(block.Data.Data, txEnvBytes)
112
	}
113
	block.Header.DataHash = block.Data.Hash()
114
115
	return block
}