Search in sources :

Example 26 with StandaloneBlockchain

use of org.aion.zero.impl.blockchain.StandaloneBlockchain in project aion by aionnetwork.

the class TaskImportBlocksTest method testFilterBatch_woPruningRestrictions.

@Test
public void testFilterBatch_woPruningRestrictions() {
    StandaloneBlockchain.Bundle bundle = builder.withValidatorConfiguration("simple").withDefaultAccounts(accounts).build();
    StandaloneBlockchain chain = bundle.bc;
    // populate chain at random
    generateRandomChain(chain, 3, 1, accounts, 10);
    // populate initial input lists
    List<Block> batch = new ArrayList<>();
    Map<ByteArrayWrapper, Object> imported = new HashMap<>();
    Block current = chain.getBestBlock();
    while (current.getNumber() > 0) {
        batch.add(current);
        imported.put(ByteArrayWrapper.wrap(current.getHash()), true);
        current = chain.getBlockByHash(current.getParentHash());
    }
    batch.add(current);
    imported.put(ByteArrayWrapper.wrap(current.getHash()), true);
    // will filter out all blocks
    assertThat(filterBatch(batch, chain, imported)).isEmpty();
    // will filter out none of the blocks
    assertThat(filterBatch(batch, chain, new HashMap<>())).isEqualTo(batch);
}
Also used : ByteArrayWrapper(org.aion.util.types.ByteArrayWrapper) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) Block(org.aion.zero.impl.types.Block) BlockchainTestUtils.generateNewBlock(org.aion.zero.impl.blockchain.BlockchainTestUtils.generateNewBlock) BlockchainTestUtils.generateNextBlock(org.aion.zero.impl.blockchain.BlockchainTestUtils.generateNextBlock) StandaloneBlockchain(org.aion.zero.impl.blockchain.StandaloneBlockchain) Test(org.junit.Test)

Example 27 with StandaloneBlockchain

use of org.aion.zero.impl.blockchain.StandaloneBlockchain in project aion by aionnetwork.

the class TaskImportBlocksTest method testFilterBatch_wPruningRestrictions.

@Test
public void testFilterBatch_wPruningRestrictions() {
    int current_count = 5, height = 10;
    StandaloneBlockchain.Bundle bundle = builder.withValidatorConfiguration("simple").withDefaultAccounts(accounts).withRepoConfig(new RepositoryConfig() {

        @Override
        public String getDbPath() {
            return "";
        }

        @Override
        public PruneConfig getPruneConfig() {
            // top pruning without archiving
            return new PruneConfig() {

                @Override
                public boolean isEnabled() {
                    return true;
                }

                @Override
                public boolean isArchived() {
                    return false;
                }

                @Override
                public int getCurrentCount() {
                    return current_count;
                }

                @Override
                public int getArchiveRate() {
                    return 0;
                }
            };
        }

        @Override
        public Properties getDatabaseConfig(String db_name) {
            Properties props = new Properties();
            props.setProperty(DatabaseFactory.Props.DB_TYPE, DBVendor.MOCKDB.toValue());
            return props;
        }
    }).build();
    StandaloneBlockchain chain = bundle.bc;
    // populate chain at random
    generateRandomChain(chain, height, 1, accounts, 10);
    // populate initial input lists
    List<Block> allBlocks = new ArrayList<>();
    Map<ByteArrayWrapper, Object> allHashes = new HashMap<>();
    List<Block> unrestrictedBlocks = new ArrayList<>();
    Map<ByteArrayWrapper, Object> unrestrictedHashes = new HashMap<>();
    for (long i = 0; i <= height; i++) {
        Block current = chain.getBlockByNumber(i);
        allBlocks.add(current);
        allHashes.put(ByteArrayWrapper.wrap(current.getHash()), true);
        if (i >= height - current_count + 1) {
            unrestrictedBlocks.add(current);
            unrestrictedHashes.put(ByteArrayWrapper.wrap(current.getHash()), true);
        }
    }
    // will filter out all blocks
    assertThat(filterBatch(allBlocks, chain, allHashes)).isEmpty();
    // will filter out all blocks
    assertThat(filterBatch(allBlocks, chain, unrestrictedHashes)).isEmpty();
    // will filter out the prune restricted blocks
    assertThat(filterBatch(allBlocks, chain, new HashMap<>())).isEqualTo(unrestrictedBlocks);
}
Also used : RepositoryConfig(org.aion.zero.impl.db.RepositoryConfig) PruneConfig(org.aion.zero.impl.config.PruneConfig) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) StandaloneBlockchain(org.aion.zero.impl.blockchain.StandaloneBlockchain) Properties(java.util.Properties) ByteArrayWrapper(org.aion.util.types.ByteArrayWrapper) Block(org.aion.zero.impl.types.Block) BlockchainTestUtils.generateNewBlock(org.aion.zero.impl.blockchain.BlockchainTestUtils.generateNewBlock) BlockchainTestUtils.generateNextBlock(org.aion.zero.impl.blockchain.BlockchainTestUtils.generateNextBlock) Test(org.junit.Test)

Example 28 with StandaloneBlockchain

use of org.aion.zero.impl.blockchain.StandaloneBlockchain in project aion by aionnetwork.

the class TxRecptLgTest method TestTxRecptLg.

