Skip to content

Commit

Permalink
<feat>(transaction): add interfaces of new transaction manager.
Browse files Browse the repository at this point in the history
  • Loading branch information
kyonRay committed Nov 10, 2023
1 parent 09478eb commit 249546c
Show file tree
Hide file tree
Showing 25 changed files with 2,099 additions and 51 deletions.
15 changes: 11 additions & 4 deletions .ci/ci_check.sh
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ check_standard_node()
build_node "normal" "${3}"
prepare_environment "${2}"
## run integration test
bash gradlew clean integrationTest --info
bash gradlew clean integrationTest --info ${4}
## clean
clean_node "${1}"
}
Expand Down Expand Up @@ -170,26 +170,33 @@ LOG_INFO "------ download_binary: v3.1.0---------"
download_build_chain "v3.1.0"
download_binary "v3.1.0"
LOG_INFO "------ check_standard_node---------"
check_standard_node
check_standard_node "true" "normal" "-A" "-x test"
rm -rf ./bin

LOG_INFO "------ download_binary: v3.2.0---------"
download_build_chain "v3.2.0"
download_binary "v3.2.0"
LOG_INFO "------ check_standard_node---------"
check_standard_node "true" "sm" "-s -A"
check_standard_node "true" "normal" "-A" "-x test"
rm -rf ./bin

LOG_INFO "------ download_build_chain: v3.3.0---------"
download_binary "v3.3.0"
download_build_chain "v3.3.0"
LOG_INFO "------ check_standard_node---------"
check_standard_node "true" "sm" "-s"
check_standard_node "true" "normal" "" "-x test"
rm -rf ./bin

LOG_INFO "------ download_build_chain: v3.4.0---------"
download_binary "v3.4.0"
download_build_chain "v3.4.0"
LOG_INFO "------ check_standard_node---------"
check_standard_node "true" "normal" "" "-x test"
rm -rf ./bin

LOG_INFO "------ download_build_chain: v3.5.0---------"
download_binary "v3.5.0"
download_build_chain "v3.5.0"
LOG_INFO "------ check_wasm_node---------"
check_wasm_node "false"
LOG_INFO "------ check_standard_node---------"
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/workflow.yml
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ jobs:
run: ./gradlew.bat build
- name: run integration testing
# FIXME: macOS WASM integration testing failed
if: runner.os != 'Windows' && runner.os != 'macOS'
if: runner.os != 'Windows'
run: /bin/bash .ci/ci_check.sh

build-centos:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,24 @@
import java.util.Collections;
import java.util.List;
import java.util.concurrent.CompletableFuture;

