github.com/tenywen/fabric@v1.0.0-beta.0.20170620030522-a5b1ed380643/common/ledger/blkstorage/fsblkstorage/fs_blockstore_provider_test.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 fsblkstorage 18 19 import ( 20 "testing" 21 22 "fmt" 23 24 "github.com/hyperledger/fabric/common/ledger/blkstorage" 25 "github.com/hyperledger/fabric/common/ledger/testutil" 26 "github.com/hyperledger/fabric/core/ledger/util" 27 "github.com/hyperledger/fabric/protos/common" 28 "github.com/hyperledger/fabric/protos/peer" 29 "github.com/hyperledger/fabric/protos/utils" 30 ) 31 32 func TestMultipleBlockStores(t *testing.T) { 33 env := newTestEnv(t, NewConf(testPath(), 0)) 34 defer env.Cleanup() 35 36 provider := env.provider 37 store1, _ := provider.OpenBlockStore("ledger1") 38 defer store1.Shutdown() 39 40 store2, _ := provider.CreateBlockStore("ledger2") 41 defer store2.Shutdown() 42 43 blocks1 := testutil.ConstructTestBlocks(t, 5) 44 for _, b := range blocks1 { 45 store1.AddBlock(b) 46 } 47 48 blocks2 := testutil.ConstructTestBlocks(t, 10) 49 for _, b := range blocks2 { 50 store2.AddBlock(b) 51 } 52 checkBlocks(t, blocks1, store1) 53 checkBlocks(t, blocks2, store2) 54 checkWithWrongInputs(t, store1, 5) 55 checkWithWrongInputs(t, store2, 10) 56 } 57 58 func checkBlocks(t *testing.T, expectedBlocks []*common.Block, store blkstorage.BlockStore) { 59 bcInfo, _ := store.GetBlockchainInfo() 60 testutil.AssertEquals(t, bcInfo.Height, uint64(len(expectedBlocks))) 61 testutil.AssertEquals(t, bcInfo.CurrentBlockHash, expectedBlocks[len(expectedBlocks)-1].GetHeader().Hash()) 62 63 itr, _ := store.RetrieveBlocks(0) 64 for i := 0; i < len(expectedBlocks); i++ { 65 block, _ := itr.Next() 66 testutil.AssertEquals(t, block, expectedBlocks[i]) 67 } 68 69 for blockNum := 0; blockNum < len(expectedBlocks); blockNum++ { 70 block := expectedBlocks[blockNum] 71 flags := util.TxValidationFlags(block.Metadata.Metadata[common.BlockMetadataIndex_TRANSACTIONS_FILTER]) 72 retrievedBlock, _ := store.RetrieveBlockByNumber(uint64(blockNum)) 73 testutil.AssertEquals(t, retrievedBlock, block) 74 75 retrievedBlock, _ = store.RetrieveBlockByHash(block.Header.Hash()) 76 testutil.AssertEquals(t, retrievedBlock, block) 77 78 for txNum := 0; txNum < len(block.Data.Data); txNum++ { 79 txEnvBytes := block.Data.Data[txNum] 80 txEnv, _ := utils.GetEnvelopeFromBlock(txEnvBytes) 81 txid, err := extractTxID(txEnvBytes) 82 testutil.AssertNoError(t, err, "") 83 84 retrievedBlock, _ := store.RetrieveBlockByTxID(txid) 85 testutil.AssertEquals(t, retrievedBlock, block) 86 87 retrievedTxEnv, _ := store.RetrieveTxByID(txid) 88 testutil.AssertEquals(t, retrievedTxEnv, txEnv) 89 90 retrievedTxEnv, _ = store.RetrieveTxByBlockNumTranNum(uint64(blockNum), uint64(txNum)) 91 testutil.AssertEquals(t, retrievedTxEnv, txEnv) 92 93 retrievedTxValCode, err := store.RetrieveTxValidationCodeByTxID(txid) 94 testutil.AssertNoError(t, err, "") 95 testutil.AssertEquals(t, retrievedTxValCode, flags.Flag(txNum)) 96 } 97 } 98 } 99 100 func checkWithWrongInputs(t *testing.T, store blkstorage.BlockStore, numBlocks int) { 101 block, err := store.RetrieveBlockByHash([]byte("non-existent-hash")) 102 testutil.AssertNil(t, block) 103 testutil.AssertEquals(t, err, blkstorage.ErrNotFoundInIndex) 104 105 block, err = store.RetrieveBlockByTxID("non-existent-txid") 106 testutil.AssertNil(t, block) 107 testutil.AssertEquals(t, err, blkstorage.ErrNotFoundInIndex) 108 109 tx, err := store.RetrieveTxByID("non-existent-txid") 110 testutil.AssertNil(t, tx) 111 testutil.AssertEquals(t, err, blkstorage.ErrNotFoundInIndex) 112 113 tx, err = store.RetrieveTxByBlockNumTranNum(uint64(numBlocks+1), uint64(0)) 114 testutil.AssertNil(t, tx) 115 testutil.AssertEquals(t, err, blkstorage.ErrNotFoundInIndex) 116 117 txCode, err := store.RetrieveTxValidationCodeByTxID("non-existent-txid") 118 testutil.AssertEquals(t, txCode, peer.TxValidationCode(-1)) 119 testutil.AssertEquals(t, err, blkstorage.ErrNotFoundInIndex) 120 } 121 122 func TestBlockStoreProvider(t *testing.T) { 123 env := newTestEnv(t, NewConf(testPath(), 0)) 124 defer env.Cleanup() 125 126 provider := env.provider 127 stores := []blkstorage.BlockStore{} 128 numStores := 10 129 for i := 0; i < numStores; i++ { 130 store, _ := provider.OpenBlockStore(constructLedgerid(i)) 131 defer store.Shutdown() 132 stores = append(stores, store) 133 } 134 135 storeNames, _ := provider.List() 136 testutil.AssertEquals(t, len(storeNames), numStores) 137 138 for i := 0; i < numStores; i++ { 139 exists, err := provider.Exists(constructLedgerid(i)) 140 testutil.AssertNoError(t, err, "") 141 testutil.AssertEquals(t, exists, true) 142 } 143 144 exists, err := provider.Exists(constructLedgerid(numStores + 1)) 145 testutil.AssertNoError(t, err, "") 146 testutil.AssertEquals(t, exists, false) 147 148 } 149 150 func constructLedgerid(id int) string { 151 return fmt.Sprintf("ledger_%d", id) 152 }