Skip to content

Commit

Permalink
validator for transactions ensuring AVM tx can be used only when the …
Browse files Browse the repository at this point in the history
…AVM is enabled
  • Loading branch information
AlexandraRoatis committed Jan 29, 2019
1 parent 1acc164 commit 9a30633
Show file tree
Hide file tree
Showing 11 changed files with 94 additions and 6 deletions.
7 changes: 7 additions & 0 deletions modAionBase/src/org/aion/base/vm/VirtualMachineSpecs.java
@@ -0,0 +1,7 @@
package org.aion.base.vm;

public final class VirtualMachineSpecs {

public static final byte AVM_VM_CODE = 0xf;
public static final byte FVM_DEFAULT_TX_TYPE = 0x01;
}
3 changes: 3 additions & 0 deletions modAionImpl/src/org/aion/zero/impl/A0BCConfig.java
Expand Up @@ -35,4 +35,7 @@ public interface A0BCConfig {

/** Retrieves the selected energy strategy algorithm */
AbstractEnergyStrategyLimit getEnergyLimitStrategy();

/** Flag for enabling AVM use. */
boolean isAvmEnabled();
}
11 changes: 10 additions & 1 deletion modAionImpl/src/org/aion/zero/impl/AionBlockchainImpl.java
Expand Up @@ -70,6 +70,7 @@
import org.aion.zero.impl.types.AionTxInfo;
import org.aion.zero.impl.types.RetValidPreBlock;
import org.aion.zero.impl.valid.TXValidator;
import org.aion.zero.impl.valid.TransactionTypeValidator;
import org.aion.zero.types.A0BlockHeader;
import org.aion.zero.types.AionTransaction;
import org.aion.zero.types.AionTxExecSummary;
Expand Down Expand Up @@ -102,6 +103,7 @@ public class AionBlockchainImpl implements IAionBlockchain {

private A0BCConfig config;
private long exitOn = Long.MAX_VALUE;
private TransactionTypeValidator vmValidator;

private AionRepositoryImpl repository;
private IRepositoryCache track;
Expand Down Expand Up @@ -192,6 +194,11 @@ public AbstractEnergyStrategyLimit getEnergyLimitStrategy() {
cfgAion.getConsensus().getEnergyStrategy(),
config);
}

@Override
public boolean isAvmEnabled() {
return cfgAion.getVm().isAvmEnabled();
}
};
}

