Skip to content

Commit

Permalink
InternalVmType class used by the interface methods
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexandraRoatis committed Apr 25, 2019
1 parent 98a74e9 commit 62dae10
Show file tree
Hide file tree
Showing 22 changed files with 130 additions and 149 deletions.
6 changes: 3 additions & 3 deletions modAionImpl/src/org/aion/zero/impl/AionBlockchainImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import org.aion.evtmgr.IEvent;
import org.aion.evtmgr.IEventMgr;
import org.aion.evtmgr.impl.evt.EventBlock;
import org.aion.interfaces.db.InternalVmType;
import org.aion.interfaces.db.Repository;
import org.aion.interfaces.db.RepositoryCache;
import org.aion.log.AionLoggerFactory;
Expand All @@ -46,7 +47,6 @@
import org.aion.mcf.trie.Trie;
import org.aion.mcf.trie.TrieImpl;
import org.aion.mcf.trie.TrieNodeResult;
import org.aion.mcf.tx.InternalVmType;
import org.aion.mcf.types.BlockIdentifierImpl;
import org.aion.mcf.valid.BlockHeaderValidator;
import org.aion.mcf.valid.GrandParentBlockHeaderValidator;
Expand Down Expand Up @@ -1029,8 +1029,8 @@ public AionBlockSummary add(AionBlock block, boolean rebuild) {
tx.getContractAddress(),
block.getNumber(),
TransactionTypeRule.isValidAVMContractDeployment(tx.getTargetVM())
? InternalVmType.AVM.getCode()
: InternalVmType.FVM.getCode(),
? InternalVmType.AVM
: InternalVmType.FVM,
true);
}
}
Expand Down
4 changes: 2 additions & 2 deletions modAionImpl/src/org/aion/zero/impl/AionHubUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

