Skip to content

Commit

Permalink
BE-776 Fix calculation of block hash (#141)
Browse files Browse the repository at this point in the history
Also added test for validating hash chain

Signed-off-by: Atsushi Neki <atsushin@fast.au.fujitsu.com>
  • Loading branch information
nekia authored Jul 15, 2020
1 parent d635e39 commit 6c38236
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 4 deletions.
7 changes: 7 additions & 0 deletions app/platform/fabric/e2e-test/specs/apitest_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,13 @@ var _ = Describe("REST API Test Suite - Single profile", func() {
Expect(CheckHowManyEventHubRegistered()).Should(Equal(true))
})

It("should be able to validate hashchain correctly", func() {
_, err := exec.Command("bash", "./validate_hash.sh", "-c", "commonchannel").Output()
Expect(err).NotTo(HaveOccurred())

_, err = exec.Command("bash", "./validate_hash.sh", "-c", "org1channel").Output()
Expect(err).NotTo(HaveOccurred())
})
})

It("stop explorer", func() {
Expand Down
40 changes: 40 additions & 0 deletions app/platform/fabric/e2e-test/specs/validate_hash.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#!/bin/bash

channel=commonchannel
while getopts "c:" opt; do
case "$opt" in
c)
channel=$OPTARG
;;
esac
done

docker exec explorerdb.mynetwork.com \
psql -t -U postgres fabricexplorer -c \
"select array_to_json(array_agg(row_to_json(t)))
from (
select blocks.blocknum, blocks.prehash, blocks.blockhash from blocks
join channel on blocks.channel_genesis_hash = channel.channel_genesis_hash
where channel.name='${channel}'
order by blocknum
) t" > result.json

length=$(jq 'length' result.json)
i=0
while [ $i -lt $(expr $length - 1) ]; do
current=$i
i=$(expr $i + 1)
next=$i
bh=$(jq '.['$current'].blockhash' result.json)
ph=$(jq '.['$next'].prehash' result.json)
echo $bh
echo $ph
if [ $bh != $ph ]; then
echo "FAIL... Invalid blockhash chain : $(jq '.['$current'].blocknum' result.json)"
exit 1
fi
done

echo 'PASS!!! Valid blockhash chain'
exit 0

9 changes: 5 additions & 4 deletions app/platform/fabric/utils/FabricUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -134,17 +134,18 @@ async function generateDir() {
async function generateBlockHash(header) {
const headerAsn = asn.define('headerAsn', function() {
this.seq().obj(
this.key('Number').octstr(),
this.key('Number').int(),
this.key('PreviousHash').octstr(),
this.key('DataHash').octstr()
);
});
logger.info('generateBlockHash', header.number.toString());
// ToDo: Need to handle Long data correctly. header.number {"low":3,"high":0,"unsigned":true}
const output = headerAsn.encode(
{
Number: header.number.toString(),
PreviousHash: header.previous_hash.toString('hex'),
DataHash: header.data_hash.toString('hex')
Number: parseInt(header.number.low),
PreviousHash: header.previous_hash,
DataHash: header.data_hash
},
'der'
);
Expand Down

0 comments on commit 6c38236

Please sign in to comment.