Skip to content

Commit

Permalink
Bootstrap blockstore from snapshotted TxIDs (hyperledger#1413)
Browse files Browse the repository at this point in the history
This commit adds the functionality in the blockstore for bootstrapping
the blockstore for a channel from previously snapshotted TxIDs

Signed-off-by: manish <[email protected]>
  • Loading branch information
manish-sethi committed Jun 18, 2020
1 parent a6ad9d8 commit cff2f18
Show file tree
Hide file tree
Showing 16 changed files with 920 additions and 131 deletions.
32 changes: 27 additions & 5 deletions common/ledger/blkstorage/blockfile_helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,12 @@ package blkstorage
import (
"io/ioutil"
"os"
"path/filepath"
"strconv"
"strings"

"github.com/davecgh/go-spew/spew"
"github.com/golang/protobuf/proto"
"github.com/hyperledger/fabric-protos-go/common"
"github.com/pkg/errors"
)
Expand All @@ -37,7 +39,12 @@ func constructCheckpointInfoFromBlockFiles(rootDir string) (*checkpointInfo, err
logger.Debugf("Last file number found = %d", lastFileNum)

if lastFileNum == -1 {
cpInfo := &checkpointInfo{0, 0, true, 0}
cpInfo := &checkpointInfo{
latestFileChunkSuffixNum: 0,
latestFileChunksize: 0,
noBlockFiles: true,
lastBlockNumberInBlockFiles: 0,
}
logger.Debugf("No block file found")
return cpInfo, nil
}
Expand Down Expand Up @@ -68,10 +75,10 @@ func constructCheckpointInfoFromBlockFiles(rootDir string) (*checkpointInfo, err
}

cpInfo := &checkpointInfo{
lastBlockNumber: lastBlockNumber,
latestFileChunksize: int(endOffsetLastBlock),
latestFileChunkSuffixNum: lastFileNum,
isChainEmpty: lastFileNum == 0 && numBlocksInFile == 0,
lastBlockNumberInBlockFiles: lastBlockNumber,
latestFileChunksize: int(endOffsetLastBlock),
latestFileChunkSuffixNum: lastFileNum,
noBlockFiles: lastFileNum == 0 && numBlocksInFile == 0,
}
logger.Debugf("Checkpoint info constructed from file system = %s", spew.Sdump(cpInfo))
return cpInfo, nil
Expand Down Expand Up @@ -162,3 +169,18 @@ func getFileInfoOrPanic(rootDir string, fileNum int) os.FileInfo {
}
return fileInfo
}

func loadBootstrappingSnapshotInfo(rootDir string) (*BootstrappingSnapshotInfo, error) {
bsiBytes, err := ioutil.ReadFile(filepath.Join(rootDir, bootstrappingSnapshotInfoFile))
if os.IsNotExist(err) {
return nil, nil
}
if err != nil {
return nil, errors.Wrapf(err, "error while reading bootstrappingSnapshotInfo file")
}
bsi := &BootstrappingSnapshotInfo{}
if err := proto.Unmarshal(bsiBytes, bsi); err != nil {
return nil, errors.Wrapf(err, "error while unmarshalling bootstrappingSnapshotInfo")
}
return bsi, nil
}
2 changes: 1 addition & 1 deletion common/ledger/blkstorage/blockfile_helper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func TestConstructCheckpointInfoFromBlockFiles(t *testing.T) {
// checkpoint constructed on an empty block folder should return CPInfo with isChainEmpty: true
cpInfo, err := constructCheckpointInfoFromBlockFiles(blkStoreDir)
assert.NoError(t, err)
assert.Equal(t, &checkpointInfo{isChainEmpty: true, lastBlockNumber: 0, latestFileChunksize: 0, latestFileChunkSuffixNum: 0}, cpInfo)
assert.Equal(t, &checkpointInfo{noBlockFiles: true, lastBlockNumberInBlockFiles: 0, latestFileChunksize: 0, latestFileChunkSuffixNum: 0}, cpInfo)

w := newTestBlockfileWrapper(env, ledgerid)
defer w.close()
Expand Down
Loading

0 comments on commit cff2f18

Please sign in to comment.