Skip to content

Commit

Permalink
Fix Transaction energy rule check within kernel and FVM
Browse files Browse the repository at this point in the history
  • Loading branch information
AionJayT committed Oct 28, 2019
1 parent 761b6c0 commit 73763ef
Show file tree
Hide file tree
Showing 8 changed files with 62 additions and 12 deletions.
Binary file renamed lib/fastvm-93a258f.jar → lib/fastvm-39cde69.jar
Binary file not shown.
2 changes: 1 addition & 1 deletion modAionImpl/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ dependencies {
compile project(':modEvtMgrImpl')
compile project(':modPrecompiled')
compile project(':modTxPool')
compile files("${rootProject.projectDir}/lib/fastvm-93a258f.jar")
compile files("${rootProject.projectDir}/lib/fastvm-39cde69.jar")
compile 'org.json:json:20180813'
compile 'info.picocli:picocli:4.0.0'
compile files("${rootProject.projectDir}/lib/aion-types-22a3be9.jar")
Expand Down
5 changes: 3 additions & 2 deletions modAionImpl/src/org/aion/zero/impl/valid/TXValidator.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import static org.aion.zero.impl.vm.common.TxNrgRule.isValidNrgContractCreate;
import static org.aion.zero.impl.vm.common.TxNrgRule.isValidNrgContractCreateAfterUnity;
import static org.aion.zero.impl.vm.common.TxNrgRule.isValidNrgTx;
import static org.aion.zero.impl.vm.common.TxNrgRule.isValidNrgTxAfterUnity;

import java.util.Collections;
import java.util.Map;
Expand Down Expand Up @@ -64,12 +65,12 @@ private static boolean isValid0(AionTransaction tx) {
private static boolean isValidAfterUnity(AionTransaction tx) {
long nrg = tx.getEnergyLimit();
if (tx.isContractCreationTransaction()) {
if (!isValidNrgContractCreateAfterUnity(nrg)) {
if (!isValidNrgContractCreateAfterUnity(nrg, tx.getData())) {
LOG.error("invalid contract create nrg!");
return false;
}
} else {
if (!isValidNrgTx(nrg)) {
if (!isValidNrgTxAfterUnity(nrg, tx.getData())) {
LOG.error("invalid tx nrg!");
return false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,12 @@ public final class AvmConfigurations {
private static AvmVersionSchedule multiVersionSchedule = null;
private static String projectRootDir = null;
private static IEnergyRules energyRules = (t, l) -> (t == IEnergyRules.TransactionType.CREATE) ? TxNrgRule.isValidNrgContractCreate(l) : TxNrgRule.isValidNrgTx(l);
// The AVM does check the energy requirement regarding the transaction data context. Therefor We only check the basic rule in here.
private static IEnergyRules energyRulesAfterUnityFork =
(t, l) ->
(t == IEnergyRules.TransactionType.CREATE)
? TxNrgRule.isValidNrgContractCreateAfterUnity(l)
: TxNrgRule.isValidNrgTx(l);
? TxNrgRule.isValidNrgContractCreateAfterUnity(l, null)
: TxNrgRule.isValidNrgTxAfterUnity(l, null);

private AvmConfigurations() {}

Expand Down
35 changes: 35 additions & 0 deletions modAionImpl/src/org/aion/zero/impl/vm/common/TransactionUtil.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package org.aion.zero.impl.vm.common;

import org.aion.fastvm.FvmConstants;

/** Duplicate from the fastVM module. */
public final class TransactionUtil {

public static long computeTransactionCost(boolean isCreate, byte[] data) {
long nonZeroes = nonZeroBytesInData(data);
long zeroes = zeroBytesInData(data);

return (isCreate ? FvmConstants.CREATE_TRANSACTION_FEE : 0)
+ FvmConstants.TRANSACTION_BASE_FEE
+ zeroes * FvmConstants.ZERO_BYTE_FEE
+ nonZeroes * FvmConstants.NONZERO_BYTE_FEE;
}

private static long nonZeroBytesInData(byte[] data) {
int total = (data == null) ? 0 : data.length;

return total - zeroBytesInData(data);
}

private static long zeroBytesInData(byte[] data) {
if (data == null) {
return 0;
}

int c = 0;
for (byte b : data) {
c += (b == 0) ? 1 : 0;
}
return c;
}
}
9 changes: 7 additions & 2 deletions modAionImpl/src/org/aion/zero/impl/vm/common/TxNrgRule.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,20 @@ public static boolean isValidNrgContractCreate(long energyLimit) {
return (energyLimit >= CONTRACT_CREATE_TX_NRG_MIN) && (energyLimit <= CONTRACT_CREATE_TX_NRG_MAX);
}

public static boolean isValidNrgContractCreateAfterUnity(long energyLimit) {
return (energyLimit >= (CONTRACT_CREATE_TX_NRG_MIN + TX_NRG_MIN))
public static boolean isValidNrgContractCreateAfterUnity(long energyLimit, byte[] data) {
return (energyLimit >= TransactionUtil.computeTransactionCost(true, data))
&& (energyLimit <= CONTRACT_CREATE_TX_NRG_MAX);
}

public static boolean isValidNrgTx(long energyLimit) {
return (energyLimit >= TX_NRG_MIN) && (energyLimit <= TX_NRG_MAX);
}

public static boolean isValidNrgTxAfterUnity(long energyLimit, byte[] data) {
return (energyLimit >= TransactionUtil.computeTransactionCost(false, data))
&& (energyLimit <= TX_NRG_MAX);
}

public static boolean isValidTxNrgPrice(long energyPrice) {
return (energyPrice >= NRGPRICE_MIN) && (energyPrice <= NRGPRICE_MAX);
}
Expand Down
16 changes: 12 additions & 4 deletions modAionImpl/src/org/aion/zero/impl/vm/fvm/ExternalStateForFvm.java
Original file line number Diff line number Diff line change
Expand Up @@ -312,9 +312,9 @@ public void incrementNonce(AionAddress address) {
* @return whether the energy limit is valid.
*/
@Override
public boolean isValidEnergyLimitForCreate(long energyLimit) {
public boolean isValidEnergyLimitForCreate(long energyLimit, byte[] data) {
return (this.isLocalCall) || ((this.isUnityForkEnabled)
? TxNrgRule.isValidNrgContractCreateAfterUnity(energyLimit)
? TxNrgRule.isValidNrgContractCreateAfterUnity(energyLimit, data)
: TxNrgRule.isValidNrgContractCreate(energyLimit));
}

Expand All @@ -329,8 +329,16 @@ public boolean isValidEnergyLimitForCreate(long energyLimit) {
* @return whether the energy limit is valid.
*/
@Override
public boolean isValidEnergyLimitForNonCreate(long energyLimit) {
return (this.isLocalCall) ? true : TxNrgRule.isValidNrgTx(energyLimit);
public boolean isValidEnergyLimitForNonCreate(long energyLimit, byte[] data) {
if (this.isLocalCall) {
return true;
} else {
if (this.isUnityForkEnabled) {
return TxNrgRule.isValidNrgTxAfterUnity(energyLimit, data);
} else {
return TxNrgRule.isValidNrgTx(energyLimit);
}
}
}

/**
Expand Down
2 changes: 1 addition & 1 deletion modBase/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ test.dependsOn copyNativeLibsForModuleTests
clean.dependsOn deleteNativeLibs

dependencies {
compile files("${rootProject.projectDir}/lib/fastvm-93a258f.jar")
compile files("${rootProject.projectDir}/lib/fastvm-39cde69.jar")
compile files("${rootProject.projectDir}/lib/aion-types-22a3be9.jar")
compile project(':modUtil')
compile project(':modLogger')
Expand Down

0 comments on commit 73763ef

Please sign in to comment.