From 6c38236574f592ec4c7c3adf272e8b527794205d Mon Sep 17 00:00:00 2001 From: Atsushin Date: Wed, 15 Jul 2020 19:44:44 +1000 Subject: [PATCH] BE-776 Fix calculation of block hash (#141) Also added test for validating hash chain Signed-off-by: Atsushi Neki --- .../fabric/e2e-test/specs/apitest_test.go | 7 ++++ .../fabric/e2e-test/specs/validate_hash.sh | 40 +++++++++++++++++++ app/platform/fabric/utils/FabricUtils.js | 9 +++-- 3 files changed, 52 insertions(+), 4 deletions(-) create mode 100755 app/platform/fabric/e2e-test/specs/validate_hash.sh diff --git a/app/platform/fabric/e2e-test/specs/apitest_test.go b/app/platform/fabric/e2e-test/specs/apitest_test.go index e0fbcb2ce..0da496403 100644 --- a/app/platform/fabric/e2e-test/specs/apitest_test.go +++ b/app/platform/fabric/e2e-test/specs/apitest_test.go @@ -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() { diff --git a/app/platform/fabric/e2e-test/specs/validate_hash.sh b/app/platform/fabric/e2e-test/specs/validate_hash.sh new file mode 100755 index 000000000..65bfbed0a --- /dev/null +++ b/app/platform/fabric/e2e-test/specs/validate_hash.sh @@ -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 + diff --git a/app/platform/fabric/utils/FabricUtils.js b/app/platform/fabric/utils/FabricUtils.js index 664e115b0..f3813c486 100644 --- a/app/platform/fabric/utils/FabricUtils.js +++ b/app/platform/fabric/utils/FabricUtils.js @@ -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' );