Skip to content

Commit 25c888d

Browse files
author
Jason Yellick
committed
[FAB-1700] Determinsitic BlockData hashing
https://jira.hyperledger.org/browse/FAB-1700 This is a companion item to https://jira.hyperledger.org/browse/FAB-1699 It removes the protobuf serialization of the BlockData in favor a simple concatenation of the byte contents. Eventually, this should be encoded as a Merkle tree at the configured width, but, like with our hashing algorithm, for v1, we will make it configurable at a later date, see: https://jira.hyperledger.org/browse/FAB-1983 Change-Id: I8a09e84f1c35d8fe1269339a443c44ba01b841a8 Signed-off-by: Jason Yellick <[email protected]>
1 parent 7104614 commit 25c888d

File tree

2 files changed

+12
-9
lines changed

2 files changed

+12
-9
lines changed

common/chainconfig/chainconfig.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ package chainconfig
1818

1919
import (
2020
"fmt"
21+
"math"
2122

2223
"github.com/hyperledger/fabric/common/util"
2324
cb "github.com/hyperledger/fabric/protos/common"
@@ -144,8 +145,8 @@ func (pm *DescriptorImpl) ProposeConfig(configItem *cb.ConfigurationItem) error
144145
return fmt.Errorf("Unmarshaling error for BlockDataHashingStructure: %s", err)
145146
}
146147

147-
if blockDataHashingStructure.Width == 0 {
148-
return fmt.Errorf("BlockDataHashStructure width must not be zero")
148+
if blockDataHashingStructure.Width != math.MaxUint32 {
149+
return fmt.Errorf("BlockDataHashStructure width only supported at MaxUint32 in this version")
149150
}
150151

151152
pm.pendingConfig.blockDataHashingStructureWidth = blockDataHashingStructure.Width

protos/common/block.go

+9-7
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ import (
2121
"fmt"
2222
"math"
2323

24-
"github.com/golang/protobuf/proto"
2524
"github.com/hyperledger/fabric/common/util"
2625
)
2726

@@ -75,12 +74,15 @@ func (b *BlockHeader) Hash() []byte {
7574
return util.ComputeCryptoHash(b.Bytes())
7675
}
7776

77+
// Bytes returns a deterministically serialized version of the BlockData
78+
// eventually, this should be replaced with a true Merkle tree construction,
79+
// but for the moment, we assume a Merkle tree of infinite width (uint32_max)
80+
// which degrades to a flat hash
81+
func (b *BlockData) Bytes() []byte {
82+
return util.ConcatenateBytes(b.Data...)
83+
}
84+
7885
// Hash returns the hash of the marshaled representation of the block data.
7986
func (b *BlockData) Hash() []byte {
80-
data, err := proto.Marshal(b) // XXX this is wrong, protobuf is not the right mechanism to serialize for a hash, AND, it is not a MerkleTree hash
81-
if err != nil {
82-
panic("This should never fail and is generally irrecoverable")
83-
}
84-
85-
return util.ComputeCryptoHash(data)
87+
return util.ComputeCryptoHash(b.Bytes())
8688
}

0 commit comments

Comments
 (0)