Skip to content

Commit

Permalink
separate use of Address interface vs AionAddress implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexandraRoatis committed Jan 28, 2019
1 parent 3b4fde3 commit d6efe30
Show file tree
Hide file tree
Showing 16 changed files with 1,611 additions and 1,505 deletions.
6 changes: 3 additions & 3 deletions modFastVM/src/org/aion/fastvm/Callback.java
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,7 @@ private static FastVmTransactionResult doCreate(ExecutionContext ctx, FastVM jit

// compute new address
byte[] nonce = track.getNonce(ctx.getSenderAddress()).toByteArray();
AionAddress newAddress =
Address newAddress =
AionAddress.wrap(HashUtil.calcNewAddr(ctx.getSenderAddress().toBytes(), nonce));
ctx.setDestinationAddress(newAddress);

Expand Down Expand Up @@ -415,10 +415,10 @@ protected static ExecutionContext parseMessage(byte[] message) {

byte[] txHash = prev.getTransactionHash();

byte[] address = new byte[AionAddress.SIZE];
byte[] address = new byte[Address.SIZE];
buffer.get(address);
Address origin = prev.getOriginAddress();
byte[] caller = new byte[AionAddress.SIZE];
byte[] caller = new byte[Address.SIZE];
buffer.get(caller);

IDataWord nrgPrice = new DataWord(prev.getTransactionEnergyPrice());
Expand Down
9 changes: 2 additions & 7 deletions modFastVM/src/org/aion/fastvm/ExecutionContext.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,10 @@
import java.math.BigInteger;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import org.aion.base.type.AionAddress;
import org.aion.base.vm.IDataWord;
import org.aion.mcf.vm.types.DataWord;
import org.aion.mcf.vm.types.DoubleDataWord;
import org.aion.vm.api.interfaces.Address;
import org.aion.base.vm.IDataWord;
import org.aion.vm.api.interfaces.TransactionContext;
import org.aion.vm.api.interfaces.TransactionSideEffects;
import org.aion.zero.types.AionTransaction;
Expand All @@ -41,10 +40,7 @@
*/
public class ExecutionContext implements TransactionContext {
private static final int ENCODE_BASE_LEN =
(AionAddress.SIZE * 4)
+ (DataWord.BYTES * 3)
+ (Long.BYTES * 4)
+ (Integer.BYTES * 4);
(Address.SIZE * 4) + (DataWord.BYTES * 3) + (Long.BYTES * 4) + (Integer.BYTES * 4);
public static int CALL = 0;
public static int DELEGATECALL = 1;
public static int CALLCODE = 2;
Expand Down Expand Up @@ -312,5 +308,4 @@ public byte[] getHashOfOriginTransaction() {
public AionTransaction getTransaction() {
return this.transaction;
}

}
19 changes: 8 additions & 11 deletions modFastVM/src/org/aion/fastvm/TransactionExecutor.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@
package org.aion.fastvm;

import java.math.BigInteger;
import org.aion.base.type.AionAddress;
import org.aion.precompiled.ContractFactory;
import org.aion.precompiled.type.PrecompiledContract;
import org.aion.vm.api.interfaces.Address;
import org.aion.vm.api.interfaces.KernelInterface;
import org.aion.vm.api.interfaces.TransactionContext;
import org.aion.vm.api.interfaces.TransactionInterface;
Expand All @@ -49,17 +49,14 @@ public class TransactionExecutor {
private TransactionInterface transaction;

public TransactionExecutor(
TransactionInterface transaction,
TransactionContext context,
KernelInterface kernel) {
TransactionInterface transaction, TransactionContext context, KernelInterface kernel) {

this.kernelParent = kernel;
this.kernelChild = this.kernelParent.startTracking();
this.transaction = transaction;
this.context = context;

long energyLeft =
this.transaction.getEnergyLimit() - this.transaction.getTransactionCost();
long energyLeft = this.transaction.getEnergyLimit() - this.transaction.getTransactionCost();
this.transactionResult =
new FastVmTransactionResult(FastVmResultCode.SUCCESS, energyLeft, new byte[0]);
}
Expand Down Expand Up @@ -143,7 +140,8 @@ private boolean performChecks() {
// check balance
BigInteger txValue = new BigInteger(1, this.transaction.getValue());
BigInteger txTotal = txNrgPrice.multiply(BigInteger.valueOf(txNrgLimit)).add(txValue);
if (!this.kernelParent.accountBalanceIsAtLeast(this.transaction.getSenderAddress(), txTotal)) {
if (!this.kernelParent.accountBalanceIsAtLeast(
this.transaction.getSenderAddress(), txTotal)) {
transactionResult.setResultCode(FastVmResultCode.INSUFFICIENT_BALANCE);
transactionResult.setEnergyRemaining(0);
return false;
Expand All @@ -160,8 +158,7 @@ private void executeNonContractCreationTransaction() {
PrecompiledContract pc =
precompiledFactory.getPrecompiledContract(this.context, this.kernelChild);
if (pc != null) {
transactionResult =
pc.execute(transaction.getData(), context.getTransactionEnergy());
transactionResult = pc.execute(transaction.getData(), context.getTransactionEnergy());
} else {
// execute code
byte[] code = this.kernelChild.getCode(transaction.getDestinationAddress());
Expand All @@ -179,8 +176,8 @@ private void executeNonContractCreationTransaction() {

/** Prepares contract create. */
private void executeContractCreationTransaction() {
//TODO: computing contract address needs to be done correctly. This is a hack.
AionAddress contractAddress = ((AionTransaction) transaction).getContractAddress();
// TODO: computing contract address needs to be done correctly. This is a hack.
Address contractAddress = ((AionTransaction) transaction).getContractAddress();

if (this.kernelChild.hasAccountState(contractAddress)) {
transactionResult.setResultCode(FastVmResultCode.FAILURE);
Expand Down
19 changes: 12 additions & 7 deletions modFastVM/test/org/aion/fastvm/CacheTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,21 @@
import org.aion.base.type.AionAddress;
import org.aion.base.util.Hex;
import org.aion.mcf.vm.types.DataWord;
import org.aion.vm.DummyRepository;
import org.aion.mcf.vm.types.KernelInterfaceForFastVM;
import org.aion.vm.DummyRepository;
import org.aion.vm.api.interfaces.Address;
import org.apache.commons.lang3.RandomUtils;
import org.junit.Before;
import org.junit.Ignore;
import org.junit.Test;

public class CacheTest {
private byte[] txHash = RandomUtils.nextBytes(32);
private AionAddress origin = AionAddress.wrap(RandomUtils.nextBytes(32));
private AionAddress caller = origin;
private AionAddress address = AionAddress.wrap(RandomUtils.nextBytes(32));
private Address origin = AionAddress.wrap(RandomUtils.nextBytes(32));
private Address caller = origin;
private Address address = AionAddress.wrap(RandomUtils.nextBytes(32));

private AionAddress blockCoinbase = AionAddress.wrap(RandomUtils.nextBytes(32));
private Address blockCoinbase = AionAddress.wrap(RandomUtils.nextBytes(32));
private long blockNumber = 1;
private long blockTimestamp = System.currentTimeMillis() / 1000;
private long blockNrgLimit = 5000000;
Expand Down Expand Up @@ -50,7 +51,7 @@ public void testCache() {
callData = Hex.decode("8256cff3");
ExecutionContext ctx =
new ExecutionContext(
null,
null,
txHash,
address,
origin,
Expand All @@ -73,7 +74,11 @@ public void testCache() {
int repeat = 1000;
for (int i = 0; i < repeat; i++) {
byte[] code = generateContract(i);
FastVmTransactionResult result = vm.run(code, ctx, new KernelInterfaceForFastVM(new DummyRepository(), true, false));
FastVmTransactionResult result =
vm.run(
code,
ctx,
new KernelInterfaceForFastVM(new DummyRepository(), true, false));
assertEquals(FastVmResultCode.SUCCESS, result.getResultCode());

if (i % 100 == 0) {
Expand Down
Loading

0 comments on commit d6efe30

Please sign in to comment.