use of org.aion.zero.impl.types.MiningBlock in project aion by aionnetwork.
the class ContractIntegTest method testCallContractViaAnotherContract.
@Test
public void testCallContractViaAnotherContract() throws Exception {
// Deploy the MultiFeatureContract.
String contractName = "MultiFeatureContract";
byte[] deployCode = getDeployCode(contractName);
long nrg = 1_000_000;
long nrgPrice = energyPrice;
BigInteger value = BigInteger.TWO.pow(20);
BigInteger nonce = BigInteger.ZERO;
AionTransaction tx = AionTransaction.create(deployerKey, nonce.toByteArray(), null, value.toByteArray(), deployCode, nrg, nrgPrice, txType, null);
RepositoryCache repo = blockchain.getRepository().startTracking();
nonce = nonce.add(BigInteger.ONE);
AionAddress multiFeatureContract = deployContract(repo, tx, contractName, null, value, nrg, nrgPrice, nonce);
if (txType == TransactionTypes.AVM_CREATE_CODE) {
assertNull(multiFeatureContract);
return;
}
deployerBalance = repo.getBalance(deployer);
deployerNonce = repo.getNonce(deployer);
// Deploy the MultiFeatureCaller contract.
contractName = "MultiFeatureCaller";
deployCode = getDeployCode(contractName);
value = BigInteger.ZERO;
tx = AionTransaction.create(deployerKey, nonce.toByteArray(), null, value.toByteArray(), deployCode, nrg, nrgPrice, txType, null);
nonce = nonce.add(BigInteger.ONE);
AionAddress callerContract = deployContract(repo, tx, contractName, null, value, nrg, nrgPrice, nonce);
AionAddress recipient = new AionAddress(RandomUtils.nextBytes(AionAddress.LENGTH));
deployerBalance = repo.getBalance(deployer);
deployerNonce = repo.getNonce(deployer);
// Set the MultiFeatureCaller to call the deployed MultiFeatureContract.
byte[] input = ByteUtil.merge(Hex.decode("8c30ffe6"), multiFeatureContract.toByteArray());
tx = AionTransaction.create(deployerKey, nonce.toByteArray(), callerContract, BigInteger.ZERO.toByteArray(), input, nrg, nrgPrice, txType, null);
assertFalse(tx.isContractCreationTransaction());
MiningBlock block = makeBlock(tx);
AionTxExecSummary summary = executeTransaction(tx, block, repo);
assertEquals("", summary.getReceipt().getError());
assertNotEquals(nrg, summary.getNrgUsed().longValue());
BigInteger txCost = BigInteger.valueOf(summary.getNrgUsed().longValue()).multiply(BigInteger.valueOf(nrgPrice));
assertEquals(deployerBalance.subtract(txCost), repo.getBalance(deployer));
deployerBalance = repo.getBalance(deployer);
deployerNonce = repo.getNonce(deployer);
// Now use the MultiFeatureCaller to call the MultiFeatureContract to send funds from that
// contract to the recipient address.
assertEquals(BigInteger.ZERO, repo.getBalance(recipient));
value = BigInteger.TWO.pow(20);
input = ByteUtil.merge(Hex.decode("57a60e6b"), recipient.toByteArray());
input = ByteUtil.merge(input, new DataWord(value).getData());
nonce = nonce.add(BigInteger.ONE);
tx = AionTransaction.create(deployerKey, nonce.toByteArray(), callerContract, BigInteger.ZERO.toByteArray(), input, nrg, nrgPrice, txType, null);
assertFalse(tx.isContractCreationTransaction());
block = makeBlock(tx);
summary = executeTransaction(tx, block, repo);
assertEquals("", summary.getReceipt().getError());
assertNotEquals(nrg, summary.getNrgUsed().longValue());
txCost = BigInteger.valueOf(summary.getNrgUsed().longValue()).multiply(BigInteger.valueOf(nrgPrice));
assertEquals(deployerBalance.subtract(txCost), repo.getBalance(deployer));
assertEquals(value, repo.getBalance(recipient));
}
use of org.aion.zero.impl.types.MiningBlock in project aion by aionnetwork.
the class ContractIntegTest method testSendContractFundsToNonexistentAddress.
@Test
public void testSendContractFundsToNonexistentAddress() throws Exception {
String contractName = "MultiFeatureContract";
byte[] deployCode = getDeployCode(contractName);
long nrg = 1_000_000;
long nrgPrice = energyPrice;
BigInteger value = BigInteger.TWO.pow(13);
BigInteger nonce = BigInteger.ZERO;
AionTransaction tx = AionTransaction.create(deployerKey, nonce.toByteArray(), null, value.toByteArray(), deployCode, nrg, nrgPrice, txType, null);
RepositoryCache repo = blockchain.getRepository().startTracking();
nonce = nonce.add(BigInteger.ONE);
AionAddress contract = deployContract(repo, tx, contractName, null, value, nrg, nrgPrice, nonce);
if (txType == TransactionTypes.AVM_CREATE_CODE) {
assertNull(contract);
return;
}
BigInteger deployerBalance = repo.getBalance(deployer);
// Create a new account to be our fund recipient.
AionAddress recipient = new AionAddress(RandomUtils.nextBytes(AionAddress.LENGTH));
// Contract has 2^13 coins, let's withdraw them.
byte[] input = ByteUtil.merge(Hex.decode("8c50612c"), recipient.toByteArray());
input = ByteUtil.merge(input, new DataWord(value).getData());
tx = AionTransaction.create(deployerKey, nonce.toByteArray(), contract, BigInteger.ZERO.toByteArray(), input, nrg, nrgPrice, txType, null);
assertFalse(tx.isContractCreationTransaction());
MiningBlock block = makeBlock(tx);
AionTxExecSummary summary = executeTransaction(tx, block, repo);
assertEquals("", summary.getReceipt().getError());
assertNotEquals(nrg, summary.getNrgUsed().longValue());
BigInteger txCost = BigInteger.valueOf(summary.getNrgUsed().longValue()).multiply(BigInteger.valueOf(nrgPrice));
assertEquals(deployerBalance.subtract(txCost), repo.getBalance(deployer));
// Check that the recipient received the value.
assertEquals(value, repo.getBalance(recipient));
}
use of org.aion.zero.impl.types.MiningBlock in project aion by aionnetwork.
the class ContractIntegTest method tellFvmContractCallBalanceTransfer.
@Test
public void tellFvmContractCallBalanceTransfer() throws Exception {
if (txType == TransactionTypes.AVM_CREATE_CODE) {
return;
}
String contractName = "InternalCallContract";
byte[] deployCode = getDeployCode(contractName);
long nrg = Constants.NRG_TRANSACTION_MAX;
long nrgPrice = energyPrice;
BigInteger value = BigInteger.ZERO;
BigInteger nonce = BigInteger.ZERO;
AionTransaction tx = AionTransaction.create(deployerKey, nonce.toByteArray(), null, value.toByteArray(), deployCode, nrg, nrgPrice, txType, null);
RepositoryCache repo = blockchain.getRepository().startTracking();
nonce = nonce.add(BigInteger.ONE);
AionAddress contract = deployContract(repo, tx, contractName, null, value, nrg, nrgPrice, nonce, true);
assertNotNull(contract);
repo = blockchain.getRepository().startTracking();
byte[] input = Arrays.copyOfRange(HashUtil.keccak256("sendValueToContract()".getBytes()), 0, 4);
// input = ByteUtil.merge(input, new DataWordImpl(numRecurses + 1).copyOfData());
tx = AionTransaction.create(deployerKey, nonce.toByteArray(), contract, BigInteger.TEN.toByteArray(), input, nrg, nrgPrice, txType, null);
assertFalse(tx.isContractCreationTransaction());
BigInteger senderBalance = repo.getBalance(deployer);
MiningBlock block = makeBlock(tx);
AionTxExecSummary summary = executeTransaction(tx, block, repo);
assertEquals("", summary.getReceipt().getError());
assertNotEquals(nrg, summary.getNrgUsed().longValue());
Pair<ImportResult, AionBlockSummary> result = blockchain.tryToConnectAndFetchSummary(block);
assertTrue(result.getLeft().isSuccessful());
assertEquals(BigInteger.TEN, blockchain.getRepository().getBalance(contract));
assertEquals(senderBalance.subtract(BigInteger.TEN).subtract(BigInteger.valueOf(summary.getNrgUsed().longValue()).multiply(BigInteger.valueOf(nrgPrice))), blockchain.getRepository().getBalance(deployer));
input = Arrays.copyOfRange(HashUtil.keccak256("callBalanceTransfer(address)".getBytes()), 0, 4);
AionAddress receiver = AddressUtils.wrapAddress("0x000000000000000000000000000000000000000000000000000000000000000a");
input = ByteUtil.merge(input, receiver.toByteArray());
nonce = nonce.add(BigInteger.ONE);
tx = AionTransaction.create(deployerKey, nonce.toByteArray(), contract, BigInteger.ZERO.toByteArray(), input, nrg, nrgPrice, txType, null);
assertFalse(tx.isContractCreationTransaction());
repo = blockchain.getRepository().startTracking();
block = makeBlock(tx);
summary = executeTransaction(tx, block, repo);
assertEquals("", summary.getReceipt().getError());
assertNotEquals(nrg, summary.getNrgUsed().longValue());
result = blockchain.tryToConnectAndFetchSummary(block);
assertTrue(result.getLeft().isSuccessful());
assertEquals(1, result.getRight().getSummaries().get(0).getInternalTransactions().size());
assertEquals(BigInteger.TEN.subtract(BigInteger.ONE), blockchain.getRepository().getBalance(contract));
assertEquals(BigInteger.ONE, blockchain.getRepository().getBalance(receiver));
}
use of org.aion.zero.impl.types.MiningBlock in project aion by aionnetwork.
the class ContractIntegTest method testRedeployContractAtExistentContractAddress.
@Test
public void testRedeployContractAtExistentContractAddress() throws Exception {
String contractName = "MultiFeatureContract";
byte[] deployCode = getDeployCode(contractName);
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, txType, null);
// Mock up the repo so that the contract address already exists.
AionRepositoryCache repo = mock(AionRepositoryCache.class);
when(repo.hasAccountState(Mockito.any(AionAddress.class))).thenReturn(true);
when(repo.getNonce(Mockito.any(AionAddress.class))).thenReturn(nonce);
when(repo.getBalance(Mockito.any(AionAddress.class))).thenReturn(Builder.DEFAULT_BALANCE);
when(repo.getCode(Mockito.any(AionAddress.class))).thenReturn(new byte[1]);
when(repo.startTracking()).thenReturn(repo);
assertTrue(tx.isContractCreationTransaction());
assertEquals(Builder.DEFAULT_BALANCE, blockchain.getRepository().getBalance(deployer));
assertEquals(BigInteger.ZERO, blockchain.getRepository().getNonce(deployer));
MiningBlock block = makeBlock(tx);
AionTxExecSummary summary = executeTransaction(tx, block, repo);
if (txType == TransactionTypes.DEFAULT) {
assertEquals("FAILURE", summary.getReceipt().getError());
assertEquals(nrg, summary.getNrgUsed().longValue());
} else if (txType == TransactionTypes.AVM_CREATE_CODE) {
assertEquals("Failed: invalid data", summary.getReceipt().getError());
assertEquals(nrg, summary.getNrgUsed().longValue());
}
}
use of org.aion.zero.impl.types.MiningBlock in project aion by aionnetwork.
the class ContractIntegTest method testContractDeployCodeIsNonsensical.
@Test
public void testContractDeployCodeIsNonsensical() throws Exception {
byte[] deployCode = new byte[1];
deployCode[0] = 0x1;
long nrg = 1_000_000;
long nrgPrice = energyPrice;
BigInteger value = BigInteger.ZERO;
BigInteger nonce = BigInteger.ZERO;
// to == null signals that this is contract creation.
AionTransaction tx = AionTransaction.create(deployerKey, nonce.toByteArray(), null, value.toByteArray(), deployCode, nrg, nrgPrice, txType, null);
assertTrue(tx.isContractCreationTransaction());
assertEquals(Builder.DEFAULT_BALANCE, blockchain.getRepository().getBalance(deployer));
assertEquals(BigInteger.ZERO, blockchain.getRepository().getNonce(deployer));
MiningBlock block = makeBlock(tx);
RepositoryCache repo = blockchain.getRepository().startTracking();
AionTxExecSummary summary = executeTransaction(tx, block, repo);
if (txType == TransactionTypes.DEFAULT) {
assertEquals("OUT_OF_NRG", summary.getReceipt().getError());
assertEquals(nrg, summary.getNrgUsed().longValue());
AionAddress contract = TxUtil.calculateContractAddress(tx);
assertArrayEquals(new byte[0], summary.getResult());
assertArrayEquals(new byte[0], repo.getCode(contract));
assertEquals(BigInteger.ZERO, repo.getBalance(contract));
assertEquals(BigInteger.ZERO, repo.getNonce(contract));
assertEquals(BigInteger.ONE, repo.getNonce(deployer));
} else if (txType == TransactionTypes.AVM_CREATE_CODE) {
assertEquals("Failed: invalid data", summary.getReceipt().getError());
nonce = nonce.add(BigInteger.ONE);
checkStateOfDeployer(repo, summary, nrgPrice, value, nonce);
}
BigInteger txCost = summary.getNrgUsed().multiply(BigInteger.valueOf(nrgPrice));
assertEquals(Builder.DEFAULT_BALANCE.subtract(txCost), repo.getBalance(deployer));
}
Aggregations