add new method eth_getTransactionReceipt (from updated JSON-RPC specification).

This commit is contained in:
Yaroslav Dmytrotsa 2015-07-30 13:27:06 +03:00
parent 97417aa147
commit 2f60df4a2a
4 changed files with 115 additions and 0 deletions

View File

@ -85,6 +85,7 @@ public final class JsonRpcServer extends org.ethereum.android.jsonrpc.JsonRpcSer
this.dispatcher.register(new eth_getTransactionByBlockNumberAndIndex(this.ethereum));
this.dispatcher.register(new eth_getUncleByBlockHashAndIndex(this.ethereum));
this.dispatcher.register(new eth_getUncleByBlockNumberAndIndex(this.ethereum));
this.dispatcher.register(new eth_getTransactionReceipt(this.ethereum));
this.dispatcher.register(new eth_getCompilers(this.ethereum));
this.dispatcher.register(new eth_compileSolidity(this.ethereum));
this.dispatcher.register(new eth_compileLLL(this.ethereum));

View File

@ -14,10 +14,12 @@ import org.ethereum.core.AccountState;
import org.ethereum.core.Block;
import org.ethereum.core.BlockHeader;
import org.ethereum.core.Transaction;
import org.ethereum.core.TransactionReceipt;
import org.ethereum.crypto.HashUtil;
import org.ethereum.facade.Ethereum;
import org.ethereum.util.RLP;
import org.ethereum.vm.DataWord;
import org.ethereum.vm.LogInfo;
import org.spongycastle.util.encoders.Hex;
import java.math.BigInteger;
@ -272,6 +274,83 @@ public abstract class JsonRpcServerMethod implements RequestHandler {
return res;
}
protected JSONObject transactionReceiptToJS (Block block, TransactionReceipt transaction) {
JSONObject res = new JSONObject();
res.put("transactionHash", "0x" + Hex.toHexString(transaction.getTransaction().getHash()));
long txi = 0;
long txli = 0;
if (block == null) {
OrmLiteBlockStoreDatabase db = OrmLiteBlockStoreDatabase.getHelper(null);
BlockTransactionVO relation = db.getTransactionLocation(transaction.getTransaction().getHash());
if (relation == null) {
res.put("transactionIndex", null);
res.put("blockHash", null);
res.put("blockNumber", null);
} else {
block = ethereum.getBlockchain().getBlockByHash(relation.getBlockHash());
}
}
if (block != null) {
for (Transaction tx : block.getTransactionsList()) {
if (Arrays.equals(tx.getHash(), transaction.getTransaction().getHash()))
break;
txli += this.ethereum.getBlockchain().getTransactionReceiptByHash(transaction.getTransaction().getHash()).getLogInfoList().size();
txi++;
}
res.put("transactionIndex", "0x" + Long.toHexString(txi));
res.put("blockHash", "0x" + Hex.toHexString(block.getHash()));
res.put("blockNumber", "0x" + Long.toHexString(block.getNumber()));
}
res.put("cumulativeGasUsed", "0x" + Hex.toHexString(transaction.getCumulativeGas()));
res.put("gasUsed", "0x" + Hex.toHexString(transaction.getTransaction().getGasPrice()));
res.put("gasUsed", "0x" + Hex.toHexString(transaction.getTransaction().getContractAddress()));
res.put("contractAddress", "0x" + Hex.toHexString(transaction.getTransaction().getContractAddress()));
JSONArray tmp = new JSONArray();
for (LogInfo li : transaction.getLogInfoList()) {
JSONObject lio = new JSONObject();
if (block == null) {
lio.put("type", "pending");
lio.put("logIndex", null);
lio.put("transactionIndex", null);
lio.put("transactionHash", null);
lio.put("blockHash", null);
lio.put("blockNumber", null);
} else {
lio.put("type", "mined");
lio.put("logIndex", "0x" + Long.toHexString(txli));
lio.put("transactionIndex", "0x" + Long.toHexString(txi));
lio.put("transactionHash", "0x" + Hex.toHexString(transaction.getTransaction().getHash()));
lio.put("blockHash", "0x" + Hex.toHexString(block.getHash()));
lio.put("blockNumber", "0x" + Long.toHexString(block.getNumber()));
}
lio.put("address", "0x" + Hex.toHexString(li.getAddress()));
lio.put("data", "0x" + Hex.toHexString(li.getData()));
JSONArray topics = new JSONArray();
for (DataWord topic : li.getTopics()) {
topics.add("0x" + Hex.toHexString(topic.getData()));
}
lio.put("topics", topics);
tmp.add(lio);
txli++;
}
res.put("logs", tmp);
return res;
}
protected byte[] getCoinBase() {
return ((Account) ethereum.getWallet().getAccountCollection().toArray()[0]).getEcKey().getAddress();
}

View File

@ -0,0 +1,34 @@
package org.ethereum.android.jsonrpc.full.method;
import com.thetransactioncompany.jsonrpc2.*;
import com.thetransactioncompany.jsonrpc2.server.*;
import org.ethereum.android.jsonrpc.full.JsonRpcServerMethod;
import org.ethereum.core.TransactionReceipt;
import org.ethereum.facade.Ethereum;
import java.util.List;
public class eth_getTransactionReceipt extends JsonRpcServerMethod {
public eth_getTransactionReceipt (Ethereum ethereum) {
super(ethereum);
}
protected JSONRPC2Response worker(JSONRPC2Request req, MessageContext ctx) {
List<Object> params = req.getPositionalParams();
if (params.size() != 1) {
return new JSONRPC2Response(JSONRPC2Error.INVALID_PARAMS, req.getID());
} else {
byte[] address = jsToAddress((String) params.get(0));
TransactionReceipt transaction = ethereum.getBlockchain().getTransactionReceiptByHash(address);
if (transaction == null)
return new JSONRPC2Response(null, req.getID());
JSONRPC2Response res = new JSONRPC2Response(transactionReceiptToJS(null, transaction), req.getID());
return res;
}
}
}

View File

@ -37,6 +37,7 @@ public class proxy extends JsonRpcServerMethod {
proxyMethods.add("eth_getTransactionByHash");
proxyMethods.add("eth_getTransactionByBlockHashAndIndex");
proxyMethods.add("eth_getTransactionByBlockNumberAndIndex");
proxyMethods.add("eth_getTransactionReceipt");
proxyMethods.add("eth_getUncleByBlockHashAndIndex");
proxyMethods.add("eth_getUncleByBlockNumberAndIndex");
proxyMethods.add("eth_getCompilers");