Skip to content

Commit

Permalink
<fix>(transaction): fix JsonTransactionResponse decode bug.
Browse files Browse the repository at this point in the history
  • Loading branch information
kyonRay committed Jan 31, 2024
1 parent dd8a41a commit 940ec1d
Show file tree
Hide file tree
Showing 5 changed files with 206 additions and 58 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@
import org.fisco.bcos.sdk.jni.utilities.tx.Transaction;
import org.fisco.bcos.sdk.jni.utilities.tx.TransactionBuilderJniObj;
import org.fisco.bcos.sdk.jni.utilities.tx.TransactionBuilderV2JniObj;
import org.fisco.bcos.sdk.jni.utilities.tx.TransactionData;
import org.fisco.bcos.sdk.jni.utilities.tx.TransactionDataV2;
import org.fisco.bcos.sdk.jni.utilities.tx.TransactionStructBuilderJniObj;
import org.fisco.bcos.sdk.jni.utilities.tx.TransactionStructBuilderV2JniObj;
import org.fisco.bcos.sdk.jni.utilities.tx.TransactionV2;
import org.fisco.bcos.sdk.jni.utilities.tx.TransactionVersion;
import org.fisco.bcos.sdk.v3.client.exceptions.ClientException;
import org.fisco.bcos.sdk.v3.crypto.CryptoSuite;
Expand Down Expand Up @@ -383,52 +383,60 @@ public byte[] encodeTransactionData() throws IOException {

public static JsonTransactionResponse decodeTransactionV1(String hexString)
throws JniException {
TransactionV2 transactionV2 =
TransactionStructBuilderV2JniObj.decodeTransactionStructV2(hexString);
Transaction transactionV2 =
TransactionStructBuilderJniObj.decodeTransactionStructV2(hexString);
TransactionData transactionData = transactionV2.getTransactionData();
JsonTransactionResponse jsonTransactionResponse = new JsonTransactionResponse();
jsonTransactionResponse.setVersion(transactionV2.getTransactionData().getVersion());
jsonTransactionResponse.setHash(Hex.toHexString(transactionV2.getDataHash().getBuffer()));
jsonTransactionResponse.setNonce(transactionV2.getTransactionData().getNonce());
jsonTransactionResponse.setBlockLimit(transactionV2.getTransactionData().getBlockLimit());
jsonTransactionResponse.setTo(transactionV2.getTransactionData().getTo());
// jsonTransactionResponse.setFrom(Hex.toHexString(transactionV2.getSender().getBuffer()));
jsonTransactionResponse.setAbi(transactionV2.getTransactionData().getAbi());
jsonTransactionResponse.setVersion(transactionData.getVersion());
jsonTransactionResponse.setHash(
Hex.toHexStringWithPrefixNullable(transactionV2.getDataHash(), ""));
jsonTransactionResponse.setNonce(transactionData.getNonce());
jsonTransactionResponse.setBlockLimit(transactionData.getBlockLimit());
jsonTransactionResponse.setTo(transactionData.getTo());
jsonTransactionResponse.setFrom(
Hex.toHexStringWithPrefixNullable(transactionV2.getSender(), ""));
jsonTransactionResponse.setAbi(transactionData.getAbi());
jsonTransactionResponse.setInput(
Hex.toHexString(transactionV2.getTransactionData().getInput().getBuffer()));
jsonTransactionResponse.setChainID(transactionV2.getTransactionData().getChainId());
jsonTransactionResponse.setGroupID(transactionV2.getTransactionData().getGroupId());
Hex.toHexStringWithPrefixNullable(transactionData.getInput(), ""));
jsonTransactionResponse.setChainID(transactionData.getChainId());
jsonTransactionResponse.setGroupID(transactionData.getGroupId());
jsonTransactionResponse.setExtraData(transactionV2.getExtraData());
jsonTransactionResponse.setSignature(
Hex.toHexString(transactionV2.getSignature().getBuffer()));
Hex.toHexStringWithPrefixNullable(transactionV2.getSignature(), ""));
jsonTransactionResponse.setImportTime(transactionV2.getImportTime());

jsonTransactionResponse.setValue(transactionV2.getTransactionData().getValue());
jsonTransactionResponse.setGasPrice(transactionV2.getTransactionData().getGasPrice());
jsonTransactionResponse.setGasLimit(transactionV2.getTransactionData().getGasLimit());
jsonTransactionResponse.setMaxFeePerGas(
transactionV2.getTransactionData().getMaxFeePerGas());
jsonTransactionResponse.setMaxPriorityFeePerGas(
transactionV2.getTransactionData().getMaxPriorityFeePerGas());
if (transactionData instanceof TransactionDataV2
&& transactionData.getVersion() == TransactionVersion.V1.getValue()) {
TransactionDataV2 transactionDataV2 = (TransactionDataV2) transactionData;
jsonTransactionResponse.setValue(transactionDataV2.getValue());
jsonTransactionResponse.setGasPrice(transactionDataV2.getGasPrice());
jsonTransactionResponse.setGasLimit(transactionDataV2.getGasLimit());
jsonTransactionResponse.setMaxFeePerGas(transactionDataV2.getMaxFeePerGas());
jsonTransactionResponse.setMaxPriorityFeePerGas(
transactionDataV2.getMaxPriorityFeePerGas());
}
return jsonTransactionResponse;
}

