Skip to content

Commit 3685137

Browse files
committed
<fix>(model): fix receipt hash calculate bug.
1 parent d0b8ff0 commit 3685137

File tree

3 files changed

+181
-107
lines changed

3 files changed

+181
-107
lines changed

.ci/ci_check.sh

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -138,10 +138,8 @@ check_standard_node()
138138
prepare_environment "${2}"
139139
## run integration test
140140
bash gradlew clean integrationTest --info
141-
# if $? is not 0, then exit
142-
if [ ${?} -ne 0 ]; then
143-
cat log/*.log
144-
fi
141+
# if hs_err log exist, print it
142+
(cat hs_err_pid*.log) || true
145143
## clean
146144
clean_node "${1}"
147145
}
@@ -153,6 +151,8 @@ check_wasm_node()
153151
prepare_wasm_environment
154152
## run integration test
155153
bash gradlew clean integrationWasmTest --info
154+
(cat hs_err_pid*.log) || true
155+
156156
## clean
157157
clean_node "${1}"
158158
}

src/integration-test/java/org/fisco/bcos/sdk/v3/test/transaction/decoder/HashCalculatorTest.java

Lines changed: 174 additions & 100 deletions
Original file line numberDiff line numberDiff line change
@@ -1,56 +1,77 @@
11
package org.fisco.bcos.sdk.v3.test.transaction.decoder;
22

3-
import org.fisco.bcos.sdk.jni.utilities.tx.TransactionVersion;
43
import org.fisco.bcos.sdk.v3.BcosSDK;
54
import org.fisco.bcos.sdk.v3.client.Client;
65
import org.fisco.bcos.sdk.v3.client.protocol.model.JsonTransactionResponse;
76
import org.fisco.bcos.sdk.v3.client.protocol.response.BcosBlock;
87
import org.fisco.bcos.sdk.v3.client.protocol.response.BcosBlockHeader;
9-
import org.fisco.bcos.sdk.v3.client.protocol.response.BlockNumber;
108
import org.fisco.bcos.sdk.v3.crypto.hash.Keccak256;
119
import org.fisco.bcos.sdk.v3.crypto.hash.SM3Hash;
1210
import org.fisco.bcos.sdk.v3.model.ConstantConfig;
1311
import org.fisco.bcos.sdk.v3.model.CryptoType;
1412
import org.fisco.bcos.sdk.v3.model.EnumNodeVersion;
1513
import org.fisco.bcos.sdk.v3.model.TransactionReceipt;
14+
import org.fisco.bcos.sdk.v3.test.contract.solidity.EventSubDemo;
15+
import org.fisco.bcos.sdk.v3.test.contract.solidity.Incremental;
16+
import org.fisco.bcos.sdk.v3.transaction.model.exception.ContractException;
1617
import org.fisco.bcos.sdk.v3.utils.Hex;
1718
import org.fisco.bcos.sdk.v3.utils.MerkleProofUtility;
18-
import org.fisco.bcos.sdk.v3.utils.ObjectMapperFactory;
1919
import org.junit.Assert;
2020
import org.junit.Test;
2121

2222
import java.io.ByteArrayOutputStream;
2323
import java.io.IOException;
2424
import java.math.BigInteger;
25+
import java.util.UUID;
2526

2627
import static org.fisco.bcos.sdk.v3.utils.Numeric.toBytesPadded;
2728

2829
public class HashCalculatorTest {
2930
private static final String CONFIG_FILE =
3031
"src/integration-test/resources/" + ConstantConfig.CONFIG_FILE_NAME;
3132
private final Client client;
32-
private final BcosBlock.Block block;
33+
private BcosBlock.Block block;
3334
private JsonTransactionResponse transactionResponse = null;
35+
private JsonTransactionResponse transactionResponseV1 = null;
36+
private JsonTransactionResponse transactionResponseV2 = null;
3437

3538
private TransactionReceipt transactionReceipt = null;
39+
private TransactionReceipt transactionReceiptV1 = null;
40+
private TransactionReceipt transactionReceiptV2 = null;
3641

37-
public HashCalculatorTest() {
42+
public HashCalculatorTest() throws ContractException {
3843
BcosSDK sdk = BcosSDK.build(CONFIG_FILE);
3944
client = sdk.getClient("group0");
40-
BlockNumber blockNumber = client.getBlockNumber();
41-
block = client.getBlockByNumber(blockNumber.getBlockNumber(), false, false).getBlock();
42-
if (!block.getTransactions().isEmpty()) {
43-
BcosBlock.TransactionObject transactionObject =
44-
(BcosBlock.TransactionObject) block.getTransactions().get(0);
45-
JsonTransactionResponse transactionResponse1 = transactionObject.get();
46-
transactionResponse =
47-
client.getTransaction(transactionResponse1.getHash(), true)
48-
.getTransaction()
49-
.get();
50-
transactionReceipt =
51-
client.getTransactionReceipt(transactionResponse1.getHash(), true)
52-
.getTransactionReceipt();
45+
if (client.isSupportTransactionV1()) {
46+
Incremental incremental =
47+
Incremental.deploy(client, client.getCryptoSuite().getCryptoKeyPair());
48+
String nonce = UUID.randomUUID().toString().replace("-", "");
49+
transactionReceiptV1 = incremental.buildMethodInc(nonce).setNonce(nonce).send();
50+
transactionResponseV1 =
51+
client.getTransaction(transactionReceiptV1.getTransactionHash(), true)
52+
.getResult();
5353
}
54+
55+
if (client.isSupportTransactionV2()) {
56+
Incremental incremental =
57+
Incremental.deploy(client, client.getCryptoSuite().getCryptoKeyPair());
58+
String nonce = UUID.randomUUID().toString().replace("-", "");
59+
transactionReceiptV2 =
60+
incremental
61+
.buildMethodInc(nonce)
62+
.setNonce(nonce)
63+
.setExtension(nonce.getBytes())
64+
.send();
65+
transactionResponseV2 =
66+
client.getTransaction(transactionReceiptV2.getTransactionHash(), true)
67+
.getResult();
68+
}
69+
70+
EventSubDemo eventSubDemo =
71+
EventSubDemo.deploy(client, client.getCryptoSuite().getCryptoKeyPair());
72+
transactionReceipt = eventSubDemo.echo(BigInteger.TEN, BigInteger.valueOf(1000), "Hello");
73+
transactionResponse =
74+
client.getTransaction(transactionReceipt.getTransactionHash(), true).getResult();
5475
}
5576

5677
@Override
@@ -66,28 +87,13 @@ protected void finalize() {
6687

6788
@Test
6889
public void testTxHashCalculate() throws IOException {
69-
if (transactionResponse == null && block.getNumber() == 0) {
90+
if (transactionResponse == null) {
7091
return;
7192
}
72-
String chainId = transactionResponse.getChainID();
73-
String groupId = transactionResponse.getGroupID();
74-
long blockLimit = transactionResponse.getBlockLimit();
75-
String nonce = transactionResponse.getNonce();
76-
String contractAddress = transactionResponse.getTo();
77-
String encodedAbi = transactionResponse.getInput();
78-
String abi = transactionResponse.getAbi();
93+
block =
94+
client.getBlockByNumber(transactionReceipt.getBlockNumber(), false, false)
95+
.getBlock();
7996

80-
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
81-
// version
82-
byteArrayOutputStream.write(
83-
toBytesPadded(BigInteger.valueOf(transactionResponse.getVersion()), 4));
84-
// chainId
85-
byteArrayOutputStream.write(chainId.getBytes());
86-
// groupId
87-
byteArrayOutputStream.write(groupId.getBytes());
88-
// blockLimit
89-
byteArrayOutputStream.write(toBytesPadded(BigInteger.valueOf(blockLimit), 8));
90-
// nonce
9197
String version =
9298
client.getGroupInfo()
9399
.getResult()
@@ -101,58 +107,141 @@ public void testTxHashCalculate() throws IOException {
101107
.compareTo(EnumNodeVersion.BCOS_3_3_0.toVersionObj())
102108
>= 0) {
103109
System.out.println("use hex decode nonce");
104-
byteArrayOutputStream.write(Hex.decode(nonce));
105110
String calculateTxHashInNative =
106111
transactionResponse.calculateTxHashInNative(client.getCryptoSuite().hashImpl);
107112
Assert.assertEquals(calculateTxHashInNative, transactionResponse.getHash());
113+
String hash1 =
114+
transactionResponse.calculateHash(client.getCryptoSuite().cryptoTypeConfig);
115+
Assert.assertEquals(hash1, transactionResponse.getHash());
108116
} else {
109117
System.out.println("use string nonce");
110-
byteArrayOutputStream.write(nonce.getBytes());
118+
String jniHash = transactionResponse.calculateHash(client.getCryptoSuite());
119+
Assert.assertEquals(jniHash, transactionResponse.getHash());
111120
}
112-
// to
113-
byteArrayOutputStream.write(contractAddress.getBytes());
114-
// input
115-
byteArrayOutputStream.write(Hex.decode(encodedAbi));
116-
// abi
117-
byteArrayOutputStream.write(abi.getBytes());
118121

119-
if (transactionResponse.getVersion() >= TransactionVersion.V1.getValue()) {
120-
byteArrayOutputStream.write(transactionResponse.getValue().getBytes());
121-
byteArrayOutputStream.write(transactionResponse.getGasPrice().getBytes());
122-
byteArrayOutputStream.write(
123-
toBytesPadded(BigInteger.valueOf(transactionResponse.getGasLimit()), 8));
124-
byteArrayOutputStream.write(transactionResponse.getMaxFeePerGas().getBytes());
125-
byteArrayOutputStream.write(transactionResponse.getMaxPriorityFeePerGas().getBytes());
122+
if (client.getChainCompatibilityVersion()
123+
.compareTo(EnumNodeVersion.BCOS_3_2_0.toVersionObj())
124+
< 0) {
125+
return;
126+
}
127+
if (client.getCryptoSuite().cryptoTypeConfig == CryptoType.ECDSA_TYPE) {
128+
boolean verifyMerkle =
129+
MerkleProofUtility.verifyMerkle(
130+
block.getTransactionsRoot(),
131+
transactionResponse.getTxProof(),
132+
transactionResponse.getHash(),
133+
new Keccak256());
134+
Assert.assertTrue(verifyMerkle);
135+
}
136+
if (client.getCryptoSuite().cryptoTypeConfig == CryptoType.SM_TYPE) {
137+
boolean verifyMerkle =
138+
MerkleProofUtility.verifyMerkle(
139+
block.getTransactionsRoot(),
140+
transactionResponse.getTxProof(),
141+
transactionResponse.getHash(),
142+
new SM3Hash());
143+
Assert.assertTrue(verifyMerkle);
126144
}
145+
}
127146

128-
if(transactionResponse.getVersion() == TransactionVersion.V2.getValue()){
129-
byteArrayOutputStream.write(transactionResponse.getExtension());
147+
@Test
148+
public void testTxV1HashCalculate() throws IOException {
149+
if (transactionResponseV1 == null) {
150+
return;
130151
}
152+
block =
153+
client.getBlockByNumber(transactionReceiptV1.getBlockNumber(), false, false)
154+
.getBlock();
155+
String version =
156+
client.getGroupInfo()
157+
.getResult()
158+
.getNodeList()
159+
.get(0)
160+
.getIniConfig()
161+
.getBinaryInfo()
162+
.getVersion();
163+
System.out.println("node bin version: " + version);
164+
165+
String jniHash =
166+
transactionResponseV1.calculateHash(client.getCryptoSuite().cryptoTypeConfig);
167+
Assert.assertEquals(jniHash, transactionResponseV1.getHash());
168+
String nativeHash =
169+
transactionResponseV1.calculateTxHashInNative(client.getCryptoSuite().hashImpl);
170+
Assert.assertEquals(nativeHash, transactionResponseV1.getHash());
131171

132-
String hash = "";
133172
if (client.getCryptoSuite().cryptoTypeConfig == CryptoType.ECDSA_TYPE) {
134-
hash = Keccak256.calculateHash(byteArrayOutputStream.toByteArray());
173+
boolean verifyMerkle =
174+
MerkleProofUtility.verifyMerkle(
175+
block.getTransactionsRoot(),
176+
transactionResponseV1.getTxProof(),
177+
transactionResponseV1.getHash(),
178+
new Keccak256());
179+
Assert.assertTrue(verifyMerkle);
135180
}
136181
if (client.getCryptoSuite().cryptoTypeConfig == CryptoType.SM_TYPE) {
137-
hash = SM3Hash.calculateHash((byteArrayOutputStream.toByteArray()));
182+
boolean verifyMerkle =
183+
MerkleProofUtility.verifyMerkle(
184+
block.getTransactionsRoot(),
185+
transactionResponseV1.getTxProof(),
186+
transactionResponseV1.getHash(),
187+
new SM3Hash());
188+
Assert.assertTrue(verifyMerkle);
138189
}
139-
hash = Hex.addPrefix(hash);
140-
Assert.assertEquals(hash, transactionResponse.getHash());
141-
if (EnumNodeVersion.getClassVersion(version)
142-
.compareTo(EnumNodeVersion.BCOS_3_3_0.toVersionObj())
143-
< 0) {
144-
String jniHash = transactionResponse.calculateHash(client.getCryptoSuite());
145-
Assert.assertEquals(jniHash, transactionResponse.getHash());
146-
} else {
147-
String hash1 =
148-
transactionResponse.calculateHash(client.getCryptoSuite().cryptoTypeConfig);
149-
Assert.assertEquals(hash1, transactionResponse.getHash());
190+
}
191+
192+
@Test
193+
public void testTxV2HashCalculate() throws IOException {
194+
if (transactionResponseV2 == null) {
195+
return;
196+
}
197+
block =
198+
client.getBlockByNumber(transactionReceiptV2.getBlockNumber(), false, false)
199+
.getBlock();
200+
String version =
201+
client.getGroupInfo()
202+
.getResult()
203+
.getNodeList()
204+
.get(0)
205+
.getIniConfig()
206+
.getBinaryInfo()
207+
.getVersion();
208+
System.out.println("node bin version: " + version);
209+
String jniHash =
210+
transactionResponseV2.calculateHash(client.getCryptoSuite().cryptoTypeConfig);
211+
Assert.assertEquals(jniHash, transactionResponseV2.getHash());
212+
String nativeHash =
213+
transactionResponseV2.calculateTxHashInNative(client.getCryptoSuite().hashImpl);
214+
Assert.assertEquals(nativeHash, transactionResponseV2.getHash());
215+
216+
if (client.getCryptoSuite().cryptoTypeConfig == CryptoType.ECDSA_TYPE) {
217+
boolean verifyMerkle =
218+
MerkleProofUtility.verifyMerkle(
219+
block.getTransactionsRoot(),
220+
transactionResponseV2.getTxProof(),
221+
transactionResponseV2.getHash(),
222+
new Keccak256());
223+
Assert.assertTrue(verifyMerkle);
224+
}
225+
if (client.getCryptoSuite().cryptoTypeConfig == CryptoType.SM_TYPE) {
226+
boolean verifyMerkle =
227+
MerkleProofUtility.verifyMerkle(
228+
block.getTransactionsRoot(),
229+
transactionResponseV2.getTxProof(),
230+
transactionResponseV2.getHash(),
231+
new SM3Hash());
232+
Assert.assertTrue(verifyMerkle);
150233
}
151234
}
152235

153236
@Test
154237
public void testBlock() throws IOException {
155238

239+
if (block == null) {
240+
block =
241+
client.getBlockByNumber(transactionReceipt.getBlockNumber(), false, false)
242+
.getBlock();
243+
}
244+
156245
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
157246

158247
// version
@@ -203,45 +292,30 @@ public void testBlock() throws IOException {
203292
}
204293

205294
@Test
206-
public void testTxProof() {
207-
if (transactionResponse == null && block.getNumber() == 0) {
208-
return;
209-
}
210-
if (client.getChainCompatibilityVersion()
211-
.compareTo(EnumNodeVersion.BCOS_3_2_0.toVersionObj())
212-
< 0) {
213-
return;
214-
}
215-
if (client.getCryptoSuite().cryptoTypeConfig == CryptoType.ECDSA_TYPE) {
216-
boolean verifyMerkle =
217-
MerkleProofUtility.verifyMerkle(
218-
block.getTransactionsRoot(),
219-
transactionResponse.getTxProof(),
220-
transactionResponse.getHash(),
221-
new Keccak256());
222-
Assert.assertTrue(verifyMerkle);
223-
}
224-
if (client.getCryptoSuite().cryptoTypeConfig == CryptoType.SM_TYPE) {
225-
boolean verifyMerkle =
226-
MerkleProofUtility.verifyMerkle(
227-
block.getTransactionsRoot(),
228-
transactionResponse.getTxProof(),
229-
transactionResponse.getHash(),
230-
new SM3Hash());
231-
Assert.assertTrue(verifyMerkle);
232-
}
295+
public void testTransactionReceipt() throws IOException {
296+
checkTransactionReceipt(transactionReceipt);
233297
}
234298

235-
// @Test
236-
// FIXME: need to fix in new tx
237-
public void testTransactionReceipt() throws IOException {
238-
if (transactionReceipt == null && block.getNumber() == 0) {
299+
@Test
300+
public void testTransactionReceiptV1() throws IOException {
301+
checkTransactionReceipt(transactionReceiptV1);
302+
}
303+
304+
@Test
305+
public void testTransactionReceiptV2() throws IOException {
306+
checkTransactionReceipt(transactionReceiptV2);
307+
}
308+
309+
private void checkTransactionReceipt(TransactionReceipt transactionReceipt) throws IOException {
310+
if (transactionReceipt == null) {
239311
return;
240312
}
313+
block =
314+
client.getBlockByNumber(transactionReceipt.getBlockNumber(), false, false)
315+
.getBlock();
241316
String hash =
242317
transactionReceipt.calculateReceiptHashInNative(client.getCryptoSuite().hashImpl);
243318
Assert.assertEquals(hash, transactionReceipt.getReceiptHash());
244-
245319
if (client.getChainCompatibilityVersion()
246320
.compareTo(EnumNodeVersion.BCOS_3_2_0.toVersionObj())
247321
< 0) {

0 commit comments

Comments
 (0)