optimize eth_sendTransaction for light version
This commit is contained in:
parent
eedf40ae0a
commit
befd04478e
|
@ -108,38 +108,38 @@ public abstract class JsonRpcServerMethod implements RequestHandler {
|
|||
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
|
||||
BigInteger value = new BigInteger("1000");
|
||||
if (obj.containsKey("value") && !((String)obj.get("value")).equals("")) {
|
||||
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
|
||||
byte[] data = new byte[]{};
|
||||
if (obj.containsKey("data") && !((String)obj.get("data")).equals("")) {
|
||||
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);
|
||||
|
||||
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());
|
||||
}
|
||||
}
|
||||
|
||||
}
|
|
@ -44,9 +44,9 @@ public class eth_sendTransaction extends JsonRpcServerMethod {
|
|||
if (rres == null) {
|
||||
return new JSONRPC2Response(JSONRPC2Error.INTERNAL_ERROR, req.getID());
|
||||
}
|
||||
rres.setID(req.getID());
|
||||
|
||||
JSONRPC2Response res = new JSONRPC2Response(rres.getResult(), req.getID());
|
||||
return res;
|
||||
return rres;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue