use of org.aion.zero.impl.types.BlockContext in project aion by aionnetwork.
the class TransactionExecutorTest method testExecutorBlind.
@Test
public void testExecutorBlind() throws IOException {
byte[] deployCode = ContractUtils.getContractDeployer("ByteArrayMap.sol", "ByteArrayMap");
long nrg = 1_000_000;
long nrgPrice = energyPrice;
BigInteger value = BigInteger.ZERO;
BigInteger nonce = BigInteger.ZERO;
AionTransaction tx = AionTransaction.create(deployerKey, nonce.toByteArray(), null, value.toByteArray(), deployCode, nrg, nrgPrice, TransactionTypes.DEFAULT, null);
assertTrue(tx.isContractCreationTransaction());
assertEquals(Builder.DEFAULT_BALANCE, blockchain.getRepository().getBalance(deployer));
assertEquals(BigInteger.ZERO, blockchain.getRepository().getNonce(deployer));
BlockContext context = blockchain.createNewMiningBlockContext(blockchain.getBestBlock(), Collections.singletonList(tx), false);
Pair<ImportResult, AionBlockSummary> result = blockchain.tryToConnectAndFetchSummary(context.block);
AionBlockSummary summary = result.getRight();
assertEquals(ImportResult.IMPORTED_BEST, result.getLeft());
// We expect that there is a new account created, the contract, with 0 balance and 0 nonce
// and that its code is the contract body. We also expect that the deployer (sender) has
// its nonce incremented and its balance is now equal to its old balance minus the
// transaction
// fee plus the refund
byte[] body = ContractUtils.getContractBody("ByteArrayMap.sol", "ByteArrayMap");
AionAddress contract = TxUtil.calculateContractAddress(tx);
assertArrayEquals(body, blockchain.getRepository().getCode(contract));
assertEquals(BigInteger.ZERO, blockchain.getRepository().getBalance(contract));
assertEquals(BigInteger.ZERO, blockchain.getRepository().getNonce(contract));
assertEquals(BigInteger.ONE, blockchain.getRepository().getNonce(deployer));
assertEquals(Builder.DEFAULT_BALANCE.subtract(BigInteger.valueOf(summary.getReceipts().get(0).getEnergyUsed()).multiply(BigInteger.valueOf(nrgPrice))), blockchain.getRepository().getBalance(deployer));
}
use of org.aion.zero.impl.types.BlockContext in project aion by aionnetwork.
the class TransactionExecutorTest method testExecutor.
@Test
public void testExecutor() throws Exception {
byte[] deployCode = ContractUtils.getContractDeployer("ByteArrayMap.sol", "ByteArrayMap");
long nrg = 1_000_000;
long nrgPrice = energyPrice;
BigInteger value = BigInteger.ZERO;
BigInteger nonce = BigInteger.ZERO;
AionTransaction tx = AionTransaction.create(deployerKey, nonce.toByteArray(), null, value.toByteArray(), deployCode, nrg, nrgPrice, TransactionTypes.DEFAULT, null);
assertTrue(tx.isContractCreationTransaction());
assertEquals(Builder.DEFAULT_BALANCE, blockchain.getRepository().getBalance(deployer));
assertEquals(BigInteger.ZERO, blockchain.getRepository().getNonce(deployer));
BlockContext context = blockchain.createNewMiningBlockContext(blockchain.getBestBlock(), Collections.singletonList(tx), false);
RepositoryCache repo = blockchain.getRepository().startTracking();
AionTxExecSummary summary = executeTransaction(repo, context, tx);
BigInteger refund = summary.getRefund();
// We expect that there is a new account created, the contract, with 0 balance and 0 nonce
// and that its code is the contract body. We also expect that the deployer (sender) has
// its nonce incremented and its balance is now equal to its old balance minus the
// transaction
// fee plus the refund
byte[] body = ContractUtils.getContractBody("ByteArrayMap.sol", "ByteArrayMap");
assertEquals("", summary.getReceipt().getError());
assertArrayEquals(body, summary.getResult());
AionAddress contract = TxUtil.calculateContractAddress(summary.getTransaction());
assertArrayEquals(body, repo.getCode(contract));
assertEquals(BigInteger.ZERO, repo.getBalance(contract));
assertEquals(BigInteger.ZERO, repo.getNonce(contract));
BigInteger txFee = BigInteger.valueOf(nrg).multiply(BigInteger.valueOf(nrgPrice));
assertEquals(Builder.DEFAULT_BALANCE.subtract(txFee).add(refund), repo.getBalance(deployer));
assertEquals(BigInteger.ONE, repo.getNonce(deployer));
}
use of org.aion.zero.impl.types.BlockContext in project aion by aionnetwork.
the class TransactionExecutorTest method deployByteArrayContract.
// <-----------------------------------------HELPERS------------------------------------------->
private AionAddress deployByteArrayContract() throws IOException {
byte[] deployCode = ContractUtils.getContractDeployer("ByteArrayMap.sol", "ByteArrayMap");
long nrg = 1_000_000;
long nrgPrice = energyPrice;
BigInteger value = BigInteger.ZERO;
BigInteger nonce = BigInteger.ZERO;
AionTransaction tx = AionTransaction.create(deployerKey, nonce.toByteArray(), null, value.toByteArray(), deployCode, nrg, nrgPrice, TransactionTypes.DEFAULT, null);
assertTrue(tx.isContractCreationTransaction());
assertEquals(Builder.DEFAULT_BALANCE, blockchain.getRepository().getBalance(deployer));
assertEquals(BigInteger.ZERO, blockchain.getRepository().getNonce(deployer));
BlockContext context = blockchain.createNewMiningBlockContext(blockchain.getBestBlock(), Collections.singletonList(tx), false);
blockchain.tryToConnect(context.block);
return TxUtil.calculateContractAddress(tx);
}
use of org.aion.zero.impl.types.BlockContext in project aion by aionnetwork.
the class TransactionExecutorTest method testDeployedCodeFunctionality.
@Test
public void testDeployedCodeFunctionality() throws Exception {
AionAddress contract = deployByteArrayContract();
byte[] callingCode = Hex.decode(f_func);
BigInteger nonce = blockchain.getRepository().getNonce(deployer);
AionTransaction tx = AionTransaction.create(deployerKey, nonce.toByteArray(), contract, BigInteger.ZERO.toByteArray(), callingCode, 1_000_000, energyPrice, TransactionTypes.DEFAULT, null);
assertFalse(tx.isContractCreationTransaction());
BlockContext context = blockchain.createNewMiningBlockContext(blockchain.getBestBlock(), Collections.singletonList(tx), false);
RepositoryCache repo = blockchain.getRepository().startTracking();
AionTxExecSummary summary = executeTransaction(repo, context, tx);
assertEquals("", summary.getReceipt().getError());
System.out.println(Hex.toHexString(summary.getResult()));
// We called the function f() which returns nothing.
assertEquals(0, summary.getReceipt().getTransactionOutput().length);
byte[] body = ContractUtils.getContractBody("ByteArrayMap.sol", "ByteArrayMap");
assertArrayEquals(body, blockchain.getRepository().getCode(contract));
// Now we call the g() function, which returns a byte array of 1024 bytes that starts with
// 'a' and ends with 'b'
callingCode = Hex.decode(g_func);
nonce = repo.getNonce(deployer);
tx = AionTransaction.create(deployerKey, nonce.toByteArray(), contract, BigInteger.ZERO.toByteArray(), callingCode, 1_000_000, energyPrice, TransactionTypes.DEFAULT, null);
assertFalse(tx.isContractCreationTransaction());
context = blockchain.createNewMiningBlockContext(blockchain.getBestBlock(), Collections.singletonList(tx), false);
summary = executeTransaction(repo, context, tx);
System.out.println(Hex.toHexString(summary.getResult()));
System.out.println(summary.getResult().length);
// // I'm guessing: first data word is the number of bytes that follows. Then those
// following
// // bytes denote the size of the output, which follows these last bytes.
// int len = new DataWordImpl(Arrays.copyOfRange(output, 0,
// DataWordImpl.BYTES)).intValue();
// byte[] outputLen = new byte[len];
// System.arraycopy(output, DataWordImpl.BYTES, outputLen, 0, len);
// int outputSize = new BigInteger(outputLen).intValue();
//
// byte[] expected = new byte[1024];
// expected[0] = 'a';
// expected[1023] = 'b';
//
// byte[] out = new byte[outputSize];
// System.arraycopy(output, DataWordImpl.BYTES + len, out, 0, outputSize);
byte[] expected = new byte[1024];
expected[0] = 'a';
expected[1023] = 'b';
byte[] out = extractActualOutput(summary.getResult());
assertArrayEquals(expected, out);
}
use of org.aion.zero.impl.types.BlockContext in project aion by aionnetwork.
the class TransactionExecutorTest method testGfunction.
@Test
public void testGfunction() throws Exception {
AionAddress contract = deployByteArrayContract();
byte[] callingCode = Hex.decode(g_func);
BigInteger nonce = blockchain.getRepository().getNonce(deployer);
AionTransaction tx = AionTransaction.create(deployerKey, nonce.toByteArray(), contract, BigInteger.ZERO.toByteArray(), callingCode, 1_000_000, energyPrice, TransactionTypes.DEFAULT, null);
assertFalse(tx.isContractCreationTransaction());
BlockContext context = blockchain.createNewMiningBlockContext(blockchain.getBestBlock(), Collections.singletonList(tx), false);
RepositoryCache repo = blockchain.getRepository().startTracking();
AionTxExecSummary summary = executeTransaction(repo, context, tx);
System.out.println(summary.getReceipt());
// System.out.println(Hex.toHexString(res.getOutput()));
// System.out.println(res.getOutput().length);
byte[] out = extractActualOutput(summary.getResult());
assertEquals(0, out.length);
}
Aggregations