use of org.aion.zero.impl.types.MiningBlock in project aion by aionnetwork.
the class InternalTransactionTest method testNestedCreateWithExistedAccount.
@Test
public void testNestedCreateWithExistedAccount() throws Exception {
String contractA = "0x60506040523415600f5760006000fd5b5b60166048565b604051809103906000f0801582151615602f5760006000fd5b60006000508282909180600101839055555050505b6057565b604051605a8061009f83390190565b603a806100656000396000f30060506040526008565b60006000fd00a165627a7a72305820c0eea40d4778b01848164e58898e9e8c8ab068ed5ee36ed6f0582d119ecbbede002960506040523415600f5760006000fd5b6013565b603a8060206000396000f30060506040526008565b60006000fd00a165627a7a723058208c13bc92baf844f8574632dca44c49776516cb6cd537b10ed700bf61392b6ae80029";
StandaloneBlockchain.Bundle bundle = (new StandaloneBlockchain.Builder()).withValidatorConfiguration("simple").withDefaultAccounts().build();
StandaloneBlockchain bc = bundle.bc;
ECKey deployerAccount = bundle.privateKeys.get(0);
AionAddress firstContractAddr = TxUtil.calculateContractAddress(deployerAccount.getAddress(), BigInteger.ONE);
AionAddress internalContractAddress = TxUtil.calculateContractAddress(firstContractAddr.toByteArray(), BigInteger.ZERO);
BigInteger nonce = BigInteger.ZERO;
// ======================
// Transfer balance to the internal contract address
// ======================
AionTransaction tx = AionTransaction.create(deployerAccount, nonce.toByteArray(), internalContractAddress, BigInteger.ONE.toByteArray(), new byte[0], 1_000_000L, minEnergyPrice, TransactionTypes.DEFAULT, null);
Block parentBlock = bc.getBestBlock();
MiningBlock newBlock = bc.createBlock(parentBlock, Collections.singletonList(tx), false, parentBlock.getTimestamp());
Pair<ImportResult, AionBlockSummary> result = bc.tryToConnectAndFetchSummary(newBlock);
assertTrue(result.getLeft().isSuccessful());
nonce = nonce.add(BigInteger.ONE);
bc.forkUtility.enable040Fork(1000);
// ======================
// DEPLOY Failed
// ======================
tx = AionTransaction.create(deployerAccount, nonce.toByteArray(), null, new byte[0], ByteUtil.hexStringToBytes(contractA), 1_000_000L, minEnergyPrice, TransactionTypes.DEFAULT, null);
parentBlock = bc.getBestBlock();
newBlock = bc.createBlock(parentBlock, Collections.singletonList(tx), false, parentBlock.getTimestamp());
result = bc.tryToConnectAndFetchSummary(newBlock);
assertTrue(result.getLeft().isSuccessful());
assertEquals("reverted", result.getRight().getReceipts().get(0).getError());
nonce = nonce.add(BigInteger.ONE);
bc.forkUtility.enable040Fork(0);
// ======================
// DEPLOY
// ======================
tx = AionTransaction.create(deployerAccount, nonce.toByteArray(), null, new byte[0], ByteUtil.hexStringToBytes(contractA), 1_000_000L, minEnergyPrice, TransactionTypes.DEFAULT, null);
System.out.println("contractaddr: " + TxUtil.calculateContractAddress(tx));
BlockContext context = bc.createNewMiningBlockContext(bc.getBestBlock(), List.of(tx), false);
AionTxExecSummary summary = executeTransaction(bc, context, tx);
System.out.println(summary.getReceipt());
boolean firstItx = true;
for (InternalTransaction itx : summary.getInternalTransactions()) {
System.out.println(itx);
if (firstItx) {
AionAddress contractAddress = TxUtil.calculateContractAddress(tx);
assertNotNull(contractAddress);
assertTrue(bc.getRepository().hasAccountState(contractAddress));
assertTrue(bc.getRepository().hasAccountState(TxUtil.calculateContractAddress(contractAddress.toByteArray(), BigInteger.ZERO)));
firstItx = false;
}
}
}
use of org.aion.zero.impl.types.MiningBlock in project aion by aionnetwork.
the class InternalTransactionTest method executeTransaction.
private AionTxExecSummary executeTransaction(StandaloneBlockchain bc, BlockContext context, AionTransaction transaction) throws VmFatalException {
RepositoryCache cache = bc.getRepository().startTracking();
MiningBlock block = context.block;
AionTxExecSummary summary = BulkExecutor.executeTransactionWithNoPostExecutionWork(block.getDifficulty(), block.getNumber(), block.getTimestamp(), block.getNrgLimit(), block.getCoinbase(), transaction, cache, false, true, false, false, LOGGER_VM, BlockCachingContext.PENDING, block.getNumber() - 1, false, false);
cache.flushTo(bc.getRepository(), true);
return summary;
}
use of org.aion.zero.impl.types.MiningBlock in project aion by aionnetwork.
the class MultiVersionAvmTest method testCallInBothAvmVersions.
/**
* We test deploying & calling the same contract in version 1 and 2. We expect the same result
* each time.
*/
@Test
public void testCallInBothAvmVersions() {
Assert.assertEquals(0, this.blockchain.getBestBlock().getNumber());
// Ensure we are at a block height where avm version 1 is enabled, then deploy.
buildBlockchainToHeight(BLOCK_VERSION1_ENABLED);
AionAddress contractForVersion1 = deployHelloWorldContract(AvmVersion.VERSION_1, BigInteger.ZERO);
AionTransaction transactionForVersion1 = makeHelloWorldCallTransaction(AvmVersion.VERSION_1, BigInteger.ONE, contractForVersion1);
Block parentBlock = this.blockchain.getBestBlock();
MiningBlock block = this.blockchain.createBlock(parentBlock, Collections.singletonList(transactionForVersion1), false, parentBlock.getTimestamp());
Pair<ImportResult, AionBlockSummary> connectResult = this.blockchain.tryToConnectAndFetchSummary(block);
Assert.assertEquals(ImportResult.IMPORTED_BEST, connectResult.getLeft());
Assert.assertEquals(1, connectResult.getRight().getReceipts().size());
Assert.assertTrue(connectResult.getRight().getReceipts().get(0).isSuccessful());
// Now, climb to a block height where avm version 2 is enabled and deploy.
buildBlockchainToHeight(BLOCK_VERSION2_ENABLED);
AionAddress contractForVersion2 = deployHelloWorldContract(AvmVersion.VERSION_2, BigInteger.TWO);
AionTransaction transactionForVersion2 = makeHelloWorldCallTransaction(AvmVersion.VERSION_2, BigInteger.valueOf(3), contractForVersion2);
parentBlock = this.blockchain.getBestBlock();
block = this.blockchain.createBlock(parentBlock, Collections.singletonList(transactionForVersion2), false, parentBlock.getTimestamp());
connectResult = this.blockchain.tryToConnectAndFetchSummary(block);
Assert.assertEquals(ImportResult.IMPORTED_BEST, connectResult.getLeft());
Assert.assertEquals(1, connectResult.getRight().getReceipts().size());
Assert.assertTrue(connectResult.getRight().getReceipts().get(0).isSuccessful());
}
use of org.aion.zero.impl.types.MiningBlock in project aion by aionnetwork.
the class VMTxStateAlignTest method testDeployAndCallContractStatesAlign.
@Test
public void testDeployAndCallContractStatesAlign() throws IOException {
List<AionTransaction> txList = new ArrayList<>();
List<AionAddress> deployAddr = new ArrayList<>();
for (int i = 0; i < this.deployerKeys.size() && i < deployerNum; i++) {
ECKey senderKey = this.deployerKeys.get(i);
txList.add(makeFvmContractCreateTransaction(senderKey, BigInteger.ZERO));
deployAddr.add(TxUtil.calculateContractAddress(txList.get(i)));
}
MiningBlock deplayBlock = genNewBlock(txList, blockchainWoAVM);
tryImportNewBlock(blockchainWoAVM, deplayBlock);
txList.clear();
ECKey sender = this.deployerKeys.get(deployerKeys.size() - 1);
txList.add(makePrecompiledContractTransaction(sender, BigInteger.ZERO));
for (int i = 0; i < this.deployerKeys.size() && i < deployerNum; i++) {
ECKey senderKey = this.deployerKeys.get(i);
txList.add(makeFvmContractCallTransaction(senderKey, BigInteger.ONE, deployAddr.get(i)));
}
MiningBlock callBlock = genNewBlock(txList, blockchainWoAVM);
tryImportNewBlock(blockchainWoAVM, callBlock);
// Does not initial in the setup call due to the avmenable in the StandaloneBlockchain is a
// static variable.
StandaloneBlockchain.Bundle bundleAVM = new StandaloneBlockchain.Builder().withDefaultAccounts(deployerKeys).withAvmEnabled().withValidatorConfiguration("simple").build();
StandaloneBlockchain blockchainWithAVM = bundleAVM.bc;
tryImportNewBlock(blockchainWithAVM, deplayBlock);
tryImportNewBlock(blockchainWithAVM, callBlock);
}
use of org.aion.zero.impl.types.MiningBlock 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