blockfile_scan_test.go 3.04 KB
Newer Older
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 (
	"os"
	"testing"

23
24
	"github.com/hyperledger/fabric/common/ledger/testutil"
	"github.com/hyperledger/fabric/common/ledger/util"
25

26
	"github.com/hyperledger/fabric/protos/common"
27
28
29
)

func TestBlockFileScanSmallTxOnly(t *testing.T) {
30
	env := newTestEnv(t, NewConf(testPath(), 0))
31
	defer env.Cleanup()
manish's avatar
manish committed
32
33
	ledgerid := "testLedger"
	blkfileMgrWrapper := newTestBlockfileWrapper(env, ledgerid)
34
35
	bg, gb := testutil.NewBlockGenerator(t, ledgerid, false)
	blocks := []*common.Block{gb}
36
37
38
	blocks = append(blocks, bg.NextTestBlock(0, 0))
	blocks = append(blocks, bg.NextTestBlock(0, 0))
	blocks = append(blocks, bg.NextTestBlock(0, 0))
39
40
41
	blkfileMgrWrapper.addBlocks(blocks)
	blkfileMgrWrapper.close()

manish's avatar
manish committed
42
	filePath := deriveBlockfilePath(env.provider.conf.getLedgerBlockDir(ledgerid), 0)
43
44
45
	_, fileSize, err := util.FileExists(filePath)
	testutil.AssertNoError(t, err, "")

46
	lastBlockBytes, endOffsetLastBlock, numBlocks, err := scanForLastCompleteBlock(env.provider.conf.getLedgerBlockDir(ledgerid), 0, 0)
47
48
49
	testutil.AssertNoError(t, err, "")
	testutil.AssertEquals(t, numBlocks, len(blocks))
	testutil.AssertEquals(t, endOffsetLastBlock, fileSize)
50
51
52

	expectedLastBlockBytes, _, err := serializeBlock(blocks[len(blocks)-1])
	testutil.AssertEquals(t, lastBlockBytes, expectedLastBlockBytes)
53
54
55
}

func TestBlockFileScanSmallTxLastTxIncomplete(t *testing.T) {
56
	env := newTestEnv(t, NewConf(testPath(), 0))
57
	defer env.Cleanup()
manish's avatar
manish committed
58
59
	ledgerid := "testLedger"
	blkfileMgrWrapper := newTestBlockfileWrapper(env, ledgerid)
60
61
	bg, gb := testutil.NewBlockGenerator(t, ledgerid, false)
	blocks := []*common.Block{gb}
62
63
64
	blocks = append(blocks, bg.NextTestBlock(0, 0))
	blocks = append(blocks, bg.NextTestBlock(0, 0))
	blocks = append(blocks, bg.NextTestBlock(0, 0))
65
66
67
	blkfileMgrWrapper.addBlocks(blocks)
	blkfileMgrWrapper.close()

manish's avatar
manish committed
68
	filePath := deriveBlockfilePath(env.provider.conf.getLedgerBlockDir(ledgerid), 0)
69
70
71
72
73
74
75
76
77
	_, fileSize, err := util.FileExists(filePath)
	testutil.AssertNoError(t, err, "")

	file, err := os.OpenFile(filePath, os.O_RDWR|os.O_APPEND|os.O_CREATE, 0660)
	defer file.Close()
	testutil.AssertNoError(t, err, "")
	err = file.Truncate(fileSize - 1)
	testutil.AssertNoError(t, err, "")

78
	lastBlockBytes, _, numBlocks, err := scanForLastCompleteBlock(env.provider.conf.getLedgerBlockDir(ledgerid), 0, 0)
79
80
	testutil.AssertNoError(t, err, "")
	testutil.AssertEquals(t, numBlocks, len(blocks)-1)
81
82
83

	expectedLastBlockBytes, _, err := serializeBlock(blocks[len(blocks)-2])
	testutil.AssertEquals(t, lastBlockBytes, expectedLastBlockBytes)
84
}