import org.apache.commons.lang3.StringUtils;
import org.fisco.bcos.sdk.jni.utilities.tx.TransactionBuilderJniObj;
import org.fisco.bcos.sdk.v3.BcosSDK;
import org.fisco.bcos.sdk.v3.client.Client;
import org.fisco.bcos.sdk.v3.codec.ContractCodec;
import org.fisco.bcos.sdk.v3.crypto.keypair.CryptoKeyPair;
import org.fisco.bcos.sdk.v3.crypto.signature.SignatureResult;
import org.fisco.bcos.sdk.v3.model.ConstantConfig;
import org.fisco.bcos.sdk.v3.model.TransactionReceipt;
import org.fisco.bcos.sdk.v3.model.callback.TransactionCallback;
import org.fisco.bcos.sdk.v3.test.transaction.mock.RemoteSignCallbackMock;
import org.fisco.bcos.sdk.v3.test.transaction.mock.RemoteSignProviderMock;
import org.fisco.bcos.sdk.v3.transaction.manager.AssembleTransactionWithRemoteSignProcessor;
import org.fisco.bcos.sdk.v3.transaction.manager.TransactionProcessorFactory;
import org.fisco.bcos.sdk.v3.transaction.model.dto.CallResponse;
import org.fisco.bcos.sdk.v3.transaction.model.dto.TransactionResponse;
import org.fisco.bcos.sdk.v3.transaction.signer.RemoteSignProviderInterface;
import org.fisco.bcos.sdk.v3.utils.Hex;
import org.junit.Assert;
import org.junit.FixMethodOrder;
import org.junit.Test;
Expand Down Expand Up @@ -60,7 +65,8 @@ public class AssembleTransactionWithRemoteSignProcessorTest {
private RemoteSignProviderInterface remoteSignProviderMock =
new RemoteSignProviderMock(this.client.getCryptoSuite());

public AssembleTransactionWithRemoteSignProcessorTest() {}
public AssembleTransactionWithRemoteSignProcessorTest() {
}

@Test
public void test1HelloWorldSync() throws Exception {
Expand All @@ -77,32 +83,38 @@ public void test1HelloWorldSync() throws Exception {
assembleTransactionWithRemoteSignProcessor
.getContractLoader()
.getABIByContractName("HelloWorld");
// function1: deploy sync
TransactionResponse response =
assembleTransactionWithRemoteSignProcessor.deployByContractLoader(
"HelloWorld", new ArrayList<>());
System.out.println("--- finish deploy with sync ---");
Assert.assertEquals(response.getTransactionReceipt().getStatus(), 0);
Assert.assertEquals(0, response.getReturnCode());
Assert.assertEquals(0, response.getTransactionReceipt().getStatus());
String helloWorldAddress = response.getContractAddress();
Assert.assertTrue(
StringUtils.isNotBlank(response.getContractAddress())
&& !StringUtils.equalsIgnoreCase(
helloWorldAddress,
"0x0000000000000000000000000000000000000000000000000000000000000000"));
String bin = assembleTransactionWithRemoteSignProcessor.getContractLoader().getBinaryByContractName("HelloWorld");

// function2: send transaction `HelloWorld.set("test")` sync
TransactionResponse transactionResponse2 =
assembleTransactionWithRemoteSignProcessor.sendTransactionAndGetResponse(
helloWorldAddress, abi, "set", this.params);
Assert.assertEquals(0, transactionResponse2.getTransactionReceipt().getStatus());
ContractCodec contractCodec = new ContractCodec(client.getCryptoSuite(), client.isWASM());

long transactionData = TransactionBuilderJniObj.createTransactionData(
"group0",
"chain0",
"",
Hex.toHexString(contractCodec.encodeConstructor(abi, bin, params)),
abi,
client.getBlockLimit().longValue());

String rawTxHash = TransactionBuilderJniObj.calcTransactionDataHash(client.getCryptoSuite().cryptoTypeConfig, transactionData);

SignatureResult signatureResult = remoteSignProviderMock.requestForSign(Hex.decode(rawTxHash), this.client.getCryptoSuite().cryptoTypeConfig);

String signedTransaction = TransactionBuilderJniObj.createSignedTransaction(transactionData, signatureResult.toString(), rawTxHash, 0);

CompletableFuture<TransactionReceipt> receiptCompletableFuture = new CompletableFuture<>();
assembleTransactionWithRemoteSignProcessor.sendTransactionAsync(signedTransaction, new TransactionCallback() {
@Override
public void onResponse(TransactionReceipt receipt) {
receiptCompletableFuture.complete(receipt);
}
});

TransactionReceipt transactionReceipt = receiptCompletableFuture.get();

// function3: call, which only support sync mode.
CallResponse callResponse1 =
assembleTransactionWithRemoteSignProcessor.sendCallByContractLoader(
"HelloWorld", helloWorldAddress, "get", new ArrayList<>());
Assert.assertEquals("test", callResponse1.getResults().get(0).getValue());
Assert.assertEquals(transactionReceipt.getStatus(), 0);
Assert.assertFalse(transactionReceipt.getContractAddress().isEmpty());
String abi1 = client.getABI(transactionReceipt.getContractAddress()).getABI();
Assert.assertEquals(abi1, abi);
}

@Test
Expand Down Expand Up @@ -135,8 +147,8 @@ public void test2HelloWorldAsync() throws Exception {
Assert.assertTrue(
StringUtils.isNotBlank(response.getContractAddress())
&& !StringUtils.equalsIgnoreCase(
helloWorldAddress,
"0x0000000000000000000000000000000000000000000000000000000000000000"));
helloWorldAddress,
"0x0000000000000000000000000000000000000000000000000000000000000000"));

// function2: deploy async with callback
long transactionData =
Expand Down
39 changes: 36 additions & 3 deletions src/main/java/org/fisco/bcos/sdk/v3/codec/ContractCodec.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@
import org.fisco.bcos.sdk.v3.codec.wrapper.ContractCodecJsonWrapper;
import org.fisco.bcos.sdk.v3.codec.wrapper.ContractCodecTools;
import org.fisco.bcos.sdk.v3.crypto.CryptoSuite;
import org.fisco.bcos.sdk.v3.crypto.hash.Hash;
import org.fisco.bcos.sdk.v3.crypto.hash.Keccak256;
import org.fisco.bcos.sdk.v3.crypto.hash.SM3Hash;
import org.fisco.bcos.sdk.v3.model.CryptoType;
import org.fisco.bcos.sdk.v3.model.EventLog;
import org.fisco.bcos.sdk.v3.utils.Hex;
import org.fisco.bcos.sdk.v3.utils.Numeric;
Expand All @@ -61,6 +65,7 @@ public class ContractCodec {
private static final Logger logger = LoggerFactory.getLogger(ContractCodec.class);

private final CryptoSuite cryptoSuite;
private final Hash hashImpl;
private final boolean isWasm;
private final ObjectMapper objectMapper = ObjectMapperFactory.getObjectMapper();
private FunctionEncoderInterface functionEncoder = null;
Expand All @@ -69,25 +74,53 @@ public class ContractCodec {
private final ContractCodecJsonWrapper contractCodecJsonWrapper =
new ContractCodecJsonWrapper();

@Deprecated
public ContractCodec(CryptoSuite cryptoSuite, boolean isWasm) {
this.cryptoSuite = cryptoSuite;
this.hashImpl = cryptoSuite.getHashImpl();
this.isWasm = isWasm;
if (isWasm) {
this.functionEncoder =
new org.fisco.bcos.sdk.v3.codec.scale.FunctionEncoder(this.hashImpl);
this.functionReturnDecoder = new FunctionReturnDecoder();
} else {
this.functionEncoder =
new org.fisco.bcos.sdk.v3.codec.abi.FunctionEncoder(this.hashImpl);
this.functionReturnDecoder =
new org.fisco.bcos.sdk.v3.codec.abi.FunctionReturnDecoder();
}
this.abiDefinitionFactory = new ABIDefinitionFactory(hashImpl);
}

public ContractCodec(Hash hashImpl, boolean isWasm) {
this.hashImpl = hashImpl;
this.isWasm = isWasm;
if (isWasm) {
this.functionEncoder =
new org.fisco.bcos.sdk.v3.codec.scale.FunctionEncoder(cryptoSuite);
new org.fisco.bcos.sdk.v3.codec.scale.FunctionEncoder(this.hashImpl);
this.functionReturnDecoder = new FunctionReturnDecoder();
} else {
this.functionEncoder = new org.fisco.bcos.sdk.v3.codec.abi.FunctionEncoder(cryptoSuite);
this.functionEncoder =
new org.fisco.bcos.sdk.v3.codec.abi.FunctionEncoder(this.hashImpl);
this.functionReturnDecoder =
new org.fisco.bcos.sdk.v3.codec.abi.FunctionReturnDecoder();
}
this.abiDefinitionFactory = new ABIDefinitionFactory(cryptoSuite);
this.abiDefinitionFactory = new ABIDefinitionFactory(this.hashImpl);
// for compatibility
if (this.hashImpl instanceof SM3Hash) {
this.cryptoSuite = new CryptoSuite(CryptoType.SM_TYPE);
} else if (this.hashImpl instanceof Keccak256) {
this.cryptoSuite = new CryptoSuite(CryptoType.ECDSA_TYPE);
} else {
this.cryptoSuite = null;
}
}

public boolean isWasm() {
return isWasm;
}

@Deprecated
public CryptoSuite getCryptoSuite() {
return this.cryptoSuite;
}
Expand Down
30 changes: 30 additions & 0 deletions src/main/java/org/fisco/bcos/sdk/v3/codec/Encoder.java
Original file line number Diff line number Diff line change
@@ -1,21 +1,51 @@
package org.fisco.bcos.sdk.v3.codec;

import org.fisco.bcos.sdk.v3.crypto.CryptoSuite;
import org.fisco.bcos.sdk.v3.crypto.hash.Hash;
import org.fisco.bcos.sdk.v3.crypto.hash.Keccak256;
import org.fisco.bcos.sdk.v3.crypto.hash.SM3Hash;
import org.fisco.bcos.sdk.v3.model.CryptoType;

public class Encoder {
private CryptoSuite cryptoSuite;

private Hash hashImpl;

@Deprecated
public Encoder(CryptoSuite cryptoSuite) {
this.cryptoSuite = cryptoSuite;
this.hashImpl = cryptoSuite.getHashImpl();
}

public Encoder(Hash hash) {
// for compatibility
if (hashImpl instanceof SM3Hash) {
this.cryptoSuite = new CryptoSuite(CryptoType.SM_TYPE);
}
if (hashImpl instanceof Keccak256) {
this.cryptoSuite = new CryptoSuite(CryptoType.ECDSA_TYPE);
}
this.hashImpl = hash;
}

/** @return the cryptoSuite */
@Deprecated
public CryptoSuite getCryptoSuite() {
return this.cryptoSuite;
}

/** @param cryptoSuite the cryptoSuite to set */
@Deprecated
public void setCryptoSuite(CryptoSuite cryptoSuite) {
this.cryptoSuite = cryptoSuite;
this.hashImpl = cryptoSuite.getHashImpl();
}

public Hash getHashImpl() {
return hashImpl;
}

public void setHashImpl(Hash hashImpl) {
this.hashImpl = hashImpl;
}
}
12 changes: 10 additions & 2 deletions src/main/java/org/fisco/bcos/sdk/v3/codec/EventEncoder.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,25 @@
import org.fisco.bcos.sdk.v3.codec.datatypes.Type;
import org.fisco.bcos.sdk.v3.codec.datatypes.TypeReference;
import org.fisco.bcos.sdk.v3.crypto.CryptoSuite;
import org.fisco.bcos.sdk.v3.crypto.hash.Hash;
import org.fisco.bcos.sdk.v3.utils.Numeric;

/**
* Ethereum filter encoding. Further limited details are available <a
* href="https://github.com/ethereum/wiki/wiki/Ethereum-Contract-ABI#events">here</a>.
*/
public class EventEncoder {
private final CryptoSuite cryptoSuite;
@Deprecated private CryptoSuite cryptoSuite;
private final Hash hashImpl;

@Deprecated
public EventEncoder(CryptoSuite cryptoSuite) {
this.cryptoSuite = cryptoSuite;
this.hashImpl = cryptoSuite.getHashImpl();
}

public EventEncoder(Hash hashImpl) {
this.hashImpl = hashImpl;
}

public String encode(Event event) {
Expand All @@ -39,7 +47,7 @@ public <T extends Type> String buildMethodSignature(

public String buildEventSignature(String methodSignature) {
byte[] input = methodSignature.getBytes();
byte[] hash = this.cryptoSuite.hash(input);
byte[] hash = this.hashImpl.hash(input);
return Numeric.toHexString(hash);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,18 @@
import org.fisco.bcos.sdk.v3.codec.datatypes.Function;
import org.fisco.bcos.sdk.v3.codec.datatypes.Type;
import org.fisco.bcos.sdk.v3.crypto.CryptoSuite;
import org.fisco.bcos.sdk.v3.crypto.hash.Hash;

public abstract class FunctionEncoderInterface extends Encoder {
@Deprecated
public FunctionEncoderInterface(CryptoSuite cryptoSuite) {
super(cryptoSuite);
}

public FunctionEncoderInterface(Hash hash) {
super(hash);
}

public abstract byte[] encode(Function function);

public static String buildMethodSignature(String methodName, List<Type> parameters) {
Expand All @@ -27,7 +33,7 @@ public static String buildMethodSignature(String methodName, List<Type> paramete

public byte[] buildMethodId(String methodSignature) {
byte[] input = methodSignature.getBytes();
byte[] hash = this.getCryptoSuite().hash(input);
byte[] hash = this.getHashImpl().hash(input);
return Arrays.copyOfRange(hash, 0, 4);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,22 @@
import org.fisco.bcos.sdk.v3.codec.datatypes.Type;
import org.fisco.bcos.sdk.v3.codec.datatypes.Uint;
import org.fisco.bcos.sdk.v3.crypto.CryptoSuite;
import org.fisco.bcos.sdk.v3.crypto.hash.Hash;

/**
* Ethereum Contract Application Binary Interface (ABI) encoding for functions. Further details are
* available <a href="https://github.com/ethereum/wiki/wiki/Ethereum-Contract-ABI">here</a>.
*/
public class FunctionEncoder extends FunctionEncoderInterface {
@Deprecated
public FunctionEncoder(CryptoSuite cryptoSuite) {
super(cryptoSuite);
}

public FunctionEncoder(Hash hash) {
super(hash);
}

@Override
public byte[] encode(Function function) {
List<Type> parameters = function.getInputParameters();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,18 @@
import org.fisco.bcos.sdk.v3.codec.datatypes.Function;
import org.fisco.bcos.sdk.v3.codec.datatypes.Type;
import org.fisco.bcos.sdk.v3.crypto.CryptoSuite;
import org.fisco.bcos.sdk.v3.crypto.hash.Hash;

public class FunctionEncoder extends FunctionEncoderInterface {
@Deprecated
public FunctionEncoder(CryptoSuite cryptoSuite) {
super(cryptoSuite);
}

public FunctionEncoder(Hash hash) {
super(hash);
}

@Override
public byte[] encode(Function function) {
List<Type> parameters = function.getInputParameters();
Expand Down
Loading

0 comments on commit 249546c

Please sign in to comment.