blockindex_test.go 6.17 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 fsblkstorage

import (
	"fmt"
	"testing"

manish's avatar
manish committed
23
	"github.com/hyperledger/fabric/core/ledger/blkstorage"
24
	"github.com/hyperledger/fabric/core/ledger/testutil"
manish's avatar
manish committed
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
)

type noopIndex struct {
}

func (i *noopIndex) getLastBlockIndexed() (uint64, error) {
	return 0, nil
}
func (i *noopIndex) indexBlock(blockIdxInfo *blockIdxInfo) error {
	return nil
}
func (i *noopIndex) getBlockLocByHash(blockHash []byte) (*fileLocPointer, error) {
	return nil, nil
}
func (i *noopIndex) getBlockLocByBlockNum(blockNum uint64) (*fileLocPointer, error) {
	return nil, nil
}
func (i *noopIndex) getTxLoc(txID string) (*fileLocPointer, error) {
	return nil, nil
}
45
46
47
func (i *noopIndex) getTXLocForBlockNumTranNum(blockNum uint64, tranNum uint64) (*fileLocPointer, error) {
	return nil, nil
}
manish's avatar
manish committed
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

func TestBlockIndexSync(t *testing.T) {
	testBlockIndexSync(t, 10, 5, false)
	testBlockIndexSync(t, 10, 5, true)
	testBlockIndexSync(t, 10, 0, true)
	testBlockIndexSync(t, 10, 10, true)
}

func testBlockIndexSync(t *testing.T, numBlocks int, numBlocksToIndex int, syncByRestart bool) {
	env := newTestEnv(t)
	defer env.Cleanup()
	blkfileMgrWrapper := newTestBlockfileWrapper(t, env)
	defer blkfileMgrWrapper.close()
	blkfileMgr := blkfileMgrWrapper.blockfileMgr
	origIndex := blkfileMgr.index
	// construct blocks for testing
	blocks := testutil.ConstructTestBlocks(t, numBlocks)
	// add a few blocks
	blkfileMgrWrapper.addBlocks(blocks[:numBlocksToIndex])

	// Plug-in a noop index and add remaining blocks
	blkfileMgr.index = &noopIndex{}
	blkfileMgrWrapper.addBlocks(blocks[numBlocksToIndex:])

	// Plug-in back the original index
	blkfileMgr.index = origIndex
	// The first set of blocks should be present in the orginal index
	for i := 1; i <= numBlocksToIndex; i++ {
		block, err := blkfileMgr.retrieveBlockByNumber(uint64(i))
		testutil.AssertNoError(t, err, fmt.Sprintf("block [%d] should have been present in the index", i))
		testutil.AssertEquals(t, block, blocks[i-1])
	}

	// The last set of blocks should not be present in the original index
	for i := numBlocksToIndex + 1; i <= numBlocks; i++ {
		_, err := blkfileMgr.retrieveBlockByNumber(uint64(i))
manish's avatar
manish committed
84
		testutil.AssertSame(t, err, blkstorage.ErrNotFoundInIndex)
manish's avatar
manish committed
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
	}

	// perform index sync
	if syncByRestart {
		blkfileMgrWrapper.close()
		blkfileMgrWrapper = newTestBlockfileWrapper(t, env)
		defer blkfileMgrWrapper.close()
		blkfileMgr = blkfileMgrWrapper.blockfileMgr
	} else {
		blkfileMgr.syncIndex()
	}

	// Now, last set of blocks should also be present in original index
	for i := numBlocksToIndex + 1; i <= numBlocks; i++ {
		block, err := blkfileMgr.retrieveBlockByNumber(uint64(i))
		testutil.AssertNoError(t, err, fmt.Sprintf("block [%d] should have been present in the index", i))
		testutil.AssertEquals(t, block, blocks[i-1])
	}
}
manish's avatar
manish committed
104
105
106
107
108

