Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a test for internal transactions in the AVM #890

Merged
merged 1 commit into from
Apr 30, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
158 changes: 158 additions & 0 deletions modAionImpl/test/org/aion/zero/impl/vm/AvmInternalTxTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
package org.aion.zero.impl.vm;

import static com.google.common.truth.Truth.assertThat;

import java.math.BigInteger;
import java.util.Collections;
import org.aion.avm.core.dappreading.JarBuilder;
import org.aion.avm.core.util.ABIUtil;
import org.aion.avm.core.util.CodeAndArguments;
import org.aion.crypto.AddressSpecs;
import org.aion.crypto.ECKey;
import org.aion.mcf.core.ImportResult;
import org.aion.mcf.tx.TransactionTypes;
import org.aion.mcf.valid.TransactionTypeRule;
import org.aion.types.Address;
import org.aion.vm.VirtualMachineProvider;
import org.aion.zero.impl.StandaloneBlockchain;
import org.aion.zero.impl.types.AionBlock;
import org.aion.zero.impl.types.AionBlockSummary;
import org.aion.zero.impl.vm.contracts.AvmInternalTx;
import org.aion.zero.types.AionTransaction;
import org.aion.zero.types.AionTxReceipt;
import org.apache.commons.lang3.tuple.Pair;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;

public class AvmInternalTxTest {
private StandaloneBlockchain blockchain;
private ECKey deployerKey;

@BeforeClass
public static void setupAvm() {
if (VirtualMachineProvider.isMachinesAreLive()) {
return;
}
VirtualMachineProvider.initializeAllVirtualMachines();
}

@AfterClass
public static void tearDownAvm() {
if (VirtualMachineProvider.isMachinesAreLive()) {
VirtualMachineProvider.shutdownAllVirtualMachines();
}
TransactionTypeRule.disallowAVMContractTransaction();
}

@Before
public void setup() {
StandaloneBlockchain.Bundle bundle =
new StandaloneBlockchain.Builder()
.withDefaultAccounts()
.withValidatorConfiguration("simple")
.withAvmEnabled()
.build();
this.blockchain = bundle.bc;
this.deployerKey = bundle.privateKeys.get(0);
}

@After
public void tearDown() {
this.blockchain = null;
this.deployerKey = null;
}

@Test
public void testDeployAndCallContract() {
TransactionTypeRule.allowAVMContractTransaction();
// Deploy the contract.
byte[] jar = getJarBytes();
AionTransaction transaction =
newTransaction(
BigInteger.ZERO,
Address.wrap(deployerKey.getAddress()),
null,
jar,
5_000_000,
TransactionTypes.AVM_CREATE_CODE);
transaction.sign(this.deployerKey);

AionBlock block =
this.blockchain.createNewBlock(
this.blockchain.getBestBlock(),
Collections.singletonList(transaction),
false);
Pair<ImportResult, AionBlockSummary> connectResult =
this.blockchain.tryToConnectAndFetchSummary(block);
AionTxReceipt receipt = connectResult.getRight().getReceipts().get(0);

// Check the block was imported, the contract has the Avm prefix, and deployment succeeded.
assertThat(connectResult.getLeft()).isEqualTo(ImportResult.IMPORTED_BEST);
assertThat(receipt.getTransactionOutput()[0]).isEqualTo(AddressSpecs.A0_IDENTIFIER);
assertThat(receipt.isSuccessful()).isTrue();

Address contract = Address.wrap(receipt.getTransactionOutput());
// verify that the output is indeed the contract address
assertThat(transaction.getContractAddress()).isEqualTo(contract);

byte[] call = ABIUtil.encodeMethodArguments("recursivelyGetValue");
makeCall(BigInteger.ONE, contract, call);
makeCall(BigInteger.TWO, contract, call);
}

private byte[] getJarBytes() {
return new CodeAndArguments(
JarBuilder.buildJarForMainAndClassesAndUserlib(AvmInternalTx.class),
new byte[0])
.encodeToBytes();
}

private AionTransaction newTransaction(
BigInteger nonce,
Address sender,
Address destination,
byte[] data,
long energyLimit,
byte type) {
return new AionTransaction(
nonce.toByteArray(),
sender,
destination,
BigInteger.ZERO.toByteArray(),
data,
energyLimit,
1,
type);
}

private void makeCall(BigInteger nonce, Address contract, byte[] call) {
AionTransaction transaction =
newTransaction(
nonce,
Address.wrap(deployerKey.getAddress()),
contract,
call,
2_000_000,
TransactionTypes.DEFAULT);

transaction.sign(this.deployerKey);

AionBlock block =
this.blockchain.createNewBlock(
this.blockchain.getBestBlock(),
Collections.singletonList(transaction),
false);

Pair<ImportResult, AionBlockSummary> connectResult = this.blockchain.tryToConnectAndFetchSummary(block);
AionTxReceipt receipt = connectResult.getRight().getReceipts().get(0);

// Check the block was imported and the transaction was successful.
assertThat(connectResult.getLeft()).isEqualTo(ImportResult.IMPORTED_BEST);
assertThat(receipt.isSuccessful()).isTrue();
System.out.println(block);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package org.aion.zero.impl.vm.contracts;

import avm.Blockchain;
import avm.Result;
import java.math.BigInteger;
import org.aion.avm.userlib.abi.ABIDecoder;
import org.aion.avm.userlib.abi.ABIEncoder;

public class AvmInternalTx {

private static int value;

public static int recursivelyGetValue() {
byte[] args = ABIEncoder.encodeOneString("getValue");
Result result = Blockchain.call(Blockchain.getAddress(), BigInteger.ZERO, args, Blockchain.getRemainingEnergy());
Blockchain.require(result.isSuccess());
ABIDecoder decoder = new ABIDecoder(result.getReturnData());
return decoder.decodeOneInteger();
}

public static int getValue() {
return value;
}

public static void setValue(int val) {
value = val;
}

public static byte[] main() {
ABIDecoder decoder = new ABIDecoder(Blockchain.getData());
String methodName = decoder.decodeMethodName();
if (methodName == null) {
return new byte[0];
} else {
if (methodName.equals("getValue")) {
return ABIEncoder.encodeOneInteger(getValue());
} else if (methodName.equals("setValue")) {
setValue(decoder.decodeOneInteger());
return new byte[0];
} else if (methodName.equals("recursivelyGetValue")) {
return ABIEncoder.encodeOneInteger(recursivelyGetValue());
} else {
return new byte[0];
}
}
}
}