optimize eth_sendTransaction for light version

This commit is contained in:
Yaroslav Dmytrotsa 2015-08-12 16:40:27 +03:00
parent eedf40ae0a
commit befd04478e
2 changed files with 34 additions and 22 deletions

View File

@ -108,38 +108,38 @@ public abstract class JsonRpcServerMethod implements RequestHandler {
to = jsToAddress((String) obj.get("to")); to = jsToAddress((String) obj.get("to"));
} }
BigInteger gasPrice = getGasPrice();
if (obj.containsKey("gasPrice") && !((String)obj.get("gasPrice")).equals("")) {
gasPrice = jsToBigInteger((String) obj.get("gasPrice"));
}
// default - from cpp-ethereum
BigInteger gas = getBalance(from).divide(gasPrice);
BigInteger gasBBRemaining = getLatestBlockGasRemaining();
if (gasBBRemaining.compareTo(gas) < 0)
gas = gasBBRemaining;
if (obj.containsKey("gas") && !((String)obj.get("gas")).equals("")) {
gas = jsToBigInteger((String) obj.get("gas"));
}
// default - from ethereumj-studio // default - from ethereumj-studio
BigInteger value = new BigInteger("1000"); BigInteger value = new BigInteger("1000");
if (obj.containsKey("value") && !((String)obj.get("value")).equals("")) { if (obj.containsKey("value") && !((String)obj.get("value")).equals("")) {
value = jsToBigInteger((String) obj.get("value")); value = jsToBigInteger((String) obj.get("value"));
} }
// default - from ethereumj-studio
BigInteger nonce = getTransactionCount(from);
if (obj.containsKey("nonce") && !((String)obj.get("nonce")).equals("")) {
nonce = jsToBigInteger((String) obj.get("nonce"));
}
// default - from ethereumj-studio // default - from ethereumj-studio
byte[] data = new byte[]{}; byte[] data = new byte[]{};
if (obj.containsKey("data") && !((String)obj.get("data")).equals("")) { if (obj.containsKey("data") && !((String)obj.get("data")).equals("")) {
data = jsToAddress((String) obj.get("data")); data = jsToAddress((String) obj.get("data"));
} }
BigInteger gasPrice = getGasPrice();
if (obj.containsKey("gasPrice") && !((String)obj.get("gasPrice")).equals("")) {
gasPrice = jsToBigInteger((String) obj.get("gasPrice"));
}
JSONObject tmp = new JSONObject();
if (to != null)
tmp.put("to", "0x" + Hex.toHexString(to));
tmp.put("data", Hex.toHexString(data));
BigInteger gas = getEstimateGas(tmp);
if (obj.containsKey("gas") && !((String)obj.get("gas")).equals("")) {
gas = jsToBigInteger((String) obj.get("gas"));
}
// default - from ethereumj-studio
BigInteger nonce = getTransactionCount(from);
if (obj.containsKey("nonce") && !((String)obj.get("nonce")).equals("")) {
nonce = jsToBigInteger((String) obj.get("nonce"));
}
Transaction tx = ethereum.createTransaction(nonce, gasPrice, gas, to, value, data); Transaction tx = ethereum.createTransaction(nonce, gasPrice, gas, to, value, data);
tx.sign(senderPrivKey); tx.sign(senderPrivKey);
@ -219,4 +219,16 @@ public abstract class JsonRpcServerMethod implements RequestHandler {
} }
} }
protected BigInteger getEstimateGas(JSONObject obj) {
ArrayList<Object> params = new ArrayList<Object>();
params.add(obj);
JSONRPC2Request req = new JSONRPC2Request("eth_estimateGas", params, 1000);
JSONRPC2Response res = getRemoteData(req);
if (res == null || !res.indicatesSuccess()) {
return BigInteger.valueOf(90000);
} else {
return jsToBigInteger(res.getResult().toString());
}
}
} }

View File

@ -44,9 +44,9 @@ public class eth_sendTransaction extends JsonRpcServerMethod {
if (rres == null) { if (rres == null) {
return new JSONRPC2Response(JSONRPC2Error.INTERNAL_ERROR, req.getID()); return new JSONRPC2Response(JSONRPC2Error.INTERNAL_ERROR, req.getID());
} }
rres.setID(req.getID());
JSONRPC2Response res = new JSONRPC2Response(rres.getResult(), req.getID()); return rres;
return res;
} }
} }