Skip to content

Commit

Permalink
Switch to new type AionAddress
Browse files Browse the repository at this point in the history
- This commit changes the AVM to rely on AionAddress, an immutable type found in the aion_types repo
- This commit updates the vm-api jar to also use this new AionAddress
  • Loading branch information
arajasek committed Jun 12, 2019
1 parent 1e896d1 commit cd2e8d4
Show file tree
Hide file tree
Showing 94 changed files with 881 additions and 878 deletions.
Binary file added lib/aion-types-e367876.jar
Binary file not shown.
Binary file renamed lib/vm-api-b6419e2.jar → lib/vm-api-99c0084.jar
Binary file not shown.
6 changes: 4 additions & 2 deletions org.aion.avm.core/build.xml
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,8 @@
<pathelement location="${dir.global.lib}/asm-tree-6.2.1.jar"/>
<pathelement location="${dir.global.lib}/asm-util-6.2.1.jar"/>
<pathelement location="${dir.global.lib}/asm-analysis-6.2.1.jar"/>
<pathelement location="${dir.global.lib}/vm-api-b6419e2.jar"/>
<pathelement location="${dir.global.lib}/vm-api-99c0084.jar"/>
<pathelement location="${dir.global.lib}/aion-types-e367876.jar"/>
<pathelement location="${dir.global.build.main}/org-aion-avm-core.jar"/>
<pathelement location="${dir.global.build.main}/org-aion-avm-rt.jar"/>
<pathelement location="${dir.global.build.main}/org-aion-avm-userlib.jar"/>
Expand All @@ -101,7 +102,8 @@
<pathelement location="${dir.global.lib}/asm-tree-6.2.1.jar"/>
<pathelement location="${dir.global.lib}/asm-util-6.2.1.jar"/>
<pathelement location="${dir.global.lib}/asm-analysis-6.2.1.jar"/>
<pathelement location="${dir.global.lib}/vm-api-b6419e2.jar"/>
<pathelement location="${dir.global.lib}/vm-api-99c0084.jar"/>
<pathelement location="${dir.global.lib}/aion-types-e367876.jar"/>
<pathelement location="${dir.global.build.main}/org-aion-avm-core.jar"/>
<pathelement location="${dir.global.build.main}/org-aion-avm-rt.jar"/>
<pathelement location="${dir.global.build.main}/org-aion-avm-userlib.jar"/>
Expand Down
1 change: 1 addition & 0 deletions org.aion.avm.core/src/module-info.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,5 @@
requires org.objectweb.asm.util;
requires org.objectweb.asm.tree.analysis;
requires aion.vm.api;
requires aion.types;
}
16 changes: 8 additions & 8 deletions org.aion.avm.core/src/org/aion/avm/core/AvmImpl.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.aion.avm.core;

import org.aion.types.AionAddress;
import org.aion.avm.core.util.Helpers;
import org.aion.kernel.*;

Expand All @@ -21,7 +22,6 @@
import org.aion.kernel.AvmTransactionResult.Code;
import org.aion.parallel.AddressResourceMonitor;
import org.aion.parallel.TransactionTask;
import org.aion.vm.api.types.Address;
import org.aion.vm.api.interfaces.KernelInterface;
import org.aion.vm.api.interfaces.SimpleFuture;
import org.aion.vm.api.interfaces.TransactionInterface;
Expand Down Expand Up @@ -216,11 +216,11 @@ private AvmTransactionResult backgroundProcessTransaction(TransactionTask task)
}

// Acquire both sender and target resources
Address sender = tx.senderAddress;
Address target = tx.destinationAddress;
AionAddress sender = tx.senderAddress;
AionAddress target = tx.destinationAddress;

this.resourceMonitor.acquire(sender.toBytes(), task);
this.resourceMonitor.acquire(target.toBytes(), task);
this.resourceMonitor.acquire(sender.toByteArray(), task);
this.resourceMonitor.acquire(target.toByteArray(), task);