func TestBlockIndexSelectiveIndexing(t *testing.T) {
	testBlockIndexSelectiveIndexing(t, []blkstorage.IndexableAttr{blkstorage.IndexableAttrBlockHash})
	testBlockIndexSelectiveIndexing(t, []blkstorage.IndexableAttr{blkstorage.IndexableAttrBlockNum})
	testBlockIndexSelectiveIndexing(t, []blkstorage.IndexableAttr{blkstorage.IndexableAttrTxID})
109
	testBlockIndexSelectiveIndexing(t, []blkstorage.IndexableAttr{blkstorage.IndexableAttrBlockNumTranNum})
manish's avatar
manish committed
110
	testBlockIndexSelectiveIndexing(t, []blkstorage.IndexableAttr{blkstorage.IndexableAttrBlockHash, blkstorage.IndexableAttrBlockNum})
111
	testBlockIndexSelectiveIndexing(t, []blkstorage.IndexableAttr{blkstorage.IndexableAttrTxID, blkstorage.IndexableAttrBlockNumTranNum})
manish's avatar
manish committed
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
}

func testBlockIndexSelectiveIndexing(t *testing.T, indexItems []blkstorage.IndexableAttr) {
	env := newTestEnv(t)
	env.indexConfig.AttrsToIndex = indexItems
	defer env.Cleanup()
	blkfileMgrWrapper := newTestBlockfileWrapper(t, env)
	defer blkfileMgrWrapper.close()

	blocks := testutil.ConstructTestBlocks(t, 3)
	// add test blocks
	blkfileMgrWrapper.addBlocks(blocks)
	blockfileMgr := blkfileMgrWrapper.blockfileMgr

	// if index has been configured for an indexItem then the item should be indexed else not
	// test 'retrieveBlockByHash'
128
	block, err := blockfileMgr.retrieveBlockByHash(blocks[0].Header.Hash())
manish's avatar
manish committed
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
	if testutil.Contains(indexItems, blkstorage.IndexableAttrBlockHash) {
		testutil.AssertNoError(t, err, "Error while retrieving block by hash")
		testutil.AssertEquals(t, block, blocks[0])
	} else {
		testutil.AssertSame(t, err, blkstorage.ErrAttrNotIndexed)
	}

	// test 'retrieveBlockByNumber'
	block, err = blockfileMgr.retrieveBlockByNumber(1)
	if testutil.Contains(indexItems, blkstorage.IndexableAttrBlockNum) {
		testutil.AssertNoError(t, err, "Error while retrieving block by number")
		testutil.AssertEquals(t, block, blocks[0])
	} else {
		testutil.AssertSame(t, err, blkstorage.ErrAttrNotIndexed)
	}

	// test 'retrieveTransactionByID'
146
147
148
	txid, err := extractTxID(blocks[0].Data.Data[0])
	testutil.AssertNoError(t, err, "")
	tx, err := blockfileMgr.retrieveTransactionByID(txid)
manish's avatar
manish committed
149
150
	if testutil.Contains(indexItems, blkstorage.IndexableAttrTxID) {
		testutil.AssertNoError(t, err, "Error while retrieving tx by id")
151
		txOrig, err := extractTransaction(blocks[0].Data.Data[0])
152
		testutil.AssertNoError(t, err, "")
manish's avatar
manish committed
153
154
155
156
		testutil.AssertEquals(t, tx, txOrig)
	} else {
		testutil.AssertSame(t, err, blkstorage.ErrAttrNotIndexed)
	}
157
158
159
160
161
162
163
164
165
166
167

	//test 'retrieveTrasnactionsByBlockNumTranNum
	tx2, err := blockfileMgr.retrieveTransactionForBlockNumTranNum(1, 1)
	if testutil.Contains(indexItems, blkstorage.IndexableAttrBlockNumTranNum) {
		testutil.AssertNoError(t, err, "Error while retrieving tx by blockNum and tranNum")
		txOrig2, err2 := extractTransaction(blocks[0].Data.Data[0])
		testutil.AssertNoError(t, err2, "")
		testutil.AssertEquals(t, tx2, txOrig2)
	} else {
		testutil.AssertSame(t, err, blkstorage.ErrAttrNotIndexed)
	}
manish's avatar
manish committed
168
}