use of org.aion.zero.impl.types.BlockContext in project aion by aionnetwork.
the class OpcodeIntegTest method testDelegateCallStorage.
// ===================================== test DELEGATECALL =====================================
@Test
public void testDelegateCallStorage() throws Exception {
RepositoryCache repo = blockchain.getRepository().startTracking();
AionAddress D = deployContract(repo, "D", "D.sol", BigInteger.ZERO);
AionAddress E = deployContract(repo, "E", "D.sol", BigInteger.ZERO);
BigInteger n = new BigInteger("23786523");
// Deployer calls contract D which performs DELEGATECALL to call contract E.
long nrg = 1_000_000;
long nrgPrice = 1;
BigInteger value = new BigInteger("4364463");
BigInteger nonce = BigInteger.TWO;
byte[] input = // use DELEGATECALL on E.
ByteUtil.merge(Hex.decode("32817e1d"), E.toByteArray());
// pass in 'n' also.
input = ByteUtil.merge(input, new DataWord(n).getData());
AionTransaction tx = AionTransaction.create(deployerKey, nonce.toByteArray(), D, value.toByteArray(), input, nrg, nrgPrice, TransactionTypes.DEFAULT, null);
assertEquals(deployer, tx.getSenderAddress());
assertEquals(D, tx.getDestinationAddress());
BlockContext context = blockchain.createNewMiningBlockContext(blockchain.getBestBlock(), Collections.singletonList(tx), false);
AionTxExecSummary summary = executeTransaction(tx, context.block, repo);
assertEquals("", summary.getReceipt().getError());
assertEquals(summary.getNrgUsed().longValue(), summary.getNrgUsed().longValue());
nonce = nonce.add(BigInteger.ONE);
// When we call into contract D we should find its storage is modified so that 'n' is set.
input = Hex.decode("3e955225");
tx = AionTransaction.create(deployerKey, nonce.toByteArray(), D, BigInteger.ZERO.toByteArray(), input, nrg, nrgPrice, TransactionTypes.DEFAULT, null);
assertEquals(deployer, tx.getSenderAddress());
assertEquals(D, tx.getDestinationAddress());
context = blockchain.createNewMiningBlockContext(blockchain.getBestBlock(), Collections.singletonList(tx), false);
BigInteger inStore = new BigInteger(executeTransaction(tx, context.block, repo).getResult());
assertEquals(n, inStore);
nonce = nonce.add(BigInteger.ONE);
// When we call into contract E we should find its storage is unmodified.
tx = AionTransaction.create(deployerKey, nonce.toByteArray(), E, BigInteger.ZERO.toByteArray(), input, nrg, nrgPrice, TransactionTypes.DEFAULT, null);
assertEquals(deployer, tx.getSenderAddress());
assertEquals(E, tx.getDestinationAddress());
context = blockchain.createNewMiningBlockContext(blockchain.getBestBlock(), Collections.singletonList(tx), false);
inStore = new BigInteger(executeTransaction(tx, context.block, repo).getResult());
assertEquals(BigInteger.ZERO, inStore);
}
use of org.aion.zero.impl.types.BlockContext in project aion by aionnetwork.
the class OpcodeIntegTest method testDelegateCallValueTransfer.
@Test
public void testDelegateCallValueTransfer() throws Exception {
RepositoryCache repo = blockchain.getRepository().startTracking();
AionAddress D = deployContract(repo, "D", "D.sol", BigInteger.ZERO);
AionAddress E = deployContract(repo, "E", "D.sol", BigInteger.ZERO);
BigInteger n = new BigInteger("23786523");
BigInteger balanceDeployer = repo.getBalance(deployer);
BigInteger balanceD = repo.getBalance(D);
BigInteger balanceE = repo.getBalance(E);
// Deployer calls contract D which performs DELEGATECALL to call contract E.
long nrg = 1_000_000;
long nrgPrice = 1;
BigInteger value = new BigInteger("4364463");
BigInteger nonce = BigInteger.TWO;
byte[] input = // use DELEGATECALL on E.
ByteUtil.merge(Hex.decode("32817e1d"), E.toByteArray());
// pass in 'n' also.
input = ByteUtil.merge(input, new DataWord(n).getData());
AionTransaction tx = AionTransaction.create(deployerKey, nonce.toByteArray(), D, value.toByteArray(), input, nrg, nrgPrice, TransactionTypes.DEFAULT, null);
assertEquals(deployer, tx.getSenderAddress());
assertEquals(D, tx.getDestinationAddress());
BlockContext context = blockchain.createNewMiningBlockContext(blockchain.getBestBlock(), Collections.singletonList(tx), false);
AionTxExecSummary summary = executeTransaction(tx, context.block, repo);
assertEquals("", summary.getReceipt().getError());
assertEquals(summary.getNrgUsed().longValue(), summary.getNrgUsed().longValue());
// We expect that deployer paid the tx cost and sent value. We expect that D received value.
// We expect that E received nothing.
BigInteger txCost = BigInteger.valueOf(summary.getNrgUsed().longValue() * nrgPrice);
assertEquals(balanceDeployer.subtract(value).subtract(txCost), repo.getBalance(deployer));
assertEquals(balanceD.add(value), repo.getBalance(D));
assertEquals(balanceE, repo.getBalance(E));
}
use of org.aion.zero.impl.types.BlockContext in project aion by aionnetwork.
the class OpcodeIntegTest method deployContract.
/**
* Deploys a contract named contractName in a file named contractFilename and checks the state
* of the deployed contract and the contract deployer.
*
* <p>Returns the address of the newly deployed contract.
*/
private AionAddress deployContract(RepositoryCache repo, AionTransaction tx, String contractName, String contractFilename, BigInteger value, long nrg, long nrgPrice, BigInteger expectedNonce) throws Exception {
assertTrue(tx.isContractCreationTransaction());
assertEquals(deployerBalance, repo.getBalance(deployer));
assertEquals(expectedNonce, repo.getNonce(deployer));
BlockContext context = blockchain.createNewMiningBlockContext(blockchain.getBestBlock(), Collections.singletonList(tx), false);
AionTxExecSummary summary = executeTransaction(tx, context.block, repo);
assertEquals("", summary.getReceipt().getError());
assertNotEquals(nrg, summary.getNrgUsed().longValue());
AionAddress contract = TxUtil.calculateContractAddress(tx);
checkStateOfNewContract(repo, contractName, contractFilename, contract, value);
checkStateOfDeployer(repo, deployerBalance, summary.getNrgUsed().longValue(), nrgPrice, value, expectedNonce);
return contract;
}
use of org.aion.zero.impl.types.BlockContext in project aion by aionnetwork.
the class OpcodeIntegTest method testSuicideRecipientNewlyCreated.
@Test
public void testSuicideRecipientNewlyCreated() throws Exception {
RepositoryCache repo = blockchain.getRepository().startTracking();
BigInteger balance = new BigInteger("32522224");
AionAddress recipient = new AionAddress(RandomUtils.nextBytes(AionAddress.LENGTH));
AionAddress contract = deployContract(repo, "Suicide", "Suicide.sol", BigInteger.ZERO);
repo.addBalance(contract, balance);
BigInteger balanceDeployer = repo.getBalance(deployer);
BigInteger balanceRecipient = BigInteger.ZERO;
assertEquals(balance, repo.getBalance(contract));
long nrg = 1_000_000;
long nrgPrice = 1;
BigInteger nonce = BigInteger.ONE;
byte[] input = ByteUtil.merge(Hex.decode("fc68521a"), recipient.toByteArray());
AionTransaction tx = AionTransaction.create(deployerKey, nonce.toByteArray(), contract, BigInteger.ZERO.toByteArray(), input, nrg, nrgPrice, TransactionTypes.DEFAULT, null);
BlockContext context = blockchain.createNewMiningBlockContext(blockchain.getBestBlock(), Collections.singletonList(tx), false);
AionTxExecSummary summary = executeTransaction(tx, context.block, repo);
assertEquals("", summary.getReceipt().getError());
assertEquals(summary.getNrgUsed().longValue(), summary.getNrgUsed().longValue());
// We expect that deployer paid the tx cost. We expect that a new account was created and
// all of the balance in the contract has been transferred to it. We expect that the
// contract has been deleted.
BigInteger txCost = BigInteger.valueOf(summary.getNrgUsed().longValue() * nrgPrice);
assertEquals(balanceDeployer.subtract(txCost), repo.getBalance(deployer));
assertTrue(repo.hasAccountState(recipient));
assertEquals(balanceRecipient.add(balance), repo.getBalance(recipient));
assertEquals(BigInteger.ZERO, repo.getBalance(contract));
assertFalse(repo.hasAccountState(contract));
assertEquals(1, summary.getDeletedAccounts().size());
assertEquals(contract, summary.getDeletedAccounts().get(0));
}
use of org.aion.zero.impl.types.BlockContext in project aion by aionnetwork.
the class OpcodeIntegTest method testCallcodeStorage.
// ======================================= test CALLCODE =======================================
@Test
public void testCallcodeStorage() throws Exception {
RepositoryCache repo = blockchain.getRepository().startTracking();
BigInteger n = new BigInteger("7638523");
AionAddress D = deployContract(repo, "D", "D.sol", BigInteger.ZERO);
AionAddress E = deployContract(repo, "E", "D.sol", BigInteger.ZERO);
// Deployer calls contract D which performs CALLCODE to call contract E. We expect that the
// storage in contract D is modified by the code that is called in contract E.
long nrg = 1_000_000;
long nrgPrice = 1;
BigInteger nonce = BigInteger.TWO;
byte[] input = // use CALLCODE on E.
ByteUtil.merge(Hex.decode("5cce9fc2"), E.toByteArray());
// pass in 'n' also.
input = ByteUtil.merge(input, new DataWord(n).getData());
AionTransaction tx = AionTransaction.create(deployerKey, nonce.toByteArray(), D, BigInteger.ZERO.toByteArray(), input, nrg, nrgPrice, TransactionTypes.DEFAULT, null);
assertEquals(deployer, tx.getSenderAddress());
assertEquals(D, tx.getDestinationAddress());
BlockContext context = blockchain.createNewMiningBlockContext(blockchain.getBestBlock(), Collections.singletonList(tx), false);
AionTxExecSummary summary = executeTransaction(tx, context.block, repo);
assertEquals("", summary.getReceipt().getError());
assertEquals(summary.getNrgUsed().longValue(), summary.getNrgUsed().longValue());
nonce = nonce.add(BigInteger.ONE);
// When we call into contract D we should find its storage is modified so that 'n' is set.
input = Hex.decode("3e955225");
tx = AionTransaction.create(deployerKey, nonce.toByteArray(), D, BigInteger.ZERO.toByteArray(), input, nrg, nrgPrice, TransactionTypes.DEFAULT, null);
assertEquals(deployer, tx.getSenderAddress());
assertEquals(D, tx.getDestinationAddress());
context = blockchain.createNewMiningBlockContext(blockchain.getBestBlock(), Collections.singletonList(tx), false);
BigInteger inStore = new BigInteger(executeTransaction(tx, context.block, repo).getResult());
System.err.println("Found in D's storage for n: " + inStore);
assertEquals(n, inStore);
nonce = nonce.add(BigInteger.ONE);
// When we call into contract E we should find its storage is unmodified.
tx = AionTransaction.create(deployerKey, nonce.toByteArray(), E, BigInteger.ZERO.toByteArray(), input, nrg, nrgPrice, TransactionTypes.DEFAULT, null);
assertEquals(deployer, tx.getSenderAddress());
assertEquals(E, tx.getDestinationAddress());
context = blockchain.createNewMiningBlockContext(blockchain.getBestBlock(), Collections.singletonList(tx), false);
inStore = new BigInteger(executeTransaction(tx, context.block, repo).getResult());
assertEquals(BigInteger.ZERO, inStore);
}
Aggregations