github.com/adnan-c/fabric_e2e_couchdb@v0.6.1-preview.0.20170228180935-21ce6b23cf91/common/ledger/testutil/test_helper.go (about) 1 /* 2 Copyright IBM Corp. 2016 All Rights Reserved. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package testutil 18 19 import ( 20 "testing" 21 22 "github.com/golang/protobuf/proto" 23 "github.com/hyperledger/fabric/common/util" 24 lutils "github.com/hyperledger/fabric/core/ledger/util" 25 "github.com/hyperledger/fabric/protos/common" 26 ptestutils "github.com/hyperledger/fabric/protos/testutils" 27 "github.com/hyperledger/fabric/protos/utils" 28 ) 29 30 //BlockGenerator generates a series of blocks for testing 31 type BlockGenerator struct { 32 blockNum uint64 33 previousHash []byte 34 t *testing.T 35 } 36 37 // NewBlockGenerator instantiates new BlockGenerator for testing 38 func NewBlockGenerator(t *testing.T) *BlockGenerator { 39 return &BlockGenerator{0, []byte{}, t} 40 } 41 42 // NextBlock constructs next block in sequence that includes a number of transactions - one per simulationResults 43 func (bg *BlockGenerator) NextBlock(simulationResults [][]byte, sign bool) *common.Block { 44 envs := []*common.Envelope{} 45 for i := 0; i < len(simulationResults); i++ { 46 env, _, err := ConstructTransaction(bg.t, simulationResults[i], sign) 47 if err != nil { 48 bg.t.Fatalf("ConstructTestTransaction failed, err %s", err) 49 } 50 envs = append(envs, env) 51 } 52 block := newBlock(envs, bg.blockNum, bg.previousHash) 53 bg.blockNum++ 54 bg.previousHash = block.Header.Hash() 55 return block 56 } 57 58 // NextTestBlock constructs next block in sequence block with 'numTx' number of transactions for testing 59 func (bg *BlockGenerator) NextTestBlock(numTx int, txSize int) *common.Block { 60 simulationResults := [][]byte{} 61 for i := 0; i < numTx; i++ { 62 simulationResults = append(simulationResults, ConstructRandomBytes(bg.t, txSize)) 63 } 64 return bg.NextBlock(simulationResults, false) 65 } 66 67 // NextTestBlocks constructs 'numBlocks' number of blocks for testing 68 func (bg *BlockGenerator) NextTestBlocks(numBlocks int) []*common.Block { 69 blocks := []*common.Block{} 70 for i := 0; i < numBlocks; i++ { 71 blocks = append(blocks, bg.NextTestBlock(10, 100)) 72 } 73 return blocks 74 } 75 76 // ConstructBlock constructs a single block with blockNum=1 77 func ConstructBlock(t *testing.T, simulationResults [][]byte, sign bool) *common.Block { 78 bg := NewBlockGenerator(t) 79 return bg.NextBlock(simulationResults, sign) 80 } 81 82 // ConstructTestBlock constructs a single block with blocknum=1 83 func ConstructTestBlock(t *testing.T, numTx int, txSize int) *common.Block { 84 bg := NewBlockGenerator(t) 85 return bg.NextTestBlock(numTx, txSize) 86 } 87 88 // ConstructTestBlocks returns a series of blocks starting with blockNum=1 89 func ConstructTestBlocks(t *testing.T, numBlocks int) []*common.Block { 90 bg := NewBlockGenerator(t) 91 return bg.NextTestBlocks(numBlocks) 92 } 93 94 // ConstructTransaction constructs a transaction for testing 95 func ConstructTransaction(_ *testing.T, simulationResults []byte, sign bool) (*common.Envelope, string, error) { 96 ccName := "foo" 97 //response := &pb.Response{Status: 200} 98 var txID string 99 var txEnv *common.Envelope 100 var err error 101 if sign { 102 txEnv, txID, err = ptestutils.ConstructSingedTxEnvWithDefaultSigner(util.GetTestChainID(), ccName, nil, simulationResults, nil, nil) 103 } else { 104 txEnv, txID, err = ptestutils.ConstructUnsingedTxEnv(util.GetTestChainID(), ccName, nil, simulationResults, nil, nil) 105 } 106 return txEnv, txID, err 107 } 108 109 func newBlock(env []*common.Envelope, blockNum uint64, previousHash []byte) *common.Block { 110 block := common.NewBlock(blockNum, previousHash) 111 for i := 0; i < len(env); i++ { 112 txEnvBytes, _ := proto.Marshal(env[i]) 113 block.Data.Data = append(block.Data.Data, txEnvBytes) 114 } 115 block.Header.DataHash = block.Data.Hash() 116 utils.InitBlockMetadata(block) 117 118 block.Metadata.Metadata[common.BlockMetadataIndex_TRANSACTIONS_FILTER] = lutils.NewTxValidationFlags(len(env)) 119 120 return block 121 }