use of org.aion.util.types.DataWord in project aion by aionnetwork.
the class AionTransactionTest method testTransactionCost.
@Test
public void testTransactionCost() {
byte[] nonce = BigInteger.ONE.toByteArray();
byte[] to = RandomUtils.nextBytes(AionAddress.LENGTH);
byte[] value = BigInteger.ONE.toByteArray();
byte[] data = RandomUtils.nextBytes(128);
long nrg = new DataWord(1000L).longValue();
long nrgPrice = DataWord.ONE.longValue();
AionTransaction tx = AionTransaction.create(key, nonce, new AionAddress(to), value, data, nrg, nrgPrice, TransactionTypes.DEFAULT, null);
long expected = 21000;
for (byte b : data) {
expected += (b == 0) ? 4 : 64;
}
assertEquals(expected, TxUtil.calculateTransactionCost(tx));
}
use of org.aion.util.types.DataWord in project aion by aionnetwork.
the class ContractIntegTest method testFvmOverWithdrawFromContract.
@Test
public void testFvmOverWithdrawFromContract() 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);
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);
repo.flushTo(blockchain.getRepository(), true);
repo = blockchain.getRepository().startTracking();
// Contract has no funds, try to withdraw just 1 coin.
byte[] input = ByteUtil.merge(Hex.decode("9424bba3"), 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("reverted", summary.getReceipt().getError());
assertNotEquals(nrg, summary.getNrgUsed().longValue());
System.out.println("DEP: " + deployerBalance);
BigInteger txCost = BigInteger.valueOf(summary.getNrgUsed().longValue()).multiply(BigInteger.valueOf(nrgPrice));
assertEquals(deployerBalance.subtract(txCost), repo.getBalance(deployer));
}
use of org.aion.util.types.DataWord in project aion by aionnetwork.
the class ContractIntegTest method testWithdrawFromFvmContract.
@Test
public void testWithdrawFromFvmContract() throws Exception {
String contractName = "MultiFeatureContract";
byte[] deployCode = getDeployCode(contractName);
long nrg = 1_000_000;
long nrgPrice = energyPrice;
BigInteger value = BigInteger.TWO.pow(32);
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);
// Contract has 2^32 coins, let's withdraw them.
byte[] input = ByteUtil.merge(Hex.decode("9424bba3"), 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());
// Check that the deployer did get the requested value sent back.
BigInteger txCost = BigInteger.valueOf(summary.getNrgUsed().longValue()).multiply(BigInteger.valueOf(nrgPrice));
assertEquals(deployerBalance.subtract(txCost).add(value), repo.getBalance(deployer));
}
use of org.aion.util.types.DataWord in project aion by aionnetwork.
the class ContractIntegTest method testSendContractFundsToOtherAddress.
@Test
public void testSendContractFundsToOtherAddress() 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));
repo.createAccount(recipient);
// 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 AionRepositoryImplTest method testAccountStateUpdateStorageRowFlush.
@Test
public void testAccountStateUpdateStorageRowFlush() {
AionRepositoryImpl repository = AionRepositoryImpl.createForTesting(repoConfig);
RepositoryCache track = repository.startTracking();
AionAddress defaultAccount = new AionAddress(ByteUtil.hexStringToBytes(value1));
track.addBalance(defaultAccount, BigInteger.valueOf(1));
// Consider the original root the one after an account has been added
byte[] key = HashUtil.blake128("hello".getBytes());
byte[] value = HashUtil.blake128("world".getBytes());
track.addStorageRow(defaultAccount, new DataWord(key).toWrapper(), new DataWord(value).toWrapper());
track.saveVmType(defaultAccount, InternalVmType.FVM);
// does not call parent's flush
track.flushTo(repository, true);
repository.flush();
/**
* Verify that the account has been flushed
*/
StoredContractDetails snapshot = repository.getContractDetails(defaultAccount);
assertThat(snapshot instanceof FvmContractDetails).isTrue();
FvmContractDetails details = (FvmContractDetails) snapshot;
assertThat(details.get(new DataWord(key).toWrapper())).isEqualTo(new DataWord(value).toWrapper());
}
Aggregations