Skip to content

Commit

Permalink
[FAB-17691] Add BootstrappingSnapshotInfo to BlockchainInfo
Browse files Browse the repository at this point in the history
When a block store is created from a snapshot, adding BootstrappingSnapshotInfo to BlockchainInfo.
If the store is created from a genesis block, this field is nil.

Signed-off-by: Wenjian Qiao <wenjianq@gmail.com>
  • Loading branch information
wenjianqiao committed Sep 24, 2020
1 parent 3a8440d commit e1cad2d
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 10 deletions.
20 changes: 11 additions & 9 deletions common/ledger/blkstorage/blockfile_mgr.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,19 +145,19 @@ func newBlockfileMgr(id string, conf *Conf, indexConfig *IndexConfig, indexStore
bcInfo.Height = mgr.bootstrappingSnapshotInfo.LastBlockNum + 1
bcInfo.CurrentBlockHash = mgr.bootstrappingSnapshotInfo.LastBlockHash
bcInfo.PreviousBlockHash = mgr.bootstrappingSnapshotInfo.PreviousBlockHash
bcInfo.BootstrappingSnapshotInfo = &common.BootstrappingSnapshotInfo{}
bcInfo.BootstrappingSnapshotInfo.LastBlockInSnapshot = mgr.bootstrappingSnapshotInfo.LastBlockNum
}

if !blockfilesInfo.noBlockFiles {
lastBlockHeader, err := mgr.retrieveBlockHeaderByNumber(blockfilesInfo.lastPersistedBlock)
if err != nil {
panic(fmt.Sprintf("Could not retrieve header of the last block form file: %s", err))
}
lastBlockHash := protoutil.BlockHeaderHash(lastBlockHeader)
previousBlockHash := lastBlockHeader.PreviousHash
bcInfo = &common.BlockchainInfo{
Height: blockfilesInfo.lastPersistedBlock + 1,
CurrentBlockHash: lastBlockHash,
PreviousBlockHash: previousBlockHash}
// update bcInfo with lastPersistedBlock
bcInfo.Height = blockfilesInfo.lastPersistedBlock + 1
bcInfo.CurrentBlockHash = protoutil.BlockHeaderHash(lastBlockHeader)
bcInfo.PreviousBlockHash = lastBlockHeader.PreviousHash
}
mgr.bcInfo.Store(bcInfo)
return mgr, nil
Expand Down Expand Up @@ -490,9 +490,11 @@ func (mgr *blockfileMgr) updateBlockfilesInfo(blkfilesInfo *blockfilesInfo) {
func (mgr *blockfileMgr) updateBlockchainInfo(latestBlockHash []byte, latestBlock *common.Block) {
currentBCInfo := mgr.getBlockchainInfo()
newBCInfo := &common.BlockchainInfo{
Height: currentBCInfo.Height + 1,
CurrentBlockHash: latestBlockHash,
PreviousBlockHash: latestBlock.Header.PreviousHash}
Height: currentBCInfo.Height + 1,
CurrentBlockHash: latestBlockHash,
PreviousBlockHash: latestBlock.Header.PreviousHash,
BootstrappingSnapshotInfo: currentBCInfo.BootstrappingSnapshotInfo,
}

mgr.bcInfo.Store(newBCInfo)
}
Expand Down
27 changes: 27 additions & 0 deletions common/ledger/blkstorage/snapshot_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,18 @@ func TestImportFromSnapshot(t *testing.T) {
err := originalBlockStore.AddBlock(b)
require.NoError(t, err)
}

// verify blockchain info for original store (created by genesisblock)
lastBlock := blocksBeforeSnapshot[len(blocksBeforeSnapshot)-1]
prevBlock := blocksBeforeSnapshot[len(blocksBeforeSnapshot)-2]
bcInfo, err := originalBlockStore.GetBlockchainInfo()
require.NoError(t, err)
require.Equal(t, &common.BlockchainInfo{
Height: uint64(len(blocksBeforeSnapshot)),
CurrentBlockHash: protoutil.BlockHeaderHash(lastBlock.Header),
PreviousBlockHash: protoutil.BlockHeaderHash(prevBlock.Header),
}, bcInfo)

