github.com/darrenli6/fabric-sdk-example@v0.0.0-20220109053535-94b13b56df8c/common/ledger/blkstorage/fsblkstorage/blockfile_helper_test.go (about)

     1  /*
     2  Copyright IBM Corp. All Rights Reserved.
     3  
     4  SPDX-License-Identifier: Apache-2.0
     5  */
     6  
     7  package fsblkstorage
     8  
     9  import (
    10  	"os"
    11  	"testing"
    12  
    13  	"github.com/golang/protobuf/proto"
    14  	"github.com/hyperledger/fabric/common/ledger/testutil"
    15  	"github.com/hyperledger/fabric/common/ledger/util"
    16  )
    17  
    18  func TestConstructCheckpointInfoFromBlockFiles(t *testing.T) {
    19  	testPath := "/tmp/tests/fabric/common/ledger/blkstorage/fsblkstorage"
    20  	ledgerid := "testLedger"
    21  	conf := NewConf(testPath, 0)
    22  	blkStoreDir := conf.getLedgerBlockDir(ledgerid)
    23  	env := newTestEnv(t, conf)
    24  	util.CreateDirIfMissing(blkStoreDir)
    25  	defer env.Cleanup()
    26  
    27  	// checkpoint constructed on an empty block folder should return CPInfo with isChainEmpty: true
    28  	cpInfo, err := constructCheckpointInfoFromBlockFiles(blkStoreDir)
    29  	testutil.AssertNoError(t, err, "")
    30  	testutil.AssertEquals(t, cpInfo, &checkpointInfo{isChainEmpty: true, lastBlockNumber: 0, latestFileChunksize: 0, latestFileChunkSuffixNum: 0})
    31  
    32  	w := newTestBlockfileWrapper(env, ledgerid)
    33  	defer w.close()
    34  	blockfileMgr := w.blockfileMgr
    35  	bg, gb := testutil.NewBlockGenerator(t, ledgerid, false)
    36  
    37  	// Add a few blocks and verify that cpinfo derived from filesystem should be same as from the blockfile manager
    38  	blockfileMgr.addBlock(gb)
    39  	for _, blk := range bg.NextTestBlocks(3) {
    40  		blockfileMgr.addBlock(blk)
    41  	}
    42  	checkCPInfoFromFile(t, blkStoreDir, blockfileMgr.cpInfo)
    43  
    44  	// Move the chain to new file and check cpinfo derived from file system
    45  	blockfileMgr.moveToNextFile()
    46  	checkCPInfoFromFile(t, blkStoreDir, blockfileMgr.cpInfo)
    47  
    48  	// Add a few blocks that would go to new file and verify that cpinfo derived from filesystem should be same as from the blockfile manager
    49  	for _, blk := range bg.NextTestBlocks(3) {
    50  		blockfileMgr.addBlock(blk)
    51  	}
    52  	checkCPInfoFromFile(t, blkStoreDir, blockfileMgr.cpInfo)
    53  
    54  	// Write a partial block (to simulate a crash) and verify that cpinfo derived from filesystem should be same as from the blockfile manager
    55  	lastTestBlk := bg.NextTestBlocks(1)[0]
    56  	blockBytes, _, err := serializeBlock(lastTestBlk)
    57  	testutil.AssertNoError(t, err, "")
    58  	partialByte := append(proto.EncodeVarint(uint64(len(blockBytes))), blockBytes[len(blockBytes)/2:]...)
    59  	blockfileMgr.currentFileWriter.append(partialByte, true)
    60  	checkCPInfoFromFile(t, blkStoreDir, blockfileMgr.cpInfo)
    61  
    62  	// Close the block storage, drop the index and restart and verify
    63  	cpInfoBeforeClose := blockfileMgr.cpInfo
    64  	w.close()
    65  	env.provider.Close()
    66  	indexFolder := conf.getIndexDir()
    67  	testutil.AssertNoError(t, os.RemoveAll(indexFolder), "")
    68  
    69  	env = newTestEnv(t, conf)
    70  	w = newTestBlockfileWrapper(env, ledgerid)
    71  	blockfileMgr = w.blockfileMgr
    72  	testutil.AssertEquals(t, blockfileMgr.cpInfo, cpInfoBeforeClose)
    73  
    74  	lastBlkIndexed, err := blockfileMgr.index.getLastBlockIndexed()
    75  	testutil.AssertNoError(t, err, "")
    76  	testutil.AssertEquals(t, lastBlkIndexed, uint64(6))
    77  
    78  	// Add the last block again after start and check cpinfo again
    79  	testutil.AssertNoError(t, blockfileMgr.addBlock(lastTestBlk), "")
    80  	checkCPInfoFromFile(t, blkStoreDir, blockfileMgr.cpInfo)
    81  }
    82  
    83  func checkCPInfoFromFile(t *testing.T, blkStoreDir string, expectedCPInfo *checkpointInfo) {
    84  	cpInfo, err := constructCheckpointInfoFromBlockFiles(blkStoreDir)
    85  	testutil.AssertNoError(t, err, "")
    86  	testutil.AssertEquals(t, cpInfo, expectedCPInfo)
    87  }