Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

API: Transaction result included in getTransaction #97

Merged
merged 5 commits into from
Dec 11, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions src/main/java/org/semux/api/v2/SemuxApiImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@
import org.semux.core.PendingManager;
import org.semux.core.SyncManager;
import org.semux.core.Transaction;
import org.semux.core.TransactionResult;
import org.semux.core.TransactionType;
import org.semux.core.exception.WalletLockedException;
import org.semux.core.state.Account;
Expand Down Expand Up @@ -533,9 +534,11 @@ public Response getTransaction(String hash) {
return badRequest(resp, "The request transaction was not found");
}

resp.setResult(TypeFactory.transactionType(
TransactionResult result = kernel.getBlockchain().getTransactionResult(hashBytes);

resp.setResult(TypeFactory.fullTransactionType(
kernel.getBlockchain().getTransactionBlockNumber(transaction.getHash()),
transaction));
transaction, result));

return success(resp);
}
Expand Down
55 changes: 47 additions & 8 deletions src/main/java/org/semux/api/v2/TypeFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,26 @@
import java.util.Set;
import java.util.stream.Collectors;

import org.ethereum.vm.LogInfo;
import org.semux.Kernel;
import org.semux.api.v2.model.AccountType;
import org.semux.api.v2.model.AccountVoteType;
import org.semux.api.v2.model.BasicTransactionType;
import org.semux.api.v2.model.BlockType;
import org.semux.api.v2.model.DelegateType;
import org.semux.api.v2.model.FullTransactionType;
import org.semux.api.v2.model.InfoType;
import org.semux.api.v2.model.LogInfoType;
import org.semux.api.v2.model.PeerType;
import org.semux.api.v2.model.PendingTransactionType;
import org.semux.api.v2.model.TransactionLimitsType;
import org.semux.api.v2.model.TransactionType;
import org.semux.api.v2.model.TransactionResultType;
import org.semux.core.Amount;
import org.semux.core.Block;
import org.semux.core.Blockchain;
import org.semux.core.BlockchainImpl;
import org.semux.core.Transaction;
import org.semux.core.TransactionResult;
import org.semux.core.state.Account;
import org.semux.core.state.Delegate;
import org.semux.crypto.Hex;
Expand Down Expand Up @@ -136,18 +141,50 @@ public static TransactionLimitsType transactionLimitsType(Kernel kernel,
transactionType.equals(DELEGATE) ? kernel.getConfig().minDelegateBurnAmount() : null));
}

