use of org.aion.util.types.DataWord in project aion by aionnetwork.
the class ContractIntegTest method testFvmCallFunction.
@Test
public void testFvmCallFunction() throws Exception {
String contractName = "MultiFeatureContract";
byte[] deployCode = getDeployCode(contractName);
long nrg = 1_000_000;
long nrgPrice = energyPrice;
BigInteger value = BigInteger.ONE;
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;
}
// ---------- This command will perform addition. ----------
int num = 53475374;
byte[] input = ByteUtil.merge(Hex.decode("f601704f"), new DataWord(num).getData());
input = ByteUtil.merge(input, new DataWord(1).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());
// Since input takes in uint8 we only want the last byte of num. Output size is well-defined
// at 128 bits, or 16 bytes.
int expectedResult = 1111 + (num & 0xFF);
assertEquals(expectedResult, new DataWord(summary.getResult()).intValue());
// --------- This command will perform subtraction. ----------
input = ByteUtil.merge(Hex.decode("f601704f"), new DataWord(num).getData());
input = ByteUtil.merge(input, new DataWord(0).getData());
nonce = nonce.add(BigInteger.ONE);
tx = AionTransaction.create(deployerKey, nonce.toByteArray(), contract, BigInteger.ZERO.toByteArray(), input, nrg, nrgPrice, TransactionTypes.DEFAULT, null);
assertFalse(tx.isContractCreationTransaction());
block = makeBlock(tx);
summary = executeTransaction(tx, block, repo);
assertEquals("", summary.getReceipt().getError());
assertNotEquals(nrg, summary.getNrgUsed().longValue());
// Since input takes in uint8 we only want the last byte of num. Output size is well-defined
// at 128 bits, or 16 bytes.
expectedResult = 1111 - (num & 0xFF);
assertEquals(expectedResult, new DataWord(summary.getResult()).intValue());
}
use of org.aion.util.types.DataWord in project aion by aionnetwork.
the class ContractIntegTest method extractOutput.
/**
* Extracts output from rawOutput under the assumption that rawOutput is the result output of a
* call to the fastVM and this output is of variable length not predefined length.
*/
private byte[] extractOutput(byte[] rawOutput) {
int headerLen = new DataWord(Arrays.copyOfRange(rawOutput, 0, DataWord.BYTES)).intValue();
int outputLen = new DataWord(Arrays.copyOfRange(rawOutput, (DataWord.BYTES * 2) - headerLen, DataWord.BYTES * 2)).intValue();
byte[] output = new byte[outputLen];
System.arraycopy(rawOutput, DataWord.BYTES * 2, output, 0, outputLen);
return output;
}
use of org.aion.util.types.DataWord 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.util.types.DataWord 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.util.types.DataWord in project aion by aionnetwork.
the class Benchmark method createDummyBlock.
private static MiningBlock createDummyBlock() {
byte[] parentHash = new byte[32];
byte[] coinbase = RandomUtils.nextBytes(AionAddress.LENGTH);
byte[] logsBloom = new byte[0];
byte[] difficulty = new DataWord(0x1000000L).getData();
long number = 1;
long timestamp = System.currentTimeMillis() / 1000;
byte[] extraData = new byte[0];
byte[] nonce = new byte[32];
byte[] receiptsRoot = new byte[32];
byte[] transactionsRoot = new byte[32];
byte[] stateRoot = new byte[32];
List<AionTransaction> transactionsList = Collections.emptyList();
byte[] solutions = new byte[0];
// TODO: set a dummy limit of 5000000 for now
return new MiningBlock(parentHash, new AionAddress(coinbase), logsBloom, difficulty, number, timestamp, extraData, nonce, receiptsRoot, transactionsRoot, stateRoot, transactionsList, solutions, 0, 5000000);
}
Aggregations