_, err = originalBlockStore.ExportTxIds(snapshotDir, testNewHashFunc)
require.NoError(t, err)
lastBlockInSnapshot := blocksBeforeSnapshot[len(blocksBeforeSnapshot)-1]
Expand Down Expand Up @@ -139,6 +151,9 @@ func TestImportFromSnapshot(t *testing.T) {
Height: snapshotInfo.LastBlockNum + 1,
CurrentBlockHash: snapshotInfo.LastBlockHash,
PreviousBlockHash: snapshotInfo.PreviousBlockHash,
BootstrappingSnapshotInfo: &common.BootstrappingSnapshotInfo{
LastBlockInSnapshot: uint64(snapshotInfo.LastBlockNum),
},
},
blocksDetailsBeforeSnapshot,
blocksBeforeSnapshot,
Expand All @@ -162,6 +177,9 @@ func TestImportFromSnapshot(t *testing.T) {
Height: finalBlock.Header.Number + 1,
CurrentBlockHash: protoutil.BlockHeaderHash(finalBlock.Header),
PreviousBlockHash: finalBlock.Header.PreviousHash,
BootstrappingSnapshotInfo: &common.BootstrappingSnapshotInfo{
LastBlockInSnapshot: uint64(snapshotInfo.LastBlockNum),
},
}
verifyQueriesOnBlocksPriorToSnapshot(t,
bootstrappedBlockStore,
Expand Down Expand Up @@ -189,6 +207,9 @@ func TestImportFromSnapshot(t *testing.T) {
Height: snapshotInfo.LastBlockNum + 1,
CurrentBlockHash: snapshotInfo.LastBlockHash,
PreviousBlockHash: snapshotInfo.PreviousBlockHash,
BootstrappingSnapshotInfo: &common.BootstrappingSnapshotInfo{
LastBlockInSnapshot: uint64(snapshotInfo.LastBlockNum),
},
},
blocksDetailsBeforeSnapshot,
blocksBeforeSnapshot,
Expand All @@ -204,6 +225,9 @@ func TestImportFromSnapshot(t *testing.T) {
Height: finalBlock.Header.Number + 1,
CurrentBlockHash: protoutil.BlockHeaderHash(finalBlock.Header),
PreviousBlockHash: finalBlock.Header.PreviousHash,
BootstrappingSnapshotInfo: &common.BootstrappingSnapshotInfo{
LastBlockInSnapshot: uint64(snapshotInfo.LastBlockNum),
},
}
verifyQueriesOnBlocksAddedAfterBootstrapping(t,
bootstrappedBlockStore,
Expand Down Expand Up @@ -275,6 +299,9 @@ func TestImportFromSnapshot(t *testing.T) {
Height: finalBlock.Header.Number + 1,
CurrentBlockHash: protoutil.BlockHeaderHash(finalBlock.Header),
PreviousBlockHash: finalBlock.Header.PreviousHash,
BootstrappingSnapshotInfo: &common.BootstrappingSnapshotInfo{
LastBlockInSnapshot: uint64(snapshotInfo.LastBlockNum),
},
},
blockDetails,
blocks,
Expand Down
3 changes: 3 additions & 0 deletions core/ledger/kvledger/snapshot_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -838,6 +838,9 @@ func verifyCreatedLedger(t *testing.T,
Height: e.lastBlockNumber + 1,
CurrentBlockHash: e.lastBlockHash,
PreviousBlockHash: e.previousBlockHash,
BootstrappingSnapshotInfo: &common.BootstrappingSnapshotInfo{
LastBlockInSnapshot: e.lastBlockNumber,
},
},
destBCInfo,
)
Expand Down
7 changes: 6 additions & 1 deletion core/ledger/ledgermgmt/ledger_mgmt_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,12 @@ func TestCreateLedgerFromSnapshot(t *testing.T) {
require.NoError(t, err)
bcInfo, _ := l.GetBlockchainInfo()
require.Equal(t, &common.BlockchainInfo{
Height: 1, CurrentBlockHash: protoutil.BlockHeaderHash(gb.Header), PreviousBlockHash: nil,
Height: 1,
CurrentBlockHash: protoutil.BlockHeaderHash(gb.Header),
PreviousBlockHash: nil,
BootstrappingSnapshotInfo: &common.BootstrappingSnapshotInfo{
LastBlockInSnapshot: 0,
},
}, bcInfo)
})

Expand Down

0 comments on commit e1cad2d

Please sign in to comment.