From 1ee023337f163a1595a9c7b93a3e4bac3ed0db48 Mon Sep 17 00:00:00 2001 From: duanyytop Date: Thu, 28 Mar 2019 17:28:50 +0800 Subject: [PATCH 1/3] chore: refactor json parse method --- .../cryptape/cita/protocol/core/Request.java | 2 - .../core/methods/response/AppBlock.java | 108 ---------- .../response/AppGetTransactionCount.java | 2 +- .../response/AppGetTransactionReceipt.java | 25 +-- .../core/methods/response/AppLog.java | 56 ------ .../core/methods/response/AppTransaction.java | 38 ---- .../cryptape/cita/tests/InterfaceTest.java | 186 +++--------------- 7 files changed, 28 insertions(+), 389 deletions(-) diff --git a/core/src/main/java/com/cryptape/cita/protocol/core/Request.java b/core/src/main/java/com/cryptape/cita/protocol/core/Request.java index e2dc091f..9ce31548 100644 --- a/core/src/main/java/com/cryptape/cita/protocol/core/Request.java +++ b/core/src/main/java/com/cryptape/cita/protocol/core/Request.java @@ -19,8 +19,6 @@ public class Request { private CITAjService CITAjService; - // Unfortunately require an instance of the type too, see - // http://stackoverflow.com/a/3437930/3211687 private Class responseType; public Request() { diff --git a/core/src/main/java/com/cryptape/cita/protocol/core/methods/response/AppBlock.java b/core/src/main/java/com/cryptape/cita/protocol/core/methods/response/AppBlock.java index 14c40d10..d6e45e70 100644 --- a/core/src/main/java/com/cryptape/cita/protocol/core/methods/response/AppBlock.java +++ b/core/src/main/java/com/cryptape/cita/protocol/core/methods/response/AppBlock.java @@ -1,26 +1,13 @@ package com.cryptape.cita.protocol.core.methods.response; -import java.io.IOException; import java.math.BigInteger; -import java.util.ArrayList; -import java.util.Iterator; import java.util.List; -import com.fasterxml.jackson.core.JsonParser; -import com.fasterxml.jackson.core.JsonToken; -import com.fasterxml.jackson.databind.DeserializationContext; -import com.fasterxml.jackson.databind.JsonDeserializer; -import com.fasterxml.jackson.databind.JsonNode; -import com.fasterxml.jackson.databind.annotation.JsonDeserialize; - import com.cryptape.cita.protocol.core.Response; import com.cryptape.cita.utils.Numeric; - public class AppBlock extends Response { - @Override - @JsonDeserialize(using = AppBlock.ResponseDeserialiser.class) public void setResult(Block result) { super.setResult(result); } @@ -550,99 +537,4 @@ public Transaction get() { } } - public static class ResponseDeserialiser extends JsonDeserializer { - //block meta - String blockVersion; - String blockHash; - - //block header - Long timeStamp; - String prevHash; - String number; - String stateRoot; - String transactionsRoot; - String receiptsRoot; - String quotaUsed; - - //proof tendermint - String proposer; - String proposal; - String height; - String round; - - @Override - public Block deserialize( - JsonParser jsonParser, - DeserializationContext deserializationContext) throws IOException { - if (jsonParser.getCurrentToken() != JsonToken.VALUE_NULL) { - JsonNode node = jsonParser.getCodec().readTree(jsonParser); - //block meta - blockVersion = node.get("version").asText(); - blockHash = node.get("hash").asText(); - - //block header - JsonNode headerNode = node.get("header"); - timeStamp = headerNode.get("timestamp").asLong(); - prevHash = headerNode.get("prevHash").asText(); - number = headerNode.get("number").asText(); - stateRoot = headerNode.get("stateRoot").asText(); - transactionsRoot = headerNode.get("transactionsRoot").asText(); - receiptsRoot = headerNode.get("receiptsRoot").asText(); - - //pre 0.20 gasUsed. 0.20 quotaUsed - if (headerNode.get("gasUsed") != null) { - quotaUsed = headerNode.get("gasUsed").asText(); - } else { - quotaUsed = headerNode.get("quotaUsed").asText(); - } - - proposer = headerNode.get("proposer").asText(); - - //proof tendermint - JsonNode proofNode = node.get("header").get("proof").get("Bft"); - proposal = proofNode.get("proposal").asText(); - height = proofNode.get("height").asText(); - round = proofNode.get("round").asText(); - - //proof tendermint commits - List tendermintCommits = new ArrayList<>(); - JsonNode commitsNode = node.get("header") - .get("proof").get("Bft").get("commits"); - Iterator commitsAddress = commitsNode.fieldNames(); - while (commitsAddress.hasNext()) { - String commitAddress = commitsAddress.next(); - String commit = commitsNode.get(commitAddress).asText(); - tendermintCommits.add(new TendermintCommit(commitAddress, commit)); - } - - //body transactions - List transactionObjs = new ArrayList(); - JsonNode transactionNode = node.get("body").get("transactions"); - Iterator txNodes = transactionNode.elements(); - while (txNodes.hasNext()) { - JsonNode txNode = txNodes.next(); - TransactionObject txToAdd = new TransactionObject(); - if (txNode.get("hash") == null && txNode.get("content") == null) { - txToAdd.setHash(txNode.asText()); - } else { - txToAdd.setHash(txNode.get("hash").asText()); - txToAdd.setContent(txNode.get("content").asText()); - } - transactionObjs.add(txToAdd); - } - - Tendermint tendermint = new Tendermint( - proposal, height, round, - tendermintCommits.toArray( - new TendermintCommit[tendermintCommits.size()])); - - Header header = new Header(timeStamp, prevHash, number, stateRoot, - transactionsRoot, receiptsRoot, quotaUsed, new Proof(tendermint), proposer); - Body body = new Body(transactionObjs); - return new Block(blockVersion, blockHash, header, body); - } else { - return null; // null is wrapped by Optional in above getter - } - } - } } diff --git a/core/src/main/java/com/cryptape/cita/protocol/core/methods/response/AppGetTransactionCount.java b/core/src/main/java/com/cryptape/cita/protocol/core/methods/response/AppGetTransactionCount.java index 971e6ab4..a5cc5b91 100644 --- a/core/src/main/java/com/cryptape/cita/protocol/core/methods/response/AppGetTransactionCount.java +++ b/core/src/main/java/com/cryptape/cita/protocol/core/methods/response/AppGetTransactionCount.java @@ -6,7 +6,7 @@ import com.cryptape.cita.utils.Numeric; /** - * eth_getTransactionCount. + * app_getTransactionCount. */ public class AppGetTransactionCount extends Response { public boolean isEmpty() { diff --git a/core/src/main/java/com/cryptape/cita/protocol/core/methods/response/AppGetTransactionReceipt.java b/core/src/main/java/com/cryptape/cita/protocol/core/methods/response/AppGetTransactionReceipt.java index 4afa2eb1..472b58b8 100644 --- a/core/src/main/java/com/cryptape/cita/protocol/core/methods/response/AppGetTransactionReceipt.java +++ b/core/src/main/java/com/cryptape/cita/protocol/core/methods/response/AppGetTransactionReceipt.java @@ -1,17 +1,9 @@ package com.cryptape.cita.protocol.core.methods.response; -import java.io.IOException; - -import com.cryptape.cita.protocol.ObjectMapperFactory; import com.cryptape.cita.protocol.core.Response; -import com.fasterxml.jackson.core.JsonParser; -import com.fasterxml.jackson.core.JsonToken; -import com.fasterxml.jackson.databind.DeserializationContext; -import com.fasterxml.jackson.databind.JsonDeserializer; -import com.fasterxml.jackson.databind.ObjectReader; /** - * eth_getTransactionReceipt. + * app_getTransactionReceipt. */ public class AppGetTransactionReceipt extends Response { @@ -19,19 +11,4 @@ public TransactionReceipt getTransactionReceipt() { return getResult(); } - public static class ResponseDeserialiser extends JsonDeserializer { - - private ObjectReader objectReader = ObjectMapperFactory.getObjectReader(); - - @Override - public TransactionReceipt deserialize( - JsonParser jsonParser, - DeserializationContext deserializationContext) throws IOException { - if (jsonParser.getCurrentToken() != JsonToken.VALUE_NULL) { - return objectReader.readValue(jsonParser, TransactionReceipt.class); - } else { - return null; // null is wrapped by Optional in above getter - } - } - } } diff --git a/core/src/main/java/com/cryptape/cita/protocol/core/methods/response/AppLog.java b/core/src/main/java/com/cryptape/cita/protocol/core/methods/response/AppLog.java index d00c3fbd..cc39b8de 100644 --- a/core/src/main/java/com/cryptape/cita/protocol/core/methods/response/AppLog.java +++ b/core/src/main/java/com/cryptape/cita/protocol/core/methods/response/AppLog.java @@ -1,36 +1,12 @@ package com.cryptape.cita.protocol.core.methods.response; -import java.io.IOException; -import java.util.ArrayList; -import java.util.Iterator; import java.util.List; -import com.fasterxml.jackson.core.JsonParser; -import com.fasterxml.jackson.core.JsonToken; -import com.fasterxml.jackson.databind.DeserializationContext; -import com.fasterxml.jackson.databind.JsonDeserializer; -import com.fasterxml.jackson.databind.ObjectReader; -import com.fasterxml.jackson.databind.annotation.JsonDeserialize; - -import com.cryptape.cita.protocol.ObjectMapperFactory; import com.cryptape.cita.protocol.core.Response; -/** - * Log object returned by: - *
    - *
  • getFilterChanges
  • - *
  • getFilterLogs
  • - *
  • getLogs
  • - *
- * - *

See - * docs - * for further details.

- */ public class AppLog extends Response> { @Override - @JsonDeserialize(using = LogResultDeserialiser.class) public void setResult(List result) { super.setResult(result); } @@ -101,36 +77,4 @@ public int hashCode() { } } - - public static class LogResultDeserialiser - extends JsonDeserializer> { - - private ObjectReader objectReader = ObjectMapperFactory.getObjectReader(); - - @Override - public List deserialize( - JsonParser jsonParser, - DeserializationContext deserializationContext) throws IOException { - - List logResults = new ArrayList<>(); - JsonToken nextToken = jsonParser.nextToken(); - - if (nextToken == JsonToken.START_OBJECT) { - Iterator logObjectIterator = - objectReader.readValues(jsonParser, LogObject.class); - while (logObjectIterator.hasNext()) { - logResults.add(logObjectIterator.next()); - } - } else if (nextToken == JsonToken.VALUE_STRING) { - jsonParser.getValueAsString(); - - Iterator transactionHashIterator = - objectReader.readValues(jsonParser, Hash.class); - while (transactionHashIterator.hasNext()) { - logResults.add(transactionHashIterator.next()); - } - } - return logResults; - } - } } diff --git a/core/src/main/java/com/cryptape/cita/protocol/core/methods/response/AppTransaction.java b/core/src/main/java/com/cryptape/cita/protocol/core/methods/response/AppTransaction.java index 6da8856a..91e24df2 100644 --- a/core/src/main/java/com/cryptape/cita/protocol/core/methods/response/AppTransaction.java +++ b/core/src/main/java/com/cryptape/cita/protocol/core/methods/response/AppTransaction.java @@ -1,49 +1,11 @@ package com.cryptape.cita.protocol.core.methods.response; -import java.io.IOException; - -import com.fasterxml.jackson.core.JsonParser; -import com.fasterxml.jackson.core.JsonToken; -import com.fasterxml.jackson.databind.DeserializationContext; -import com.fasterxml.jackson.databind.JsonDeserializer; -import com.fasterxml.jackson.databind.ObjectReader; - -import com.cryptape.cita.protocol.ObjectMapperFactory; import com.cryptape.cita.protocol.core.Response; -/** - * Transaction object returned by: - *
    - *
  • eth_getTransactionByHash
  • - *
  • eth_getTransactionByBlockHashAndIndex
  • - *
  • eth_getTransactionByBlockNumberAndIndex
  • - *
- * - *

This differs slightly from the request {@link AppSendTransaction} Transaction object.

- * - *

See - * docs - * for further details.

- */ public class AppTransaction extends Response { public Transaction getTransaction() { return getResult(); } - public static class ResponseDeserialiser extends JsonDeserializer { - - private ObjectReader objectReader = ObjectMapperFactory.getObjectReader(); - - @Override - public Transaction deserialize( - JsonParser jsonParser, - DeserializationContext deserializationContext) throws IOException { - if (jsonParser.getCurrentToken() != JsonToken.VALUE_NULL) { - return objectReader.readValue(jsonParser, Transaction.class); - } else { - return null; // null is wrapped by Optional in above getter - } - } - } } diff --git a/tests/src/main/java/com/cryptape/cita/tests/InterfaceTest.java b/tests/src/main/java/com/cryptape/cita/tests/InterfaceTest.java index f69279ba..15d455cd 100644 --- a/tests/src/main/java/com/cryptape/cita/tests/InterfaceTest.java +++ b/tests/src/main/java/com/cryptape/cita/tests/InterfaceTest.java @@ -1,15 +1,14 @@ package com.cryptape.cita.tests; +import java.io.IOException; import java.math.BigInteger; import java.util.Arrays; -import java.util.Collections; import com.cryptape.cita.abi.FunctionEncoder; import com.cryptape.cita.abi.TypeReference; import com.cryptape.cita.abi.datatypes.Address; import com.cryptape.cita.abi.datatypes.Function; import com.cryptape.cita.abi.datatypes.Uint; -import com.cryptape.cita.abi.datatypes.generated.Uint256; import com.cryptape.cita.crypto.Credentials; import com.cryptape.cita.protocol.CITAj; import com.cryptape.cita.protocol.core.DefaultBlockParameter; @@ -30,6 +29,7 @@ import com.cryptape.cita.protocol.core.methods.response.AppTransaction; import com.cryptape.cita.protocol.core.methods.response.NetPeerCount; import com.cryptape.cita.protocol.core.methods.response.TransactionReceipt; +import com.google.gson.Gson; public class InterfaceTest { @@ -39,7 +39,6 @@ public class InterfaceTest { private static String value; private static String privateKey; private static long quotaToDeploy; - private static String validTransactionHash; private static Transaction.CryptoTx cryptoTx; @@ -57,47 +56,24 @@ public class InterfaceTest { public static void main(String[] args) throws Exception { + testGetBlockByNumber(BigInteger.valueOf(0)); - System.out.println("======================================"); - System.out.println("*** 0. getbalance ***"); testGetBalance(); - System.out.println("======================================"); - System.out.println("*** 1. getMetaData ***"); testMetaData(); - System.out.println("======================================"); - System.out.println("*** 2. net_peerCount ***"); testNetPeerCount(); - System.out.println("======================================"); - System.out.println("*** 3. blockNumber ***"); BigInteger validBlockNumber = testBlockNumber(); - System.out.println(validBlockNumber); - System.out.println(validBlockNumber.toString(16)); - System.out.println(DefaultBlockParameter.valueOf(validBlockNumber).getValue()); + System.out.println(validBlockNumber.toString(10)); - System.out.println("======================================"); - System.out.println("*** 4. getBlockByNumber ***"); - boolean returnFullTransactions = true; String blockByNumberHash = testAppGetBlockByNumber( - validBlockNumber, returnFullTransactions); - String blockHash = ""; - if (blockByNumberHash != null) { - blockHash = blockByNumberHash; - } else { - System.out.println("Failed to get block by number: " + validBlockNumber); - System.exit(1); - } + validBlockNumber, true); - System.out.println("======================================"); - System.out.println("*** 5. getBlockByHash ***"); - testAppGetBlockByHash(blockHash, returnFullTransactions); + testAppGetBlockByHash(blockByNumberHash, true); //because unsigned transaction is not supported in cita, there is no method sendTransaction. - System.out.println("======================================"); - System.out.println("*** 6. sendRawTransaction ***"); String code = "6060604052341561000f57600080fd5b600160a060020" + "a033316600090815260208190526040902061271090556101" + "df8061003b6000396000f3006060604052600436106100565" @@ -127,33 +103,20 @@ public static void main(String[] args) throws Exception { Transaction rtx = Transaction.createContractTransaction( nonce, quotaToDeploy, validUtil.longValue(), version, chainId, value, code); - String signedTx = rtx.sign(privateKey, cryptoTx, false); - String optionTxHash = testAppSendRawTransaction(signedTx); - - if (optionTxHash != null) { - validTransactionHash = optionTxHash; - } else { - System.out.println("Failed to get deployment tx hash, maybe it failed to be validated"); - System.exit(1); - } + String signedTransaction = rtx.sign(privateKey, cryptoTx, false); + String transactionHash = testAppSendRawTransaction(signedTransaction); System.out.println("waiting for tx into chain ..."); Thread.sleep(8000); - System.out.println("======================================"); - System.out.println("*** 7. getTransactionByHash ***"); - testAppGetTransactionByHash(validTransactionHash); + testAppGetTransactionByHash(transactionHash); - System.out.println("======================================"); - System.out.println("*** 8. getTransactionCount ***"); Credentials credentials = Credentials.create(privateKey); String validAccount = credentials.getAddress(); testAppGetTransactionCount(validAccount, DefaultBlockParameterName.PENDING); - System.out.println("======================================"); - System.out.println("*** 9. getTransactionReceipt ***"); String validContractAddress = ""; - String contractAddr = testAppGetTransactionReceipt(validTransactionHash); + String contractAddr = testAppGetTransactionReceipt(transactionHash); if (contractAddr != null) { validContractAddress = contractAddr; } else { @@ -161,26 +124,26 @@ public static void main(String[] args) throws Exception { System.exit(1); } - System.out.println("======================================"); - System.out.println("*** 10. getCode ***"); testAppGetCode(validContractAddress, DefaultBlockParameterName.PENDING); - System.out.println("======================================"); - System.out.println("*** 11. appCall ***"); - String fromAddr = Credentials.create(privateKey).getAddress(); + String fromAddress = Credentials.create(privateKey).getAddress(); Function getBalanceFunc = new Function( "getBalance", - Arrays.asList(new Address(fromAddr)), + Arrays.asList(new Address(fromAddress)), Arrays.asList(new TypeReference() { }) ); String funcCallData = FunctionEncoder.encode(getBalanceFunc); - testAppCall(fromAddr, validContractAddress, funcCallData, DefaultBlockParameterName.PENDING); + testAppCall(fromAddress, validContractAddress, funcCallData, DefaultBlockParameterName.PENDING); } - //0. getBalance + private static void testGetBlockByNumber(BigInteger number) throws IOException { + AppBlock.Block block = service.appGetBlockByNumber(DefaultBlockParameter.valueOf(number), true).send().getBlock(); + System.out.println("block " + number.toString() + " is: " + new Gson().toJson(block)); + } + private static void testGetBalance() throws Exception { Credentials c = Credentials.create(privateKey); String addr = c.getAddress(); @@ -190,54 +153,20 @@ private static void testGetBalance() throws Exception { System.out.println("the result is null"); } else { BigInteger balance = appGetbalance.getBalance(); - System.out.println("Balance for addr " + addr + "is " + balance); + System.out.println("Balance for address " + addr + "is " + balance); } } - //1. getMetaData private static void testMetaData() throws Exception { AppMetaData appMetaData = service.appMetaData(DefaultBlockParameterName.PENDING).send(); - if (appMetaData == null) { - System.out.println("the result is null"); - } else { - System.out.println("Version: " - + appMetaData.getAppMetaDataResult().getVersion()); - version = appMetaData.getAppMetaDataResult().getVersion(); - System.out.println("TokenName: " - + appMetaData.getAppMetaDataResult().getTokenName()); - System.out.println("TokenSymbol: " - + appMetaData.getAppMetaDataResult().getTokenSymbol()); - System.out.println("TokenAvator: " - + appMetaData.getAppMetaDataResult().getTokenAvatar()); - System.out.println("ChainName: " - + appMetaData.getAppMetaDataResult().getChainName()); - System.out.println("Genesis TS: " - + appMetaData.getAppMetaDataResult().getGenesisTimestamp()); - System.out.println("Operator: " - + appMetaData.getAppMetaDataResult().getOperator()); - System.out.println("Website: " - + appMetaData.getAppMetaDataResult().getWebsite()); - System.out.println("Block Interval: " - + appMetaData.getAppMetaDataResult().getBlockInterval()); - System.out.println("Chain Id: " - + appMetaData.getAppMetaDataResult().getChainId()); - System.out.println("Validators: "); - Arrays.asList(appMetaData.getAppMetaDataResult().getValidators()) - .forEach(x -> System.out.println("Address: " + x.toString())); - System.out.println("Economical Model: " - + appMetaData.getAppMetaDataResult().getEconomicalModel()); - System.out.println("Chain Id V1: " - + appMetaData.getAppMetaDataResult().getChainIdV1()); - } + System.out.println("AppMetaData: " + new Gson().toJson(appMetaData)); } - //1. net_peerCount private static void testNetPeerCount() throws Exception { NetPeerCount netPeerCount = service.netPeerCount().send(); System.out.println("net_peerCount:" + netPeerCount.getQuantity()); } - //2. blockNumber private static BigInteger testBlockNumber() throws Exception { AppBlockNumber appBlockNumber = service.appBlockNumber().send(); @@ -253,7 +182,6 @@ private static BigInteger testBlockNumber() throws Exception { return validBlockNumber; } - //3. getBlockByNumber private static String testAppGetBlockByNumber( BigInteger validBlockNumber, boolean isfullTranobj) throws Exception { @@ -265,12 +193,11 @@ private static String testAppGetBlockByNumber( return null; } else { AppBlock.Block block = appBlock.getBlock(); - printBlock(block); + System.out.println("Block: " + new Gson().toJson(block)); return block.getHash(); } } - //4. cita_getBlockByHash private static void testAppGetBlockByHash( String validBlockHash, boolean isfullTran) throws Exception { @@ -281,12 +208,11 @@ private static void testAppGetBlockByHash( System.out.println("the result is null"); } else { AppBlock.Block block = appBlock.getBlock(); - printBlock(block); + System.out.println("Block: " + new Gson().toJson(block)); } } - //5. sendRawTransaction private static String testAppSendRawTransaction( String rawData) throws Exception { AppSendTransaction appSendTx = service @@ -305,7 +231,6 @@ private static String testAppSendRawTransaction( } - //6. getTransactionByHash private static void testAppGetTransactionByHash( String validTransactionHash) throws Exception { AppTransaction appTransaction = service.appGetTransactionByHash( @@ -316,16 +241,11 @@ private static void testAppGetTransactionByHash( } else { com.cryptape.cita.protocol.core.methods.response.Transaction transaction = appTransaction.getTransaction(); - System.out.println("hash(Transaction):" + transaction.getHash()); - System.out.println("content:" + transaction.getContent()); - System.out.println("blockNumber(dec):" + transaction.getBlockNumber()); - System.out.println("blockHash:" + transaction.getBlockHash()); - System.out.println("index:" + transaction.getIndex()); + System.out.println("Transaction: " + new Gson().toJson(transaction)); } } - //7. getTransactionCount private static void testAppGetTransactionCount( String validAccount, DefaultBlockParameter param) throws Exception { AppGetTransactionCount appGetTransactionCount = service.appGetTransactionCount( @@ -339,7 +259,6 @@ private static void testAppGetTransactionCount( } } - //8. getTransactionReceipt private static String testAppGetTransactionReceipt( String validTransactionHash) throws Exception { AppGetTransactionReceipt appGetTransactionReceipt = service.appGetTransactionReceipt( @@ -376,7 +295,6 @@ private static void printTransactionReceiptInfo( } } - //9. eth_getCode private static void testAppGetCode( String validContractAddress, DefaultBlockParameter param) throws Exception { @@ -386,71 +304,19 @@ private static void testAppGetCode( if (appGetCode.isEmpty()) { System.out.println("the result is null"); } else { - System.out.println("contractcode:" + appGetCode.getCode()); + System.out.println("contract code:" + appGetCode.getCode()); } } - public Function totalSupply() { - return new Function( - "get", - Collections.emptyList(), - Collections.singletonList(new TypeReference() { - })); - } - - //10. call private static void testAppCall( - String fromaddress, String contractAddress, String encodedFunction, + String fromAddress, String contractAddress, String encodedFunction, DefaultBlockParameter param) throws Exception { AppCall appCall = service.appCall( - new Call(fromaddress, contractAddress, encodedFunction), + new Call(fromAddress, contractAddress, encodedFunction), param).send(); System.out.println("call result:" + appCall.getValue()); } - private static void printBlock(AppBlock.Block block) { - System.out.println("hash(blockhash):" - + block.getHash()); - System.out.println("version:" - + block.getVersion()); - System.out.println("header.timestamp:" - + block.getHeader().getTimestamp()); - System.out.println("header.prevHash:" - + block.getHeader().getPrevHash()); - System.out.println("header.number(hex):" - + block.getHeader().getNumber()); - System.out.println("header.number(dec):" - + block.getHeader().getNumberDec()); - System.out.println("header.stateRoot:" - + block.getHeader().getStateRoot()); - System.out.println("header.transactionsRoot:" - + block.getHeader().getTransactionsRoot()); - System.out.println("header.receiptsRoot:" - + block.getHeader().getReceiptsRoot()); - System.out.println("header.gasUsed:" - + block.getHeader().getGasUsed()); - System.out.println("header.proof.proposal:" - + block.getHeader().getProof().getTendermint().getProposal()); - System.out.println("header.proof.height:" - + block.getHeader().getProof().getTendermint().getHeight()); - System.out.println("header.proof.round:" - + block.getHeader().getProof().getTendermint().getRound()); - - if (!block.getBody().getTransactions().isEmpty()) { - System.out.println("number of transaction:" - + block.getBody().getTransactions().size()); - - for (int i = 0; i < block.getBody().getTransactions().size(); i++) { - com.cryptape.cita.protocol.core.methods.response.Transaction tx = - (com.cryptape.cita.protocol.core.methods.response.Transaction) - block.getBody().getTransactions().get(i).get(); - System.out.println("body.transactions.tranhash:" + tx.getHash()); - System.out.println("body.transactions.content:" + tx.getContent()); - } - } else { - System.out.println("the block transactions is null"); - } - } } From b2933e11bc195d6a82b6c66961213be0b83ada97 Mon Sep 17 00:00:00 2001 From: duanyytop Date: Thu, 28 Mar 2019 18:24:15 +0800 Subject: [PATCH 2/3] chore: update rpc request test case --- .../core/methods/response/AppLog.java | 44 +++++++ .../cita/protocol/core/ResponseTest.java | 107 +----------------- .../cryptape/cita/tests/InterfaceTest.java | 2 +- 3 files changed, 46 insertions(+), 107 deletions(-) diff --git a/core/src/main/java/com/cryptape/cita/protocol/core/methods/response/AppLog.java b/core/src/main/java/com/cryptape/cita/protocol/core/methods/response/AppLog.java index cc39b8de..044e2cf8 100644 --- a/core/src/main/java/com/cryptape/cita/protocol/core/methods/response/AppLog.java +++ b/core/src/main/java/com/cryptape/cita/protocol/core/methods/response/AppLog.java @@ -1,12 +1,24 @@ package com.cryptape.cita.protocol.core.methods.response; +import java.io.IOException; +import java.util.ArrayList; +import java.util.Iterator; import java.util.List; +import com.fasterxml.jackson.core.JsonParser; +import com.fasterxml.jackson.core.JsonToken; +import com.fasterxml.jackson.databind.DeserializationContext; +import com.fasterxml.jackson.databind.JsonDeserializer; +import com.fasterxml.jackson.databind.ObjectReader; +import com.fasterxml.jackson.databind.annotation.JsonDeserialize; + +import com.cryptape.cita.protocol.ObjectMapperFactory; import com.cryptape.cita.protocol.core.Response; public class AppLog extends Response> { @Override + @JsonDeserialize(using = LogResultDeserialiser.class) public void setResult(List result) { super.setResult(result); } @@ -77,4 +89,36 @@ public int hashCode() { } } + + public static class LogResultDeserialiser + extends JsonDeserializer> { + + private ObjectReader objectReader = ObjectMapperFactory.getObjectReader(); + + @Override + public List deserialize( + JsonParser jsonParser, + DeserializationContext deserializationContext) throws IOException { + + List logResults = new ArrayList<>(); + JsonToken nextToken = jsonParser.nextToken(); + + if (nextToken == JsonToken.START_OBJECT) { + Iterator logObjectIterator = + objectReader.readValues(jsonParser, LogObject.class); + while (logObjectIterator.hasNext()) { + logResults.add(logObjectIterator.next()); + } + } else if (nextToken == JsonToken.VALUE_STRING) { + jsonParser.getValueAsString(); + + Iterator transactionHashIterator = + objectReader.readValues(jsonParser, Hash.class); + while (transactionHashIterator.hasNext()) { + logResults.add(transactionHashIterator.next()); + } + } + return logResults; + } + } } diff --git a/core/src/test/java/com/cryptape/cita/protocol/core/ResponseTest.java b/core/src/test/java/com/cryptape/cita/protocol/core/ResponseTest.java index 9a28a353..212bcbe2 100644 --- a/core/src/test/java/com/cryptape/cita/protocol/core/ResponseTest.java +++ b/core/src/test/java/com/cryptape/cita/protocol/core/ResponseTest.java @@ -250,110 +250,6 @@ public void testEthCall() { assertThat(appCall.getValue(), is("0x")); } - @Test - public void testEthBlockTransactionHashes() { - //CHECKSTYLE:OFF - buildResponse( - "{\n" - + "\"jsonrpc\":\"2.0\",\n" - + "\"id\":1,\n" - + "\"result\":{\n" - + " \"version\":0," - + " \"hash\":\"0xda9e8497221e9d18131292f8b459d62e03c882be4666d084c67b8dcebcce91d1\",\n" - + " \"header\":{\n" - + " \"timestamp\":1533101297835,\n" - + " \"prevHash\":\"0x4391078f4c03c028178e8f1a9f25392a634b098d7737601f1679a8b197fafcf9\"," - + " \"number\":\"0x3ff59\"," - + " \"stateRoot\":\"0x60fb67e5ca3868d292ea44e2c28bb16d83d25793ed629b28f4c8d18de8742a72\"," - + " \"transactionsRoot\":\"0x62d54aa5b82a5813c87a3a4d1d2c3a02e6c88f037c8fb2461e5116e7a9dc2149\"," - + " \"receiptsRoot\":\"0x918334ef71e85bed370065d1f2758d1eb3f089f7e5323a78a633de7a5f07f371\"," - + " \"gasUsed\":\"0x132bd\"," - + " \"proof\":" - + " {\"Bft\":{" - + " \"proposal\":\"0xe1b9bba13cb64a920c04f3abc2ea0a98d2db4fb65d233df3afc31c5321bb6054\"," - + " \"height\":261976," - + " \"round\":0," - + " \"commits\":{" - + " \"0x486bb688c8d29056bd7f87c26733048b0a6abda6\":\"0x0fc60edaff5f00329e088750119e92af8940e9612d1dbf3ce4158e721faaff592ab43f4bc3c24718631aa4140f8412cefb9086416ba2c280fa85ec83b511ed5f00\"," - + " \"0x31042d4f7662cddf8ded5229db3c5e7302875e10\":\"0x68e3701cec53f96e792ecb02eb390dad074eb26b0d5472be8804929e016cc99d647b9c40e30942270f1fed91f3fc14479c47de603a5de99735b3122b5d08e2c601\"," - + " \"0xee01b9ba97671e8a1891e85b206b499f106822a1\":\"0x34272376d15b8e0658efa8cce056f59d5054b5863a9945c5e8e232c65f2d434476833899912a871e58374c08da78e8279dc02c877221f6a880134ed050bae44700\"," - + " \"0x71b028e49c6f41aaa74932d703c707ecca6d732e\":\"0x7ed41b1ccf137a77eade8bbd4e348534dc26c45c7d8a547adc7286746497f6bf3587f886c87e97dbfadd8e2698827c53123a3fbfddb682c887509342ae030dec01\"" - + " }" - + " }" - + " }," - + " \"proposer\":\"0xee01b9ba97671e8a1891e85b206b499f106822a1\"" - + " }," - + " \"body\":{" - + " \"transactions\":[" - + " {" - + " \"hash\":\"0xab64a7be5f38ab8061419472402c52c9a26f5989b26da5eec4d59d7aa68348e1\"," - + " \"content\":\"0x0aad010a2839663864396337633336616632336561656230323032363938333135656163323334653334373039120f65333031643366313762346566393518c0843d20a6ff0f2a44a9059cbb000000000000000000000000bac68e5cb986ead0253e0632da1131a0a96efa1800000000000000000000000000000000000000000000000000000000000003e83220000000000000000000000000000000000000000000000000000000000000000038011241e1e76fe6f033db9527f0714ba81b4af1f26b9d70210acbf3cebcb916c2f4c7ae70c3fe80a2032d748caa0b90b70159263880ea6b230912f5bb5c62b234ed67a401\"" - + " }," - + " {" - + " \"hash\":\"0xcd7fc94a452d78b0041abac36de489c19432d3c208e795099ac13a6327bb4bd8\"," - + " \"content\":\"0x0aad010a2839663864396337633336616632336561656230323032363938333135656163323334653334373039120f65316239383236653737326132306518c0843d20a7ff0f2a44a9059cbb000000000000000000000000bac68e5cb986ead0253e0632da1131a0a96efa1800000000000000000000000000000000000000000000000000000000000003e83220000000000000000000000000000000000000000000000000000000000000000038011241d5c91c9e262c57fd79d6979f8b9205d7d49ed8918edaf8eb8b8e3bf5447b7d6745e57ebcb741486064758c0058750569c4e6a759de293563e9fa156172abcc3500\"" - + " }" - + " ]" - + " }" - + " }" - + "}"); - - - AppBlock.TendermintCommit[] tendermintCommits = { - new AppBlock.TendermintCommit( - "0x486bb688c8d29056bd7f87c26733048b0a6abda6", - "0x0fc60edaff5f00329e088750119e92af8940e9612d1dbf3ce4158e721faaff592ab43f4bc3c24718631aa4140f8412cefb9086416ba2c280fa85ec83b511ed5f00"), - new AppBlock.TendermintCommit( - "0x31042d4f7662cddf8ded5229db3c5e7302875e10", - "0x68e3701cec53f96e792ecb02eb390dad074eb26b0d5472be8804929e016cc99d647b9c40e30942270f1fed91f3fc14479c47de603a5de99735b3122b5d08e2c601"), - new AppBlock.TendermintCommit( - "0xee01b9ba97671e8a1891e85b206b499f106822a1", - "0x34272376d15b8e0658efa8cce056f59d5054b5863a9945c5e8e232c65f2d434476833899912a871e58374c08da78e8279dc02c877221f6a880134ed050bae44700"), - new AppBlock.TendermintCommit( - "0x71b028e49c6f41aaa74932d703c707ecca6d732e", - "0x7ed41b1ccf137a77eade8bbd4e348534dc26c45c7d8a547adc7286746497f6bf3587f886c87e97dbfadd8e2698827c53123a3fbfddb682c887509342ae030dec01"), - }; - AppBlock.Tendermint tendermint = new AppBlock.Tendermint( - "0xe1b9bba13cb64a920c04f3abc2ea0a98d2db4fb65d233df3afc31c5321bb6054", - "261976", "0",tendermintCommits); - - AppBlock.Header header = new AppBlock.Header( - 1533101297835L, - "0x4391078f4c03c028178e8f1a9f25392a634b098d7737601f1679a8b197fafcf9", - "0x3ff59", - "0x60fb67e5ca3868d292ea44e2c28bb16d83d25793ed629b28f4c8d18de8742a72", - "0x62d54aa5b82a5813c87a3a4d1d2c3a02e6c88f037c8fb2461e5116e7a9dc2149", - "0x918334ef71e85bed370065d1f2758d1eb3f089f7e5323a78a633de7a5f07f371", - "0x132bd", - new AppBlock.Proof(tendermint), - "0xee01b9ba97671e8a1891e85b206b499f106822a1" - ); - - List transactionObjects = new ArrayList<>(); - AppBlock.TransactionObject txObj1 = new AppBlock.TransactionObject(); - txObj1.setHash("0xab64a7be5f38ab8061419472402c52c9a26f5989b26da5eec4d59d7aa68348e1"); - txObj1.setContent("0x0aad010a2839663864396337633336616632336561656230323032363938333135656163323334653334373039120f65333031643366313762346566393518c0843d20a6ff0f2a44a9059cbb000000000000000000000000bac68e5cb986ead0253e0632da1131a0a96efa1800000000000000000000000000000000000000000000000000000000000003e83220000000000000000000000000000000000000000000000000000000000000000038011241e1e76fe6f033db9527f0714ba81b4af1f26b9d70210acbf3cebcb916c2f4c7ae70c3fe80a2032d748caa0b90b70159263880ea6b230912f5bb5c62b234ed67a401"); - - AppBlock.TransactionObject txObj2 = new AppBlock.TransactionObject(); - txObj2.setHash("0xcd7fc94a452d78b0041abac36de489c19432d3c208e795099ac13a6327bb4bd8"); - txObj2.setContent("0x0aad010a2839663864396337633336616632336561656230323032363938333135656163323334653334373039120f65316239383236653737326132306518c0843d20a7ff0f2a44a9059cbb000000000000000000000000bac68e5cb986ead0253e0632da1131a0a96efa1800000000000000000000000000000000000000000000000000000000000003e83220000000000000000000000000000000000000000000000000000000000000000038011241d5c91c9e262c57fd79d6979f8b9205d7d49ed8918edaf8eb8b8e3bf5447b7d6745e57ebcb741486064758c0058750569c4e6a759de293563e9fa156172abcc3500"); - - transactionObjects.add(txObj1); - transactionObjects.add(txObj2); - AppBlock.Body body = new AppBlock.Body(transactionObjects); - - String version = "0"; - String hash = "0xda9e8497221e9d18131292f8b459d62e03c882be4666d084c67b8dcebcce91d1"; - AppBlock.Block block = new AppBlock.Block( - version, - hash, - header, - body - ); - //CHECKSTYLE:ON - AppBlock appBlock = deserialiseResponse(AppBlock.class); - assertThat(appBlock.getBlock(), equalTo(block)); - } @Ignore //no support for parity @Test @@ -423,8 +319,7 @@ public void testEthBlockFullTransactionsParity() { assertThat(appBlock.getBlock(), equalTo(block)); } - // Remove once Geth & Parity return the same v value in transactions - @Ignore //no support for geth + @Ignore @Test public void testEthBlockFullTransactionsGeth() { //CHECKSTYLE:OFF diff --git a/tests/src/main/java/com/cryptape/cita/tests/InterfaceTest.java b/tests/src/main/java/com/cryptape/cita/tests/InterfaceTest.java index 15d455cd..71862668 100644 --- a/tests/src/main/java/com/cryptape/cita/tests/InterfaceTest.java +++ b/tests/src/main/java/com/cryptape/cita/tests/InterfaceTest.java @@ -56,7 +56,7 @@ public class InterfaceTest { public static void main(String[] args) throws Exception { - testGetBlockByNumber(BigInteger.valueOf(0)); + testGetBlockByNumber(BigInteger.valueOf(1)); testGetBalance(); From d3deb086d132587fb1fad21e035ee6a3adff50c5 Mon Sep 17 00:00:00 2001 From: duanyytop Date: Thu, 28 Mar 2019 18:31:31 +0800 Subject: [PATCH 3/3] chore: update version to v0.22.1 --- CHANGELOG.md | 7 +++++++ README.md | 8 ++++---- build.gradle | 4 ++-- 3 files changed, 13 insertions(+), 6 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index da542562..69f1c402 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,12 @@ All notable changes to this project will be documented in this file. +# [v0.22.1](https://github.com/cryptape/cita-sdk-java/compare/v0.22...v0.22.1) (2019-03-29) + +### Feature + +* update RPC request cases +* fix block response parse bug + # [v0.22](https://github.com/cryptape/cita-sdk-java/compare/v0.21...v0.22) (2019-03-28) ### BREAKING CHANGES diff --git a/README.md b/README.md index 465e86f7..eb406bfa 100644 --- a/README.md +++ b/README.md @@ -27,12 +27,12 @@ maven com.cryptape.cita core - 0.21 + 0.22.1 ``` Gradle ``` -compile 'com.cryptape.cita:core:0.21' +compile 'com.cryptape.cita:core:0.22.1' ``` Install manually @@ -161,12 +161,12 @@ Gradle 4.3 com.cryptape.cita core - 0.21 + 0.22.1 ``` Gradle ``` -compile 'com.cryptape.cita:core:0.21' +compile 'com.cryptape.cita:core:0.22.1' ``` 手动安装 diff --git a/build.gradle b/build.gradle index a8738600..94da240c 100644 --- a/build.gradle +++ b/build.gradle @@ -46,7 +46,7 @@ allprojects { targetCompatibility = 1.8 group 'com.cryptape.cita' - version '0.22' + version '0.22.1' apply plugin: 'java' apply plugin: 'jacoco' @@ -142,7 +142,7 @@ configure(subprojects.findAll {it.name != 'integration-tests'}) { publications { mavenJava(MavenPublication) { groupId 'com.cryptape.cita' - version '0.22' + version '0.22.1' from components.java artifact sourcesJar {