import java.math.BigInteger;
import java.util.Map;
import org.aion.interfaces.db.InternalVmType;
import org.aion.interfaces.db.RepositoryCache;
import org.aion.mcf.tx.InternalVmType;
import org.aion.mcf.vm.types.DataWordImpl;
import org.aion.precompiled.ContractFactory;
import org.aion.types.Address;
Expand All @@ -20,7 +20,7 @@ public static void buildGenesis(AionGenesis genesis, AionRepositoryImpl reposito
Address networkBalanceAddress = ContractFactory.getTotalCurrencyContractAddress();
track.createAccount(networkBalanceAddress);
// saving FVM type for networkBalance contract
track.saveVmType(networkBalanceAddress, InternalVmType.FVM.getCode());
track.saveVmType(networkBalanceAddress, InternalVmType.FVM);

for (Map.Entry<Integer, BigInteger> addr : genesis.getNetworkBalances().entrySet()) {
// assumes only additions are performed in the genesis
Expand Down
6 changes: 2 additions & 4 deletions modAionImpl/src/org/aion/zero/impl/StandaloneBlockchain.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@
import org.aion.db.impl.DBVendor;
import org.aion.db.impl.DatabaseFactory;
import org.aion.interfaces.db.ContractDetails;
import org.aion.interfaces.db.InternalVmType;
import org.aion.interfaces.db.PruneConfig;
import org.aion.interfaces.db.RepositoryCache;
import org.aion.interfaces.db.RepositoryConfig;
import org.aion.mcf.config.CfgPrune;
import org.aion.mcf.core.AccountState;
import org.aion.mcf.core.ImportResult;
import org.aion.mcf.tx.InternalVmType;
import org.aion.mcf.valid.BlockHeaderValidator;
import org.aion.mcf.vm.types.DataWordImpl;
import org.aion.precompiled.ContractFactory;
Expand Down Expand Up @@ -360,9 +360,7 @@ public AbstractEnergyStrategyLimit getEnergyLimitStrategy() {

RepositoryCache track = bc.getRepository().startTracking();
track.createAccount(ContractFactory.getTotalCurrencyContractAddress());
track.saveVmType(
ContractFactory.getTotalCurrencyContractAddress(),
InternalVmType.FVM.getCode());
track.saveVmType(ContractFactory.getTotalCurrencyContractAddress(), InternalVmType.FVM);

for (Map.Entry<Integer, BigInteger> key : genesis.getNetworkBalances().entrySet()) {
// assumes only additions can be made in the genesis
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import java.util.HashMap;
import java.util.Map;
import org.aion.interfaces.db.ContractDetails;
import org.aion.mcf.tx.InternalVmType;
import org.aion.interfaces.db.InternalVmType;
import org.aion.types.ByteArrayWrapper;
import org.aion.util.conversions.Hex;

Expand Down
40 changes: 31 additions & 9 deletions modAionImpl/src/org/aion/zero/impl/db/AionContractDetailsImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@
import java.util.Optional;
import org.aion.interfaces.db.ByteArrayKeyValueStore;
import org.aion.interfaces.db.ContractDetails;
import org.aion.interfaces.db.InternalVmType;
import org.aion.mcf.ds.XorDataSource;
import org.aion.mcf.trie.SecureTrie;
import org.aion.mcf.tx.InternalVmType;
import org.aion.precompiled.ContractFactory;
import org.aion.rlp.RLP;
import org.aion.rlp.RLPElement;
Expand Down Expand Up @@ -117,17 +117,17 @@ public ByteArrayWrapper get(ByteArrayWrapper key) {
: new ByteArrayWrapper(RLP.decode2(data).get(0).getRLPData());
}

public void setVmType(byte vmType) {
if (this.vmType.getCode() != vmType && vmType != InternalVmType.EITHER.getCode()) {
this.vmType = InternalVmType.getInstance(vmType);
public void setVmType(InternalVmType vmType) {
if (this.vmType != vmType && vmType != InternalVmType.EITHER) {
this.vmType = vmType;

setDirty(true);
rlpEncoded = null;
}
}

public byte getVmType() {
return vmType.getCode();
public InternalVmType getVmType() {
return vmType;
}

@Override
Expand Down Expand Up @@ -219,8 +219,9 @@ public void decode(byte[] rlpCode, boolean fastCheck) {
boolean keepStorageInMem = decodeEncodingWithoutVmType(rlpList, fastCheck);

if (rlpList.size() == 5) {
// only FVM contracts used the old encoding
vmType = InternalVmType.FVM;
// the old encoding is used by FVM contracts
// or by accounts accidentally mislabeled as contracts (issue in the repository cache)
vmType = InternalVmType.UNKNOWN;

// force a save with new encoding
this.rlpEncoded = null;
Expand All @@ -239,8 +240,29 @@ public void decode(byte[] rlpCode, boolean fastCheck) {
this.rlpEncoded = rlpCode;
}

// both sides of the if above can return the UNKNOWN type
// UNKNOWN type + code => FVM contract
byte[] code = getCode();
if (code != null && code.length > 0) {
vmType = InternalVmType.FVM;
// force a save with new encoding
this.rlpEncoded = null;
}

if (!fastCheck || externalStorage || !keepStorageInMem) { // it was not a fast check
decodeStorage(rlpList.get(2), rlpList.get(3), keepStorageInMem);

if (vmType == InternalVmType.UNKNOWN) {
if (!Arrays.equals(storageTrie.getRootHash(), EMPTY_TRIE_HASH)) {
// old encoding of FVM contract without code
vmType = InternalVmType.FVM;
} else {
// account mislabeled as contract
vmType = InternalVmType.EITHER;
}
// force a save with new encoding
this.rlpEncoded = null;
}
}
}

Expand Down Expand Up @@ -414,7 +436,7 @@ public void setAddress(Address address) {
}
this.address = address;
if (ContractFactory.isPrecompiledContract(address)) {
setVmType(InternalVmType.FVM.getCode());
setVmType(InternalVmType.FVM);
}
this.rlpEncoded = null;
}
Expand Down
10 changes: 5 additions & 5 deletions modAionImpl/src/org/aion/zero/impl/db/AionRepositoryCache.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@
import java.util.concurrent.locks.ReentrantReadWriteLock;
import org.aion.interfaces.db.ByteArrayKeyValueStore;
import org.aion.interfaces.db.ContractDetails;
import org.aion.interfaces.db.InternalVmType;
import org.aion.interfaces.db.Repository;
import org.aion.interfaces.db.RepositoryCache;
import org.aion.log.AionLoggerFactory;
import org.aion.log.LogEnum;
import org.aion.mcf.core.AccountState;
import org.aion.mcf.db.IBlockStoreBase;
import org.aion.mcf.tx.InternalVmType;
import org.aion.precompiled.ContractFactory;
import org.aion.types.Address;
import org.aion.types.ByteArrayWrapper;
Expand Down Expand Up @@ -326,7 +326,7 @@ public byte[] getTransformedCode(Address address) {
}

@Override
public void saveVmType(Address contract, byte vmType) {
public void saveVmType(Address contract, InternalVmType vmType) {
fullyWriteLock();
try {
getContractDetails(contract).setVmType(vmType);
Expand All @@ -337,10 +337,10 @@ public void saveVmType(Address contract, byte vmType) {

/** IMPORTNAT: a new cache must be created before calling this method */
@Override
public byte getVmType(Address contract) {
public InternalVmType getVmType(Address contract) {
if (ContractFactory.isPrecompiledContract(contract)) {
// skip the call to disk
return InternalVmType.FVM.getCode();
return InternalVmType.FVM;
}
// retrieving the VM type involves updating the contract details values
// this requires loading the account and details
Expand Down Expand Up @@ -690,7 +690,7 @@ public List<byte[]> getCacheTx() {
"getCachelTx should be called on the tracked repository.");
}

public byte getVMUsed(Address contract) {
public InternalVmType getVMUsed(Address contract) {
return repository.getVMUsed(contract);
}

Expand Down
16 changes: 8 additions & 8 deletions modAionImpl/src/org/aion/zero/impl/db/AionRepositoryImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import org.aion.interfaces.db.ByteArrayKeyValueDatabase;
import org.aion.interfaces.db.ByteArrayKeyValueStore;
import org.aion.interfaces.db.ContractDetails;
import org.aion.interfaces.db.InternalVmType;
import org.aion.interfaces.db.Repository;
import org.aion.interfaces.db.RepositoryCache;
import org.aion.interfaces.db.RepositoryConfig;
Expand All @@ -31,7 +32,6 @@
import org.aion.mcf.trie.Trie;
import org.aion.mcf.trie.TrieImpl;
import org.aion.mcf.trie.TrieNodeResult;
import org.aion.mcf.tx.InternalVmType;
import org.aion.p2p.V1Constants;
import org.aion.precompiled.ContractFactory;
import org.aion.rlp.RLP;
Expand Down Expand Up @@ -151,7 +151,7 @@ public void updateBatch(
} else {

if (!contractDetails.isDirty()
|| (contractDetails.getVmType() == InternalVmType.EITHER.getCode()
|| (contractDetails.getVmType() == InternalVmType.EITHER
&& !ContractFactory.isPrecompiledContract(address))) {
// code added because contract details are not reliably
// marked as dirty at present
Expand Down Expand Up @@ -421,9 +421,9 @@ public byte[] getTransformedCode(Address address) {
}

@Override
public byte getVmType(Address contract) {
public InternalVmType getVmType(Address contract) {
ContractDetails details = getContractDetails(contract);
return (details == null) ? InternalVmType.EITHER.getCode() : details.getVmType();
return (details == null) ? InternalVmType.EITHER : details.getVmType();
}

@Override
Expand Down Expand Up @@ -1064,22 +1064,22 @@ public ContractInformation getIndexedContractInformation(Address contract) {
}

public void saveIndexedContractInformation(
Address contract, long inceptionBlock, byte vmUsed, boolean complete) {
Address contract, long inceptionBlock, InternalVmType vmUsed, boolean complete) {
if (contract != null) {
contractInfoSource.put(
contract.toBytes(), new ContractInformation(inceptionBlock, vmUsed, complete));
}
}

public byte getVMUsed(Address contract) {
public InternalVmType getVMUsed(Address contract) {
if (ContractFactory.isPrecompiledContract(contract)) {
// skip the call to disk
return InternalVmType.FVM.getCode();
return InternalVmType.FVM;
} else {
ContractInformation ci = getIndexedContractInformation(contract);
if (ci == null) {
// signals that the value is not set
return InternalVmType.UNKNOWN.getCode();
return InternalVmType.UNKNOWN;
} else {
return ci.getVmUsed();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import java.util.Objects;
import org.aion.interfaces.db.ByteArrayKeyValueStore;
import org.aion.interfaces.db.ContractDetails;
import org.aion.mcf.tx.InternalVmType;
import org.aion.interfaces.db.InternalVmType;
import org.aion.types.Address;
import org.aion.types.ByteArrayWrapper;

Expand Down Expand Up @@ -105,20 +105,20 @@ public ByteArrayWrapper get(ByteArrayWrapper key) {
return value;
}

public void setVmType(byte vmType) {
if (this.vmType.getCode() != vmType && vmType != InternalVmType.EITHER.getCode()) {
this.vmType = InternalVmType.getInstance(vmType);
public void setVmType(InternalVmType vmType) {
if (this.vmType != vmType && vmType != InternalVmType.EITHER) {
this.vmType = vmType;

setDirty(true);
}
}

public byte getVmType() {
public InternalVmType getVmType() {
if (vmType == InternalVmType.EITHER && origContract != null) {
// not necessary to set as dirty
vmType = InternalVmType.getInstance(origContract.getVmType());
vmType = origContract.getVmType();
}
return vmType.getCode();
return vmType;
}

@Override
Expand Down Expand Up @@ -213,7 +213,7 @@ public void commit() {

// passing on the vm type
if (vmType != InternalVmType.EITHER) {
origContract.setVmType(vmType.getCode());
origContract.setVmType(vmType);
}

// passing on the object graph
Expand Down
11 changes: 6 additions & 5 deletions modAionImpl/src/org/aion/zero/impl/db/ContractInformation.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.aion.zero.impl.db;

import java.math.BigInteger;
import org.aion.interfaces.db.InternalVmType;
import org.aion.mcf.ds.Serializer;
import org.aion.rlp.RLP;
import org.aion.rlp.RLPList;
Expand All @@ -18,12 +19,12 @@
*/
public class ContractInformation {
private long inceptionBlock;
private byte vmUsed;
private InternalVmType vmUsed;
private boolean complete;

private ContractInformation() {}

public ContractInformation(long inceptionBlock, byte vmUsed, boolean complete) {
public ContractInformation(long inceptionBlock, InternalVmType vmUsed, boolean complete) {
this.inceptionBlock = inceptionBlock;
this.vmUsed = vmUsed;
this.complete = complete;
Expand All @@ -41,7 +42,7 @@ public ContractInformation(long inceptionBlock, byte vmUsed, boolean complete) {
public byte[] serialize(ContractInformation info) {
// NOTE: not using encodeLong because of the non-standard RLP
byte[] rlpBlockNumber = RLP.encode(info.inceptionBlock);
byte[] rlpVmUsed = RLP.encodeByte(info.vmUsed);
byte[] rlpVmUsed = RLP.encodeByte(info.vmUsed.getCode());
byte[] rlpIsComplete = RLP.encodeByte((byte) (info.complete ? 1 : 0));

return RLP.encodeList(rlpBlockNumber, rlpVmUsed, rlpIsComplete);
Expand Down Expand Up @@ -76,7 +77,7 @@ public ContractInformation deserialize(byte[] rlpEncoded) {
if (array.length != 1) {
return null;
} else {
info.vmUsed = array[0];
info.vmUsed = InternalVmType.getInstance(array[0]);
}

// decode the completeness status
Expand All @@ -97,7 +98,7 @@ public long getInceptionBlock() {
return inceptionBlock;
}

public byte getVmUsed() {
public InternalVmType getVmUsed() {
return vmUsed;
}

Expand Down
Loading

0 comments on commit 62dae10

Please sign in to comment.