use of org.aion.zero.impl.types.BlockContext in project aion by aionnetwork.
the class InternalTransactionTest method testLogs.
@Test
public void testLogs() throws InterruptedException {
String contractA = "0x605060405234156100105760006000fd5b610015565b61013c806100246000396000f30060506040526000356c01000000000000000000000000900463ffffffff1680632d7df21a146100335761002d565b60006000fd5b341561003f5760006000fd5b61006660048080806010013590359091602001909192908035906010019091905050610068565b005b7fc1599bd9a91e57420b9b93745d7475dc054736a3f2becd4f08b450b7012e125760405160405180910390a1828282600060405180806f662829000000000000000000000000008152601001506003019050604051809103902090506c01000000000000000000000000900491906040518363ffffffff166c01000000000000000000000000028152600401600060405180830381858a8a89f195505050505050505b5050505600a165627a7a723058205e51c42347e4353247e8419ef6cda02250d358868e2cb3782d0d5d74065f2ef70029";
String contractB = "0x605060405234156100105760006000fd5b610015565b60cb806100236000396000f30060506040526000356c01000000000000000000000000900463ffffffff16806326121ff014603157602b565b60006000fd5b3415603c5760006000fd5b60426044565b005b600060007f45b3fe4256d6d198dc4c34457a04e8c048ce54df933a93061f1a0e386b52f7a260405160405180910390a160009150600090505b6103e8811015609a57808201915081505b8080600101915050607d565b5b50505600a165627a7a72305820b2bf8aef36001079d347d250e50b098ad52629336644a841d19db288f30667470029";
StandaloneBlockchain.Bundle bundle = (new StandaloneBlockchain.Builder()).withValidatorConfiguration("simple").withDefaultAccounts().build();
StandaloneBlockchain bc = bundle.bc;
ECKey deployerAccount = bundle.privateKeys.get(0);
// ======================
// DEPLOY
// ======================
BigInteger nonce = BigInteger.ZERO;
AionTransaction tx1 = AionTransaction.create(deployerAccount, nonce.toByteArray(), null, new byte[0], ByteUtil.hexStringToBytes(contractA), 1_000_000L, minEnergyPrice, TransactionTypes.DEFAULT, null);
nonce = nonce.add(BigInteger.ONE);
AionTransaction tx2 = AionTransaction.create(deployerAccount, nonce.toByteArray(), null, new byte[0], ByteUtil.hexStringToBytes(contractB), 1_000_000L, minEnergyPrice, TransactionTypes.DEFAULT, null);
BlockContext context = bc.createNewMiningBlockContext(bc.getBestBlock(), List.of(tx1, tx2), false);
ImportResult result = bc.tryToConnect(context.block);
assertThat(result).isEqualTo(ImportResult.IMPORTED_BEST);
AionAddress addressA = TxUtil.calculateContractAddress(tx1);
System.out.println("contract A = " + addressA);
AionAddress addressB = TxUtil.calculateContractAddress(tx2);
System.out.println("contract B = " + addressB);
Thread.sleep(1000);
// ======================
// CALL B
// ======================
nonce = nonce.add(BigInteger.ONE);
AionTransaction tx3 = AionTransaction.create(deployerAccount, nonce.toByteArray(), addressB, new byte[0], ByteUtil.hexStringToBytes("0x26121ff0"), 1_000_000L, minEnergyPrice, TransactionTypes.DEFAULT, null);
context = bc.createNewMiningBlockContext(bc.getBestBlock(), List.of(tx3), false);
result = bc.tryToConnect(context.block);
assertThat(result).isEqualTo(ImportResult.IMPORTED_BEST);
AionTxInfo info = bc.getTransactionInfo(tx3.getTransactionHash());
System.out.println(info.getReceipt());
assertEquals(1, info.getReceipt().getLogInfoList().size());
Thread.sleep(1000);
// ======================
// CALL A (calls B, 80k)
// ======================
nonce = nonce.add(BigInteger.ONE);
AionTransaction tx4 = AionTransaction.create(deployerAccount, nonce.toByteArray(), addressA, new byte[0], ByteUtil.merge(ByteUtil.hexStringToBytes("0x2d7df21a"), addressB.toByteArray(), new DataWord(80_000).getData()), 1_000_000L, minEnergyPrice, TransactionTypes.DEFAULT, null);
context = bc.createNewMiningBlockContext(bc.getBestBlock(), List.of(tx4), false);
result = bc.tryToConnect(context.block);
assertThat(result).isEqualTo(ImportResult.IMPORTED_BEST);
info = bc.getTransactionInfo(tx4.getTransactionHash());
System.out.println(info.getReceipt());
assertEquals(2, info.getReceipt().getLogInfoList().size());
Thread.sleep(1000);
// ======================
// CALL A (calls B, 20k)
// ======================
nonce = nonce.add(BigInteger.ONE);
AionTransaction tx6 = AionTransaction.create(deployerAccount, nonce.toByteArray(), addressA, new byte[0], ByteUtil.merge(ByteUtil.hexStringToBytes("0x2d7df21a"), addressB.toByteArray(), new DataWord(20_000).getData()), 1_000_000L, minEnergyPrice, TransactionTypes.DEFAULT, null);
context = bc.createNewMiningBlockContext(bc.getBestBlock(), List.of(tx6), false);
result = bc.tryToConnect(context.block);
assertThat(result).isEqualTo(ImportResult.IMPORTED_BEST);
info = bc.getTransactionInfo(tx6.getTransactionHash());
System.out.println(info.getReceipt());
assertEquals(1, info.getReceipt().getLogInfoList().size());
Thread.sleep(1000);
}
use of org.aion.zero.impl.types.BlockContext in project aion by aionnetwork.
the class StandaloneBlockchain method createBlockAndBlockTemplate.
/**
* create a testing mining block and the block template
*/
public MiningBlock createBlockAndBlockTemplate(Block parent, List<AionTransaction> txs, boolean waitUntilBlockTime, long currTimeSeconds) {
boolean unityForkEnabled = forkUtility.isUnityForkActive(parent.getNumber() + 1);
boolean signatureSwapForkEnabled = forkUtility.isSignatureSwapForkActive(parent.getNumber() + 1);
for (AionTransaction tx : txs) {
if (TXValidator.validateTx(tx, unityForkEnabled, signatureSwapForkEnabled).isFail()) {
throw new InvalidParameterException("invalid transaction input! " + tx.toString());
}
}
BlockContext context = createNewMiningBlockInternal(parent, txs, waitUntilBlockTime, currTimeSeconds);
if (context != null) {
MiningBlock block = context.block;
boolean newblock = miningBlockTemplate.put(ByteArrayWrapper.wrap(block.getHash()), block) == null;
return newblock ? block : null;
}
return null;
}
use of org.aion.zero.impl.types.BlockContext in project aion by aionnetwork.
the class BlockchainImplementationTest method testIsPruneRestricted_wSpreadState.
/**
* In SPREAD mode the top K blocks and the blocks that are multiples of the archive rate have a
* stored state. There are no restrictions due to pruning.
*/
@Test
public void testIsPruneRestricted_wSpreadState() {
// number of blocks stored by the blockchain
int stored = 150;
// the maximum height considered by this test
int height = 1200;
// the interval at which blocks are indexed
int index = 1000;
StandaloneBlockchain.Bundle bundle = new StandaloneBlockchain.Builder().withValidatorConfiguration("simple").withRepoConfig(new MockRepositoryConfig(new CfgPrune(stored, index))).withDefaultAccounts(accounts).build();
StandaloneBlockchain chain = bundle.bc;
AionRepositoryImpl repo = chain.getRepository();
BlockContext context;
List<AionTransaction> txs;
// creating (height) blocks
long time = System.currentTimeMillis();
for (int i = 0; i < height; i++) {
txs = BlockchainTestUtils.generateTransactions(MAX_TX_PER_BLOCK, accounts, repo);
context = chain.createNewMiningBlockInternal(chain.getBestBlock(), txs, true, time / 100000L);
assertThat(chain.tryToConnect(context.block)).isEqualTo(ImportResult.IMPORTED_BEST);
}
// testing restriction for unrestricted blocks
for (int i = height; i >= 0; i--) {
assertThat(chain.isPruneRestricted(i)).isFalse();
if (i % index == 0 || i >= height - stored) {
// ensure the state exists
assertThat(repo.isValidRoot(chain.getBlockByNumber(i).getStateRoot())).isTrue();
} else {
// ensure the state is missing
assertThat(repo.isValidRoot(chain.getBlockByNumber(i).getStateRoot())).isFalse();
}
}
}
use of org.aion.zero.impl.types.BlockContext 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.types.BlockContext in project aion by aionnetwork.
the class BlockchainDataRecoveryTest method testRecoverIndex_wRepositorySnapshot.
@Test
public void testRecoverIndex_wRepositorySnapshot() {
StandaloneBlockchain.Builder builder = new StandaloneBlockchain.Builder();
StandaloneBlockchain.Bundle bundle = builder.withValidatorConfiguration("simple").withDefaultAccounts(accounts).build();
StandaloneBlockchain chain = bundle.bc;
AionRepositoryImpl repo = chain.getRepository();
BlockContext context;
List<AionTransaction> txs;
// all blocks will be incorrect
long time = System.currentTimeMillis();
for (int i = 0; i < NUMBER_OF_BLOCKS; i++) {
txs = BlockchainTestUtils.generateTransactions(MAX_TX_PER_BLOCK, accounts, repo);
context = chain.createNewMiningBlockInternal(chain.getBestBlock(), txs, true, time / 10000L);
assertThat(chain.tryToConnect(context.block)).isEqualTo(ImportResult.IMPORTED_BEST);
}
repo.flush();
assertThat(chain.recoverIndexEntry((AionRepositoryImpl) repo.getSnapshotTo(repo.getRoot()), chain.getBestBlock())).isFalse();
}
Aggregations