Skip to content

Commit 11f8ac7

Browse files
committed
[FAB-14019] move proto common.Block helpers
Replace common/Blockdata.Hash, common/BlockData.Bytes, common/BlockHeader.Hash, and common/BlockHeader.Bytes with pure functions in protoutil. Change-Id: I1d9d441ae16e762377e6585941293fa301d2a292 Signed-off-by: Matthew Sykes <sykesmat@us.ibm.com>
1 parent 7be4a41 commit 11f8ac7

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+191
-232
lines changed

common/genesis/genesis.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ func (f *factory) Block(channelID string) *cb.Block {
4444

4545
block := protoutil.NewBlock(0, nil)
4646
block.Data = &cb.BlockData{Data: [][]byte{protoutil.MarshalOrPanic(envelope)}}
47-
block.Header.DataHash = block.Data.Hash()
47+
block.Header.DataHash = protoutil.BlockDataHash(block.Data)
4848
block.Metadata.Metadata[cb.BlockMetadataIndex_LAST_CONFIG] = protoutil.MarshalOrPanic(&cb.Metadata{
4949
Value: protoutil.MarshalOrPanic(&cb.LastConfig{Index: 0}),
5050
})

common/ledger/blkstorage/fsblkstorage/blockfile_mgr.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ func newBlockfileMgr(id string, conf *Conf, indexConfig *blkstorage.IndexConfig,
157157
if err != nil {
158158
panic(fmt.Sprintf("Could not retrieve header of the last block form file: %s", err))
159159
}
160-
lastBlockHash := lastBlockHeader.Hash()
160+
lastBlockHash := protoutil.BlockHeaderHash(lastBlockHeader)
161161
previousBlockHash := lastBlockHeader.PreviousHash
162162
bcInfo = &common.BlockchainInfo{
163163
Height: cpInfo.lastBlockNumber + 1,
@@ -260,7 +260,7 @@ func (mgr *blockfileMgr) addBlock(block *common.Block) error {
260260
if err != nil {
261261
return errors.WithMessage(err, "error serializing block")
262262
}
263-
blockHash := block.Header.Hash()
263+
blockHash := protoutil.BlockHeaderHash(block.Header)
264264
//Get the location / offset where each transaction starts in the block and where the block ends
265265
txOffsets := info.txOffsets
266266
currentOffset := mgr.cpInfo.latestFileChunksize
@@ -408,7 +408,7 @@ func (mgr *blockfileMgr) syncIndex() error {
408408
}
409409

410410
//Update the blockIndexInfo with what was actually stored in file system
411-
blockIdxInfo.blockHash = info.blockHeader.Hash()
411+
blockIdxInfo.blockHash = protoutil.BlockHeaderHash(info.blockHeader)
412412
blockIdxInfo.blockNum = info.blockHeader.Number
413413
blockIdxInfo.flp = &fileLocPointer{fileSuffixNum: blockPlacementInfo.fileNum,
414414
locPointer: locPointer{offset: int(blockPlacementInfo.blockStartOffset)}}

common/ledger/blkstorage/fsblkstorage/blockindex_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ func testBlockIndexSelectiveIndexing(t *testing.T, indexItems []blkstorage.Index
162162

163163
// if index has been configured for an indexItem then the item should be indexed else not
164164
// test 'retrieveBlockByHash'
165-
block, err := blockfileMgr.retrieveBlockByHash(blocks[0].Header.Hash())
165+
block, err := blockfileMgr.retrieveBlockByHash(protoutil.BlockHeaderHash(blocks[0].Header))
166166
if containsAttr(indexItems, blkstorage.IndexableAttrBlockHash) {
167167
assert.NoError(t, err, "Error while retrieving block by hash")
168168
assert.Equal(t, blocks[0], block)

common/ledger/blkstorage/fsblkstorage/fs_blockstore_provider_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ func TestMultipleBlockStores(t *testing.T) {
4848
func checkBlocks(t *testing.T, expectedBlocks []*common.Block, store blkstorage.BlockStore) {
4949
bcInfo, _ := store.GetBlockchainInfo()
5050
assert.Equal(t, uint64(len(expectedBlocks)), bcInfo.Height)
51-
assert.Equal(t, expectedBlocks[len(expectedBlocks)-1].GetHeader().Hash(), bcInfo.CurrentBlockHash)
51+
assert.Equal(t, protoutil.BlockHeaderHash(expectedBlocks[len(expectedBlocks)-1].GetHeader()), bcInfo.CurrentBlockHash)
5252

5353
itr, _ := store.RetrieveBlocks(0)
5454
for i := 0; i < len(expectedBlocks); i++ {
@@ -62,7 +62,7 @@ func checkBlocks(t *testing.T, expectedBlocks []*common.Block, store blkstorage.
6262
retrievedBlock, _ := store.RetrieveBlockByNumber(uint64(blockNum))
6363
assert.Equal(t, block, retrievedBlock)
6464

65-
retrievedBlock, _ = store.RetrieveBlockByHash(block.Header.Hash())
65+
retrievedBlock, _ = store.RetrieveBlockByHash(protoutil.BlockHeaderHash(block.Header))
6666
assert.Equal(t, block, retrievedBlock)
6767

6868
for txNum := 0; txNum < len(block.Data.Data); txNum++ {

common/ledger/blkstorage/fsblkstorage/pkg_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import (
2525
"github.com/hyperledger/fabric/common/flogging"
2626
"github.com/hyperledger/fabric/common/ledger/blkstorage"
2727
"github.com/hyperledger/fabric/protos/common"
28+
"github.com/hyperledger/fabric/protoutil"
2829
"github.com/stretchr/testify/assert"
2930
)
3031

@@ -93,7 +94,7 @@ func (w *testBlockfileMgrWrapper) addBlocks(blocks []*common.Block) {
9394

9495
func (w *testBlockfileMgrWrapper) testGetBlockByHash(blocks []*common.Block) {
9596
for i, block := range blocks {
96-
hash := block.Header.Hash()
97+
hash := protoutil.BlockHeaderHash(block.Header)
9798
b, err := w.blockfileMgr.retrieveBlockByHash(hash)
9899
assert.NoError(w.t, err, "Error while retrieving [%d]th block from blockfileMgr", i)
99100
assert.Equal(w.t, block, b)

common/ledger/blockledger/blackbox_test.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414
"github.com/hyperledger/fabric/common/ledger/blockledger"
1515
cb "github.com/hyperledger/fabric/protos/common"
1616
ab "github.com/hyperledger/fabric/protos/orderer"
17+
"github.com/hyperledger/fabric/protoutil"
1718
)
1819

1920
type ledgerTestable interface {
@@ -91,7 +92,7 @@ func testReinitialization(lf ledgerTestFactory, t *testing.T) {
9192
if block == nil {
9293
t.Fatalf("Error retrieving block 1")
9394
}
94-
if !bytes.Equal(block.Header.Hash(), aBlock.Header.Hash()) {
95+
if !bytes.Equal(protoutil.BlockHeaderHash(block.Header), protoutil.BlockHeaderHash(aBlock.Header)) {
9596
t.Fatalf("Block hashes did no match")
9697
}
9798
}
@@ -106,7 +107,7 @@ func testAddition(lf ledgerTestFactory, t *testing.T) {
106107
if genesis == nil {
107108
t.Fatalf("Could not retrieve genesis block")
108109
}
109-
prevHash := genesis.Header.Hash()
110+
prevHash := protoutil.BlockHeaderHash(genesis.Header)
110111

111112
li.Append(blockledger.CreateNextBlock(li, []*cb.Envelope{{Payload: []byte("My Data")}}))
112113
if li.Height() != 2 {

common/ledger/blockledger/file/impl_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ func TestInitialization(t *testing.T) {
132132

133133
block := blockledger.GetBlock(fl, 0)
134134
assert.NotNil(t, block, "Error retrieving genesis block")
135-
assert.Equal(t, genesisBlock.Header.Hash(), block.Header.Hash(), "Block hashes did no match")
135+
assert.Equal(t, protoutil.BlockHeaderHash(genesisBlock.Header), protoutil.BlockHeaderHash(block.Header), "Block hashes did no match")
136136
}
137137

138138
func TestReinitialization(t *testing.T) {
@@ -170,7 +170,7 @@ func TestReinitialization(t *testing.T) {
170170

171171
block := blockledger.GetBlock(fl, 1)
172172
assert.NotNil(t, block, "Error retrieving block 1")
173-
assert.Equal(t, b1.Header.Hash(), block.Header.Hash(), "Block hashes did no match")
173+
assert.Equal(t, protoutil.BlockHeaderHash(b1.Header), protoutil.BlockHeaderHash(block.Header), "Block hashes did no match")
174174
}
175175

176176
func TestAddition(t *testing.T) {

common/ledger/blockledger/json/factory.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import (
1515

1616
"github.com/golang/protobuf/jsonpb"
1717
"github.com/hyperledger/fabric/common/ledger/blockledger"
18+
"github.com/hyperledger/fabric/protoutil"
1819
"github.com/pkg/errors"
1920
)
2021

@@ -95,7 +96,7 @@ func (jl *jsonLedger) initializeBlockHeight() {
9596
if block == nil {
9697
logger.Panicf("Error reading block %d", jl.height-1)
9798
}
98-
jl.lastHash = block.Header.Hash()
99+
jl.lastHash = protoutil.BlockHeaderHash(block.Header)
99100
}
100101

101102
// ChainIDs returns the chain IDs the factory is aware of

common/ledger/blockledger/json/impl.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import (
1818
"github.com/hyperledger/fabric/common/ledger/blockledger"
1919
cb "github.com/hyperledger/fabric/protos/common"
2020
ab "github.com/hyperledger/fabric/protos/orderer"
21+
"github.com/hyperledger/fabric/protoutil"
2122
"github.com/pkg/errors"
2223
)
2324

@@ -135,7 +136,7 @@ func (jl *jsonLedger) Append(block *cb.Block) error {
135136
}
136137

137138
jl.writeBlock(block)
138-
jl.lastHash = block.Header.Hash()
139+
jl.lastHash = protoutil.BlockHeaderHash(block.Header)
139140
jl.height++
140141

141142
// Manage the signal channel under lock to avoid race with read in Next

common/ledger/blockledger/json/impl_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ func TestInitialization(t *testing.T) {
6262
block, found := fl.readBlock(0)
6363
assert.NotNil(t, block, "Error retrieving genesis block")
6464
assert.True(t, found, "Error retrieving genesis block")
65-
assert.Equal(t, fl.lastHash, block.Header.Hash(), "Block hashes did no match")
65+
assert.Equal(t, fl.lastHash, protoutil.BlockHeaderHash(block.Header), "Block hashes did no match")
6666

6767
}
6868

@@ -83,7 +83,7 @@ func TestReinitialization(t *testing.T) {
8383
block, found := fl.readBlock(1)
8484
assert.NotNil(t, block, "Error retrieving block")
8585
assert.True(t, found, "Error retrieving block")
86-
assert.Equal(t, fl.lastHash, block.Header.Hash(), "Block hashes did no match")
86+
assert.Equal(t, fl.lastHash, protoutil.BlockHeaderHash(block.Header), "Block hashes did no match")
8787
}
8888

8989
func TestMultiReinitialization(t *testing.T) {

0 commit comments

Comments
 (0)