Skip to content

Commit

Permalink
replacing old functions that are not in the new protobuf package
Browse files Browse the repository at this point in the history
Signed-off-by: Fedor Partanskiy <[email protected]>
  • Loading branch information
pfi79 committed Aug 30, 2024
1 parent 9ae1574 commit 6933629
Show file tree
Hide file tree
Showing 21 changed files with 217 additions and 194 deletions.
64 changes: 23 additions & 41 deletions common/ledger/blkstorage/block_serialization.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ SPDX-License-Identifier: Apache-2.0
package blkstorage

import (
"github.com/golang/protobuf/proto"
"github.com/hyperledger/fabric-protos-go/common"
"github.com/hyperledger/fabric/protoutil"
"github.com/pkg/errors"
"google.golang.org/protobuf/encoding/protowire"
)

type serializedBlockInfo struct {
Expand All @@ -26,21 +26,14 @@ type txindexInfo struct {
}

func serializeBlock(block *common.Block) ([]byte, *serializedBlockInfo, error) {
buf := proto.NewBuffer(nil)
var err error
var buf []byte
info := &serializedBlockInfo{}
info.blockHeader = block.Header
info.metadata = block.Metadata
if err = addHeaderBytes(block.Header, buf); err != nil {
return nil, nil, err
}
if info.txOffsets, err = addDataBytesAndConstructTxIndexInfo(block.Data, buf); err != nil {
return nil, nil, err
}
if err = addMetadataBytes(block.Metadata, buf); err != nil {
return nil, nil, err
}
return buf.Bytes(), info, nil
buf = addHeaderBytes(block.Header, buf)
info.txOffsets, buf = addDataBytesAndConstructTxIndexInfo(block.Data, buf)
buf = addMetadataBytes(block.Metadata, buf)
return buf, info, nil
}

func deserializeBlock(serializedBlockBytes []byte) (*common.Block, error) {
Expand Down Expand Up @@ -79,55 +72,44 @@ func extractSerializedBlockInfo(serializedBlockBytes []byte) (*serializedBlockIn
return info, nil
}

func addHeaderBytes(blockHeader *common.BlockHeader, buf *proto.Buffer) error {
if err := buf.EncodeVarint(blockHeader.Number); err != nil {
return errors.Wrapf(err, "error encoding the block number [%d]", blockHeader.Number)
}
if err := buf.EncodeRawBytes(blockHeader.DataHash); err != nil {
return errors.Wrapf(err, "error encoding the data hash [%v]", blockHeader.DataHash)
}
if err := buf.EncodeRawBytes(blockHeader.PreviousHash); err != nil {
return errors.Wrapf(err, "error encoding the previous hash [%v]", blockHeader.PreviousHash)
}
return nil
func addHeaderBytes(blockHeader *common.BlockHeader, buf []byte) []byte {
buf = protowire.AppendVarint(buf, blockHeader.Number)
buf = protowire.AppendBytes(buf, blockHeader.DataHash)
buf = protowire.AppendBytes(buf, blockHeader.PreviousHash)
return buf
}

func addDataBytesAndConstructTxIndexInfo(blockData *common.BlockData, buf *proto.Buffer) ([]*txindexInfo, error) {
func addDataBytesAndConstructTxIndexInfo(blockData *common.BlockData, buf []byte) ([]*txindexInfo, []byte) {
var txOffsets []*txindexInfo

if err := buf.EncodeVarint(uint64(len(blockData.Data))); err != nil {
return nil, errors.Wrap(err, "error encoding the length of block data")
}
buf = protowire.AppendVarint(buf, uint64(len(blockData.Data)))
for _, txEnvelopeBytes := range blockData.Data {
offset := len(buf.Bytes())
offset := len(buf)
txid, err := protoutil.GetOrComputeTxIDFromEnvelope(txEnvelopeBytes)
if err != nil {
logger.Warningf("error while extracting txid from tx envelope bytes during serialization of block. Ignoring this error as this is caused by a malformed transaction. Error:%s",
err)
}
if err := buf.EncodeRawBytes(txEnvelopeBytes); err != nil {
return nil, errors.Wrap(err, "error encoding the transaction envelope")
}
idxInfo := &txindexInfo{txID: txid, loc: &locPointer{offset, len(buf.Bytes()) - offset}}
buf = protowire.AppendBytes(buf, txEnvelopeBytes)
idxInfo := &txindexInfo{txID: txid, loc: &locPointer{offset, len(buf) - offset}}
txOffsets = append(txOffsets, idxInfo)
}
return txOffsets, nil
return txOffsets, buf
}

func addMetadataBytes(blockMetadata *common.BlockMetadata, buf *proto.Buffer) error {
func addMetadataBytes(blockMetadata *common.BlockMetadata, buf []byte) []byte {
numItems := uint64(0)
if blockMetadata != nil {
numItems = uint64(len(blockMetadata.Metadata))
}
if err := buf.EncodeVarint(numItems); err != nil {
return errors.Wrap(err, "error encoding the length of metadata")
buf = protowire.AppendVarint(buf, numItems)
if blockMetadata == nil {
return buf
}
for _, b := range blockMetadata.Metadata {
if err := buf.EncodeRawBytes(b); err != nil {
return errors.Wrap(err, "error encoding the block metadata")
}
buf = protowire.AppendBytes(buf, b)
}
return nil
return buf
}

func extractHeader(buf *buffer) (*common.BlockHeader, error) {
Expand Down
7 changes: 5 additions & 2 deletions common/ledger/blkstorage/block_serialization_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ package blkstorage
import (
"testing"

"github.com/golang/protobuf/proto"
"github.com/hyperledger/fabric-protos-go/common"
"github.com/hyperledger/fabric/common/ledger/testutil"
"github.com/hyperledger/fabric/protoutil"
"github.com/stretchr/testify/require"
"google.golang.org/protobuf/encoding/protowire"
)

func TestBlockSerialization(t *testing.T) {
Expand Down Expand Up @@ -101,7 +101,10 @@ func testSerializedBlockInfo(t *testing.T, block *common.Block, c *testutilTxIDC

require.Equal(t, indexTxID, txid)
b := bb[indexOffset.offset:]
length, num := proto.DecodeVarint(b)
length, num := protowire.ConsumeVarint(b)
if num < 0 {
length, num = 0, 0
}
txEnvBytesFromBB := b[num : num+int(length)]
require.Equal(t, txEnvBytes, txEnvBytesFromBB)
}
Expand Down
6 changes: 3 additions & 3 deletions common/ledger/blkstorage/block_stream.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ import (
"io"
"os"

"github.com/golang/protobuf/proto"
"github.com/pkg/errors"
"google.golang.org/protobuf/encoding/protowire"
)

// ErrUnexpectedEndOfBlockfile error used to indicate an unexpected end of a file segment
Expand Down Expand Up @@ -105,8 +105,8 @@ func (s *blockfileStream) nextBlockBytesAndPlacementInfo() ([]byte, *blockPlacem
if lenBytes, err = s.reader.Peek(peekBytes); err != nil {
return nil, nil, errors.Wrapf(err, "error peeking [%d] bytes from block file", peekBytes)
}
length, n := proto.DecodeVarint(lenBytes)
if n == 0 {
length, n := protowire.ConsumeVarint(lenBytes)
if n <= 0 {
// proto.DecodeVarint did not consume any byte at all which means that the bytes
// representing the size of the block are partial bytes
if !moreContentAvailable {
Expand Down
4 changes: 2 additions & 2 deletions common/ledger/blkstorage/block_stream_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ package blkstorage
import (
"testing"

"github.com/golang/protobuf/proto"
"github.com/hyperledger/fabric-protos-go/common"
"github.com/hyperledger/fabric/common/ledger/testutil"
"github.com/stretchr/testify/require"
"google.golang.org/protobuf/encoding/protowire"
)

func TestBlockfileStream(t *testing.T) {
Expand Down Expand Up @@ -53,7 +53,7 @@ func testBlockfileStream(t *testing.T, numBlocks int) {
func TestBlockFileStreamUnexpectedEOF(t *testing.T) {
partialBlockBytes := []byte{}
dummyBlockBytes := testutil.ConstructRandomBytes(t, 100)
lenBytes := proto.EncodeVarint(uint64(len(dummyBlockBytes)))
lenBytes := protowire.AppendVarint(nil, uint64(len(dummyBlockBytes)))
partialBlockBytes = append(partialBlockBytes, lenBytes...)
partialBlockBytes = append(partialBlockBytes, dummyBlockBytes...)
testBlockFileStreamUnexpectedEOF(t, 10, partialBlockBytes[:1])
Expand Down
4 changes: 2 additions & 2 deletions common/ledger/blkstorage/blockfile_helper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ import (
"path/filepath"
"testing"

"github.com/golang/protobuf/proto"
"github.com/hyperledger/fabric/common/ledger/testutil"
"github.com/stretchr/testify/require"
"google.golang.org/protobuf/encoding/protowire"
)

func TestConstructBlockfilesInfo(t *testing.T) {
Expand Down Expand Up @@ -64,7 +64,7 @@ func TestConstructBlockfilesInfo(t *testing.T) {
lastTestBlk := bg.NextTestBlocks(1)[0]
blockBytes, _, err := serializeBlock(lastTestBlk)
require.NoError(t, err)
partialByte := append(proto.EncodeVarint(uint64(len(blockBytes))), blockBytes[len(blockBytes)/2:]...)
partialByte := append(protowire.AppendVarint(nil, uint64(len(blockBytes))), blockBytes[len(blockBytes)/2:]...)
blockfileMgr.currentFileWriter.append(partialByte, true)
checkBlockfilesInfoFromFS(t, blkStoreDir, blockfileMgr.blockfilesInfo)

Expand Down
64 changes: 34 additions & 30 deletions common/ledger/blkstorage/blockfile_mgr.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"github.com/hyperledger/fabric/internal/fileutil"
"github.com/hyperledger/fabric/protoutil"
"github.com/pkg/errors"
"google.golang.org/protobuf/encoding/protowire"
)

const (
Expand Down Expand Up @@ -195,7 +196,7 @@ func bootstrapFromSnapshottedTxIDs(
return err
}

if err := fileutil.CreateAndSyncFileAtomically(
if err = fileutil.CreateAndSyncFileAtomically(
rootDir,
bootstrappingSnapshotInfoTempFile,
bootstrappingSnapshotInfoFile,
Expand Down Expand Up @@ -307,7 +308,7 @@ func (mgr *blockfileMgr) addBlock(block *common.Block) error {
currentOffset := mgr.blockfilesInfo.latestFileSize

blockBytesLen := len(blockBytes)
blockBytesEncodedLen := proto.EncodeVarint(uint64(blockBytesLen))
blockBytesEncodedLen := protowire.AppendVarint(nil, uint64(blockBytesLen))
totalBytesToAppend := blockBytesLen + len(blockBytesEncodedLen)

// Determine if we need to start a new file since the size of this block
Expand Down Expand Up @@ -649,7 +650,10 @@ func (mgr *blockfileMgr) fetchTransactionEnvelope(lp *fileLocPointer) (*common.E
if txEnvelopeBytes, err = mgr.fetchRawBytes(lp); err != nil {
return nil, err
}
_, n := proto.DecodeVarint(txEnvelopeBytes)
_, n := protowire.ConsumeVarint(txEnvelopeBytes)
if n < 0 {
n = 0
}
return protoutil.GetEnvelopeFromBlock(txEnvelopeBytes[n:])
}

Expand Down Expand Up @@ -757,49 +761,49 @@ type blockfilesInfo struct {
}

func (i *blockfilesInfo) marshal() ([]byte, error) {
buffer := proto.NewBuffer([]byte{})
var err error
if err = buffer.EncodeVarint(uint64(i.latestFileNumber)); err != nil {
return nil, errors.Wrapf(err, "error encoding the latestFileNumber [%d]", i.latestFileNumber)
}
if err = buffer.EncodeVarint(uint64(i.latestFileSize)); err != nil {
return nil, errors.Wrapf(err, "error encoding the latestFileSize [%d]", i.latestFileSize)
}
if err = buffer.EncodeVarint(i.lastPersistedBlock); err != nil {
return nil, errors.Wrapf(err, "error encoding the lastPersistedBlock [%d]", i.lastPersistedBlock)
}
var buf []byte
buf = protowire.AppendVarint(buf, uint64(i.latestFileNumber))
buf = protowire.AppendVarint(buf, uint64(i.latestFileSize))
buf = protowire.AppendVarint(buf, i.lastPersistedBlock)
var noBlockFilesMarker uint64
if i.noBlockFiles {
noBlockFilesMarker = 1
}
if err = buffer.EncodeVarint(noBlockFilesMarker); err != nil {
return nil, errors.Wrapf(err, "error encoding noBlockFiles [%d]", noBlockFilesMarker)
}
return buffer.Bytes(), nil
buf = protowire.AppendVarint(buf, noBlockFilesMarker)

return buf, nil
}

func (i *blockfilesInfo) unmarshal(b []byte) error {
buffer := proto.NewBuffer(b)
var val uint64
var noBlockFilesMarker uint64
var err error
var (
val uint64
noBlockFilesMarker uint64
position int
)

if val, err = buffer.DecodeVarint(); err != nil {
return err
val, n := protowire.ConsumeVarint(b[position:])
if n < 0 {
return protowire.ParseError(n)
}
position += n
i.latestFileNumber = int(val)

if val, err = buffer.DecodeVarint(); err != nil {
return err
val, n = protowire.ConsumeVarint(b[position:])
if n < 0 {
return protowire.ParseError(n)
}
position += n
i.latestFileSize = int(val)

if val, err = buffer.DecodeVarint(); err != nil {
return err
val, n = protowire.ConsumeVarint(b[position:])
if n < 0 {
return protowire.ParseError(n)
}
position += n
i.lastPersistedBlock = val
if noBlockFilesMarker, err = buffer.DecodeVarint(); err != nil {
return err
noBlockFilesMarker, n = protowire.ConsumeVarint(b[position:])
if n < 0 {
return protowire.ParseError(n)
}
i.noBlockFiles = noBlockFilesMarker == 1
return nil
Expand Down
8 changes: 4 additions & 4 deletions common/ledger/blkstorage/blockfile_mgr_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@ import (
"os"
"testing"

"github.com/golang/protobuf/proto"
"github.com/hyperledger/fabric-protos-go/common"
"github.com/hyperledger/fabric-protos-go/peer"
"github.com/hyperledger/fabric/common/ledger/testutil"
"github.com/hyperledger/fabric/internal/pkg/txflags"
"github.com/hyperledger/fabric/protoutil"
"github.com/stretchr/testify/require"
"google.golang.org/protobuf/encoding/protowire"
)

func TestBlockfileMgrBlockReadWrite(t *testing.T) {
Expand Down Expand Up @@ -101,7 +101,7 @@ func testBlockfileMgrCrashDuringWriting(t *testing.T, numBlksBeforeSavingBlkfile

// simulate a crash scenario
lastBlockBytes := []byte{}
encodedLen := proto.EncodeVarint(uint64(numLastBlockBytes))
encodedLen := protowire.AppendVarint(nil, uint64(numLastBlockBytes))
randomBytes := testutil.ConstructRandomBytes(t, numLastBlockBytes)
lastBlockBytes = append(lastBlockBytes, encodedLen...)
lastBlockBytes = append(lastBlockBytes, randomBytes...)
Expand Down Expand Up @@ -397,7 +397,7 @@ func TestBlockfileMgrFileRolling(t *testing.T) {
by, _, err := serializeBlock(block)
require.NoError(t, err, "Error while serializing block")
blockBytesSize := len(by)
encodedLen := proto.EncodeVarint(uint64(blockBytesSize))
encodedLen := protowire.AppendVarint(nil, uint64(blockBytesSize))
size += blockBytesSize + len(encodedLen)
}

Expand Down Expand Up @@ -466,7 +466,7 @@ func testBlockfileMgrSimulateCrashAtFirstBlockInFile(t *testing.T, deleteBlkfile
// move to next file and simulate crash scenario while writing the first block
blockfileMgr.moveToNextFile()
partialBytesForNextBlock := append(
proto.EncodeVarint(uint64(10000)),
protowire.AppendVarint(nil, uint64(10000)),
[]byte("partialBytesForNextBlock depicting a crash during first block in file")...,
)
blockfileMgr.currentFileWriter.append(partialBytesForNextBlock, true)
Expand Down
Loading

0 comments on commit 6933629

Please sign in to comment.