Skip to content
This repository was archived by the owner on Apr 22, 2025. It is now read-only.

Commit 10323e4

Browse files
[This fixes FAB-3969]
Change-Id: I35884c0d32fc2a201313839d1c4ad180c49972ea Signed-off-by: liuwenliang0632@qq.com <liuwenliang0632@qq.com>
1 parent 40bbb9d commit 10323e4

File tree

2 files changed

+67
-1
lines changed

2 files changed

+67
-1
lines changed
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/*
2+
Copyright DTCC 2016 All Rights Reserved.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package org.hyperledger.fabric.sdk.helper;
18+
19+
import java.io.ByteArrayOutputStream;
20+
import java.io.IOException;
21+
22+
import org.bouncycastle.asn1.ASN1Integer;
23+
import org.bouncycastle.asn1.DEROctetString;
24+
import org.bouncycastle.asn1.DERSequenceGenerator;
25+
import org.hyperledger.fabric.sdk.exception.InvalidArgumentException;
26+
import org.hyperledger.fabric.sdk.security.CryptoSuite;
27+
28+
29+
public class ChainUtils {
30+
31+
public static CryptoSuite suite = null;
32+
/**
33+
* used asn1 and get hash
34+
* @param blockNumber
35+
* @param previousHash
36+
* @param dataHash
37+
* @return byte[]
38+
* @throws IOException
39+
* @throws InvalidArgumentException
40+
*/
41+
public static byte[] calculateBlockHash(long blockNumber, byte[] previousHash, byte[] dataHash) throws IOException, InvalidArgumentException{
42+
43+
if (previousHash == null) {
44+
throw new InvalidArgumentException("previousHash parameter is null.");
45+
}
46+
if (dataHash == null) {
47+
throw new InvalidArgumentException("dataHash parameter is null.");
48+
}
49+
50+
if(null == suite){
51+
suite = CryptoSuite.Factory.getCryptoSuite();
52+
}
53+
54+
ByteArrayOutputStream s = new ByteArrayOutputStream();
55+
DERSequenceGenerator seq = new DERSequenceGenerator(s);
56+
seq.addObject(new ASN1Integer(blockNumber));
57+
seq.addObject(new DEROctetString(previousHash));
58+
seq.addObject(new DEROctetString(dataHash));
59+
seq.close();
60+
return suite.hash(s.toByteArray());
61+
62+
}
63+
}

src/test/java/org/hyperledger/fabric/sdkintegration/End2endIT.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
package org.hyperledger.fabric.sdkintegration;
1616

1717
import java.io.File;
18+
import java.io.IOException;
1819
import java.io.UnsupportedEncodingException;
1920
import java.net.MalformedURLException;
2021
import java.nio.file.Paths;
@@ -52,6 +53,7 @@
5253
import org.hyperledger.fabric.sdk.exception.InvalidProtocolBufferRuntimeException;
5354
import org.hyperledger.fabric.sdk.exception.ProposalException;
5455
import org.hyperledger.fabric.sdk.exception.TransactionEventException;
56+
import org.hyperledger.fabric.sdk.helper.ChainUtils;
5557
import org.hyperledger.fabric.sdk.security.CryptoSuite;
5658
import org.hyperledger.fabric.sdk.testutils.TestConfig;
5759
import org.hyperledger.fabric_ca.sdk.HFCAClient;
@@ -631,7 +633,7 @@ File findFile_sk(File directory) {
631633
txExpected.put("writeset1", "Missing writeset for channel bar block 1");
632634
}
633635

634-
void blockWalker(Channel channel) throws InvalidProtocolBufferException, InvalidArgumentException, ProposalException, UnsupportedEncodingException {
636+
void blockWalker(Channel channel) throws InvalidProtocolBufferException, InvalidArgumentException, ProposalException, UnsupportedEncodingException, IOException {
635637

636638
try {
637639
BlockchainInfo channelInfo = channel.queryBlockchainInfo();
@@ -642,6 +644,7 @@ void blockWalker(Channel channel) throws InvalidProtocolBufferException, Invalid
642644

643645
out("current block number %d has data hash: %s", blockNumber, Hex.encodeHexString(returnedBlock.getDataHash()));
644646
out("current block number %d has previous hash id: %s", blockNumber, Hex.encodeHexString(returnedBlock.getPreviousHash()));
647+
out("current block number %d has calculated block hash is %s", blockNumber, Hex.encodeHexString(ChainUtils.calculateBlockHash(blockNumber, returnedBlock.getPreviousHash(), returnedBlock.getDataHash())));
645648

646649
final int envelopCount = returnedBlock.getEnvelopCount();
647650
assertEquals(1, envelopCount);

0 commit comments

Comments
 (0)