Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix TxValidator for the FVM contract deploy mininum energy issue #997

Merged
merged 1 commit into from
Oct 1, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -1734,10 +1734,11 @@ private boolean isValid(Block block) {

Map<AionAddress, BigInteger> nonceCache = new HashMap<>();

boolean unityForkEnabled = block.getHeader().getNumber() >= FORK_5_BLOCK_NUMBER;
if (txs.parallelStream()
.anyMatch(
tx ->
!TXValidator.isValid(tx)
unityForkEnabled ? !TXValidator.isValidAfterUnity(tx) : !TXValidator.isValid(tx)
|| !TransactionTypeValidator.isValid(tx)
|| !beaconHashValidator.validateTxForBlock(tx, block))) {
LOG.error("Some transactions in the block are invalid");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -559,8 +559,9 @@ public AionBlock createBlock(
boolean waitUntilBlockTime,
long currTimeSeconds) {

boolean unityForkEnabled = (parent.getHeader().getNumber() + 1) >= getUnityForkNumber();
for (AionTransaction tx : txs) {
if (!TXValidator.isValid(tx)) {
if (unityForkEnabled ? !TXValidator.isValidAfterUnity(tx): !TXValidator.isValid(tx)) {
throw new InvalidParameterException("invalid transaction input! " + tx.toString());
}
}
Expand Down
50 changes: 37 additions & 13 deletions modAionImpl/src/org/aion/zero/impl/valid/TXValidator.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.aion.zero.impl.valid;

import static org.aion.vm.common.TxNrgRule.isValidNrgContractCreate;
import static org.aion.vm.common.TxNrgRule.isValidNrgContractCreateAfterUnity;
import static org.aion.vm.common.TxNrgRule.isValidNrgTx;

import java.util.Collections;
Expand Down Expand Up @@ -39,6 +40,42 @@ public static boolean isInCache(ByteArrayWrapper hash) {
}

public static boolean isValid0(AionTransaction tx) {

long nrg = tx.getEnergyLimit();
if (tx.isContractCreationTransaction()) {
if (!isValidNrgContractCreate(nrg)) {
LOG.error("invalid contract create nrg!");
return false;
}
} else {
if (!isValidNrgTx(nrg)) {
LOG.error("invalid tx nrg!");
return false;
}
}

return isValidInner(tx);
}

public static boolean isValidAfterUnity(AionTransaction tx) {

long nrg = tx.getEnergyLimit();
if (tx.isContractCreationTransaction()) {
if (!isValidNrgContractCreateAfterUnity(nrg)) {
LOG.error("invalid contract create nrg!");
return false;
}
} else {
if (!isValidNrgTx(nrg)) {
LOG.error("invalid tx nrg!");
return false;
}
}

return isValidInner(tx);
}

private static boolean isValidInner(AionTransaction tx) {
byte[] check = tx.getNonce();
if (check == null || check.length > DataWord.BYTES) {
LOG.error("invalid tx nonce!");
Expand All @@ -63,19 +100,6 @@ public static boolean isValid0(AionTransaction tx) {
return false;
}

long nrg = tx.getEnergyLimit();
if (tx.isContractCreationTransaction()) {
if (!isValidNrgContractCreate(nrg)) {
LOG.error("invalid contract create nrg!");
return false;
}
} else {
if (!isValidNrgTx(nrg)) {
LOG.error("invalid tx nrg!");
return false;
}
}

if (tx.getEnergyPrice() < 0) {
LOG.error("invalid tx nrgprice!");
return false;
Expand Down
152 changes: 152 additions & 0 deletions modAionImpl/test/org/aion/zero/impl/vm/ContractIntegTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@

import java.io.IOException;
import java.math.BigInteger;
import java.security.InvalidParameterException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
Expand Down Expand Up @@ -130,6 +131,7 @@ public void setup() throws Exception {

@After
public void tearDown() throws Exception {
blockchain.setUnityForkNumber(Long.MAX_VALUE);
blockchain = null;
deployerKey = null;
deployer = null;
Expand Down Expand Up @@ -1996,4 +1998,154 @@ private AionAddress deployAvmContract(AvmVersion version, BigInteger nonce) {

return contractAddress;
}

@Test
public void testFvmEmptyContractWith200KEnrygyBeforeUnity() throws IOException, VmFatalException {
String contractName = "EmptyContract";
byte[] deployCode = getDeployCode(contractName);
long nrg = 200_000;
long nrgPrice = 1;
BigInteger value = BigInteger.ZERO;
BigInteger nonce = BigInteger.ZERO;

// to == null signals that this is contract creation.
AionTransaction tx =
AionTransaction.create(
deployerKey,
nonce.toByteArray(),
null,
value.toByteArray(),
deployCode,
nrg,
nrgPrice,
txType, null);
assertTrue(tx.isContractCreationTransaction());

assertEquals(Builder.DEFAULT_BALANCE, blockchain.getRepository().getBalance(deployer));
assertEquals(BigInteger.ZERO, blockchain.getRepository().getNonce(deployer));

AionBlock block = makeBlock(tx);
RepositoryCache repo = blockchain.getRepository().startTracking();

AionTxExecSummary summary =
BulkExecutor.executeTransactionWithNoPostExecutionWork(
block.getDifficulty(),
block.getNumber(),
block.getTimestamp(),
block.getNrgLimit(),
block.getCoinbase(),
tx,
repo,
false,
true,
false,
false,
LOGGER_VM,
BlockCachingContext.PENDING,
block.getNumber() - 1);

if (txType == TransactionTypes.DEFAULT) {
assertEquals("OUT_OF_NRG", summary.getReceipt().getError());

checkStateOfDeployer(repo, summary, nrgPrice, value, nonce.add(BigInteger.ONE));
assertEquals(nrg, summary.getReceipt().getEnergyUsed());
assertEquals(nrg, repo.getBalance(block.getCoinbase()).intValue());

} else if (txType == TransactionTypes.AVM_CREATE_CODE) {
assertEquals("Failed: invalid data", summary.getReceipt().getError());
nonce = nonce.add(BigInteger.ONE);
checkStateOfDeployer(repo, summary, nrgPrice, value, nonce);
assertEquals(nrg, summary.getReceipt().getEnergyUsed());
}
}

@Test (expected = InvalidParameterException.class)
public void testFvmEmptyContractWith200KEnrygyAfterUnity() throws IOException {
blockchain.setUnityForkNumber(1);
String contractName = "EmptyContract";
byte[] deployCode = getDeployCode(contractName);
long nrg = 200_000;
long nrgPrice = 1;
BigInteger value = BigInteger.ZERO;
BigInteger nonce = BigInteger.ZERO;

// to == null signals that this is contract creation.
AionTransaction tx =
AionTransaction.create(
deployerKey,
nonce.toByteArray(),
null,
value.toByteArray(),
deployCode,
nrg,
nrgPrice,
txType, null);
assertTrue(tx.isContractCreationTransaction());

assertEquals(Builder.DEFAULT_BALANCE, blockchain.getRepository().getBalance(deployer));
assertEquals(BigInteger.ZERO, blockchain.getRepository().getNonce(deployer));

// The transaction has invalid energylimit settings
AionBlock block = makeBlock(tx);
}

@Test
public void testFvmEmptyContractWith221KEnrygyAfterUnity() throws IOException, VmFatalException {
blockchain.setUnityForkNumber(1);
String contractName = "EmptyContract";
byte[] deployCode = getDeployCode(contractName);
long nrg = 221_000;
long nrgPrice = 1;
BigInteger value = BigInteger.ZERO;
BigInteger nonce = BigInteger.ZERO;

// to == null signals that this is contract creation.
AionTransaction tx =
AionTransaction.create(
deployerKey,
nonce.toByteArray(),
null,
value.toByteArray(),
deployCode,
nrg,
nrgPrice,
txType, null);
assertTrue(tx.isContractCreationTransaction());

assertEquals(Builder.DEFAULT_BALANCE, blockchain.getRepository().getBalance(deployer));
assertEquals(BigInteger.ZERO, blockchain.getRepository().getNonce(deployer));

// The transaction has invalid energylimit settings
AionBlock block = makeBlock(tx);
RepositoryCache repo = blockchain.getRepository().startTracking();

AionTxExecSummary summary =
BulkExecutor.executeTransactionWithNoPostExecutionWork(
block.getDifficulty(),
block.getNumber(),
block.getTimestamp(),
block.getNrgLimit(),
block.getCoinbase(),
tx,
repo,
false,
true,
false,
false,
LOGGER_VM,
BlockCachingContext.PENDING,
block.getNumber() - 1);

if (txType == TransactionTypes.DEFAULT) {
assertEquals("OUT_OF_NRG", summary.getReceipt().getError());
checkStateOfDeployer(repo, summary, nrgPrice, value, nonce.add(BigInteger.ONE));
assertEquals(nrg, summary.getReceipt().getEnergyUsed());
assertEquals(nrg, repo.getBalance(block.getCoinbase()).intValue());
} else if (txType == TransactionTypes.AVM_CREATE_CODE) {
assertEquals("Failed: invalid data", summary.getReceipt().getError());
nonce = nonce.add(BigInteger.ONE);
checkStateOfDeployer(repo, summary, nrgPrice, value, nonce);
assertEquals(nrg, summary.getReceipt().getEnergyUsed());
}
}
}
5 changes: 5 additions & 0 deletions modVM/src/org/aion/vm/common/TxNrgRule.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ 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))
&& (energyLimit <= CONTRACT_CREATE_TX_NRG_MAX);
}

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