Skip to content

Commit

Permalink
Merge pull request #289 from nervosnetwork/rc/v0.29.0
Browse files Browse the repository at this point in the history
[ᚬmaster] Rc/v0.29.0
  • Loading branch information
duanyytop authored Feb 28, 2020
2 parents f2999ba + e8537e3 commit 406ecd8
Show file tree
Hide file tree
Showing 11 changed files with 40 additions and 40 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
All notable changes to this project will be documented in this file.
See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.

# [v0.29.0](https://github.com/nervosnetwork/ckb-sdk-java/compare/v0.28.0...v0.29.0) (2020-2-28)

### Refactor

* Remove useless code ([a858559](https://github.com/nervosnetwork/ckb-sdk-java/commit/a858559abd8dee81d0969334b5d6487533545ca6))
* Update transaction fee calculating ([004c6e3](https://github.com/nervosnetwork/ckb-sdk-java/commit/004c6e30b571b805ec2905df0c909a1287c6917d))

# [v0.28.0](https://github.com/nervosnetwork/ckb-sdk-java/compare/v0.27.1...v0.28.0) (2020-2-7)

Bump version to v0.28.0
Expand Down
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,9 @@ You can see more JSON-RPC requests from [RPC Document](https://github.com/nervos

#### Single-sig Transfer

> Note: If you want to run transfer example, you should update example private key of sender whose balance is not zero.
> And if you want to use example default private key to run, you should make the example sender's balance is not zero or set the blake160 of default sender's public key to CKB dev chain node configuration file to be a miner.
[SingleKeySingleSigTxExample](https://github.com/nervosnetwork/ckb-sdk-java/tree/develop/example/src/main/java/org/nervos/ckb/SingleKeySingleSigTxExample.java) provides `sendCapacity` method with any amount inputs which belong to a private key.

[MultiKeySingleSigTxExample](https://github.com/nervosnetwork/ckb-sdk-java/tree/develop/example/src/main/java/org/nervos/ckb/MultiKeySingleSigTxExample.java) provides `sendCapacity` method with any amount inputs which belong to any amount private keys.
Expand Down Expand Up @@ -138,6 +141,9 @@ You can reference detail example in `example/MultiKeySingleSigTxExample.java`.

#### Multi-sig Transfer

> Note: If you want to run transfer example, you should update example private key of sender whose balance is not zero.
> And if you want to use example default private key to run, you should make the example sender's balance is not zero or set the blake160 of default sender's public key to CKB dev chain node configuration file to be a miner.
[SendToMultiSigAddressTxExample](https://github.com/nervosnetwork/ckb-sdk-java/tree/develop/example/src/main/java/org/nervos/ckb/SendToMultiSigAddressTxExample.java) provides `sendCapacity` method which single-sig address sends capacity to 2/3 format multi-sig address.

[MultiSignTransactionExample](https://github.com/nervosnetwork/ckb-sdk-java/tree/develop/example/src/main/java/org/nervos/ckb/MultiSignTransactionExample.java) provides `sendCapacity` method which 2/3 format multi-sig address sends capacity to single-sig address.
Expand Down
10 changes: 5 additions & 5 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
buildscript {

ext.bouncycastleVersion = '1.64'
ext.rxjavaVersion = '2.2.17'
ext.rxjavaVersion = '2.2.18'
ext.gsonVersion = '2.8.6'
ext.okhttpVersion = '4.3.1'
ext.loggingOkhttpVersion = '4.3.1'
ext.okhttpVersion = '4.4.0'
ext.loggingOkhttpVersion = '4.4.0'
ext.slf4jVersion = '1.7.30'
ext.guavaVersion = '28.2-jre'

Expand Down Expand Up @@ -43,7 +43,7 @@ allprojects {
targetCompatibility = 1.8

group 'org.nervos.ckb'
version '0.28.0'
version '0.29.0'

apply plugin: 'java'

Expand Down Expand Up @@ -113,7 +113,7 @@ configure(subprojects.findAll { it.name != 'tests' }) {
publications {
mavenJava(MavenPublication) {
groupId 'org.nervos.ckb'
version '0.28.0'
version '0.29.0'
from components.java
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,8 +130,7 @@ public CollectResult collectInputs(
}

private BigInteger calculateTxFee(Transaction transaction, BigInteger feeRate) {
int txSize = Serializer.serializeTransaction(transaction).toBytes().length;
return Calculator.calculateTransactionFee(BigInteger.valueOf(txSize), feeRate);
return Calculator.calculateTransactionFee(transaction, feeRate);
}

private BigInteger calculateOutputSize(CellOutput cellOutput) {
Expand Down
12 changes: 9 additions & 3 deletions ckb/src/main/java/org/nervos/ckb/utils/Calculator.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,15 @@ public class Calculator {
private static final int SERIALIZED_TX_OFFSET_BYTE_SIZE = 4;
private static final int MIN_CONFIRM_BLOCKS = 3;

public static int calculateSerializedSizeInBlock(Transaction transaction) {
public static int calculateTransactionSize(Transaction transaction) {
Table serializedTx = Serializer.serializeTransaction(transaction);
return serializedTx.getLength() + SERIALIZED_TX_OFFSET_BYTE_SIZE;
}

private static final BigInteger RADIO = BigInteger.valueOf(1000);

public static BigInteger calculateTransactionFee(BigInteger transactionSize, BigInteger feeRate) {
private static BigInteger calculateTransactionFee(
BigInteger transactionSize, BigInteger feeRate) {
BigInteger base = transactionSize.multiply(feeRate);
BigInteger fee = base.divide(RADIO);
if (fee.multiply(RADIO).compareTo(base) < 0) {
Expand All @@ -35,7 +36,12 @@ public static BigInteger calculateTransactionFee(
}
BigInteger feeRate =
Numeric.toBigInt(api.estimateFeeRate(String.valueOf(expectedConfirmBlocks)).feeRate);
BigInteger txSize = BigInteger.valueOf(calculateSerializedSizeInBlock(transaction));
BigInteger txSize = BigInteger.valueOf(calculateTransactionSize(transaction));
return calculateTransactionFee(txSize, feeRate);
}

public static BigInteger calculateTransactionFee(Transaction transaction, BigInteger feeRate) {
BigInteger txSize = BigInteger.valueOf(calculateTransactionSize(transaction));
return calculateTransactionFee(txSize, feeRate);
}
}
21 changes: 12 additions & 9 deletions ckb/src/test/java/utils/CalculatorTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@
import java.util.Collections;
import java.util.List;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestInstance;
import org.nervos.ckb.type.OutPoint;
import org.nervos.ckb.type.Script;
import org.nervos.ckb.type.cell.CellDep;
Expand All @@ -16,10 +18,12 @@
import org.nervos.ckb.utils.Calculator;

/** Copyright © 2019 Nervos Foundation. All rights reserved. */
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
public class CalculatorTest {
private Transaction tx;

@Test
void testCalculateTransactionSize() {
@BeforeAll
void init() {
List<CellOutput> cellOutputs = new ArrayList<>();
cellOutputs.add(
new CellOutput(
Expand All @@ -40,7 +44,7 @@ void testCalculateTransactionSize() {
"0x59a27ef3ba84f061517d13f42cf44ed020610061",
Script.TYPE)));

Transaction tx =
tx =
new Transaction(
"0x0",
Arrays.asList(
Expand All @@ -65,17 +69,16 @@ void testCalculateTransactionSize() {
Arrays.asList("0x1234", "0x"),
Collections.singletonList(
"0x82df73581bcd08cb9aa270128d15e79996229ce8ea9e4f985b49fbf36762c5c37936caf3ea3784ee326f60b8992924fcf496f9503c907982525a3436f01ab32900"));
}

Assertions.assertEquals(Calculator.calculateSerializedSizeInBlock(tx), 536);
@Test
void testCalculateTransactionSize() {
Assertions.assertEquals(Calculator.calculateTransactionSize(tx), 536);
}

@Test
public void testCalculateTransactionFee() {
Assertions.assertEquals(
Calculator.calculateTransactionFee(BigInteger.valueOf(1035), BigInteger.valueOf(900)),
BigInteger.valueOf(932));
Assertions.assertEquals(
Calculator.calculateTransactionFee(BigInteger.valueOf(900), BigInteger.valueOf(900)),
BigInteger.valueOf(810));
Calculator.calculateTransactionFee(tx, BigInteger.valueOf(900)), BigInteger.valueOf(483));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -137,10 +137,6 @@ private static String sendCapacity(

private static Transaction generateTx(
String address, List<Receiver> receivers, String changeAddress) throws IOException {
BigInteger needCapacity = BigInteger.ZERO;
for (Receiver receiver : receivers) {
needCapacity = needCapacity.add(receiver.capacity);
}
return generateTx(Collections.singletonList(address), receivers, changeAddress);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,6 @@ private static String getMultiSigBalance() {

private static String sendCapacity(List<Receiver> receivers, String changeAddress)
throws IOException {
BigInteger needCapacity = BigInteger.ZERO;
for (Receiver receiver : receivers) {
needCapacity = needCapacity.add(receiver.capacity);
}

TransactionBuilder txBuilder = new TransactionBuilder(api);
CollectUtils txUtils = new CollectUtils(api);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,11 +71,7 @@ private static String getBalance(String address) {

private static String sendCapacity(List<Receiver> receivers, String changeAddress)
throws IOException {
BigInteger needCapacity = BigInteger.ZERO;
List<ScriptGroupWithPrivateKeys> scriptGroupWithPrivateKeysList = new ArrayList<>();
for (Receiver receiver : receivers) {
needCapacity = needCapacity.add(receiver.capacity);
}

TransactionBuilder txBuilder = new TransactionBuilder(api);
CollectUtils txUtils = new CollectUtils(api);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,11 +83,7 @@ private static BigInteger getBalance(String address) {

private static String sendCapacity(List<Receiver> receivers, String changeAddress)
throws IOException {
BigInteger needCapacity = BigInteger.ZERO;
List<ScriptGroupWithPrivateKeys> scriptGroupWithPrivateKeysList = new ArrayList<>();
for (Receiver receiver : receivers) {
needCapacity = needCapacity.add(receiver.capacity);
}

TransactionBuilder txBuilder = new TransactionBuilder(api);
CollectUtils txUtils = new CollectUtils(api);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,10 +79,6 @@ public List<CellOutput> generateOutputs(List<Receiver> receivers, String changeA
new CellOutput(
Numeric.toHexStringWithPrefix(receiver.capacity), addressParseResult.script));
}
BigInteger needCapacity = BigInteger.ZERO;
for (Receiver receiver : receivers) {
needCapacity = needCapacity.add(receiver.capacity);
}
AddressParseResult addressParseResult = AddressParser.parse(changeAddress);
cellOutputs.add(new CellOutput("0x0", addressParseResult.script));
return cellOutputs;
Expand Down

0 comments on commit 406ecd8

Please sign in to comment.