use of org.aion.zero.impl.vm.TestResourceProvider in project aion by aionnetwork.
the class PendingStateTest method testAddPendingTransaction_AVMContractCall_Success.
@Test
public void testAddPendingTransaction_AVMContractCall_Success() throws Exception {
TestResourceProvider resourceProvider = TestResourceProvider.initializeAndCreateNewProvider(AvmPathManager.getPathOfProjectRootDirectory());
IAvmResourceFactory resourceFactory = resourceProvider.factoryForVersion1;
// Successful transaction
byte[] jar = resourceFactory.newContractFactory().getDeploymentBytes(AvmContract.HELLO_WORLD);
AionTransaction createTransaction = AionTransaction.create(deployerKey, BigInteger.ZERO.toByteArray(), null, BigInteger.ZERO.toByteArray(), jar, 5_000_000, energyPrice, TransactionTypes.AVM_CREATE_CODE, null);
assertEquals(pendingState.addTransactionFromApiServer(createTransaction), TxResponse.SUCCESS);
MiningBlock block = blockchain.createNewMiningBlock(blockchain.getBestBlock(), pendingState.getPendingTransactions(), false);
Pair<ImportResult, AionBlockSummary> connectResult = blockchain.tryToConnectAndFetchSummary(block);
AionTxReceipt receipt = connectResult.getRight().getReceipts().get(0);
// Check the block was imported, the contract has the Avm prefix, and deployment succeeded.
assertThat(connectResult.getLeft()).isEqualTo(ImportResult.IMPORTED_BEST);
// verify that the output is indeed the contract address
AionAddress contractAddress = TxUtil.calculateContractAddress(createTransaction);
assertThat(contractAddress.toByteArray()).isEqualTo(receipt.getTransactionOutput());
AionAddress contract = new AionAddress(receipt.getTransactionOutput());
byte[] call = resourceFactory.newStreamingEncoder().encodeOneString("sayHello").getEncoding();
AionTransaction callTransaction = AionTransaction.create(deployerKey, BigInteger.ONE.toByteArray(), contract, BigInteger.ZERO.toByteArray(), call, 2_000_000, energyPrice, TransactionTypes.DEFAULT, null);
assertEquals(pendingState.addTransactionFromApiServer(callTransaction), TxResponse.SUCCESS);
}
use of org.aion.zero.impl.vm.TestResourceProvider in project aion by aionnetwork.
the class BlockchainForkingTest method testVmTypeRetrieval_ForkWithConflictingContractVM.
/**
* Ensures that if a side-chain block is imported after a main-chain block creating the same
* contract address X but using different VMs, then each chain will operate on the correct VM.
*/
@Test
public void testVmTypeRetrieval_ForkWithConflictingContractVM() throws Exception {
TestResourceProvider resourceProvider = TestResourceProvider.initializeAndCreateNewProvider(AvmPathManager.getPathOfProjectRootDirectory());
IAvmResourceFactory resourceFactory = resourceProvider.factoryForVersion1;
// blocks to be built
MiningBlock block, fastBlock, slowBlock, lowBlock, highBlock;
// transactions used in blocks
AionTransaction deployOnAVM, deployOnFVM, callTxOnFVM;
// for processing block results
Pair<ImportResult, AionBlockSummary> connectResult;
ImportResult result;
AionTxReceipt receipt;
// build a blockchain
TransactionTypeRule.allowAVMContractTransaction();
List<ECKey> accounts = generateAccounts(10);
StandaloneBlockchain.Builder builder = new StandaloneBlockchain.Builder();
StandaloneBlockchain sourceChain = builder.withValidatorConfiguration("simple").withDefaultAccounts(accounts).build().bc;
StandaloneBlockchain testChain = builder.withValidatorConfiguration("simple").withDefaultAccounts(accounts).build().bc;
ECKey sender = accounts.remove(0);
assertThat(testChain).isNotEqualTo(sourceChain);
assertThat(testChain.genesis).isEqualTo(sourceChain.genesis);
long time = System.currentTimeMillis();
// add a block to both chains
block = sourceChain.createNewMiningBlockInternal(sourceChain.getBestBlock(), Collections.emptyList(), true, time / 10_000L).block;
assertThat(sourceChain.tryToConnect(block)).isEqualTo(ImportResult.IMPORTED_BEST);
assertThat(testChain.tryToConnect(block)).isEqualTo(ImportResult.IMPORTED_BEST);
// ****** setup side chain ******
// deploy contracts on different VMs for the two chains
deployOnAVM = deployStatefulnessAVMContract(resourceFactory, sender);
fastBlock = sourceChain.createNewMiningBlockInternal(sourceChain.getBestBlock(), Arrays.asList(deployOnAVM), true, time / 10_000L).block;
deployOnFVM = deployContract(sender);
slowBlock = new MiningBlock(sourceChain.createNewMiningBlockInternal(sourceChain.getBestBlock(), Arrays.asList(deployOnFVM), true, time / 10_000L).block);
MiningBlockHeader newBlockHeader = MiningBlockHeader.Builder.newInstance().withHeader(slowBlock.getHeader()).withTimestamp(time / 10_000L + 100).build();
slowBlock.updateHeader(newBlockHeader);
time += 100;
// sourceChain imports only fast block
connectResult = sourceChain.tryToConnectAndFetchSummary(fastBlock);
result = connectResult.getLeft();
receipt = connectResult.getRight().getReceipts().get(0);
assertThat(result).isEqualTo(ImportResult.IMPORTED_BEST);
assertThat(receipt.isSuccessful()).isTrue();
AionAddress contract = TxUtil.calculateContractAddress(receipt.getTransaction());
// testChain imports both blocks
connectResult = testChain.tryToConnectAndFetchSummary(fastBlock);
result = connectResult.getLeft();
receipt = connectResult.getRight().getReceipts().get(0);
assertThat(result).isEqualTo(ImportResult.IMPORTED_BEST);
assertThat(receipt.isSuccessful()).isTrue();
assertThat(TxUtil.calculateContractAddress(receipt.getTransaction())).isEqualTo(contract);
connectResult = testChain.tryToConnectAndFetchSummary(slowBlock);
result = connectResult.getLeft();
receipt = connectResult.getRight().getReceipts().get(0);
assertThat(result).isEqualTo(ImportResult.IMPORTED_NOT_BEST);
assertThat(receipt.isSuccessful()).isTrue();
assertThat(TxUtil.calculateContractAddress(receipt.getTransaction())).isEqualTo(contract);
// ****** check that the correct contract details are kept ******
// check that both chains have the correct vm type for the AVM contract
byte[] codeHashAVM = sourceChain.getRepository().getAccountState(contract).getCodeHash();
assertThat(testChain.getRepository().getVMUsed(contract, codeHashAVM)).isEqualTo(sourceChain.getRepository().getVMUsed(contract, codeHashAVM));
// check that only the second chain has the vm type for the FVM contract
byte[] codeHashFVM = ((AionRepositoryImpl) testChain.getRepository().getSnapshotTo(slowBlock.getStateRoot())).getAccountState(contract).getCodeHash();
assertThat(sourceChain.getRepository().getVMUsed(contract, codeHashFVM)).isEqualTo(InternalVmType.UNKNOWN);
assertThat(testChain.getRepository().getVMUsed(contract, codeHashFVM)).isEqualTo(InternalVmType.FVM);
// check the stored information details
ContractInformation infoSingleImport = sourceChain.getRepository().getIndexedContractInformation(contract);
System.out.println("without side chain:" + infoSingleImport);
assertThat(infoSingleImport.getVmUsed(codeHashAVM)).isEqualTo(InternalVmType.AVM);
assertThat(infoSingleImport.getInceptionBlocks(codeHashAVM)).isEqualTo(Set.of(fastBlock.getHashWrapper()));
assertThat(infoSingleImport.getVmUsed(codeHashFVM)).isEqualTo(InternalVmType.UNKNOWN);
assertThat(infoSingleImport.getInceptionBlocks(codeHashFVM)).isEmpty();
ContractInformation infoMultiImport = testChain.getRepository().getIndexedContractInformation(contract);
System.out.println("with side chain:" + infoMultiImport);
assertThat(infoMultiImport.getVmUsed(codeHashAVM)).isEqualTo(InternalVmType.AVM);
assertThat(infoMultiImport.getInceptionBlocks(codeHashAVM)).isEqualTo(Set.of(fastBlock.getHashWrapper()));
assertThat(infoMultiImport.getVmUsed(codeHashFVM)).isEqualTo(InternalVmType.FVM);
assertThat(infoMultiImport.getInceptionBlocks(codeHashFVM)).isEqualTo(Set.of(slowBlock.getHashWrapper()));
// build two blocks where the second block has a higher total difficulty
callTxOnFVM = callSetValue(sender, contract, 9, BigInteger.ONE);
lowBlock = testChain.createNewMiningBlockInternal(slowBlock, Arrays.asList(callTxOnFVM), true, time / 10_000L + 101).block;
int expectedCount = 3;
List<AionTransaction> callTxOnAVM = callStatefulnessAVM(resourceFactory, sender, expectedCount, BigInteger.ONE, contract);
highBlock = sourceChain.createNewMiningBlockInternal(fastBlock, callTxOnAVM, true, time / 10_000L).block;
assertThat(highBlock.getDifficultyBI()).isGreaterThan(lowBlock.getDifficultyBI());
// build first chain with highBlock applied directly
connectResult = sourceChain.tryToConnectAndFetchSummary(highBlock);
// get last tx
receipt = connectResult.getRight().getReceipts().get(expectedCount);
assertThat(receipt.isSuccessful()).isTrue();
result = connectResult.getLeft();
assertThat(result).isEqualTo(ImportResult.IMPORTED_BEST);
// collect the consensus information from the block & receipt
AionBlockSummary blockSummary = connectResult.getRight();
byte[] stateRoot = blockSummary.getBlock().getStateRoot();
byte[] blockReceiptsRoot = blockSummary.getBlock().getReceiptsRoot();
byte[] receiptTrieEncoded = receipt.getReceiptTrieEncoded();
int returnedCount = resourceFactory.newDecoder(blockSummary.getReceipts().get(expectedCount).getTransactionOutput()).decodeOneInteger();
assertThat(returnedCount).isEqualTo(expectedCount);
// ****** test fork behavior ******
// first import lowBlock
assertThat(testChain.tryToConnect(lowBlock)).isEqualTo(ImportResult.IMPORTED_BEST);
// next import highBlock causing the fork
connectResult = testChain.tryToConnectAndFetchSummary(highBlock);
receipt = connectResult.getRight().getReceipts().get(expectedCount);
assertThat(receipt.isSuccessful()).isTrue();
System.out.println(receipt);
result = connectResult.getLeft();
assertThat(result).isEqualTo(ImportResult.IMPORTED_BEST);
// collect the consensus information from the block & receipt
blockSummary = connectResult.getRight();
assertThat(testChain.getBestBlock()).isEqualTo(sourceChain.getBestBlock());
assertThat(blockSummary.getBlock().getStateRoot()).isEqualTo(stateRoot);
assertThat(blockSummary.getBlock().getReceiptsRoot()).isEqualTo(blockReceiptsRoot);
assertThat(receipt.getReceiptTrieEncoded()).isEqualTo(receiptTrieEncoded);
returnedCount = resourceFactory.newDecoder(blockSummary.getReceipts().get(expectedCount).getTransactionOutput()).decodeOneInteger();
assertThat(returnedCount).isEqualTo(expectedCount);
}
use of org.aion.zero.impl.vm.TestResourceProvider in project aion by aionnetwork.
the class SignatureSchemeSwapForkTest method testRejectFvmContractDeploy.
@Test
// We ignore this test due to the FVM contract deploy is not disable in ver1.6
@Ignore
public void testRejectFvmContractDeploy() throws ClassNotFoundException, InstantiationException, IllegalAccessException, IOException {
// setup Unity fork and AVM
long unityForkBlock = 2;
long signatureSwapForkBlockHeight = unityForkBlock + 3;
setupAVM(unityForkBlock);
TestResourceProvider resourceProvider = TestResourceProvider.initializeAndCreateNewProvider(AvmPathManager.getPathOfProjectRootDirectory());
// setup an identical blockchains
StandaloneBlockchain blockchain = setupIdenticalBlockchain(unityForkBlock, signatureSwapForkBlockHeight);
// create block with staker registry
Block blockWithRegistry = BlockchainTestUtils.generateNextMiningBlockWithStakerRegistry(blockchain, blockchain.getGenesis(), resourceProvider, stakingRegistryOwner);
// import block on firstChain
Pair<ImportResult, AionBlockSummary> result = blockchain.tryToConnectAndFetchSummary(blockWithRegistry);
assertThat(result.getLeft()).isEqualTo(ImportResult.IMPORTED_BEST);
assertThat(result.getRight().getReceipts().get(0).isSuccessful()).isTrue();
assertThat(result.getRight().getReceipts().get(0).getLogInfoList()).isNotEmpty();
assertThat(result.getRight().getReceipts().get(0).getEnergyUsed()).isEqualTo(1_225_655L);
// set the staking contract address in the staking genesis
AionTransaction deploy = blockWithRegistry.getTransactionsList().get(0);
AionAddress contract = TxUtil.calculateContractAddress(deploy.getSenderAddress().toByteArray(), deploy.getNonceBI());
blockchain.getGenesis().setStakingContractAddress(contract);
// create Unity block with stakers
Block block2Unity = BlockchainTestUtils.generateNextMiningBlockWithStakers(blockchain, blockchain.getBestBlock(), resourceProvider, stakers, MIN_SELF_STAKE);
// import block2Unity on blockchain
result = blockchain.tryToConnectAndFetchSummary(block2Unity);
assertThat(result.getLeft()).isEqualTo(ImportResult.IMPORTED_BEST);
verifyReceipts(result.getRight().getReceipts(), 3, true);
// create staking block
Block block3Staking = BlockchainTestUtils.generateNextStakingBlock(blockchain, blockchain.getBestBlock(), Collections.emptyList(), stakers.get(0));
assertThat(block3Staking).isNotNull();
// import block3Staking on blockchain
result = blockchain.tryToConnectAndFetchSummary(block3Staking);
assertThat(result.getLeft()).isEqualTo(ImportResult.IMPORTED_BEST);
// create next mining block
AionTransaction tx = BlockchainTestUtils.deployFvmTickerContractTransaction(accounts.get(4), BigInteger.ZERO);
Block block4Mining = BlockchainTestUtils.generateNextMiningBlock(blockchain, blockchain.getBestBlock(), Collections.singletonList(tx));
assertThat(block4Mining.getTransactionsList().size() == 1).isTrue();
// import block4Mining on blockchain
result = blockchain.tryToConnectAndFetchSummary(block4Mining);
assertThat(result.getLeft()).isEqualTo(ImportResult.IMPORTED_BEST);
// create the first signatureSchemeSwap block
tx = BlockchainTestUtils.deployFvmTickerContractTransaction(accounts.get(5), BigInteger.ZERO);
Block block5SignatureSchemeSwapStaking = BlockchainTestUtils.generateNextStakingBlock(blockchain, blockchain.getBestBlock(), Collections.singletonList(tx), stakers.get(0));
assertThat(block5SignatureSchemeSwapStaking).isNotNull();
assertThat(block5SignatureSchemeSwapStaking.getTransactionsList().size() == 0).isTrue();
// import block5SignatureSchemeSwapStaking on blockchain
result = blockchain.tryToConnectAndFetchSummary(block5SignatureSchemeSwapStaking);
assertThat(result.getLeft()).isEqualTo(ImportResult.IMPORTED_BEST);
// create next mining block with rejected transaction
Block block6Mining = BlockchainTestUtils.generateNextMiningBlock(blockchain, blockchain.getBestBlock(), Collections.singletonList(tx));
assertThat(block6Mining.getTransactionsList().size() == 0).isTrue();
}
use of org.aion.zero.impl.vm.TestResourceProvider in project aion by aionnetwork.
the class SignatureSchemeSwapForkTest method testSigatureSchemeSwapForkWithFallbackTransaction.
@Test
public void testSigatureSchemeSwapForkWithFallbackTransaction() throws ClassNotFoundException, InstantiationException, IllegalAccessException, IOException {
// setup Unity fork and AVM
long unityForkBlock = 2;
long signatureSwapForkBlockHeight = unityForkBlock + 3;
setupAVM(unityForkBlock);
TestResourceProvider resourceProvider = TestResourceProvider.initializeAndCreateNewProvider(AvmPathManager.getPathOfProjectRootDirectory());
// setup an identical blockchains
StandaloneBlockchain blockchain = setupIdenticalBlockchain(unityForkBlock, signatureSwapForkBlockHeight);
// create block with staker registry
Block blockWithRegistry = BlockchainTestUtils.generateNextMiningBlockWithStakerRegistry(blockchain, blockchain.getGenesis(), resourceProvider, stakingRegistryOwner);
// import block on firstChain
Pair<ImportResult, AionBlockSummary> result = blockchain.tryToConnectAndFetchSummary(blockWithRegistry);
assertThat(result.getLeft()).isEqualTo(ImportResult.IMPORTED_BEST);
assertThat(result.getRight().getReceipts().get(0).isSuccessful()).isTrue();
assertThat(result.getRight().getReceipts().get(0).getLogInfoList()).isNotEmpty();
assertThat(result.getRight().getReceipts().get(0).getEnergyUsed()).isEqualTo(1_225_655L);
// set the staking contract address in the staking genesis
AionTransaction deploy = blockWithRegistry.getTransactionsList().get(0);
AionAddress contract = TxUtil.calculateContractAddress(deploy.getSenderAddress().toByteArray(), deploy.getNonceBI());
blockchain.getGenesis().setStakingContractAddress(contract);
// create Unity block with stakers
Block block2Unity = BlockchainTestUtils.generateNextMiningBlockWithStakers(blockchain, blockchain.getBestBlock(), resourceProvider, stakers, MIN_SELF_STAKE);
// import block2Unity on blockchain
result = blockchain.tryToConnectAndFetchSummary(block2Unity);
assertThat(result.getLeft()).isEqualTo(ImportResult.IMPORTED_BEST);
verifyReceipts(result.getRight().getReceipts(), 3, true);
// create staking block
Block block3Staking = BlockchainTestUtils.generateNextStakingBlock(blockchain, blockchain.getBestBlock(), Collections.emptyList(), stakers.get(0));
assertThat(block3Staking).isNotNull();
// import block3Staking on blockchain
result = blockchain.tryToConnectAndFetchSummary(block3Staking);
assertThat(result.getLeft()).isEqualTo(ImportResult.IMPORTED_BEST);
// create an invalid transaction for the hardfork fallback.
List<AionTransaction> fallbackTx = BlockchainTestUtils.generateTransactions(1, accounts, blockchain.getRepository(), true);
List<byte[]> fallbackTxHash = new ArrayList<>();
for (AionTransaction a : fallbackTx) {
fallbackTxHash.add(a.getTransactionHash());
}
// cracking the config settings
CfgAion.inst().getFork().setRollbackTx(fallbackTxHash);
// create next mining block
Block block4Mining = BlockchainTestUtils.generateNextMiningBlock(blockchain, blockchain.getBestBlock(), fallbackTx);
// import block4Mining on blockchain
result = blockchain.tryToConnectAndFetchSummary(block4Mining);
assertThat(result.getLeft()).isEqualTo(ImportResult.IMPORTED_BEST);
// Check the transaction has been processed
assertThat(fallbackTx.get(0).getValueBI().equals(blockchain.getRepository().getBalance(fallbackTx.get(0).getDestinationAddress())));
BigInteger senderBalance = blockchain.getRepository().getBalance(fallbackTx.get(0).getSenderAddress());
// create the first signatureSchemeSwap block
Block block5SignatureSchemeSwapStaking = BlockchainTestUtils.generateNextStakingBlock(blockchain, blockchain.getBestBlock(), Collections.emptyList(), stakers.get(0));
assertThat(block5SignatureSchemeSwapStaking).isNotNull();
// import block5SignatureSchemeSwapStaking on blockchain
result = blockchain.tryToConnectAndFetchSummary(block5SignatureSchemeSwapStaking);
assertThat(result.getLeft()).isEqualTo(ImportResult.IMPORTED_BEST);
// Check the invalid account has been fallbacked
assertThat(blockchain.getRepository().getAccountState(fallbackTx.get(0).getDestinationAddress()) == null);
BigInteger senderBalanceNew = blockchain.getRepository().getBalance(fallbackTx.get(0).getSenderAddress());
assertThat(senderBalanceNew.equals(senderBalance.add(fallbackTx.get(0).getValueBI())));
// create next mining block
Block block6Mining = BlockchainTestUtils.generateNextMiningBlock(blockchain, blockchain.getBestBlock(), Collections.emptyList());
// import block6Mining on blockchain
result = blockchain.tryToConnectAndFetchSummary(block6Mining);
assertThat(result.getLeft()).isEqualTo(ImportResult.IMPORTED_BEST);
// create next staking block
Block block7Staking = BlockchainTestUtils.generateNextStakingBlock(blockchain, blockchain.getBestBlock(), Collections.emptyList(), stakers.get(0));
// import block7Staking on blockchain
result = blockchain.tryToConnectAndFetchSummary(block7Staking);
assertThat(result.getLeft()).isEqualTo(ImportResult.IMPORTED_BEST);
}
use of org.aion.zero.impl.vm.TestResourceProvider in project aion by aionnetwork.
the class PendingStateTest method testAddPendingTransaction_AVMContractDeploy_Success.
@Test
public void testAddPendingTransaction_AVMContractDeploy_Success() throws Exception {
TestResourceProvider resourceProvider = TestResourceProvider.initializeAndCreateNewProvider(AvmPathManager.getPathOfProjectRootDirectory());
IAvmResourceFactory resourceFactory = resourceProvider.factoryForVersion1;
// Successful transaction
byte[] jar = resourceFactory.newContractFactory().getDeploymentBytes(AvmContract.HELLO_WORLD);
AionTransaction transaction = AionTransaction.create(deployerKey, BigInteger.ZERO.toByteArray(), null, BigInteger.ZERO.toByteArray(), jar, 5_000_000, energyPrice, TransactionTypes.AVM_CREATE_CODE, null);
assertEquals(pendingState.addTransactionFromApiServer(transaction), TxResponse.SUCCESS);
}
Aggregations