From e5ce7344c0e06d5e78feb29de325a86ed6024913 Mon Sep 17 00:00:00 2001 From: Pana Date: Tue, 18 Jun 2024 15:15:28 +0800 Subject: [PATCH 1/3] support conflux-rust 2.4 rpc changes --- src/main/java/conflux/web3j/Cfx.java | 8 ++++ src/main/java/conflux/web3j/Web3j.java | 29 ++++++++++++ src/main/java/conflux/web3j/request/Call.java | 39 +++++++++++++++ .../conflux/web3j/response/BlockHeader.java | 9 ++++ .../conflux/web3j/response/FeeHistory.java | 47 +++++++++++++++++++ .../conflux/web3j/response/ParamsOfVote.java | 46 ++++++++++++++++++ .../java/conflux/web3j/response/Receipt.java | 30 ++++++++++++ .../conflux/web3j/response/Transaction.java | 47 +++++++++++++++++++ .../conflux/web3j/types/AccessListEntry.java | 24 ++++++++++ 9 files changed, 279 insertions(+) create mode 100644 src/main/java/conflux/web3j/response/FeeHistory.java create mode 100644 src/main/java/conflux/web3j/response/ParamsOfVote.java create mode 100644 src/main/java/conflux/web3j/types/AccessListEntry.java diff --git a/src/main/java/conflux/web3j/Cfx.java b/src/main/java/conflux/web3j/Cfx.java index bb91d84..5f42ea8 100644 --- a/src/main/java/conflux/web3j/Cfx.java +++ b/src/main/java/conflux/web3j/Cfx.java @@ -47,6 +47,10 @@ static Cfx create(Web3jService service, int retry, long intervalMillis) { BigInteger getChainId(); Request getGasPrice(); + + Request getMaxPriorityFeePerGas(); + + Request getFeeBurnt(); Request getEpochNumber(Epoch... epoch); @@ -128,6 +132,10 @@ static Cfx create(Web3jService service, int retry, long intervalMillis) { Request getPoSRewardByEpoch(Address address, Epoch epoch); + Request getParamsFromVote(Epoch... epoch); + + Request getFeeHistory(int count, Epoch epoch, List percentiles); + & HasValue> Request getCustomizedRequest(Class responseType, String method, Object... params); default Receipt waitForReceipt(String txHash) throws InterruptedException { diff --git a/src/main/java/conflux/web3j/Web3j.java b/src/main/java/conflux/web3j/Web3j.java index df7395f..434dda6 100644 --- a/src/main/java/conflux/web3j/Web3j.java +++ b/src/main/java/conflux/web3j/Web3j.java @@ -72,6 +72,24 @@ public Request getGasPrice() { .withRetry(this.retry, this.intervalMillis); } + @Override + public Request getMaxPriorityFeePerGas() { + return new Request<>(this.service, "cfx_maxPriorityFeePerGas", BigIntResponse.class) + .withRetry(this.retry, this.intervalMillis); + } + + @Override + public Request getFeeBurnt() { + return new Request<>(this.service, "cfx_getFeeBurnt", BigIntResponse.class) + .withRetry(this.retry, this.intervalMillis); + } + + @Override + public Request getFeeHistory(int count, Epoch epoch, List percentiles) { + return new Request<>(this.service, "cfx_feeHistory", FeeHistory.Response.class, count, epoch.getValue(), percentiles) + .withRetry(this.retry, this.intervalMillis); + } + @Override public Request getEpochNumber(Epoch... epoch) { if (epoch.length == 0) { @@ -83,6 +101,17 @@ public Request getEpochNumber(Epoch... epoch) { } } + @Override + public Request getParamsFromVote(Epoch... epoch) { + if (epoch.length == 0) { + return new Request<>(this.service, "cfx_getParamsFromVote", ParamsOfVote.Response.class) + .withRetry(this.retry, this.intervalMillis); + } else { + return new Request<>(this.service, "cfx_getParamsFromVote", ParamsOfVote.Response.class, epoch[0].getValue()) + .withRetry(this.retry, this.intervalMillis); + } + } + @Override public Request getBalance(Address address, Epoch... epoch) { if (epoch.length == 0) { diff --git a/src/main/java/conflux/web3j/request/Call.java b/src/main/java/conflux/web3j/request/Call.java index 290a1f8..9811acd 100644 --- a/src/main/java/conflux/web3j/request/Call.java +++ b/src/main/java/conflux/web3j/request/Call.java @@ -1,8 +1,11 @@ package conflux.web3j.request; import java.math.BigInteger; + +import conflux.web3j.types.AccessListEntry; import conflux.web3j.types.Address; import org.web3j.utils.Numeric; +import java.util.List; public class Call { private Address from; @@ -13,6 +16,10 @@ public class Call { private String data; private BigInteger nonce; private BigInteger storageLimit; + private BigInteger type; + private BigInteger maxFeePerGas; + private BigInteger maxPriorityFeePerGas; + private List accessList; public Address getFrom() { return from; @@ -33,6 +40,38 @@ public void setTo(Address to) { public String getGasPrice() { return this.gasPrice == null ? null : Numeric.encodeQuantity(this.gasPrice); } + + public String getMaxFeePerGas() { + return this.maxFeePerGas == null ? null : Numeric.encodeQuantity(this.maxFeePerGas); + } + + public void setMaxFeePerGas(BigInteger maxFeePerGas) { + this.maxFeePerGas = maxFeePerGas; + } + + public String getMaxPriorityFeePerGas() { + return this.maxPriorityFeePerGas == null ? null : Numeric.encodeQuantity(this.maxPriorityFeePerGas); + } + + public void setMaxPriorityFeePerGas(BigInteger maxPriorityFeePerGas) { + this.maxPriorityFeePerGas = maxPriorityFeePerGas; + } + + public void setType(BigInteger type) { + this.type = type; + } + + public String getType() { + return this.type == null ? null : Numeric.encodeQuantity(this.type); + } + + public List getAccessList() { + return accessList; + } + + public void setAccessList(List accessList) { + this.accessList = accessList; + } public void setGasPrice(BigInteger gasPrice) { this.gasPrice = gasPrice; diff --git a/src/main/java/conflux/web3j/response/BlockHeader.java b/src/main/java/conflux/web3j/response/BlockHeader.java index 8af3b43..ab7dd65 100644 --- a/src/main/java/conflux/web3j/response/BlockHeader.java +++ b/src/main/java/conflux/web3j/response/BlockHeader.java @@ -27,6 +27,15 @@ public class BlockHeader { private String size; private String posReference; private List custom; + private String baseFeePerGas; + + public BigInteger getBaseFeePerGas() { + return Numeric.decodeQuantity(this.baseFeePerGas); + } + + public void setBaseFeePerGas(String baseFeePerGas) { + this.baseFeePerGas = baseFeePerGas; + } public String getHash() { return hash; diff --git a/src/main/java/conflux/web3j/response/FeeHistory.java b/src/main/java/conflux/web3j/response/FeeHistory.java new file mode 100644 index 0000000..bf6e4bf --- /dev/null +++ b/src/main/java/conflux/web3j/response/FeeHistory.java @@ -0,0 +1,47 @@ +package conflux.web3j.response; +import org.web3j.utils.Numeric; + +import java.math.BigInteger; +import java.util.List; +import java.util.stream.Collectors; + +public class FeeHistory { + public static class Response extends CfxResponse {} + + private String oldestEpoch; + private List baseFeePerGas; + private List gasUsedRatio; + private List reward; + + public BigInteger getOldestEpoch() { + return Numeric.decodeQuantity(oldestEpoch); + } + + public void setOldestEpoch(String oldestEpoch) { + this.oldestEpoch = oldestEpoch; + } + + public List getBaseFeePerGas() { + return baseFeePerGas.stream().map(Numeric::decodeQuantity).collect(Collectors.toList()); + } + + public void setBaseFeePerGas(List baseFeePerGas) { + this.baseFeePerGas = baseFeePerGas; + } + + public List getGasUsedRatio() { + return gasUsedRatio; + } + + public void setGasUsedRatio(List gasUsedRatio) { + this.gasUsedRatio = gasUsedRatio; + } + + public List getReward() { + return reward.stream().map(Numeric::decodeQuantity).collect(Collectors.toList()); + } + + public void setReward(List reward) { + this.reward = reward; + } +} diff --git a/src/main/java/conflux/web3j/response/ParamsOfVote.java b/src/main/java/conflux/web3j/response/ParamsOfVote.java new file mode 100644 index 0000000..b25de4c --- /dev/null +++ b/src/main/java/conflux/web3j/response/ParamsOfVote.java @@ -0,0 +1,46 @@ +package conflux.web3j.response; + +import org.web3j.utils.Numeric; + +import java.math.BigInteger; + +public class ParamsOfVote { + public static class Response extends CfxResponse {} + + private String baseFeeShareProp; + private String interestRate; + private String powBaseReward; + private String storagePointProp; + + public BigInteger getBaseFeeShareProp() { + return Numeric.decodeQuantity(baseFeeShareProp); + } + + public void setBaseFeeShareProp(String baseFeeShareProp) { + this.baseFeeShareProp = baseFeeShareProp; + } + + public BigInteger getInterestRate() { + return Numeric.decodeQuantity(interestRate); + } + + public void setInterestRate(String interestRate) { + this.interestRate = interestRate; + } + + public BigInteger getPowBaseReward() { + return Numeric.decodeQuantity(powBaseReward); + } + + public void setPowBaseReward(String powBaseReward) { + this.powBaseReward = powBaseReward; + } + + public BigInteger getStoragePointProp() { + return Numeric.decodeQuantity(storagePointProp); + } + + public void setStoragePointProp(String storagePointProp) { + this.storagePointProp = storagePointProp; + } +} diff --git a/src/main/java/conflux/web3j/response/Receipt.java b/src/main/java/conflux/web3j/response/Receipt.java index 22bca46..f4be1d7 100644 --- a/src/main/java/conflux/web3j/response/Receipt.java +++ b/src/main/java/conflux/web3j/response/Receipt.java @@ -30,7 +30,33 @@ public static class ListResponse extends CfxNullableResponse> private String storageCollateralized; private List storageReleased; private String gasFee; + private String type; + private String burntGasFee; + private String effectiveGasPrice; + public BigInteger getBurntGasFee() { + return Numeric.decodeQuantity(burntGasFee); + } + + public void setBurntGasFee(String burntGasFee) { + this.burntGasFee = burntGasFee; + } + + public BigInteger getEffectiveGasPrice() { + return Numeric.decodeQuantity(effectiveGasPrice); + } + + public void setEffectiveGasPrice(String effectiveGasPrice) { + this.effectiveGasPrice = effectiveGasPrice; + } + + public int getType() { + return Numeric.decodeQuantity(type).intValue(); + } + + public void setType(String type) { + this.type = type; + } public String getTransactionHash() { return transactionHash; @@ -177,4 +203,8 @@ public void setStorageReleased(List change) { public BigInteger getGasFee(){ return Numeric.decodeQuantity(gasFee); } + + public void setGasFee(String gasFee) { + this.gasFee = gasFee; + } } diff --git a/src/main/java/conflux/web3j/response/Transaction.java b/src/main/java/conflux/web3j/response/Transaction.java index 5e25e76..8b592bd 100644 --- a/src/main/java/conflux/web3j/response/Transaction.java +++ b/src/main/java/conflux/web3j/response/Transaction.java @@ -3,8 +3,10 @@ import java.math.BigInteger; import java.util.Optional; +import conflux.web3j.types.AccessListEntry; import conflux.web3j.types.Address; import org.web3j.utils.Numeric; +import java.util.List; public class Transaction { @@ -28,6 +30,51 @@ public static class Response extends CfxNullableResponse {} private String v; private String r; private String s; + private String type; + private String yParity; + private String maxFeePerGas; + private String maxPriorityFeePerGas; + private List accessList; + + public BigInteger getMaxFeePerGas() { + return Numeric.decodeQuantity(this.maxFeePerGas); + } + + public void setMaxFeePerGas(String maxFeePerGas) { + this.maxFeePerGas = maxFeePerGas; + } + + public BigInteger getMaxPriorityFeePerGas() { + return Numeric.decodeQuantity(this.maxPriorityFeePerGas); + } + + public void setMaxPriorityFeePerGas(String maxPriorityFeePerGas) { + this.maxPriorityFeePerGas = maxPriorityFeePerGas; + } + + public BigInteger getYParity() { + return Numeric.decodeQuantity(yParity); + } + + public void setYParity(String yParity) { + this.yParity = yParity; + } + + public int getType() { + return Numeric.decodeQuantity(type).intValue(); + } + + public void setType(String type) { + this.type = type; + } + + public List getAccessList() { + return accessList; + } + + public void setAccessList(List accessList) { + this.accessList = accessList; + } public String getHash() { return hash; diff --git a/src/main/java/conflux/web3j/types/AccessListEntry.java b/src/main/java/conflux/web3j/types/AccessListEntry.java new file mode 100644 index 0000000..3c77096 --- /dev/null +++ b/src/main/java/conflux/web3j/types/AccessListEntry.java @@ -0,0 +1,24 @@ +package conflux.web3j.types; +import java.util.List; + +public class AccessListEntry { + + private Address address; + private List storageKeys; + + public List getStorageKeys() { + return storageKeys; + } + + public void setStorageKeys(List storageKeys) { + this.storageKeys = storageKeys; + } + + public Address getAddress() { + return address; + } + + public void setAddress(CfxAddress address) { + this.address = address; + } +} From 30ada3089f19fec4be549232de6c56a67f1367af Mon Sep 17 00:00:00 2001 From: Pana Date: Fri, 28 Jun 2024 18:39:49 +0800 Subject: [PATCH 2/3] support for 1559 transaction type --- CHANGELOG.md | 7 ++ build.gradle | 2 +- .../conflux/web3j/types/RawTransaction.java | 109 +++++++++++++++++- .../web3j/types/TransactionBuilder.java | 55 +++++++-- .../web3j/types/RawTransactionTests.java | 66 +++++++++++ 5 files changed, 226 insertions(+), 13 deletions(-) create mode 100644 src/test/java/conflux/web3j/types/RawTransactionTests.java diff --git a/CHANGELOG.md b/CHANGELOG.md index 620715c..0fa9066 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,12 @@ # CHANGELOG +### 1.3.0 + +This version add support for [Conflux hardfork v2.4.0](https://doc.confluxnetwork.org/docs/general/hardforks/v2.4), including: + +1. New added RPC methods: cfx_maxPriorityFeePerGas, cfx_feeHistory, cfx_getFeeBurnt +2. Support for CIP-1559 transaction + ### 1.2.10 1. Fix SendTransactionError parse method to handle data is null diff --git a/build.gradle b/build.gradle index 1f8e415..4752f5c 100644 --- a/build.gradle +++ b/build.gradle @@ -13,7 +13,7 @@ plugins { } group = 'io.github.conflux-chain' -version = '1.2.10' // SNAPSHOT +version = '1.3.0' // SNAPSHOT repositories { jcenter() diff --git a/src/main/java/conflux/web3j/types/RawTransaction.java b/src/main/java/conflux/web3j/types/RawTransaction.java index dfbbc1d..0e8f556 100644 --- a/src/main/java/conflux/web3j/types/RawTransaction.java +++ b/src/main/java/conflux/web3j/types/RawTransaction.java @@ -3,7 +3,10 @@ import java.math.BigInteger; import java.util.ArrayList; import java.util.List; +import java.util.Objects; import java.util.concurrent.atomic.AtomicReference; +import java.nio.ByteBuffer; +import java.util.Arrays; import org.web3j.crypto.ECKeyPair; import org.web3j.crypto.Sign; @@ -18,8 +21,8 @@ public class RawTransaction { - private static AtomicReference DefaultGasPrice = new AtomicReference(BigInteger.ONE); - private static AtomicReference DefaultChainId = new AtomicReference(BigInteger.valueOf(1029)); + private static final AtomicReference DefaultGasPrice = new AtomicReference(BigInteger.ONE); + private static final AtomicReference DefaultChainId = new AtomicReference(BigInteger.valueOf(1029)); public static BigInteger getDefaultGasPrice() { return DefaultGasPrice.get(); @@ -37,8 +40,18 @@ public static void setDefaultChainId(BigInteger defaultChainId) { DefaultChainId.set(defaultChainId); } + public static BigInteger TYPE_LEGACY = new BigInteger("0"); + public static BigInteger TYPE_2930 = new BigInteger("1"); + public static BigInteger TYPE_1559 = new BigInteger("2"); + + private static final byte[] TYPE_2930_PREFIX = {99, 102, 120, 1}; // cfx + 1 + private static final byte[] TYPE_1559_PREFIX = {99, 102, 120, 2}; // cfx + 2 + + private BigInteger type; private BigInteger nonce; private BigInteger gasPrice; + private BigInteger maxPriorityFeePerGas; + private BigInteger maxFeePerGas; private BigInteger gas; private Address to; private BigInteger value; @@ -46,6 +59,7 @@ public static void setDefaultChainId(BigInteger defaultChainId) { private BigInteger epochHeight; private BigInteger chainId; private String data; + private List accessList; // Note default will use Mainnet chainId. // To create a testnet, user need invoke RawTransaction.setDefaultChainId(BigInteger.ONE) before invoke the create method. @@ -61,6 +75,7 @@ public static RawTransaction create(BigInteger nonce, BigInteger gas, Address to tx.epochHeight = epochHeight; tx.data = data; tx.chainId = DefaultChainId.get(); + tx.type = BigInteger.ZERO; return tx; } @@ -96,6 +111,16 @@ public String sign(ECKeyPair ecKeyPair) { RlpString.create(v), RlpString.create(r), RlpString.create(s))); + + System.out.println(Numeric.toHexString(signedTx)); + + // add prefix for typed transaction + if (Objects.equals(this.type, RawTransaction.TYPE_1559)) { + signedTx = concat(TYPE_1559_PREFIX, signedTx); + } + if (Objects.equals(this.type, RawTransaction.TYPE_2930)) { + signedTx = concat(TYPE_2930_PREFIX, signedTx); + } return Numeric.toHexString(signedTx); } @@ -104,7 +129,14 @@ public RlpType toRlp() { List values = new ArrayList(); values.add(RlpString.create(this.nonce)); - values.add(RlpString.create(this.gasPrice)); + + if (Objects.equals(this.type, RawTransaction.TYPE_1559)) { + values.add(RlpString.create(this.maxPriorityFeePerGas)); + values.add(RlpString.create(this.maxFeePerGas)); + } else { + values.add(RlpString.create(this.gasPrice)); + } + values.add(RlpString.create(this.gas)); if (this.to != null) { @@ -119,9 +151,30 @@ public RlpType toRlp() { values.add(RlpString.create(this.chainId)); values.add(RlpString.create(Numeric.hexStringToByteArray(this.data == null ? "" : this.data))); + if (Objects.equals(this.type, RawTransaction.TYPE_1559) || Objects.equals(this.type, RawTransaction.TYPE_2930)) { + List rlpAccessList = new ArrayList<>(); + + accessList.forEach(entry -> { + List rlpAccessListObject = new ArrayList<>(); + // add address + rlpAccessListObject.add(RlpString.create(Numeric.hexStringToByteArray(entry.getAddress().getHexAddress()))); + + // add storage keys + List keyList = new ArrayList<>(); + entry.getStorageKeys().forEach(key -> { + keyList.add(RlpString.create(Numeric.hexStringToByteArray(key))); + }); + rlpAccessListObject.add(new RlpList(keyList)); + + rlpAccessList.add(new RlpList(rlpAccessListObject)); + }); + + values.add(new RlpList(rlpAccessList)); + } + return new RlpList(values); } - + public BigInteger getNonce() { return nonce; } @@ -194,4 +247,52 @@ public void setData(String data) { this.data = data; } + public BigInteger getType() { + return type; + } + + public void setType(BigInteger type) { + this.type = type; + } + + public BigInteger getMaxPriorityFeePerGas() { + return maxPriorityFeePerGas; + } + + public void setMaxPriorityFeePerGas(BigInteger maxPriorityFeePerGas) { + this.maxPriorityFeePerGas = maxPriorityFeePerGas; + } + + public BigInteger getMaxFeePerGas() { + return maxFeePerGas; + } + + public void setMaxFeePerGas(BigInteger maxFeePerGas) { + this.maxFeePerGas = maxFeePerGas; + } + + public List getAccessList() { + return accessList; + } + + public void setAccessList(List accessList) { + this.accessList = accessList; + } + + public static byte[] concat(byte[]... arrays) { + // Calculate the total length of the resulting array + int totalLength = Arrays.stream(arrays).mapToInt(arr -> arr.length).sum(); + + // Create a ByteBuffer with the total length + ByteBuffer buffer = ByteBuffer.allocate(totalLength); + + // Put each byte array into the ByteBuffer + for (byte[] array : arrays) { + buffer.put(array); + } + + // Return the underlying byte array + return buffer.array(); + } + } diff --git a/src/main/java/conflux/web3j/types/TransactionBuilder.java b/src/main/java/conflux/web3j/types/TransactionBuilder.java index 87136f3..c9e01ff 100644 --- a/src/main/java/conflux/web3j/types/TransactionBuilder.java +++ b/src/main/java/conflux/web3j/types/TransactionBuilder.java @@ -2,7 +2,11 @@ import java.math.BigDecimal; import java.math.BigInteger; +import java.util.List; +import java.util.Objects; +import conflux.web3j.request.Epoch; +import conflux.web3j.response.Block; import org.web3j.utils.Strings; import conflux.web3j.Cfx; @@ -12,7 +16,7 @@ public class TransactionBuilder { public static final BigDecimal DEFAULT_GAS_OVERFLOW_RATIO = BigDecimal.valueOf(1.3); - public static final BigDecimal DEFAULT_COLLATERAL_OVERFLOW_RATIO = BigDecimal.valueOf(2); + public static final BigDecimal DEFAULT_COLLATERAL_OVERFLOW_RATIO = BigDecimal.valueOf(1.3); private Address from; private BigDecimal gasOverflowRatio; @@ -38,6 +42,28 @@ public TransactionBuilder withNonce(BigInteger nonce) { this.tx.setNonce(nonce); return this; } + + public TransactionBuilder withType(BigInteger val) { + this.tx.setType(val); + return this; + } + + public TransactionBuilder withAccessList(List val) { + this.tx.setAccessList(val); + return this; + } + + public TransactionBuilder withMaxPriorityFeePerGas(BigInteger val) { + this.tx.setType(RawTransaction.TYPE_1559); + this.tx.setMaxPriorityFeePerGas(val); + return this; + } + + public TransactionBuilder withMaxFeePerGas(BigInteger val) { + this.tx.setType(RawTransaction.TYPE_1559); + this.tx.setMaxFeePerGas(val); + return this; + } public TransactionBuilder withGasPrice(BigInteger price) { this.tx.setGasPrice(price); @@ -105,12 +131,25 @@ public RawTransaction build(Cfx cfx) { if (this.tx.getNonce() == null) { this.tx.setNonce(cfx.getNonce(this.from).sendAndGet()); } - - if (this.tx.getGasPrice() == null) { - BigInteger gasPrice = cfx.getGasPrice().sendAndGet(); - this.tx.setGasPrice(gasPrice); + + if (Objects.equals(this.tx.getType(), RawTransaction.TYPE_1559)) { + if (this.tx.getMaxPriorityFeePerGas() == null) { + BigInteger maxPriorityFeePerGas = cfx.getMaxPriorityFeePerGas().sendAndGet(); + this.tx.setMaxPriorityFeePerGas(maxPriorityFeePerGas); + } + if (this.tx.getMaxFeePerGas() == null) { + Block b = cfx.getBlockByEpoch(Epoch.latestState()).sendAndGet().get(); + + BigInteger maxFeePerGas = b.getBaseFeePerGas().multiply(new BigInteger("2")).add(this.tx.getMaxPriorityFeePerGas()); + this.tx.setMaxFeePerGas(maxFeePerGas); + } + } else { + if (this.tx.getGasPrice() == null) { + BigInteger gasPrice = cfx.getGasPrice().sendAndGet(); + this.tx.setGasPrice(gasPrice); + } } - + // if (this.tx.getTo() == null) { // this.tx.setTo(null); // } @@ -156,8 +195,8 @@ private void estimateLimit(Cfx cfx) { UsedGasAndCollateral estimation = cfx.estimateGasAndCollateral(call).sendAndGet(); if (this.tx.getGas() == null) { - BigDecimal gasLimit = new BigDecimal(estimation.getGasUsed()).multiply(this.gasOverflowRatio); - this.tx.setGas(gasLimit.toBigInteger()); +// BigDecimal gasLimit = new BigDecimal(estimation.getGasUsed()).multiply(this.gasOverflowRatio); + this.tx.setGas(estimation.getGasUsed()); } if (this.tx.getStorageLimit() == null) { diff --git a/src/test/java/conflux/web3j/types/RawTransactionTests.java b/src/test/java/conflux/web3j/types/RawTransactionTests.java new file mode 100644 index 0000000..887a4cc --- /dev/null +++ b/src/test/java/conflux/web3j/types/RawTransactionTests.java @@ -0,0 +1,66 @@ +package conflux.web3j.types; + +//import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; +import org.web3j.rlp.RlpEncoder; +import org.web3j.utils.Numeric; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import java.math.BigInteger; +import java.util.Arrays; + +public class RawTransactionTests { + @Test + @DisplayName("Raw2930TransactionRlp encode") + void raw2930TransactionRlpEncodeTest() { + RawTransaction tx = new RawTransaction(); + tx.setType(RawTransaction.TYPE_2930); + tx.setNonce(new BigInteger("100")); + tx.setGas(new BigInteger("100")); + tx.setGasPrice(new BigInteger("100")); + tx.setChainId(new BigInteger("100")); + tx.setEpochHeight(new BigInteger("100")); + tx.setStorageLimit(new BigInteger("100")); + tx.setValue(new BigInteger("100")); + tx.setTo(new Address("0x19578cf3c71eab48cf810c78b5175d5c9e6ef441", 1)); + tx.setData(Numeric.toHexString("Hello, World".getBytes())); + + AccessListEntry entry = new AccessListEntry(); + entry.setAddress(new CfxAddress("0x19578cf3c71eab48cf810c78b5175d5c9e6ef441", 1)); + entry.setStorageKeys(Arrays.asList(new String[]{"0x1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef"})); + + tx.setAccessList(Arrays.asList(new AccessListEntry[]{entry})); + + byte[] encoded = RlpEncoder.encode(tx.toRlp()); + + assertEquals(Numeric.toHexString(encoded), "0xf8636464649419578cf3c71eab48cf810c78b5175d5c9e6ef441646464648c48656c6c6f2c20576f726c64f838f79419578cf3c71eab48cf810c78b5175d5c9e6ef441e1a01234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef", ""); + } + + @Test + @DisplayName("Raw1559TransactionRlp encode") + void raw1559TransactionRlpEncodeTest() { + RawTransaction tx = new RawTransaction(); + tx.setType(RawTransaction.TYPE_1559); + tx.setNonce(new BigInteger("100")); + tx.setGas(new BigInteger("100")); + tx.setMaxPriorityFeePerGas(new BigInteger("100")); + tx.setMaxFeePerGas(new BigInteger("100")); + tx.setChainId(new BigInteger("100")); + tx.setEpochHeight(new BigInteger("100")); + tx.setStorageLimit(new BigInteger("100")); + tx.setValue(new BigInteger("100")); + tx.setTo(new Address("0x19578cf3c71eab48cf810c78b5175d5c9e6ef441", 1)); + tx.setData(Numeric.toHexString("Hello, World".getBytes())); + + AccessListEntry entry = new AccessListEntry(); + entry.setAddress(new CfxAddress("0x19578cf3c71eab48cf810c78b5175d5c9e6ef441", 1)); + entry.setStorageKeys(Arrays.asList(new String[]{"0x1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef"})); + + tx.setAccessList(Arrays.asList(new AccessListEntry[]{entry})); + + byte[] encoded = RlpEncoder.encode(tx.toRlp()); + + assertEquals(Numeric.toHexString(encoded), "0xf864646464649419578cf3c71eab48cf810c78b5175d5c9e6ef441646464648c48656c6c6f2c20576f726c64f838f79419578cf3c71eab48cf810c78b5175d5c9e6ef441e1a01234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef", ""); + } +} From 33ccf2530eb2986eff1f88f828439b95faf710a5 Mon Sep 17 00:00:00 2001 From: Pana Date: Fri, 28 Jun 2024 18:46:37 +0800 Subject: [PATCH 3/3] remove log --- src/main/java/conflux/web3j/types/RawTransaction.java | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/main/java/conflux/web3j/types/RawTransaction.java b/src/main/java/conflux/web3j/types/RawTransaction.java index 0e8f556..097b3bb 100644 --- a/src/main/java/conflux/web3j/types/RawTransaction.java +++ b/src/main/java/conflux/web3j/types/RawTransaction.java @@ -112,8 +112,6 @@ public String sign(ECKeyPair ecKeyPair) { RlpString.create(r), RlpString.create(s))); - System.out.println(Numeric.toHexString(signedTx)); - // add prefix for typed transaction if (Objects.equals(this.type, RawTransaction.TYPE_1559)) { signedTx = concat(TYPE_1559_PREFIX, signedTx);