Expand All @@ -212,6 +219,7 @@ protected AionBlockchainImpl(
* blockHash and number.
*/
this.chainConfiguration = chainConfig;
this.vmValidator = new TransactionTypeValidator(config.isAvmEnabled());

this.grandParentBlockHeaderValidator =
this.chainConfiguration.createGrandParentHeaderValidator();
Expand Down Expand Up @@ -992,7 +1000,8 @@ private boolean isValid(AionBlock block) {

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

if (txs.parallelStream().anyMatch(tx -> !TXValidator.isValid(tx))) {
if (txs.parallelStream()
.anyMatch(tx -> !TXValidator.isValid(tx) || !vmValidator.isValid(tx))) {
LOG.error("Some transactions in the block are invalid");
return false;
}
Expand Down
5 changes: 5 additions & 0 deletions modAionImpl/src/org/aion/zero/impl/StandaloneBlockchain.java
Expand Up @@ -250,6 +250,11 @@ public AbstractEnergyStrategyLimit getEnergyLimitStrategy() {
.getEnergyDivisorLimitLong(),
10_000_000L);
}

@Override
public boolean isAvmEnabled() {
return false;
}
}
: this.a0Config;

Expand Down
Expand Up @@ -54,6 +54,7 @@
import org.aion.zero.impl.types.AionBlock;
import org.aion.zero.impl.types.AionTxInfo;
import org.aion.zero.impl.valid.TXValidator;
import org.aion.zero.impl.valid.TransactionTypeValidator;
import org.aion.zero.types.AionTransaction;
import org.aion.zero.types.AionTxExecSummary;
import org.aion.zero.types.AionTxReceipt;
Expand Down Expand Up @@ -94,6 +95,8 @@ public TransactionSortedSet() {

private TransactionStore<AionTransaction, AionTxReceipt, AionTxInfo> transactionStore;

private TransactionTypeValidator vmValidator;

private IRepository repository;

private ITxPool<AionTransaction> txPool;
Expand Down Expand Up @@ -281,6 +284,8 @@ public static AionPendingStateImpl createForTesting(
}

private AionPendingStateImpl(CfgAion _cfgAion, AionRepositoryImpl _repository) {
this.vmValidator = new TransactionTypeValidator(_cfgAion.getVm().isAvmEnabled());

this.repository = _repository;

this.isSeed = _cfgAion.getConsensus().isSeed();
Expand Down Expand Up @@ -427,6 +432,10 @@ public synchronized TxResponse addPendingTransaction(AionTransaction tx) {
return addPendingTransactions(Collections.singletonList(tx)).get(0);
}

public boolean isValid(AionTransaction tx) {
return TXValidator.isValid(tx) && vmValidator.isValid(tx);
}

/**
* Tries to add the given transactions to the PendingState
*
Expand Down Expand Up @@ -612,7 +621,7 @@ private List<TxResponse> seedProcess(List<AionTransaction> transactions) {
List<AionTransaction> newTx = new ArrayList<>();
List<TxResponse> txResponses = new ArrayList<>();
for (AionTransaction tx : transactions) {
if (TXValidator.isValid(tx)) {
if (isValid(tx)) {
newTx.add(tx);
txResponses.add(TxResponse.SUCCESS);
} else {
Expand Down Expand Up @@ -670,7 +679,7 @@ private void fireTxUpdate(
*/
private TxResponse addPendingTransactionImpl(final AionTransaction tx, BigInteger txNonce) {

if (!TXValidator.isValid(tx)) {
if (!isValid(tx)) {
LOGGER_TX.error("invalid Tx [{}]", tx.toString());
fireDroppedTx(tx, "INVALID_TX");
return TxResponse.INVALID_TX;
Expand Down
2 changes: 2 additions & 0 deletions modAionImpl/src/org/aion/zero/impl/config/CfgAion.java
Expand Up @@ -24,6 +24,7 @@
import org.aion.mcf.config.CfgReports;
import org.aion.mcf.config.CfgSync;
import org.aion.mcf.config.CfgTx;
import org.aion.mcf.config.CfgVm;
import org.aion.zero.exceptions.HeaderStructureException;
import org.aion.zero.impl.AionGenesis;
import org.aion.zero.impl.GenesisBlockLoader;
Expand Down Expand Up @@ -53,6 +54,7 @@ public CfgAion() {
this.reports = new CfgReports();
this.gui = new CfgGui();
this.fork = new CfgFork();
this.vm = new CfgVm();
initializeConfiguration();
}

Expand Down
Expand Up @@ -123,7 +123,7 @@ private List<AionTransaction> castRawTx(List<byte[]> broadCastTx) {
AionTransaction tx = new AionTransaction(raw);
if (tx.getTransactionHash() != null) {
if (!TXValidator.isInCache(ByteArrayWrapper.wrap(tx.getTransactionHash()))) {
if (TXValidator.isValid(tx)) {
if (pendingState.isValid(tx)) {
rtn.add(tx);
}
}
Expand Down
@@ -0,0 +1,32 @@
package org.aion.zero.impl.valid;

import static org.aion.mcf.valid.TransactionTypeRule.isValidAVMTransactionType;
import static org.aion.mcf.valid.TransactionTypeRule.isValidFVMTransactionType;

import org.aion.zero.types.AionTransaction;

/**
* Validator for the type field of transactions allowed by the network. The transaction types
* currently correlate with which virtual machines are enabled. This field mainly impacts contract
* creation. Contracts created using the default transaction type should be deployed on the FastVM.
* AVM contracts creations/transactions are declared valid only if the AVM is enabled from the
* configuration and the transaction has the correct type associated with the AVM.
*
* @author Alexandra Roatis
*/
public class TransactionTypeValidator {

private boolean avmEnabled;

public TransactionTypeValidator(boolean enableAVM) {
this.avmEnabled = enableAVM;
}

public boolean isValid(AionTransaction tx) {
// verify transaction type
byte type = tx.getTransactionType();
// the type must be either valid for the FVM
// or for the AVM when the AVM is enabled
return isValidFVMTransactionType(type) || (avmEnabled && isValidAVMTransactionType(type));
}
}
2 changes: 2 additions & 0 deletions modMcf/src/org/aion/mcf/blockchain/IPendingState.java
Expand Up @@ -12,6 +12,8 @@ public interface IPendingState<TX extends ITransaction> {

TxResponse addPendingTransaction(TX tx);

boolean isValid(TX tx);

IRepositoryCache<?, ?> getRepository();

List<TX> getPendingTransactions();
Expand Down
4 changes: 2 additions & 2 deletions modMcf/src/org/aion/mcf/types/AbstractTransaction.java
@@ -1,8 +1,8 @@
package org.aion.mcf.types;

import java.math.BigInteger;
import org.aion.base.type.AionAddress;
import org.aion.base.type.ITransaction;
import org.aion.base.vm.VirtualMachineSpecs;
import org.aion.crypto.ISignature;
import org.aion.log.AionLoggerFactory;
import org.aion.log.LogEnum;
Expand Down Expand Up @@ -55,7 +55,7 @@ public AbstractTransaction(byte[] nonce, Address receiveAddress, byte[] value, b
this.value = value;
this.data = data;
// default type 0x01; reserve date for multi-type transaction
this.type = 0x01;
this.type = VirtualMachineSpecs.FVM_DEFAULT_TX_TYPE;
}

public AbstractTransaction(
Expand Down
19 changes: 19 additions & 0 deletions modMcf/src/org/aion/mcf/valid/TransactionTypeRule.java
@@ -0,0 +1,19 @@
package org.aion.mcf.valid;

import org.aion.base.vm.VirtualMachineSpecs;

/**
* Rules for validating transactions based on allowed types.
*
* @author Alexandra Roatis
*/
public class TransactionTypeRule {

public static boolean isValidFVMTransactionType(byte type) {
return type == VirtualMachineSpecs.FVM_DEFAULT_TX_TYPE;
}

public static boolean isValidAVMTransactionType(byte type) {
return type == VirtualMachineSpecs.AVM_VM_CODE;
}
}

0 comments on commit 9a30633

Please sign in to comment.