@Test
public void TestTxRecptLg() throws InterruptedException, IOException {
    StandaloneBlockchain.Bundle bundle = new StandaloneBlockchain.Builder().withValidatorConfiguration("simple").withDefaultAccounts().build();
    StandaloneBlockchain bc = bundle.bc;
    ECKey deployerAccount = bundle.privateKeys.get(0);
    // ======================
    // DEPLOY contract A & B
    // ======================
    Compiler.Result r = Compiler.getInstance().compile(readContract("contract/contract.sol"), Compiler.Options.ABI, Compiler.Options.BIN);
    CompilationResult cr = CompilationResult.parse(r.output);
    String contractA = cr.contracts.get("A").bin;
    String contractB = cr.contracts.get("B").bin;
    BigInteger nonce = BigInteger.ZERO;
    AionTransaction tx1 = AionTransaction.create(deployerAccount, nonce.toByteArray(), null, new byte[0], ByteUtil.hexStringToBytes(contractA), 1_000_000L, energyPrice, 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, energyPrice, TransactionTypes.DEFAULT, null);
    BlockContext context = bc.createNewMiningBlockContext(bc.getBestBlock(), List.of(tx1, tx2), false);
    ImportResult result = bc.tryToConnect(context.block);
    assertEquals(result, ImportResult.IMPORTED_BEST);
    AionAddress addressA = TxUtil.calculateContractAddress(tx1);
    System.out.println("contract A address = " + addressA);
    AionAddress addressB = TxUtil.calculateContractAddress(tx2);
    System.out.println("contract B address = " + addressB);
    Thread.sleep(1000);
    // ======================
    // CALL function A.AA
    // ======================
    nonce = nonce.add(BigInteger.ONE);
    byte[] functionAA = new byte[4];
    System.arraycopy(HashUtil.keccak256("AA(address)".getBytes()), 0, functionAA, 0, 4);
    AionTransaction tx3 = AionTransaction.create(deployerAccount, nonce.toByteArray(), addressA, new byte[0], ByteUtil.merge(functionAA, addressB.toByteArray()), 1_000_000L, energyPrice, TransactionTypes.DEFAULT, null);
    context = bc.createNewMiningBlockContext(bc.getBestBlock(), List.of(tx3), false);
    result = bc.tryToConnect(context.block);
    assertEquals(result, ImportResult.IMPORTED_BEST);
    AionTxInfo info = bc.getTransactionInfo(tx3.getTransactionHash());
    AionTxReceipt receipt = info.getReceipt();
    System.out.println(receipt);
    assertEquals(4, receipt.getLogInfoList().size());
    // ======================
    // Test
    // ======================
    TxRecptLg[] logs = new TxRecptLg[receipt.getLogInfoList().size()];
    for (int i = 0; i < logs.length; i++) {
        Log logInfo = receipt.getLogInfoList().get(i);
        logs[i] = new TxRecptLg(logInfo, context.block, info.getIndex(), receipt.getTransaction(), i, true);
    }
    String ctAddrA = "0x" + addressA.toString();
    String ctAddrB = "0x" + addressB.toString();
    // AE
    assertEquals(ctAddrA, logs[0].address);
    // AEA
    assertEquals(ctAddrA, logs[1].address);
    // b.BB
    assertEquals(ctAddrB, logs[2].address);
    // AEB
    assertEquals(ctAddrA, logs[3].address);
}
Also used : Compiler(org.aion.solidity.Compiler) ImportResult(org.aion.zero.impl.core.ImportResult) AionAddress(org.aion.types.AionAddress) AionTxInfo(org.aion.zero.impl.types.AionTxInfo) Log(org.aion.types.Log) BlockContext(org.aion.zero.impl.types.BlockContext) StandaloneBlockchain(org.aion.zero.impl.blockchain.StandaloneBlockchain) ECKey(org.aion.crypto.ECKey) AionTransaction(org.aion.base.AionTransaction) TxRecptLg(org.aion.api.server.types.TxRecptLg) BigInteger(java.math.BigInteger) CompilationResult(org.aion.solidity.CompilationResult) AionTxReceipt(org.aion.base.AionTxReceipt) Test(org.junit.Test)

Aggregations

StandaloneBlockchain (org.aion.zero.impl.blockchain.StandaloneBlockchain)28 Test (org.junit.Test)28 BigInteger (java.math.BigInteger)23 ImportResult (org.aion.zero.impl.core.ImportResult)22 Block (org.aion.zero.impl.types.Block)19 AionTxReceipt (org.aion.base.AionTxReceipt)15 AionBlockSummary (org.aion.zero.impl.types.AionBlockSummary)15 AionAddress (org.aion.types.AionAddress)14 Builder (org.aion.zero.impl.blockchain.StandaloneBlockchain.Builder)14 AionTransaction (org.aion.base.AionTransaction)12 ECKey (org.aion.crypto.ECKey)11 BlockContext (org.aion.zero.impl.types.BlockContext)8 ArrayList (java.util.ArrayList)5 MiningBlock (org.aion.zero.impl.types.MiningBlock)4 AionTxExecSummary (org.aion.base.AionTxExecSummary)3 InternalTransaction (org.aion.types.InternalTransaction)3 BlockchainTestUtils.generateNewBlock (org.aion.zero.impl.blockchain.BlockchainTestUtils.generateNewBlock)3 BlockchainTestUtils.generateNextBlock (org.aion.zero.impl.blockchain.BlockchainTestUtils.generateNextBlock)3 AionTxInfo (org.aion.zero.impl.types.AionTxInfo)3 HashMap (java.util.HashMap)2