From a466863a5af006deb985782a9dcd4812f484d418 Mon Sep 17 00:00:00 2001 From: jayTseng Date: Tue, 19 Jun 2018 16:53:38 -0400 Subject: [PATCH] fix nrgEstimate API issue when the contract check the sender address --- .../org/aion/zero/types/AionTransaction.java | 36 ++++----- .../src/org/aion/api/server/ApiAion.java | 73 +++++-------------- .../org/aion/api/server/http/ApiWeb3Aion.java | 35 ++++----- 3 files changed, 56 insertions(+), 88 deletions(-) diff --git a/modAion/src/org/aion/zero/types/AionTransaction.java b/modAion/src/org/aion/zero/types/AionTransaction.java index 1c9e4c4498..108dd4ead3 100644 --- a/modAion/src/org/aion/zero/types/AionTransaction.java +++ b/modAion/src/org/aion/zero/types/AionTransaction.java @@ -1,27 +1,27 @@ -/******************************************************************************* +/* * Copyright (c) 2017-2018 Aion foundation. * - * This file is part of the aion network project. + * This file is part of the aion network project. * - * The aion network project is free software: you can redistribute it - * and/or modify it under the terms of the GNU General Public License - * as published by the Free Software Foundation, either version 3 of - * the License, or any later version. + * The aion network project is free software: you can redistribute it + * and/or modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation, either version 3 of + * the License, or any later version. * - * The aion network project is distributed in the hope that it will - * be useful, but WITHOUT ANY WARRANTY; without even the implied - * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. - * See the GNU General Public License for more details. + * The aion network project is distributed in the hope that it will + * be useful, but WITHOUT ANY WARRANTY; without even the implied + * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + * See the GNU General Public License for more details. * - * You should have received a copy of the GNU General Public License - * along with the aion network project source files. - * If not, see . + * You should have received a copy of the GNU General Public License + * along with the aion network project source files. + * If not, see . * + * Contributors to the aion source files in decreasing order of code volume: * - * Contributors: - * Aion foundation. - * - ******************************************************************************/ + * Aion foundation. + * + */ package org.aion.zero.types; @@ -91,7 +91,7 @@ public AionTransaction(byte[] nonce, Address to, byte[] value, byte[] data, long parsed = true; } - // testing constructor, only use this for the test. + // constructor for create nrgEstimate transaction public AionTransaction(byte[] nonce, Address from, Address to, byte[] value, byte[] data, long nrg, long nrgPrice) { super(nonce, to, value, data, nrg, nrgPrice); this.from = from; diff --git a/modApiServer/src/org/aion/api/server/ApiAion.java b/modApiServer/src/org/aion/api/server/ApiAion.java index 4046facdc0..49ebf72583 100644 --- a/modApiServer/src/org/aion/api/server/ApiAion.java +++ b/modApiServer/src/org/aion/api/server/ApiAion.java @@ -1,26 +1,27 @@ -/* ****************************************************************************** +/* * Copyright (c) 2017-2018 Aion foundation. * - * This file is part of the aion network project. + * This file is part of the aion network project. * - * The aion network project is free software: you can redistribute it - * and/or modify it under the terms of the GNU General Public License - * as published by the Free Software Foundation, either version 3 of - * the License, or any later version. + * The aion network project is free software: you can redistribute it + * and/or modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation, either version 3 of + * the License, or any later version. * - * The aion network project is distributed in the hope that it will - * be useful, but WITHOUT ANY WARRANTY; without even the implied - * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. - * See the GNU General Public License for more details. + * The aion network project is distributed in the hope that it will + * be useful, but WITHOUT ANY WARRANTY; without even the implied + * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + * See the GNU General Public License for more details. * - * You should have received a copy of the GNU General Public License - * along with the aion network project source files. - * If not, see . + * You should have received a copy of the GNU General Public License + * along with the aion network project source files. + * If not, see . * - * Contributors: - * Aion foundation. + * Contributors to the aion source files in decreasing order of code volume: * - ******************************************************************************/ + * Aion foundation. + * + */ package org.aion.api.server; @@ -426,8 +427,8 @@ protected byte[] doCall(ArgTxCall _params) { return rec.getExecutionResult(); } - protected long estimateGas(ArgTxCall params) { - AionTransaction tx = new AionTransaction(params.getNonce().toByteArray(), params.getTo(), + protected long estimateNrg(ArgTxCall params) { + AionTransaction tx = new AionTransaction(params.getNonce().toByteArray(), params.getFrom(), params.getTo(), params.getValue().toByteArray(), params.getData(), params.getNrg(), params.getNrgPrice()); AionTxReceipt receipt = this.ac.callConstant(tx, this.ac.getAionHub().getBlockchain().getBestBlock()); return receipt.getEnergyUsed(); @@ -489,40 +490,6 @@ public BigInteger getNonce(Address _address) { return this.ac.getRepository().getNonce(_address); } - // TODO: refactor these ad-hoc transaction creations - violates DRY and is messy - - protected long estimateNrg(ArgTxCall _params) { - if (_params == null) { - throw new NullPointerException(); - } - - Address from = _params.getFrom(); - - if (from.equals(Address.EMPTY_ADDRESS())) { - LOG.error(""); - return -1L; - } - - ECKey key = this.getAccountKey(from.toString()); - if (key == null) { - LOG.error(""); - return -1L; - } - - try { - // Transaction is executed as local transaction, no need to retrieve the real nonce. - byte[] nonce = BigInteger.ZERO.toByteArray(); - - AionTransaction tx = new AionTransaction(nonce, _params.getTo(), _params.getValue().toByteArray(), - _params.getData(), _params.getNrg(), _params.getNrgPrice()); - tx.sign(key); - - return this.ac.estimateTxNrg(tx, this.ac.getAionHub().getBlockchain().getBestBlock()); - } catch (Exception ex) { - return -1L; - } - } - protected byte[] sendTransaction(ArgTxCall _params) { Address from = _params.getFrom(); @@ -693,7 +660,7 @@ protected NrgOracle getNrgOracle() { } protected long getDefaultNrgLimit() { - return 500_000L; + return 2_000_000L; } protected void startES(String thName) { diff --git a/modApiServer/src/org/aion/api/server/http/ApiWeb3Aion.java b/modApiServer/src/org/aion/api/server/http/ApiWeb3Aion.java index f6dc86c69f..b8cc62af79 100644 --- a/modApiServer/src/org/aion/api/server/http/ApiWeb3Aion.java +++ b/modApiServer/src/org/aion/api/server/http/ApiWeb3Aion.java @@ -1,26 +1,27 @@ -/* ****************************************************************************** +/* * Copyright (c) 2017-2018 Aion foundation. * - * This file is part of the aion network project. + * This file is part of the aion network project. * - * The aion network project is free software: you can redistribute it - * and/or modify it under the terms of the GNU General Public License - * as published by the Free Software Foundation, either version 3 of - * the License, or any later version. + * The aion network project is free software: you can redistribute it + * and/or modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation, either version 3 of + * the License, or any later version. * - * The aion network project is distributed in the hope that it will - * be useful, but WITHOUT ANY WARRANTY; without even the implied - * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. - * See the GNU General Public License for more details. + * The aion network project is distributed in the hope that it will + * be useful, but WITHOUT ANY WARRANTY; without even the implied + * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + * See the GNU General Public License for more details. * - * You should have received a copy of the GNU General Public License - * along with the aion network project source files. - * If not, see . + * You should have received a copy of the GNU General Public License + * along with the aion network project source files. + * If not, see . * - * Contributors: - * Aion foundation. + * Contributors to the aion source files in decreasing order of code volume: * - ******************************************************************************/ + * Aion foundation. + * + */ package org.aion.api.server.http; @@ -729,7 +730,7 @@ else if (_params instanceof JSONObject) { } ArgTxCall txParams = ArgTxCall.fromJSON(_tx, getNrgOracle(), getDefaultNrgLimit()); - NumericalValue estimate = new NumericalValue(estimateGas(txParams)); + NumericalValue estimate = new NumericalValue(estimateNrg(txParams)); return new RpcMsg(estimate.toHexString()); }