// nonce check
if (!task.getThisTransactionalKernel().accountNonceEquals(sender, tx.nonce)) {
Expand Down Expand Up @@ -303,7 +303,7 @@ private AvmTransactionResult runExternalInvoke(KernelInterface parentKernel, Tra

// Sanity checks around energy pricing and nonce are done in the caller.
// balance check
Address sender = tx.senderAddress;
AionAddress sender = tx.senderAddress;
long energyPrice = tx.energyPrice;
BigInteger value = tx.value;
BigInteger transactionCost = BigInteger.valueOf(tx.energyLimit).multiply(BigInteger.valueOf(energyPrice)).add(value);
Expand Down Expand Up @@ -359,7 +359,7 @@ private AvmTransactionResult commonInvoke(KernelInterface parentKernel, Transact
AvmTransactionResult result = new AvmTransactionResult(tx.energyLimit, transactionBaseCost);

// grab the recipient address as either the new contract address or the given account address.
Address recipient = tx.destinationAddress;
AionAddress recipient = tx.destinationAddress;

// conduct value transfer
BigInteger value = tx.value;
Expand All @@ -386,7 +386,7 @@ private AvmTransactionResult commonInvoke(KernelInterface parentKernel, Transact
DAppExecutor.call(this.capabilities, thisTransactionKernel, this, dapp, stateToResume, task, tx, result, this.enableVerboseContractErrors);
} else {
// If we didn't find it there (that is only for reentrant calls so it is rarely found in the stack), try the hot DApp cache.
ByteArrayWrapper addressWrapper = new ByteArrayWrapper(recipient.toBytes());
ByteArrayWrapper addressWrapper = new ByteArrayWrapper(recipient.toByteArray());
LoadedDApp dappInHotCache = this.hotCache.checkout(addressWrapper);
//'parentKernel.getTransformedCode(recipient) != null' means this recipient's DApp is not self-destructed.
if (thisTransactionKernel.getTransformedCode(recipient) != null) {
Expand Down
14 changes: 7 additions & 7 deletions org.aion.avm.core/src/org/aion/avm/core/AvmTransaction.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import java.math.BigInteger;

import org.aion.vm.api.types.Address;
import org.aion.types.AionAddress;
import org.aion.vm.api.interfaces.TransactionInterface;


Expand All @@ -23,8 +23,8 @@ public class AvmTransaction {
public static AvmTransaction from(IExternalCapabilities capabilities, TransactionInterface external) throws IllegalArgumentException {
boolean isCreate = external.isContractCreationTransaction();

Address senderAddress = external.getSenderAddress();
Address destinationAddress = isCreate
AionAddress senderAddress = external.getSenderAddress();
AionAddress destinationAddress = isCreate
? capabilities.generateContractAddress(external)
: external.getDestinationAddress();
byte[] transactionHash = external.getTransactionHash();
Expand All @@ -46,8 +46,8 @@ public static AvmTransaction from(IExternalCapabilities capabilities, Transactio
}


public final Address senderAddress;
public final Address destinationAddress;
public final AionAddress senderAddress;
public final AionAddress destinationAddress;
public final byte[] transactionHash;
public final BigInteger value;
public final BigInteger nonce;
Expand All @@ -56,8 +56,8 @@ public static AvmTransaction from(IExternalCapabilities capabilities, Transactio
public final boolean isCreate;
public final byte[] data;

private AvmTransaction(Address senderAddress
, Address destinationAddress
private AvmTransaction(AionAddress senderAddress
, AionAddress destinationAddress
, byte[] transactionHash
, BigInteger value
, BigInteger nonce
Expand Down
53 changes: 27 additions & 26 deletions org.aion.avm.core/src/org/aion/avm/core/BlockchainRuntimeImpl.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.aion.avm.core;

import java.util.Arrays;
import org.aion.types.AionAddress;
import s.java.math.BigInteger;
import p.avm.Address;
import p.avm.Result;
Expand Down Expand Up @@ -61,8 +62,8 @@ public BlockchainRuntimeImpl(IExternalCapabilities capabilities, KernelInterface
@Override
public Address avm_getAddress() {
if (null == this.addressCache) {
org.aion.vm.api.types.Address address = tx.destinationAddress;
this.addressCache = new Address(address.toBytes().clone());
AionAddress address = tx.destinationAddress;
this.addressCache = new Address(address.toByteArray());
}

return this.addressCache;
Expand All @@ -71,7 +72,7 @@ public Address avm_getAddress() {
@Override
public Address avm_getCaller() {
if (null == this.callerCache) {
this.callerCache = new Address(tx.senderAddress.toBytes().clone());
this.callerCache = new Address(tx.senderAddress.toByteArray());
}

return this.callerCache;
Expand Down Expand Up @@ -136,7 +137,7 @@ public long avm_getBlockEnergyLimit() {
@Override
public Address avm_getBlockCoinbase() {
if (null == this.blockCoinBaseCache) {
this.blockCoinBaseCache = new Address(kernel.getMinerAddress().toBytes().clone());
this.blockCoinBaseCache = new Address(kernel.getMinerAddress().toByteArray());
}

return this.blockCoinBaseCache;
Expand All @@ -159,7 +160,7 @@ public void avm_putStorage(ByteArray key, ByteArray value, boolean requiresRefun
byte[] keyCopy = Arrays.copyOf(key.getUnderlying(), key.getUnderlying().length);
byte[] valueCopy = (value == null) ? null : Arrays.copyOf(value.getUnderlying(), value.getUnderlying().length);

org.aion.vm.api.types.Address contractAddress = this.tx.destinationAddress;
AionAddress contractAddress = this.tx.destinationAddress;
if (value == null) {
kernel.removeStorage(contractAddress, keyCopy);
} else {
Expand All @@ -175,7 +176,7 @@ public ByteArray avm_getStorage(ByteArray key) {
require(key != null, "Key can't be NULL");
require(key.getUnderlying().length == 32, "Key must be 32 bytes");

org.aion.vm.api.types.Address contractAddress = this.tx.destinationAddress;
AionAddress contractAddress = this.tx.destinationAddress;
byte[] data = this.kernel.getStorage(contractAddress, key.getUnderlying());
return (null != data)
? new ByteArray(Arrays.copyOf(data, data.length))
Expand All @@ -188,16 +189,16 @@ public s.java.math.BigInteger avm_getBalance(Address address) {

// Acquire resource before reading
avm.getResourceMonitor().acquire(address.toByteArray(), this.task);
return new s.java.math.BigInteger(this.kernel.getBalance(org.aion.vm.api.types.Address.wrap(address.toByteArray())));
return new s.java.math.BigInteger(this.kernel.getBalance(new AionAddress(address.toByteArray())));
}

@Override
public s.java.math.BigInteger avm_getBalanceOfThisContract() {
// This method can be called inside clinit so CREATE is a valid context.
org.aion.vm.api.types.Address contractAddress = this.tx.destinationAddress;
AionAddress contractAddress = this.tx.destinationAddress;

// Acquire resource before reading
avm.getResourceMonitor().acquire(contractAddress.toBytes(), this.task);
avm.getResourceMonitor().acquire(contractAddress.toByteArray(), this.task);
return new s.java.math.BigInteger(this.kernel.getBalance(contractAddress));
}

Expand All @@ -207,7 +208,7 @@ public int avm_getCodeSize(Address address) {

// Acquire resource before reading
avm.getResourceMonitor().acquire(address.toByteArray(), this.task);
byte[] vc = this.kernel.getCode(org.aion.vm.api.types.Address.wrap(address.toByteArray()));
byte[] vc = this.kernel.getCode(new AionAddress(address.toByteArray()));
return vc == null ? 0 : vc.length;
}

Expand All @@ -218,7 +219,7 @@ public long avm_getRemainingEnergy() {

@Override
public Result avm_call(Address targetAddress, s.java.math.BigInteger value, ByteArray data, long energyLimit) {
org.aion.vm.api.types.Address internalSender = this.tx.destinationAddress;
AionAddress internalSender = this.tx.destinationAddress;

java.math.BigInteger underlyingValue = value.getUnderlying();
require(targetAddress != null, "Destination can't be NULL");
Expand All @@ -233,7 +234,7 @@ public Result avm_call(Address targetAddress, s.java.math.BigInteger value, Byte
throw new CallDepthLimitExceededException("Internal call depth cannot be more than 10");
}

org.aion.vm.api.types.Address target = org.aion.vm.api.types.Address.wrap(targetAddress.toByteArray());
AionAddress target = new AionAddress(targetAddress.toByteArray());
if (!kernel.destinationAddressIsSafeForThisVM(target)) {
throw new IllegalArgumentException("Attempt to execute code using a foreign virtual machine");
}
Expand All @@ -254,7 +255,7 @@ public Result avm_call(Address targetAddress, s.java.math.BigInteger value, Byte

@Override
public Result avm_create(s.java.math.BigInteger value, ByteArray data, long energyLimit) {
org.aion.vm.api.types.Address internalSender = this.tx.destinationAddress;
AionAddress internalSender = this.tx.destinationAddress;

java.math.BigInteger underlyingValue = value.getUnderlying();
require(underlyingValue.compareTo(java.math.BigInteger.ZERO) >= 0 , "Value can't be negative");
Expand Down Expand Up @@ -291,29 +292,29 @@ private void require(boolean condition, String message) {
public void avm_selfDestruct(Address beneficiary) {
require(null != beneficiary, "Beneficiary can't be NULL");

org.aion.vm.api.types.Address contractAddr = this.tx.destinationAddress;
AionAddress contractAddress = this.tx.destinationAddress;

// Acquire beneficiary address, the address of current contract is already locked at this stage.
this.avm.getResourceMonitor().acquire(beneficiary.toByteArray(), this.task);

// Value transfer
java.math.BigInteger balanceToTransfer = this.kernel.getBalance(contractAddr);
this.kernel.adjustBalance(contractAddr, balanceToTransfer.negate());
this.kernel.adjustBalance(org.aion.vm.api.types.Address.wrap(beneficiary.toByteArray()), balanceToTransfer);
java.math.BigInteger balanceToTransfer = this.kernel.getBalance(contractAddress);
this.kernel.adjustBalance(contractAddress, balanceToTransfer.negate());
this.kernel.adjustBalance(new AionAddress(beneficiary.toByteArray()), balanceToTransfer);

// Delete Account
// Note that the account being deleted means it will still run but no DApp which sees this delete
// (the current one and any callers, or any later transactions, assuming this commits) will be able
// to invoke it (the code will be missing).
this.kernel.deleteAccount(contractAddr);
task.addSelfDestructAddress(contractAddr);
this.kernel.deleteAccount(contractAddress);
task.addSelfDestructAddress(contractAddress);
}

@Override
public void avm_log(ByteArray data) {
require(null != data, "data can't be NULL");

Log log = new Log(this.capabilities, tx.destinationAddress.toBytes(),
Log log = new Log(this.capabilities, tx.destinationAddress.toByteArray(),
List.of(),
data.getUnderlying()
);
Expand All @@ -325,7 +326,7 @@ public void avm_log(ByteArray topic1, ByteArray data) {
require(null != topic1, "topic1 can't be NULL");
require(null != data, "data can't be NULL");

Log log = new Log(this.capabilities, tx.destinationAddress.toBytes(),
Log log = new Log(this.capabilities, tx.destinationAddress.toByteArray(),
List.of(LogSizeUtils.truncatePadTopic(topic1.getUnderlying())),
data.getUnderlying()
);
Expand All @@ -338,7 +339,7 @@ public void avm_log(ByteArray topic1, ByteArray topic2, ByteArray data) {
require(null != topic2, "topic2 can't be NULL");
require(null != data, "data can't be NULL");

Log log = new Log(this.capabilities, tx.destinationAddress.toBytes(),
Log log = new Log(this.capabilities, tx.destinationAddress.toByteArray(),
List.of(LogSizeUtils.truncatePadTopic(topic1.getUnderlying()), LogSizeUtils.truncatePadTopic(topic2.getUnderlying())),
data.getUnderlying()
);
Expand All @@ -352,7 +353,7 @@ public void avm_log(ByteArray topic1, ByteArray topic2, ByteArray topic3, ByteAr
require(null != topic3, "topic3 can't be NULL");
require(null != data, "data can't be NULL");

Log log = new Log(this.capabilities, tx.destinationAddress.toBytes(),
Log log = new Log(this.capabilities, tx.destinationAddress.toByteArray(),
List.of(LogSizeUtils.truncatePadTopic(topic1.getUnderlying()), LogSizeUtils.truncatePadTopic(topic2.getUnderlying()), LogSizeUtils.truncatePadTopic(topic3.getUnderlying())),
data.getUnderlying()
);
Expand All @@ -367,7 +368,7 @@ public void avm_log(ByteArray topic1, ByteArray topic2, ByteArray topic3, ByteAr
require(null != topic4, "topic4 can't be NULL");
require(null != data, "data can't be NULL");

Log log = new Log(this.capabilities, tx.destinationAddress.toBytes(),
Log log = new Log(this.capabilities, tx.destinationAddress.toByteArray(),
List.of(LogSizeUtils.truncatePadTopic(topic1.getUnderlying()), LogSizeUtils.truncatePadTopic(topic2.getUnderlying()), LogSizeUtils.truncatePadTopic(topic3.getUnderlying()), LogSizeUtils.truncatePadTopic(topic4.getUnderlying())),
data.getUnderlying()
);
Expand Down Expand Up @@ -457,8 +458,8 @@ private Result runInternalCall(InternalTransaction internalTx) {
AvmTransaction avmTransaction = AvmTransaction.from(this.capabilities, internalTx);

// Acquire the target of the internal transaction
org.aion.vm.api.types.Address target = avmTransaction.destinationAddress;
avm.getResourceMonitor().acquire(target.toBytes(), task);
AionAddress target = avmTransaction.destinationAddress;
avm.getResourceMonitor().acquire(target.toByteArray(), task);

// execute the internal transaction
AvmTransactionResult newResult = null;
Expand Down
8 changes: 3 additions & 5 deletions org.aion.avm.core/src/org/aion/avm/core/DAppCreator.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package org.aion.avm.core;

import org.aion.avm.NameStyle;
import org.aion.types.AionAddress;
import org.aion.avm.RuntimeMethodFeeSchedule;
import org.aion.avm.StorageFees;
import org.aion.avm.core.ClassRenamer.ArrayType;
Expand All @@ -27,7 +27,6 @@
import org.aion.avm.core.stacktracking.StackWatcherClassAdapter;
import org.aion.avm.core.types.ClassHierarchy;
import org.aion.avm.core.types.ClassInfo;
import org.aion.avm.core.types.CommonType;
import org.aion.avm.core.types.Forest;
import org.aion.avm.core.types.GeneratedClassConsumer;
import org.aion.avm.core.types.ImmortalDappModule;
Expand All @@ -40,7 +39,6 @@
import i.*;
import org.aion.kernel.*;
import org.aion.parallel.TransactionTask;
import org.aion.vm.api.types.Address;
import org.aion.vm.api.interfaces.KernelInterface;
import org.objectweb.asm.ClassReader;
import org.objectweb.asm.ClassWriter;
Expand Down Expand Up @@ -183,7 +181,7 @@ public static void create(IExternalCapabilities capabilities, KernelInterface ke
LoadedDApp dapp = null;
try {
// read dapp module
Address dappAddress = tx.destinationAddress;
AionAddress dappAddress = tx.destinationAddress;
CodeAndArguments codeAndArguments = CodeAndArguments.decodeFromBytes(tx.data);
if (codeAndArguments == null) {
if (verboseErrors) {
Expand Down Expand Up @@ -283,7 +281,7 @@ public static void create(IExternalCapabilities capabilities, KernelInterface ke
// Return data of a CREATE transaction is the new DApp address.
result.setResultCode(AvmTransactionResult.Code.SUCCESS);
result.setEnergyUsed(energyUsed - refund);
result.setReturnData(dappAddress.toBytes());
result.setReturnData(dappAddress.toByteArray());
} catch (OutOfEnergyException e) {
if (verboseErrors) {
System.err.println("DApp deployment failed due to Out-of-Energy EXCEPTION: \"" + e.getMessage() + "\"");
Expand Down
4 changes: 2 additions & 2 deletions org.aion.avm.core/src/org/aion/avm/core/DAppExecutor.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.aion.avm.core;

import org.aion.types.AionAddress;
import org.aion.avm.RuntimeMethodFeeSchedule;
import org.aion.avm.StorageFees;
import org.aion.avm.core.persistence.LoadedDApp;
Expand All @@ -8,7 +9,6 @@
import i.*;
import org.aion.kernel.AvmTransactionResult;
import org.aion.parallel.TransactionTask;
import org.aion.vm.api.types.Address;
import org.aion.vm.api.interfaces.KernelInterface;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand All @@ -21,7 +21,7 @@ public class DAppExecutor {
public static void call(IExternalCapabilities capabilities, KernelInterface kernel, AvmInternal avm, LoadedDApp dapp,
ReentrantDAppStack.ReentrantState stateToResume, TransactionTask task,
AvmTransaction tx, AvmTransactionResult result, boolean verboseErrors) {
Address dappAddress = tx.destinationAddress;
AionAddress dappAddress = tx.destinationAddress;

// If this is a reentrant call, we need to serialize the graph of the parent frame. This is required to both copy-back our changes but also
// is required in case we want to revert the state.
Expand Down
Loading

0 comments on commit cd2e8d4

Please sign in to comment.