Skip to content

Commit

Permalink
isPrecompiledContract() moved into PrecompiledLookup
Browse files Browse the repository at this point in the history
- This method doesn't belong in a factory class, just separating it out.
  • Loading branch information
aionick committed Jul 11, 2019
1 parent 8aac86f commit 76405b2
Show file tree
Hide file tree
Showing 9 changed files with 43 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
import org.aion.mcf.db.InternalVmType;
import org.aion.mcf.ds.XorDataSource;
import org.aion.mcf.trie.SecureTrie;
import org.aion.precompiled.ContractFactory;
import org.aion.precompiled.PrecompiledLookup;
import org.aion.rlp.RLP;
import org.aion.rlp.RLPElement;
import org.aion.rlp.RLPItem;
Expand Down Expand Up @@ -438,7 +438,7 @@ public void setAddress(AionAddress address) {
throw new IllegalArgumentException("Address can not be null!");
}
this.address = address;
if (ContractFactory.isPrecompiledContract(address)) {
if (PrecompiledLookup.isPrecompiledContract(address)) {
setVmType(InternalVmType.FVM);
}
this.rlpEncoded = null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
import org.aion.mcf.db.InternalVmType;
import org.aion.mcf.db.Repository;
import org.aion.mcf.db.RepositoryCache;
import org.aion.precompiled.ContractFactory;
import org.aion.precompiled.PrecompiledLookup;
import org.aion.types.AionAddress;
import org.aion.util.types.ByteArrayWrapper;
import org.slf4j.Logger;
Expand Down Expand Up @@ -338,7 +338,7 @@ public void saveVmType(AionAddress contract, InternalVmType vmType) {
/** IMPORTNAT: a new cache must be created before calling this method */
@Override
public InternalVmType getVmType(AionAddress contract) {
if (ContractFactory.isPrecompiledContract(contract)) {
if (PrecompiledLookup.isPrecompiledContract(contract)) {
// skip the call to disk
return InternalVmType.FVM;
}
Expand Down
6 changes: 3 additions & 3 deletions modAionImpl/src/org/aion/zero/impl/db/AionRepositoryImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
import org.aion.mcf.trie.TrieImpl;
import org.aion.mcf.trie.TrieNodeResult;
import org.aion.p2p.V1Constants;
import org.aion.precompiled.ContractFactory;
import org.aion.precompiled.PrecompiledLookup;
import org.aion.rlp.RLP;
import org.aion.rlp.RLPElement;
import org.aion.rlp.RLPList;
Expand Down Expand Up @@ -147,7 +147,7 @@ public void updateBatch(

if (!contractDetails.isDirty()
|| (contractDetails.getVmType() == InternalVmType.EITHER
&& !ContractFactory.isPrecompiledContract(address))) {
&& !PrecompiledLookup.isPrecompiledContract(address))) {
// code added because contract details are not reliably
// marked as dirty at present
// TODO: issue above will be solved with the conversion to a
Expand Down Expand Up @@ -1068,7 +1068,7 @@ public void saveIndexedContractInformation(
}

public InternalVmType getVMUsed(AionAddress contract) {
if (ContractFactory.isPrecompiledContract(contract)) {
if (PrecompiledLookup.isPrecompiledContract(contract)) {
// skip the call to disk
return InternalVmType.FVM;
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
import org.aion.base.AionTransaction;
import org.aion.crypto.ECKey;
import org.aion.mcf.core.ImportResult;
import org.aion.precompiled.ContractFactory;
import org.aion.precompiled.ContractInfo;
import org.aion.precompiled.PrecompiledLookup;
import org.aion.types.AionAddress;
import org.aion.util.conversions.Hex;
import org.aion.util.types.AddressUtils;
Expand Down Expand Up @@ -73,7 +73,7 @@ public void testTransferToPrecompiledContract() {
AionAddress bridge =
AddressUtils.wrapAddress(
"0000000000000000000000000000000000000000000000000000000000000200");
assertThat(ContractFactory.isPrecompiledContract(bridge)).isTrue();
assertThat(PrecompiledLookup.isPrecompiledContract(bridge)).isTrue();

// Make balance transfer transaction to precompiled contract.
ECKey key = org.aion.crypto.ECKeyFac.inst().fromPrivate(SENDER_KEY);
Expand Down Expand Up @@ -133,7 +133,7 @@ public void testCallToPrecompiledContract() {
AionAddress bridge =
AddressUtils.wrapAddress(
"0000000000000000000000000000000000000000000000000000000000000200");
assertThat(ContractFactory.isPrecompiledContract(bridge)).isTrue();
assertThat(PrecompiledLookup.isPrecompiledContract(bridge)).isTrue();

// Make call transaction to precompiled contract.
ECKey key = org.aion.crypto.ECKeyFac.inst().fromPrivate(SENDER_KEY);
Expand Down Expand Up @@ -193,7 +193,7 @@ public void testTransferToPrecompiledBlake2bContractWithoutData() {

// get contract address from precompiled factory
AionAddress blake2b = ContractInfo.BLAKE_2B.contractAddress;
assertThat(ContractFactory.isPrecompiledContract(blake2b)).isTrue();
assertThat(PrecompiledLookup.isPrecompiledContract(blake2b)).isTrue();

// Make balance transfer transaction to precompiled contract.
ECKey key = org.aion.crypto.ECKeyFac.inst().fromPrivate(SENDER_KEY);
Expand Down Expand Up @@ -258,7 +258,7 @@ public void testTransferToPrecompiledBlake2bContractWithData() {

// get contract address from precompiled factory
AionAddress blake2b = ContractInfo.BLAKE_2B.contractAddress;
assertThat(ContractFactory.isPrecompiledContract(blake2b)).isTrue();
assertThat(PrecompiledLookup.isPrecompiledContract(blake2b)).isTrue();

// Make balance transfer transaction to precompiled contract.
ECKey key = org.aion.crypto.ECKeyFac.inst().fromPrivate(SENDER_KEY);
Expand Down Expand Up @@ -326,7 +326,7 @@ public void testCallToPrecompiledBlake2bContract() {

// get contract address from precompiled factory
AionAddress blake2b = ContractInfo.BLAKE_2B.contractAddress;
assertThat(ContractFactory.isPrecompiledContract(blake2b)).isTrue();
assertThat(PrecompiledLookup.isPrecompiledContract(blake2b)).isTrue();

// Make call transaction to precompiled contract.
ECKey key = org.aion.crypto.ECKeyFac.inst().fromPrivate(SENDER_KEY);
Expand Down
16 changes: 0 additions & 16 deletions modPrecompiled/src/org/aion/precompiled/ContractFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -66,20 +66,4 @@ public PrecompiledContract getPrecompiledContract(
return null;
}
}

/**
* Returns true if address is the address of a pre-compiled contract and false otherwise.
*
* @param address The address to check.
* @return true iff address is address of a pre-compiled contract.
*/
public static boolean isPrecompiledContract(AionAddress address) {
for (ContractInfo contractInfo : ContractInfo.values()) {
if ((contractInfo != ContractInfo.TOTAL_CURRENCY)
&& (address.equals(contractInfo.contractAddress))) {
return true;
}
}
return false;
}
}
22 changes: 22 additions & 0 deletions modPrecompiled/src/org/aion/precompiled/PrecompiledLookup.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package org.aion.precompiled;

import org.aion.types.AionAddress;

public final class PrecompiledLookup {

/**
* Returns true if address is the address of a pre-compiled contract and false otherwise.
*
* @param address The address to check.
* @return true iff address is address of a pre-compiled contract.
*/
public static boolean isPrecompiledContract(AionAddress address) {
for (ContractInfo contractInfo : ContractInfo.values()) {
if ((contractInfo != ContractInfo.TOTAL_CURRENCY)
&& (address.equals(contractInfo.contractAddress))) {
return true;
}
}
return false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import org.aion.base.AionTransaction;
import org.aion.mcf.types.KernelInterface;
import org.aion.precompiled.ContractFactory;
import org.aion.precompiled.PrecompiledLookup;
import org.aion.precompiled.PrecompiledResultCode;
import org.aion.precompiled.PrecompiledTransactionResult;
import org.aion.types.AionAddress;
Expand Down Expand Up @@ -97,7 +98,7 @@ public static PrecompiledTransactionResult runPrecompiledContractCall(
precompiledFactory.getPrecompiledContract(context, kernel);

// Ensure we actually have a precompiled contract as our destination.
if (!ContractFactory.isPrecompiledContract(transaction.getDestinationAddress())) {
if (!PrecompiledLookup.isPrecompiledContract(transaction.getDestinationAddress())) {
throw new IllegalStateException("Expected destination to be a precompiled contract!");
}

Expand Down
8 changes: 4 additions & 4 deletions modVM/src/org/aion/vm/BulkExecutor.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import org.aion.mcf.db.InternalVmType;
import org.aion.mcf.db.RepositoryCache;
import org.aion.mcf.valid.TransactionTypeRule;
import org.aion.precompiled.ContractFactory;
import org.aion.precompiled.PrecompiledLookup;
import org.aion.types.AionAddress;
import org.aion.vm.exception.VMException;
import org.aion.zero.types.AionTxExecSummary;
Expand Down Expand Up @@ -583,16 +583,16 @@ private static boolean destinationIsFvmContract(
if (vmType == InternalVmType.UNKNOWN) {
// will load contract into memory otherwise leading to consensus issues
RepositoryCache track = repository.startTracking();
return !ContractFactory.isPrecompiledContract(destination)
return !PrecompiledLookup.isPrecompiledContract(destination)
&& (track.getVmType(destination) == InternalVmType.FVM);
} else {
return !ContractFactory.isPrecompiledContract(destination)
return !PrecompiledLookup.isPrecompiledContract(destination)
&& (vmType == InternalVmType.FVM);
}
}

/** Returns true only if the given destination address is a precompiled contract address. */
private static boolean destinationIsPrecompiledContract(AionAddress destination) {
return ContractFactory.isPrecompiledContract(destination);
return PrecompiledLookup.isPrecompiledContract(destination);
}
}
4 changes: 2 additions & 2 deletions modVM/src/org/aion/vm/ExternalStateForAvm.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import org.aion.mcf.vm.DataWord;
import org.aion.mcf.vm.types.DataWordImpl;
import org.aion.mcf.vm.types.DoubleDataWord;
import org.aion.precompiled.ContractFactory;
import org.aion.precompiled.PrecompiledLookup;
import org.aion.types.AionAddress;
import org.aion.util.types.ByteArrayWrapper;

Expand Down Expand Up @@ -226,7 +226,7 @@ public boolean isValidEnergyLimitForNonCreate(long energyLimit) {
@Override
public boolean destinationAddressIsSafeForThisVM(AionAddress address) {
// Avm cannot run pre-compiled contracts.
if (ContractFactory.isPrecompiledContract(address)) {
if (PrecompiledLookup.isPrecompiledContract(address)) {
return false;
}

Expand Down

0 comments on commit 76405b2

Please sign in to comment.