Closed samuelvenzi closed 1 month ago
Upon further investigation @Tubar2 has discovered that the data_hash
that we were using to try and get the block is not the hash needed for the function.
We got the function to work correctly by going inside the peer chains folder, opening up the LevelDB data that indexes each block with the hash. With that hash (which is not the data_hash
), it works.
The question is: how do I get the correct hash since it doesn't seem to be in the block and taking the SHA256 of the block bytes does not return an existing hash. The answer is that the hash should be taken from the header of the block. Like this:
func BlockHeaderHash(b *protos.BlockHeader) []byte {
sum := sha256.Sum256(BlockHeaderBytes(b))
return sum[:]
}
type asn1Header struct {
Number *big.Int
PreviousHash []byte
DataHash []byte
}
func BlockHeaderBytes(b *protos.BlockHeader) []byte {
asn1Header := asn1Header{
PreviousHash: b.PreviousHash,
DataHash: b.DataHash,
Number: new(big.Int).SetUint64(b.Number),
}
result, err := asn1.Marshal(asn1Header)
if err != nil {
// Errors should only arise for types which cannot be encoded, since the
// BlockHeader type is known a-priori to contain only encodable types, an
// error here is fatal and should not be propagated
panic(err)
}
return result
}
Because of that, I'll close the issue. Hopefully, this serves for anyone that thinks that data_hash
is the actual hash the block is indexed by.
Description
Using qscc to get block by hash using the GetBlockByHash transaction. I am getting the following error as logged by the peer.
I'm querying it using the Gateway SDK for Go like this (some syntax supressed for clarity):
Before the error the peer also logs:
One thing to note is that
GetBlockByNumber
works fine.This has been observed in my testing of v2.5.3 and v2.5.10.
Steps to reproduce
This has been reproducible locally with Fabric Samples'
test-network
, on Hyperledger Labs CC-Tools and in production environments. To reproduce it with thetest-network
, follow the steps.asset-transfer-basic/application-gateway-go/assetTransfer.go
with the following content:asset-transfer-basic/application-gateway-go/
runThe application will
GetBlockByNumber
with3
as the number for the block, then it will decode it partially so as to get the hash of the block, and then attempt to fetch the block by hash. This should throw a panic.