public static JsonTransactionResponse decodeTransaction(String hexString) throws JniException {
Transaction transaction = TransactionStructBuilderJniObj.decodeTransactionStruct(hexString);
JsonTransactionResponse jsonTransactionResponse = new JsonTransactionResponse();
jsonTransactionResponse.setVersion(transaction.getTransactionData().getVersion());
jsonTransactionResponse.setHash(Hex.toHexString(transaction.getDataHash().getBuffer()));
jsonTransactionResponse.setHash(
Hex.toHexStringWithPrefixNullable(transaction.getDataHash(), ""));
jsonTransactionResponse.setNonce(transaction.getTransactionData().getNonce());
jsonTransactionResponse.setBlockLimit(transaction.getTransactionData().getBlockLimit());
jsonTransactionResponse.setTo(transaction.getTransactionData().getTo());
// jsonTransactionResponse.setFrom(Hex.toHexString(transactionV2.getSender().getBuffer()));
jsonTransactionResponse.setFrom(
Hex.toHexStringWithPrefixNullable(transaction.getSender(), ""));
jsonTransactionResponse.setAbi(transaction.getTransactionData().getAbi());
jsonTransactionResponse.setInput(
Hex.toHexString(transaction.getTransactionData().getInput().getBuffer()));
Hex.toHexStringWithPrefixNullable(transaction.getTransactionData().getInput(), ""));
jsonTransactionResponse.setChainID(transaction.getTransactionData().getChainId());
jsonTransactionResponse.setGroupID(transaction.getTransactionData().getGroupId());
jsonTransactionResponse.setExtraData(transaction.getExtraData());
jsonTransactionResponse.setSignature(
Hex.toHexString(transaction.getSignature().getBuffer()));
Hex.toHexStringWithPrefixNullable(transaction.getSignature(), ""));
jsonTransactionResponse.setImportTime(transaction.getImportTime());

return jsonTransactionResponse;
Expand Down
7 changes: 6 additions & 1 deletion src/main/java/org/fisco/bcos/sdk/v3/config/Config.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,16 @@
import java.io.File;
import org.fisco.bcos.sdk.v3.config.exceptions.ConfigException;
import org.fisco.bcos.sdk.v3.config.model.ConfigProperty;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
* Config is to load config file and verify.
*
* @author Maggie
*/
public class Config {
private static final Logger logger = LoggerFactory.getLogger(Config.class);
/**
* @param tomlConfigFile the toml configuration file path
* @return ConfigOption the configuration object
Expand All @@ -38,8 +41,10 @@ public static ConfigOption load(String tomlConfigFile) throws ConfigException {
ConfigProperty configProperty = new Toml().read(configFile).to(ConfigProperty.class);
return new ConfigOption(configProperty);
} catch (Exception e) {
logger.error(
"parse Config {} failed, error info: {}", tomlConfigFile, e.getMessage(), e);
throw new ConfigException(
"parse Config " + tomlConfigFile + " failed, error info: " + e.getMessage(), e);
"parse Config " + tomlConfigFile + " failed, please check the config file.");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,12 @@ public ProxySignTransactionManager(Client client) {
asyncTxSigner = new TransactionJniSignerService(client.getCryptoSuite().getCryptoKeyPair());
}

public ProxySignTransactionManager(
Client client, AsyncTransactionSignercInterface asyncTxSigner) {
this(client);
this.asyncTxSigner = asyncTxSigner;
}

@Override
public ContractGasProvider getGasProvider() {
return contractGasProvider;
Expand All @@ -75,12 +81,6 @@ public void setNonceProvider(NonceAndBlockLimitProvider nonceProvider) {
this.nonceProvider = nonceProvider;
}

public ProxySignTransactionManager(
Client client, AsyncTransactionSignercInterface asyncTxSigner) {
super(client);
this.asyncTxSigner = asyncTxSigner;
}

public void setAsyncTransactionSigner(AsyncTransactionSignercInterface asyncTxSigner) {
this.asyncTxSigner = asyncTxSigner;
}
Expand Down
11 changes: 11 additions & 0 deletions src/main/java/org/fisco/bcos/sdk/v3/utils/Hex.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,17 @@ public static String toHexStringWithPrefix(byte[] data) {
return addPrefix(toHexString(data, 0, data.length));
}

public static String toHexStringWithPrefixNullable(byte[] data, String defaultValue) {
return addPrefix(toHexStringMaybeNullData(data, defaultValue));
}

public static String toHexStringMaybeNullData(byte[] data, String defaultValue) {
if (Objects.isNull(data)) {
return defaultValue;
}
return toHexString(data, 0, data.length);
}

public static String toHexString(byte[] data) {
return toHexString(data, 0, data.length);
}
Expand Down
Loading

0 comments on commit 940ec1d

Please sign in to comment.