use of org.aion.zero.impl.core.ImportResult in project aion by aionnetwork.
the class BlockchainIntegrationTest method testDisconnection.
/**
* This is a special case for testing that the blockchain stores disconnected blocks if we
* violate the contract of changing the block contents after importing best
*
* <p>This behaviour was originally observed when running a pooled miner
*/
@Test
public void testDisconnection() {
StandaloneBlockchain.Bundle bundle = (new StandaloneBlockchain.Builder()).withValidatorConfiguration("simple").withDefaultAccounts().build();
StandaloneBlockchain bc = bundle.bc;
// store this as the best block,
MiningBlock block = bundle.bc.createNewMiningBlock(bc.getGenesis(), Collections.emptyList(), true);
byte[] block1Hash = block.getHash();
// now modify this block to have a different value after connecting
ImportResult result = bundle.bc.tryToConnect(block);
assertThat(result).isEqualTo(ImportResult.IMPORTED_BEST);
// now modify the block in some way
MiningBlockHeader newBlockHeader = MiningBlockHeader.Builder.newInstance().withHeader(block.getHeader()).withExtraData("other block".getBytes()).build();
block.updateHeader(newBlockHeader);
// now try submitting the block again
result = bundle.bc.tryToConnect(block);
assertThat(result).isEqualTo(ImportResult.IMPORTED_NOT_BEST);
// now try creating a new block
MiningBlock newBlock = bundle.bc.createNewMiningBlock(bundle.bc.getBestBlock(), Collections.emptyList(), true);
// gets the first main-chain block
Block storedBlock1 = bundle.bc.getBlockByNumber(1L);
System.out.println(ByteUtil.toHexString(storedBlock1.getHash()));
System.out.println(ByteUtil.toHexString(newBlock.getParentHash()));
assertThat(newBlock.getParentHash()).isNotEqualTo(storedBlock1.getHash());
assertThat(newBlock.getParentHash()).isEqualTo(block.getHash());
}
use of org.aion.zero.impl.core.ImportResult in project aion by aionnetwork.
the class BlockchainIntegrationTest method testBlockContextCreation.
@Test
public void testBlockContextCreation() {
StandaloneBlockchain.Bundle bundle = (new StandaloneBlockchain.Builder()).withValidatorConfiguration("simple").withDefaultAccounts().build();
StandaloneBlockchain bc = bundle.bc;
BlockContext context = bc.createNewMiningBlockContext(bc.getBestBlock(), Collections.emptyList(), false);
ChainConfiguration configuration = new ChainConfiguration();
// check some basic fields match first
assertThat(context.block).isNotNull();
assertThat(context.baseBlockReward).isNotNull();
assertThat(context.transactionFee).isNotNull();
// no transaction so fee should be zero
assertThat(context.transactionFee).isEqualTo(BigInteger.ZERO);
AionAddress beneficiary = context.block.getCoinbase();
ImportResult result = bc.tryToConnect(context.block);
// check that the correct amount was stored
assertThat(result).isEqualTo(ImportResult.IMPORTED_BEST);
assertThat(bc.getRepository().getBalance(beneficiary)).isEqualTo(context.baseBlockReward);
// check that the correct amount was calculated
assertThat(configuration.getRewardsCalculatorBeforeSignatureSchemeSwap(false).calculateReward(context.block.getHeader().getNumber())).isEqualTo(context.baseBlockReward);
}
use of org.aion.zero.impl.core.ImportResult in project aion by aionnetwork.
the class BlockchainIntegrationTest method testSimpleFailedTransactionInsufficientBalance.
@Test
public void testSimpleFailedTransactionInsufficientBalance() {
// generate a recipient
final AionAddress receiverAddress = AddressUtils.wrapAddress("CAFECAFECAFECAFECAFECAFECAFECAFECAFECAFECAFECAFECAFECAFECAFECAFE");
StandaloneBlockchain.Bundle bundle = (new StandaloneBlockchain.Builder()).withValidatorConfiguration("simple").withDefaultAccounts().build();
StandaloneBlockchain bc = bundle.bc;
// (byte[] nonce, byte[] from, byte[] to, byte[] value, byte[] data)
ECKey key = bundle.privateKeys.get(0);
AionTransaction tx = AionTransaction.create(key, BigInteger.valueOf(1).toByteArray(), receiverAddress, BigInteger.valueOf(100).toByteArray(), ByteUtil.EMPTY_BYTE_ARRAY, 1L, energyPrice, TransactionTypes.DEFAULT, null);
MiningBlock block = bc.createNewMiningBlock(bc.getBestBlock(), Collections.singletonList(tx), true);
assertThat(block.getTransactionsList()).isEmpty();
assertThat(block.getTxTrieRoot()).isEqualTo(ConstantUtil.EMPTY_TRIE_HASH);
ImportResult connection = bc.tryToConnect(block);
assertThat(connection).isEqualTo(ImportResult.IMPORTED_BEST);
}
use of org.aion.zero.impl.core.ImportResult in project aion by aionnetwork.
the class BlockchainRewardTest method testBlockchainRewardMonotonicallyIncreasing.
/**
* Test that blocks between the lower and upper bounds follow a certain function [0, 259200]
*
* <p>Note: this test is resource consuming!
*
* <p>Check {@link org.aion.zero.impl.core.RewardsCalculator} for algorithm related to the
* ramp-up block time
*/
@Ignore
@Test
public void testBlockchainRewardMonotonicallyIncreasing() {
StandaloneBlockchain.Bundle bundle = new StandaloneBlockchain.Builder().withDefaultAccounts().withValidatorConfiguration("simple").build();
StandaloneBlockchain bc = bundle.bc;
MiningBlock block = bc.createNewMiningBlock(bc.getBestBlock(), Collections.EMPTY_LIST, true);
ImportResult res = bc.tryToConnect(block);
assertThat(res).isEqualTo(ImportResult.IMPORTED_BEST);
AionAddress coinbase = block.getCoinbase();
BigInteger previousBalance = bc.getRepository().getBalance(coinbase);
// first block already sealed
for (int i = 2; i < 99999; i++) {
MiningBlock b = bc.createNewMiningBlock(bc.getBestBlock(), Collections.EMPTY_LIST, true);
ImportResult r = bc.tryToConnect(b);
assertThat(r).isEqualTo(ImportResult.IMPORTED_BEST);
// note the assumption here that blocks are mined by one coinbase
BigInteger balance = bc.getRepository().getBalance(coinbase);
assertThat(balance).isGreaterThan(previousBalance);
previousBalance = balance;
if (b.getNumber() % 1000 == 0)
System.out.println("added block #: " + i);
}
}
use of org.aion.zero.impl.core.ImportResult in project aion by aionnetwork.
the class TransactionCreateSpecificationTests method deployInternalAvmContractOnTopOfAddressWithStorageUsingAvmVersion1.
@Test
public void deployInternalAvmContractOnTopOfAddressWithStorageUsingAvmVersion1() throws VmFatalException {
AvmTestConfig.clearConfigurations();
AvmTestConfig.supportOnlyAvmVersion1();
// Deploy AVM contract.
AionTransaction deployTxAvm = BlockchainTestUtils.deployAvmContractTransaction(AvmContract.DEPLOY_INTERNAL, resourceProvider.factoryForVersion1, SENDER_KEY, BigInteger.ZERO);
AionAddress contract = TxUtil.calculateContractAddress(deployTxAvm);
Pair<Block, ImportResult> resultImport = BlockchainTestUtils.addMiningBlock(blockchain, blockchain.getBestBlock(), List.of(deployTxAvm));
assertThat(resultImport.getRight()).isEqualTo(ImportResult.IMPORTED_BEST);
// Call AVM contract to deploy new internal AVM contract (version without required success).
long internalLimit = 1_000_000;
AionTransaction deployInternal = BlockchainTestUtils.callSimpleAvmContractTransaction(resourceProvider.factoryForVersion1, SENDER_KEY, BigInteger.ONE, contract, "deploy", deployTxAvm.getData(), internalLimit);
AionAddress internalContract = new AionAddress(Hex.decode("a0268090998a99666b72cc452b9307438a34341047d9e0d7b92c9207bf413655"));
assertThat(blockchain.getRepository().hasAccountState(internalContract)).isFalse();
// Manipulate the repository to have a non-default storage value.
RepositoryCache cache = blockchain.getRepository().startTracking();
cache.addStorageRow(internalContract, ByteArrayWrapper.wrap(RandomUtils.nextBytes(16)), ByteArrayWrapper.wrap(RandomUtils.nextBytes(16)));
cache.saveVmType(internalContract, InternalVmType.AVM);
cache.flushTo(cache.getParent(), true);
// Check assumptions about contract state.
AccountState contractState = (AccountState) blockchain.getRepository().startTracking().getAccountState(internalContract);
assertThat(contractState.getBalance()).isEqualTo(BigInteger.ZERO);
assertThat(contractState.getNonce()).isEqualTo(BigInteger.ZERO);
assertThat(contractState.getStateRoot()).isNotEqualTo(EMPTY_TRIE_HASH);
assertThat(contractState.getCodeHash()).isEqualTo(EMPTY_DATA_HASH);
byte[] oldRoot = contractState.getStateRoot();
// Next, process the deploy transaction with fork040 enabled.
AionTxExecSummary result = executeTransaction(deployInternal, true);
assertThat(result.isFailed()).isFalse();
assertThat(result.isRejected()).isFalse();
assertThat(result.getReceipt().getError()).isEmpty();
assertThat(result.getNrgUsed()).isLessThan(BigInteger.valueOf(deployInternal.getEnergyLimit()));
assertThat(result.getLogs()).isEmpty();
InternalTransaction itx = result.getInternalTransactions().get(0);
assertThat(itx.isCreate).isTrue();
assertThat(TxUtil.calculateContractAddress(itx)).isEqualTo(internalContract);
assertThat(itx.isRejected).isFalse();
assertThat(itx.energyLimit).isEqualTo(internalLimit);
assertThat(result.getNrgUsed()).isLessThan(BigInteger.valueOf(itx.energyLimit));
contractState = (AccountState) blockchain.getRepository().startTracking().getAccountState(internalContract);
assertThat(contractState.getBalance()).isEqualTo(BigInteger.ZERO);
assertThat(contractState.getNonce()).isEqualTo(BigInteger.ZERO);
assertThat(contractState.getStateRoot()).isNotEqualTo(EMPTY_TRIE_HASH);
assertThat(contractState.getStateRoot()).isNotEqualTo(oldRoot);
assertThat(contractState.getCodeHash()).isNotEqualTo(EMPTY_DATA_HASH);
}
Aggregations