Skip to content

Commit

Permalink
<fix>(model,precompiled): add tx, receipt and block java native calcu…
Browse files Browse the repository at this point in the history
…late hash methods, fix feature check. (#857)

* <fix>(model,precompiled): add tx, receipt and block java native calculate hash methods, fix feature check.

* 更新 build.gradle
  • Loading branch information
kyonRay authored Dec 28, 2023
1 parent c85c29b commit 985e86b
Show file tree
Hide file tree
Showing 5 changed files with 394 additions and 79 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.fisco.bcos.sdk.v3.test.transaction.decoder;

import org.fisco.bcos.sdk.jni.utilities.tx.TransactionVersion;
import org.fisco.bcos.sdk.v3.BcosSDK;
import org.fisco.bcos.sdk.v3.client.Client;
import org.fisco.bcos.sdk.v3.client.protocol.model.JsonTransactionResponse;
Expand All @@ -11,6 +12,7 @@
import org.fisco.bcos.sdk.v3.model.ConstantConfig;
import org.fisco.bcos.sdk.v3.model.CryptoType;
import org.fisco.bcos.sdk.v3.model.EnumNodeVersion;
import org.fisco.bcos.sdk.v3.model.TransactionReceipt;
import org.fisco.bcos.sdk.v3.utils.Hex;
import org.fisco.bcos.sdk.v3.utils.MerkleProofUtility;
import org.fisco.bcos.sdk.v3.utils.ObjectMapperFactory;
Expand All @@ -30,15 +32,24 @@ public class HashCalculatorTest {
private final BcosBlock.Block block;
private JsonTransactionResponse transactionResponse = null;

private TransactionReceipt transactionReceipt = null;

public HashCalculatorTest() {
BcosSDK sdk = BcosSDK.build(CONFIG_FILE);
client = sdk.getClient("group0");
BlockNumber blockNumber = client.getBlockNumber();
block = client.getBlockByNumber(blockNumber.getBlockNumber(), false, false).getBlock();
if (!block.getTransactions().isEmpty()) {
BcosBlock.TransactionObject transactionObject = (BcosBlock.TransactionObject) block.getTransactions().get(0);
BcosBlock.TransactionObject transactionObject =
(BcosBlock.TransactionObject) block.getTransactions().get(0);
JsonTransactionResponse transactionResponse1 = transactionObject.get();
transactionResponse = client.getTransaction(transactionResponse1.getHash(), true).getTransaction().get();
transactionResponse =
client.getTransaction(transactionResponse1.getHash(), true)
.getTransaction()
.get();
transactionReceipt =
client.getTransactionReceipt(transactionResponse1.getHash(), true)
.getTransactionReceipt();
}
}

Expand All @@ -57,19 +68,32 @@ public void testTxHashCalculate() throws IOException {

ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
// version
byteArrayOutputStream.write(toBytesPadded(BigInteger.ZERO, 4));
byteArrayOutputStream.write(
toBytesPadded(BigInteger.valueOf(transactionResponse.getVersion()), 4));
// chainId
byteArrayOutputStream.write(chainId.getBytes());
// groupId
byteArrayOutputStream.write(groupId.getBytes());
// blockLimit
byteArrayOutputStream.write(toBytesPadded(BigInteger.valueOf(blockLimit), 8));
// nonce
String version = client.getGroupInfo().getResult().getNodeList().get(0).getIniConfig().getBinaryInfo().getVersion();
String version =
client.getGroupInfo()
.getResult()
.getNodeList()
.get(0)
.getIniConfig()
.getBinaryInfo()
.getVersion();
System.out.println("node bin version: " + version);
if (EnumNodeVersion.getClassVersion(version).compareTo(EnumNodeVersion.BCOS_3_3_0.toVersionObj()) >= 0) {
if (EnumNodeVersion.getClassVersion(version)
.compareTo(EnumNodeVersion.BCOS_3_3_0.toVersionObj())
>= 0) {
System.out.println("use hex decode nonce");
byteArrayOutputStream.write(Hex.decode(nonce));
String calculateTxHashInNative =
transactionResponse.calculateTxHashInNative(client.getCryptoSuite().hashImpl);
Assert.assertEquals(calculateTxHashInNative, transactionResponse.getHash());
} else {
System.out.println("use string nonce");
byteArrayOutputStream.write(nonce.getBytes());
Expand All @@ -81,6 +105,15 @@ public void testTxHashCalculate() throws IOException {
// abi
byteArrayOutputStream.write(abi.getBytes());

if (transactionResponse.getVersion() == TransactionVersion.V1.getValue()) {
byteArrayOutputStream.write(transactionResponse.getValue().getBytes());
byteArrayOutputStream.write(transactionResponse.getGasPrice().getBytes());
byteArrayOutputStream.write(
toBytesPadded(BigInteger.valueOf(transactionResponse.getGasLimit()), 8));
byteArrayOutputStream.write(transactionResponse.getMaxFeePerGas().getBytes());
byteArrayOutputStream.write(transactionResponse.getMaxPriorityFeePerGas().getBytes());
}

String hash = "";
if (client.getCryptoSuite().cryptoTypeConfig == CryptoType.ECDSA_TYPE) {
hash = Keccak256.calculateHash(byteArrayOutputStream.toByteArray());
Expand All @@ -90,7 +123,9 @@ public void testTxHashCalculate() throws IOException {
}
hash = Hex.addPrefix(hash);
Assert.assertEquals(hash, transactionResponse.getHash());
if (EnumNodeVersion.getClassVersion(version).compareTo(EnumNodeVersion.BCOS_3_3_0.toVersionObj()) < 0) {
if (EnumNodeVersion.getClassVersion(version)
.compareTo(EnumNodeVersion.BCOS_3_3_0.toVersionObj())
< 0) {
String jniHash = transactionResponse.calculateHash(client.getCryptoSuite());
Assert.assertEquals(jniHash, transactionResponse.getHash());
}
Expand All @@ -105,7 +140,8 @@ public void testBlock() throws IOException {
byteArrayOutputStream.write(toBytesPadded(BigInteger.valueOf(block.getVersion()), 4));
// parentInfo
for (BcosBlockHeader.ParentInfo parentInfo : block.getParentInfo()) {
byteArrayOutputStream.write(toBytesPadded(BigInteger.valueOf(parentInfo.getBlockNumber()), 8));
byteArrayOutputStream.write(
toBytesPadded(BigInteger.valueOf(parentInfo.getBlockNumber()), 8));
byteArrayOutputStream.write(Hex.decode(parentInfo.getBlockHash()));
}
// tx root
Expand Down Expand Up @@ -142,22 +178,72 @@ public void testBlock() throws IOException {
calculateHash = SM3Hash.calculateHash(byteArrayOutputStream.toByteArray());
}
Assert.assertEquals(Hex.addPrefix(calculateHash), hash);

Assert.assertEquals(
block.getHash(), block.calculateBlockHeaderHash(client.getCryptoSuite().hashImpl));
}

@Test
public void testTxProof() {
if (transactionResponse == null && block.getNumber() == 0) {
return;
}
if (client.getChainCompatibilityVersion().compareTo(EnumNodeVersion.BCOS_3_2_0.toVersionObj()) < 0) {
if (client.getChainCompatibilityVersion()
.compareTo(EnumNodeVersion.BCOS_3_2_0.toVersionObj())
< 0) {
return;
}
if (client.getCryptoSuite().cryptoTypeConfig == CryptoType.ECDSA_TYPE) {
boolean verifyMerkle =
MerkleProofUtility.verifyMerkle(
block.getTransactionsRoot(),
transactionResponse.getTxProof(),
transactionResponse.getHash(),
new Keccak256());
Assert.assertTrue(verifyMerkle);
}
if (client.getCryptoSuite().cryptoTypeConfig == CryptoType.SM_TYPE) {
boolean verifyMerkle =
MerkleProofUtility.verifyMerkle(
block.getTransactionsRoot(),
transactionResponse.getTxProof(),
transactionResponse.getHash(),
new SM3Hash());
Assert.assertTrue(verifyMerkle);
}
}

// @Test
// FIXME: need to fix in new tx
public void testTransactionReceipt() throws IOException {
if (transactionReceipt == null && block.getNumber() == 0) {
return;
}
String hash =
transactionReceipt.calculateReceiptHashInNative(client.getCryptoSuite().hashImpl);
Assert.assertEquals(hash, transactionReceipt.getReceiptHash());

if (client.getChainCompatibilityVersion()
.compareTo(EnumNodeVersion.BCOS_3_2_0.toVersionObj())
< 0) {
return;
}
if (client.getCryptoSuite().cryptoTypeConfig == CryptoType.ECDSA_TYPE) {
boolean verifyMerkle = MerkleProofUtility.verifyMerkle(block.getTransactionsRoot(), transactionResponse.getTxProof(), transactionResponse.getHash(), new Keccak256());
boolean verifyMerkle =
MerkleProofUtility.verifyMerkle(
block.getReceiptsRoot(),
transactionReceipt.getTxReceiptProof(),
transactionReceipt.getReceiptHash(),
new Keccak256());
Assert.assertTrue(verifyMerkle);
}
if (client.getCryptoSuite().cryptoTypeConfig == CryptoType.SM_TYPE) {
boolean verifyMerkle = MerkleProofUtility.verifyMerkle(block.getTransactionsRoot(), transactionResponse.getTxProof(), transactionResponse.getHash(), new SM3Hash());
boolean verifyMerkle =
MerkleProofUtility.verifyMerkle(
block.getReceiptsRoot(),
transactionReceipt.getTxReceiptProof(),
transactionReceipt.getReceiptHash(),
new SM3Hash());
Assert.assertTrue(verifyMerkle);
}
}
Expand Down
Loading

0 comments on commit 985e86b

Please sign in to comment.