Skip to content

Commit

Permalink
Merge pull request #337 from nervosnetwork/rc/v0.34.0
Browse files Browse the repository at this point in the history
Rc/v0.34.0
  • Loading branch information
duanyytop authored Jul 21, 2020
2 parents e253c52 + 61c234d commit 6d83612
Show file tree
Hide file tree
Showing 26 changed files with 187 additions and 210 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ hs_err_pid*
*/out/

gradle.properties
local.properties

# Ignore RPCTest because loachost can not visit
RpcTest
Expand Down
20 changes: 20 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,25 @@
All notable changes to this project will be documented in this file.
See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.

# [v0.34.0](https://github.com/nervosnetwork/ckb-sdk-java/compare/v0.33.0...v0.34.0) (2020-7-21)

### Feature

* Add batch rpc request ([e5eef53](https://github.com/nervosnetwork/ckb-sdk-java/commit/e5eef53b98f671620f5d8c05c0cc7b1e62f7674f))
* Add `clearTxPool` rpc request([64819d9](https://github.com/nervosnetwork/ckb-sdk-java/commit/64819d9e6be352ae193c5ee4da1949868e96426a))
* Remove `estimateFeeRate` rpc request ([054bd01](https://github.com/nervosnetwork/ckb-sdk-java/commit/054bd01e22b77591ec0d5735d23f679cc6712e4b))
* Set `computeScriptHash` and `computeTransactionHash` as deprecated rpc request ([fec42ec](https://github.com/nervosnetwork/ckb-sdk-java/commit/fec42ec72248d49cb5c62f6933a3d54be35c13dd))

### BugFix

* Check full address payload length ([02a425a](https://github.com/nervosnetwork/ckb-sdk-java/commit/02a425ae70110439e8118db8dc9fbbdcc829c05b))
* Fix address parse method bug ([cf3997b](https://github.com/nervosnetwork/ckb-sdk-java/commit/cf3997b51baefd44a63d9c449b488c47c1373a74))

### BreakingChanges

* Remove `estimateFeeRate` rpc request ([054bd01](https://github.com/nervosnetwork/ckb-sdk-java/commit/054bd01e22b77591ec0d5735d23f679cc6712e4b))


# [v0.33.0](https://github.com/nervosnetwork/ckb-sdk-java/compare/v0.32.0...v0.33.0) (2020-6-22)

Bump version to v0.33.0
Expand Down
14 changes: 7 additions & 7 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
buildscript {

ext.bouncycastleVersion = '1.65.01'
ext.bouncycastleVersion = '1.66'
ext.rxjavaVersion = '2.2.19'
ext.gsonVersion = '2.8.6'
ext.okhttpVersion = '4.7.2'
ext.loggingOkhttpVersion = '4.7.2'
ext.okhttpVersion = '4.8.0'
ext.loggingOkhttpVersion = '4.8.0'
ext.slf4jVersion = '1.7.30'
ext.guavaVersion = '29.0-jre'

Expand All @@ -17,12 +17,12 @@ buildscript {

dependencies {
classpath 'io.codearte.gradle.nexus:gradle-nexus-staging-plugin:0.21.2'
classpath 'com.github.jengelman.gradle.plugins:shadow:5.1.0'
classpath 'com.github.jengelman.gradle.plugins:shadow:6.0.0'
}
}

plugins {
id 'com.github.johnrengelman.shadow' version '5.2.0'
id 'com.github.johnrengelman.shadow' version '6.0.0'
id 'java'
id 'com.github.sherter.google-java-format' version '0.8'
id 'com.jfrog.bintray' version '1.8.5'
Expand All @@ -43,7 +43,7 @@ allprojects {
targetCompatibility = 1.8

group 'org.nervos.ckb'
version '0.33.0'
version '0.34.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.33.0'
version '0.34.0'
from components.java
}
}
Expand Down
25 changes: 18 additions & 7 deletions ckb/src/main/java/org/nervos/ckb/service/Api.java
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,10 @@ public TxPoolInfo txPoolInfo() throws IOException {
return rpcService.post("tx_pool_info", Collections.emptyList(), TxPoolInfo.class);
}

public String clearTxPool() throws IOException {
return rpcService.post("clear_tx_pool", Collections.emptyList(), String.class);
}

public String sendTransaction(Transaction transaction) throws IOException {
return rpcService.post(
"send_transaction",
Expand Down Expand Up @@ -181,24 +185,19 @@ public Cycles dryRunTransaction(Transaction transaction) throws IOException {
Cycles.class);
}

@Deprecated
public String computeTransactionHash(Transaction transaction) throws IOException {
return rpcService.post(
"_compute_transaction_hash",
Collections.singletonList(Convert.parseTransaction(transaction)),
String.class);
}

@Deprecated
public String computeScriptHash(Script script) throws IOException {
return rpcService.post("_compute_script_hash", Collections.singletonList(script), String.class);
}

public FeeRate estimateFeeRate(String expectedConfirmBlocks) throws IOException {
return rpcService.post(
"estimate_fee_rate",
Collections.singletonList(Numeric.toHexString(expectedConfirmBlocks)),
FeeRate.class);
}

public String calculateDaoMaximumWithdraw(OutPoint outPoint, String withdrawBlockHash)
throws IOException {
return rpcService.post(
Expand Down Expand Up @@ -255,4 +254,16 @@ public LockHashCapacity getCapacityByLockHash(String lockHash) throws IOExceptio
return rpcService.post(
"get_capacity_by_lock_hash", Collections.singletonList(lockHash), LockHashCapacity.class);
}

/**
* Batch RPC request
*
* @param requests: A list of rpc method and parameters and the first element of each list must be
* rpc method. Example: [["get_block_hash", "0x200"],["get_block_by_number", "0x300"]]
* @return A list of rpc response
* @throws IOException Request or response error will throw exception
*/
public List<RpcResponse> batchRPC(List<List> requests) throws IOException {
return rpcService.batchPost(requests);
}
}
14 changes: 14 additions & 0 deletions ckb/src/main/java/org/nervos/ckb/service/RpcResponse.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package org.nervos.ckb.service;

/** Copyright © 2020 Nervos Foundation. All rights reserved. */
public class RpcResponse<T> {
public long id;
public String jsonrpc;
public T result;
public Error error;

static class Error {
public int code;
public String message;
}
}
44 changes: 30 additions & 14 deletions ckb/src/main/java/org/nervos/ckb/service/RpcService.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,14 @@
import com.google.gson.reflect.TypeToken;
import java.io.IOException;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.concurrent.atomic.AtomicLong;
import okhttp3.*;
import okhttp3.logging.HttpLoggingInterceptor;
import org.jetbrains.annotations.NotNull;
import org.nervos.ckb.utils.Numeric;

/** Copyright © 2019 Nervos Foundation. All rights reserved. */
class RpcService {
Expand Down Expand Up @@ -40,7 +43,7 @@ <T> T post(@NotNull String method, List params, Type cls) throws IOException {
Request request = new Request.Builder().url(url).post(body).build();
Response response = client.newCall(request).execute();
if (response.isSuccessful()) {
String responseBody = response.body().string();
String responseBody = Objects.requireNonNull(response.body()).string();
RpcResponse rpcResponse =
gson.fromJson(responseBody, new TypeToken<RpcResponse>() {}.getType());

Expand Down Expand Up @@ -78,7 +81,7 @@ public void onFailure(@NotNull Call call, @NotNull IOException e) {
public void onResponse(@NotNull Call call, @NotNull Response response)
throws IOException {
if (response.isSuccessful()) {
String responseBody = response.body().string();
String responseBody = Objects.requireNonNull(response.body()).string();
RpcResponse<T> rpcResponse =
gson.fromJson(responseBody, new TypeToken<RpcResponse<T>>() {}.getType());
if (rpcResponse.error != null) {
Expand All @@ -99,6 +102,31 @@ public void onResponse(@NotNull Call call, @NotNull Response response)
});
}

List<RpcResponse> batchPost(List<List> requests) throws IOException {
List<RequestParams> paramsList = new ArrayList<>();
for (List request : requests) {
if (request.size() == 0 || !(request.get(0) instanceof String)) {
throw new IOException("RPC method name must be a non-null string");
}
for (int i = 1; i < request.size(); i++) {
if (Numeric.isIntegerValue(request.get(i).toString())) {
request.set(i, Numeric.toHexString(request.get(i).toString()));
}
}
paramsList.add(
new RequestParams(request.get(0).toString(), request.subList(1, request.size())));
}
RequestBody body = RequestBody.create(gson.toJson(paramsList), JSON_MEDIA_TYPE);
Request request = new Request.Builder().url(url).post(body).build();
Response response = client.newCall(request).execute();
if (response.isSuccessful()) {
String responseBody = Objects.requireNonNull(response.body()).string();
return gson.fromJson(responseBody, new TypeToken<List<RpcResponse>>() {}.getType());
} else {
throw new IOException("RpcService error code " + response.code());
}
}

static class RequestParams {
String jsonrpc = "2.0";
String method;
Expand All @@ -111,16 +139,4 @@ public RequestParams(String method, List params) {
this.id = nextId.getAndIncrement();
}
}

static class RpcResponse<T> {
long id;
String jsonrpc;
T result;
Error error;

class Error {
public int code;
public String message;
}
}
}
14 changes: 0 additions & 14 deletions ckb/src/main/java/org/nervos/ckb/utils/Calculator.java
Original file line number Diff line number Diff line change
@@ -1,16 +1,13 @@
package org.nervos.ckb.utils;

import java.io.IOException;
import java.math.BigInteger;
import org.nervos.ckb.service.Api;
import org.nervos.ckb.type.dynamic.Table;
import org.nervos.ckb.type.transaction.Transaction;

/** Copyright © 2019 Nervos Foundation. All rights reserved. */
public class Calculator {
// 4 bytes for the tx offset cost with molecule vector (transactions)
private static final int SERIALIZED_TX_OFFSET_BYTE_SIZE = 4;
private static final int MIN_CONFIRM_BLOCKS = 3;

public static int calculateTransactionSize(Transaction transaction) {
Table serializedTx = Serializer.serializeTransaction(transaction);
Expand All @@ -29,17 +26,6 @@ private static BigInteger calculateTransactionFee(
return fee;
}

public static BigInteger calculateTransactionFee(
Api api, Transaction transaction, long expectedConfirmBlocks) throws IOException {
if (expectedConfirmBlocks < MIN_CONFIRM_BLOCKS) {
throw new IOException("Confirm block must not be smaller than " + MIN_CONFIRM_BLOCKS);
}
BigInteger feeRate =
Numeric.toBigInt(api.estimateFeeRate(String.valueOf(expectedConfirmBlocks)).feeRate);
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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@ public static AddressParseResult parse(String address) throws AddressFormatExcep
}
}

if (payload.length() < 66) {
throw new AddressFormatException("Invalid full address payload length");
}
String codeHash = Numeric.prependHexPrefix(payload.substring(2, 66));
String args = Numeric.prependHexPrefix(payload.substring(66));
if (TYPE_FULL_DATA.equals(type)) {
Expand Down
54 changes: 48 additions & 6 deletions ckb/src/test/java/service/ApiTest.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
package service;

import com.google.gson.Gson;
import java.io.IOException;
import java.math.BigInteger;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import org.junit.jupiter.api.*;
import org.junit.jupiter.api.function.Executable;
import org.nervos.ckb.service.Api;
import org.nervos.ckb.service.RpcResponse;
import org.nervos.ckb.type.*;
import org.nervos.ckb.type.cell.CellOutputWithOutPoint;
import org.nervos.ckb.type.cell.CellTransaction;
Expand Down Expand Up @@ -132,12 +135,18 @@ public void testGetBannedAddress() throws IOException {
}

@Test
public void txPoolInfo() throws IOException {
public void testTxPoolInfo() throws IOException {
TxPoolInfo txPoolInfo = api.txPoolInfo();
Assertions.assertNotNull(txPoolInfo);
Assertions.assertNotNull(txPoolInfo.minFeeRate);
}

@Test
public void testClearTxPool() throws IOException {
String txPoolInfo = api.clearTxPool();
Assertions.assertNull(txPoolInfo);
}

@Test
public void testGetBlockchainInfo() throws IOException {
BlockchainInfo blockchainInfo = api.getBlockchainInfo();
Expand Down Expand Up @@ -277,11 +286,6 @@ public void testComputeTransactionHash() throws IOException {
Assertions.assertNotNull(transactionHash);
}

@Test
public void testEstimateFeeRate() {
Assertions.assertThrows(IOException.class, () -> api.estimateFeeRate("0xa"));
}

@Test
public void testIndexLockHash() throws IOException {
LockHashIndexState lockHashIndexState =
Expand Down Expand Up @@ -343,4 +347,42 @@ public void testGetCapacityByLockHash() throws Exception {
Assertions.assertNotNull(lockHashCapacity.capacity);
Assertions.assertNotNull(lockHashCapacity.cellsCount);
}

@Test
public void testBatchRpc() throws IOException {
List<RpcResponse> rpcResponses =
api.batchRPC(
Arrays.asList(
Arrays.asList("get_block_hash", "0x200"),
Arrays.asList("get_block_by_number", "300"),
Arrays.asList("get_header_by_number", 100)));
Assertions.assertNotNull(rpcResponses);
Assertions.assertEquals(3, rpcResponses.size());
Assertions.assertTrue(rpcResponses.get(0).result instanceof String);
Assertions.assertTrue(
new Gson().fromJson(rpcResponses.get(1).result.toString(), Block.class).transactions.size()
> 0);
Assertions.assertNotNull(
new Gson().fromJson(rpcResponses.get(2).result.toString(), Header.class).compactTarget);

Assertions.assertThrows(
IOException.class,
new Executable() {
@Override
public void execute() throws Throwable {
api.batchRPC(Collections.singletonList(Arrays.asList(1, "0x300")));
}
},
"RPC method name must be a non-null string");

Assertions.assertThrows(
IOException.class,
new Executable() {
@Override
public void execute() throws Throwable {
api.batchRPC(Collections.singletonList(Collections.EMPTY_LIST));
}
},
"RPC method name must be a non-null string");
}
}
15 changes: 15 additions & 0 deletions ckb/src/test/java/utils/AddressParserTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -198,4 +198,19 @@ public void execute() throws Throwable {
Assertions.assertTrue(
exception.getMessage().contains("Short address args byte length must be equal to 20"));
}

@Test
void testAddressPayloadLengthException() {
String address = "ckt1qsvf96jqmq4483ncl7yrzfzshwchu9jd0glq4yy5r2jcsw04r0l5xl";
AddressFormatException exception =
Assertions.assertThrows(
AddressFormatException.class,
new Executable() {
@Override
public void execute() throws Throwable {
AddressParser.parse(address);
}
});
Assertions.assertTrue(exception.getMessage().contains("Invalid full address payload length"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,6 @@ private static Transaction generateTx(
txBuilder.addOutputs(cellOutputs);

// You can get fee rate by rpc or set a simple number
// BigInteger feeRate = Numeric.toBigInt(api.estimateFeeRate("5").feeRate);
BigInteger feeRate = BigInteger.valueOf(1024);

// initial_length = 2 * secp256k1_signature_byte.length
Expand Down
Loading

0 comments on commit 6d83612

Please sign in to comment.