Skip to content

Commit

Permalink
Merge pull request #533 from aionnetwork/estimateNrg
Browse files Browse the repository at this point in the history
fix nrgEstimate API issue when the contract check the sender address
  • Loading branch information
AionJayT authored Jul 3, 2018
2 parents 7587bfc + a466863 commit dcce1ff
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 88 deletions.
36 changes: 18 additions & 18 deletions modAion/src/org/aion/zero/types/AionTransaction.java
Original file line number Diff line number Diff line change
@@ -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 <https://www.gnu.org/licenses/>.
* You should have received a copy of the GNU General Public License
* along with the aion network project source files.
* If not, see <https://www.gnu.org/licenses/>.
*
* Contributors to the aion source files in decreasing order of code volume:
*
* Contributors:
* Aion foundation.
*
******************************************************************************/
* Aion foundation.
*
*/

package org.aion.zero.types;

Expand Down Expand Up @@ -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;
Expand Down
73 changes: 20 additions & 53 deletions modApiServer/src/org/aion/api/server/ApiAion.java
Original file line number Diff line number Diff line change
@@ -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 <https://www.gnu.org/licenses/>.
* You should have received a copy of the GNU General Public License
* along with the aion network project source files.
* If not, see <https://www.gnu.org/licenses/>.
*
* Contributors:
* Aion foundation.
* Contributors to the aion source files in decreasing order of code volume:
*
******************************************************************************/
* Aion foundation.
*
*/

package org.aion.api.server;

Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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("<send-transaction msg=invalid-from-address>");
return -1L;
}

ECKey key = this.getAccountKey(from.toString());
if (key == null) {
LOG.error("<send-transaction msg=account-not-found>");
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();
Expand Down Expand Up @@ -693,7 +660,7 @@ protected NrgOracle getNrgOracle() {
}

protected long getDefaultNrgLimit() {
return 500_000L;
return 2_000_000L;
}

protected void startES(String thName) {
Expand Down
35 changes: 18 additions & 17 deletions modApiServer/src/org/aion/api/server/http/ApiWeb3Aion.java
Original file line number Diff line number Diff line change
@@ -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 <https://www.gnu.org/licenses/>.
* You should have received a copy of the GNU General Public License
* along with the aion network project source files.
* If not, see <https://www.gnu.org/licenses/>.
*
* Contributors:
* Aion foundation.
* Contributors to the aion source files in decreasing order of code volume:
*
******************************************************************************/
* Aion foundation.
*
*/

package org.aion.api.server.http;

Expand Down Expand Up @@ -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());
}
Expand Down

0 comments on commit dcce1ff

Please sign in to comment.