public static TransactionType transactionType(Long blockNumber, Transaction tx) {
return new TransactionType()
.blockNumber(String.valueOf(blockNumber))
.hash(Hex.encode0x(tx.getHash()))
.type(TransactionType.TypeEnum.fromValue(tx.getType().name()))
public static BasicTransactionType transactionType(Long blockNumber, Transaction tx) {
BasicTransactionType txType = new BasicTransactionType();
populateTx(txType, tx);
txType.blockNumber(String.valueOf(blockNumber));
return txType;
}

public static FullTransactionType fullTransactionType(Long blockNumber, Transaction tx, TransactionResult result) {
FullTransactionType txType = new FullTransactionType();
populateTx(txType, tx);
txType.blockNumber(String.valueOf(blockNumber));
txType.transactionResult(transactionResultType(result));
return txType;
}

private static void populateTx(BasicTransactionType txType, Transaction tx) {

txType.hash(Hex.encode0x(tx.getHash()))
.type(BasicTransactionType.TypeEnum.fromValue(tx.getType().name()))
.from(Hex.encode0x(tx.getFrom()))
.to(Hex.encode0x(tx.getTo()))
.value(encodeAmount(tx.getValue()))
.fee(encodeAmount(tx.getFee()))
.nonce(String.valueOf(tx.getNonce()))
.timestamp(String.valueOf(tx.getTimestamp()))
.data(Hex.encode0x(tx.getData()));
.data(Hex.encode0x(tx.getData()))
.gas(String.valueOf(tx.getGas()))
.gasPrice(String.valueOf(tx.getGasPrice()));
}

private static TransactionResultType transactionResultType(TransactionResult result) {
return new TransactionResultType()
.logs(result.getLogs().stream().map(TypeFactory::logInfoType).collect(Collectors.toList()))
.gasUsed(String.valueOf(result.getGasUsed()))
.code(result.getCode().name())
.returnData(Hex.encode0x(result.getReturnData()));
}

private static LogInfoType logInfoType(LogInfo log) {
return new LogInfoType()
.address(Hex.encode0x(log.getAddress()))
.data(Hex.encode0x(log.getData()))
.topics(log.getTopics().stream().map(topic -> Hex.encode0x(topic.getData()))
.collect(Collectors.toList()));
}

public static PendingTransactionType pendingTransactionType(Transaction tx) {
Expand All @@ -160,7 +197,9 @@ public static PendingTransactionType pendingTransactionType(Transaction tx) {
.fee(encodeAmount(tx.getFee()))
.nonce(String.valueOf(tx.getNonce()))
.timestamp(String.valueOf(tx.getTimestamp()))
.data(Hex.encode0x(tx.getData()));
.data(Hex.encode0x(tx.getData()))
.gas(String.valueOf(tx.getGas()))
.gasPrice(String.valueOf(tx.getGasPrice()));
}

public static String encodeAmount(Amount a) {
Expand Down
97 changes: 93 additions & 4 deletions src/main/resources/org/semux/api/swagger/v2.2.0.json
Original file line number Diff line number Diff line change
Expand Up @@ -1812,7 +1812,7 @@
"transactions" : {
"type" : "array",
"items" : {
"$ref" : "#/definitions/TransactionType"
"$ref" : "#/definitions/BasicTransactionType"
}
}
}
Expand Down Expand Up @@ -1934,7 +1934,7 @@
"result" : {
"type" : "array",
"items" : {
"$ref" : "#/definitions/TransactionType"
"$ref" : "#/definitions/BasicTransactionType"
}
}
}
Expand Down Expand Up @@ -2177,7 +2177,7 @@
{
"properties" : {
"result" : {
"$ref" : "#/definitions/TransactionType"
"$ref" : "#/definitions/FullTransactionType"
}
}
}
Expand Down Expand Up @@ -2395,8 +2395,9 @@
}
}
},
"TransactionType" : {
"BasicTransactionType" : {
"type" : "object",
"discriminator": "transactionType",
"properties" : {
"blockNumber" : {
"type" : "string",
Expand Down Expand Up @@ -2448,9 +2449,38 @@
"description" : "Transaction data encoded in hexadecimal string",
"type" : "string",
"pattern" : "^(0x)?[0-9a-fA-F]*$"
},
"gas" : {
"description" : "Gas supplied on call",
"type" : "string",
"format" : "int64",
"pattern" : "^\\d+$"
},
"gasPrice" : {
"description" : "Price per gas used",
"type" : "string",
"format" : "int64",
"pattern" : "^\\d+$"
}
}
},
"FullTransactionType": {
"type": "object",
"allOf": [
{
"$ref": "#/definitions/BasicTransactionType"
},
{
"properties": {
"transactionResult": {
"description": "Transaction results",
"type": "object",
"$ref": "#/definitions/TransactionResultType"
}
}
}
]
},
"PendingTransactionType" : {
"type" : "object",
"properties" : {
Expand Down Expand Up @@ -2499,6 +2529,65 @@
"description" : "Transaction data encoded in hexadecimal string",
"type" : "string",
"pattern" : "^(0x)?[0-9a-fA-F]*$"
},
"gas" : {
"description" : "Gas supplied on call",
"type" : "string",
"format" : "int64",
"pattern" : "^\\d+$"
},
"gasPrice" : {
"description" : "Price per gas used",
"type" : "string",
"format" : "int64",
"pattern" : "^\\d+$"
}
}
},
"TransactionResultType" : {
"type" : "object",
"properties" : {
"logs": {
"type": "array",
"items": {
"$ref": "#/definitions/LogInfoType"
}
},
"returnData": {
"description": "Return data encoded in hexadecimal string",
"type": "string",
"pattern": "^(0x)?[0-9a-fA-F]*$"
},
"gasUsed": {
"type": "string",
"format": "int64",
"pattern": "^\\d+$"
},
"code": {
"description": "The status of the transaction",
"type": "string"
}
}
},
"LogInfoType": {
"type": "object",
"properties": {
"address": {
"description": "Address encoded in hexadecimal string",
"type": "string",
"pattern": "^(0x)?[0-9a-fA-F]{64}$"
},
"data": {
"description": "Log info data encoded in hexadecimal string",
"type": "string",
"pattern": "^(0x)?[0-9a-fA-F]*$"
},
"topics": {
"description": "Topics encoded in hexadecimal string",
"type" : "array",
"items" : {
"type" : "string"
}
}
}
},
Expand Down
4 changes: 2 additions & 2 deletions src/test/java/org/semux/api/v2/SemuxApiTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@
import org.semux.Network;
import org.semux.TestUtils;
import org.semux.api.v2.model.AddNodeResponse;
import org.semux.api.v2.model.BasicTransactionType;
import org.semux.api.v2.model.BlockType;
import org.semux.api.v2.model.ComposeRawTransactionResponse;
import org.semux.api.v2.model.CreateAccountResponse;
Expand Down Expand Up @@ -99,7 +100,6 @@
import org.semux.api.v2.model.SignMessageResponse;
import org.semux.api.v2.model.SignRawTransactionResponse;
import org.semux.api.v2.model.SyncingProgressType;
import org.semux.api.v2.model.TransactionType;
import org.semux.api.v2.model.VerifyMessageResponse;
import org.semux.consensus.SemuxSync;
import org.semux.core.Amount;
Expand Down Expand Up @@ -225,7 +225,7 @@ public void getAccountTransactionsTest() {
GetAccountTransactionsResponse response = api.getAccountTransactions(Hex.encode(tx.getFrom()), "0", "1024");
assertTrue(response.isSuccess());
assertNotNull(response.getResult());
for (TransactionType txType : response.getResult()) {
for (BasicTransactionType txType : response.getResult()) {
assertEquals(block.getNumber(), Long.parseLong(txType.getBlockNumber()));
}
}
Expand Down
5 changes: 3 additions & 2 deletions src/test/java/org/semux/integration/TransactTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
import org.semux.Kernel;
import org.semux.Kernel.State;
import org.semux.KernelMock;
import org.semux.api.v2.model.BasicTransactionType;
import org.semux.api.v2.model.DoTransactionResponse;
import org.semux.api.v2.model.GetAccountResponse;
import org.semux.api.v2.model.GetAccountTransactionsResponse;
Expand Down Expand Up @@ -296,7 +297,7 @@ TransactionType.UNVOTE, coinbaseOf(kernelPremine), coinbaseOf(kernelValidator1),
private void assertLatestTransaction(KernelMock kernel, byte[] address,
TransactionType type, byte[] from, byte[] to, Amount value, Amount fee, byte[] data)
throws IOException {
org.semux.api.v2.model.TransactionType result = latestTransactionOf(kernel, address);
org.semux.api.v2.model.BasicTransactionType result = latestTransactionOf(kernel, address);
assertEquals(type.name(), result.getType());
assertEquals(Hex.encode0x(from), result.getFrom());
assertEquals(Hex.encode0x(to), result.getTo());
Expand Down Expand Up @@ -351,7 +352,7 @@ private Callable<Amount> availableOf(KernelMock kernelMock, byte[] address) {
* @return
* @throws IOException
*/
private org.semux.api.v2.model.TransactionType latestTransactionOf(KernelMock kernel, byte[] address)
private org.semux.api.v2.model.BasicTransactionType latestTransactionOf(KernelMock kernel, byte[] address)
throws IOException {
SimpleApiClient apiClient = kernel.getApiClient();

Expand Down