Search in sources :

Example 41 with ByteArrayWrapper

use of org.aion.util.types.ByteArrayWrapper in project aion by aionnetwork.

the class ByteArrayWrapperTest method testWrap.

/**
 * 1. Wrap the input data 2. Assert to see if equal
 */
@Test
@Parameters(method = "hexValues")
public void testWrap(String inputString) {
    ByteArrayWrapper tempArray;
    byte[] inputByte = TestUtil.hexStringToByteArray(inputString);
    try {
        tempArray = ByteArrayWrapper.wrap(inputByte);
        assertEquals(tempArray.toString(), inputString.toLowerCase());
        assertArrayEquals(tempArray.toBytes(), tempArray.toBytes());
        System.out.println("Valid " + tempArray);
    } catch (NullPointerException e) {
        System.out.println("Invalid");
    }
}
Also used : ByteArrayWrapper(org.aion.util.types.ByteArrayWrapper) Parameters(junitparams.Parameters) Test(org.junit.Test)

Example 42 with ByteArrayWrapper

use of org.aion.util.types.ByteArrayWrapper in project aion by aionnetwork.

the class AionBlockchainImpl method getTransactionInfoByAlias.

private Map<ByteArrayWrapper, AionTxInfo> getTransactionInfoByAlias(byte[] innerHash) {
    Set<ByteArrayWrapper> metaTxHashes = transactionStore.getAliases(innerHash);
    // No aliases found
    if (metaTxHashes == null)
        return null;
    Map<ByteArrayWrapper, AionTxInfo> infoList = new HashMap<>();
    for (ByteArrayWrapper metaTxHash : metaTxHashes) {
        Map<ByteArrayWrapper, AionTxInfo> metaTxInfos = transactionStore.getTxInfo(metaTxHash.toBytes());
        if (metaTxInfos != null) {
            infoList.putAll(metaTxInfos);
        }
    }
    // Had metaTx hash, but was not found in mainchain
    return infoList;
}
Also used : ByteArrayWrapper(org.aion.util.types.ByteArrayWrapper) AionTxInfo(org.aion.zero.impl.types.AionTxInfo) HashMap(java.util.HashMap)

Example 43 with ByteArrayWrapper

use of org.aion.util.types.ByteArrayWrapper in project aion by aionnetwork.

the class AionBlockchainImpl method tryToConnect.

/**
 * Imports a batch of blocks.
 *
 * @param blockRange the block range to be imported
 * @param peerDisplayId the display identifier for the peer who provided the batch
 * @return a {@link Triple} containing:
 * <ol>
 *     <li>the best block height after the imports,</li>
 *     <li>the set of imported hashes,</li>
 *     <li>the import result for the last imported block</li>
 * </ol>
 */
public Triple<Long, Set<ByteArrayWrapper>, ImportResult> tryToConnect(final List<Block> blockRange, String peerDisplayId) {
    lock.lock();
    try {
        ImportResult importResult = null;
        Set<ByteArrayWrapper> imported = new HashSet<>();
        for (Block block : blockRange) {
            Pair<ImportResult, Long> result = tryToConnectWithTimedExecution(new BlockWrapper(block));
            importResult = result.getLeft();
            long importTime = result.getRight();
            // printing additional information when debug is enabled
            SYNC_LOG.debug("<import-status: node = {}, hash = {}, number = {}, txs = {}, block time = {}, result = {}, time elapsed = {} ns, block td = {}, chain td = {}>", peerDisplayId, block.getShortHash(), block.getNumber(), block.getTransactionsList().size(), block.getTimestamp(), importResult, importTime, block.getTotalDifficulty(), getTotalDifficulty());
            if (checkKernelShutdownForCLI()) {
                break;
            } else if (!importResult.isStored()) {
                // stop at invalid blocks
                return Triple.of(bestBlock.getNumber(), imported, importResult);
            } else {
                imported.add(block.getHashWrapper());
            }
        }
        return Triple.of(bestBlock.getNumber(), imported, importResult);
    } finally {
        lock.unlock();
        checkKernelExit();
    }
}
Also used : ImportResult(org.aion.zero.impl.core.ImportResult) FastImportResult(org.aion.zero.impl.core.FastImportResult) ByteArrayWrapper(org.aion.util.types.ByteArrayWrapper) AtomicLong(java.util.concurrent.atomic.AtomicLong) Block(org.aion.zero.impl.types.Block) BlockDetailsValidator.isValidBlock(org.aion.zero.impl.valid.BlockDetailsValidator.isValidBlock) GenesisStakingBlock(org.aion.zero.impl.types.GenesisStakingBlock) RetValidPreBlock(org.aion.zero.impl.types.RetValidPreBlock) MiningBlock(org.aion.zero.impl.types.MiningBlock) EventBlock(org.aion.evtmgr.impl.evt.EventBlock) StakingBlock(org.aion.zero.impl.types.StakingBlock) HashSet(java.util.HashSet)

Example 44 with ByteArrayWrapper

use of org.aion.util.types.ByteArrayWrapper in project aion by aionnetwork.

the class BlockchainImplementationTest method testGetListOfHeadersEndWith_wBestBlock.

@Test
public void testGetListOfHeadersEndWith_wBestBlock() {
    StandaloneBlockchain.Builder builder = new StandaloneBlockchain.Builder();
    StandaloneBlockchain.Bundle bundle = builder.withValidatorConfiguration("simple").withDefaultAccounts(accounts).build();
    StandaloneBlockchain chain = bundle.bc;
    // populate chain at random
    generateRandomChain(chain, 12, 2, accounts, MAX_TX_PER_BLOCK);
    Block best = chain.getBestBlock();
    assertThat(best.getNumber()).isGreaterThan(0L);
    byte[] bestHash = best.getHash();
    // expected result with qty >= chain height
    List<ByteArrayWrapper> expectedAll = new ArrayList<>();
    Block block = best;
    while (block.getNumber() > 0) {
        expectedAll.add(ByteArrayWrapper.wrap(block.getHash()));
        block = chain.getBlockByHash(block.getParentHash());
    }
    int qty;
    // get one block
    qty = 1;
    assertEndWith_expectedOne(chain, bestHash, qty);
    // get list of chain height from best block
    // chain height
    qty = (int) best.getNumber();
    assertEndWith(chain, bestHash, qty, expectedAll);
    // get list of half from best block
    qty = (int) (best.getNumber() / 2);
    List<ByteArrayWrapper> expectedHalf = new ArrayList<>();
    block = best;
    for (int i = 0; i < qty; i++) {
        expectedHalf.add(ByteArrayWrapper.wrap(block.getHash()));
        block = chain.getBlockByHash(block.getParentHash());
    }
    assertEndWith(chain, bestHash, qty, expectedHalf);
    // get list of 20 from genesis
    // larger than block height
    qty = 24;
    expectedAll.add(ByteArrayWrapper.wrap(chain.getGenesis().getHash()));
    assertEndWith(chain, bestHash, qty, expectedAll);
    // get list of -10 from best block
    // negative value
    qty = -10;
    assertEndWith_expectedOne(chain, bestHash, qty);
    // get list of 0 from best block
    // zero blocks requested
    qty = 0;
    assertEndWith_expectedOne(chain, bestHash, qty);
}
Also used : ByteArrayWrapper(org.aion.util.types.ByteArrayWrapper) ArrayList(java.util.ArrayList) MiningBlock(org.aion.zero.impl.types.MiningBlock) Block(org.aion.zero.impl.types.Block) Test(org.junit.Test)

Example 45 with ByteArrayWrapper

use of org.aion.util.types.ByteArrayWrapper in project aion by aionnetwork.

the class BlockchainImplementationTest method testGetListOfHeadersStartFromBlock_wGenesisBlock.

@Test
public void testGetListOfHeadersStartFromBlock_wGenesisBlock() {
    StandaloneBlockchain.Builder builder = new StandaloneBlockchain.Builder();
    StandaloneBlockchain.Bundle bundle = builder.withValidatorConfiguration("simple").withDefaultAccounts(accounts).build();
    StandaloneBlockchain chain = bundle.bc;
    // populate chain at random
    generateRandomChain(chain, 12, 2, accounts, MAX_TX_PER_BLOCK);
    MiningBlock genesis = chain.getGenesis();
    Block best = chain.getBestBlock();
    assertThat(best.getNumber()).isGreaterThan(0L);
    byte[] genesisHash = genesis.getHash();
    // expected result with qty >= chain height
    List<ByteArrayWrapper> expectedAll = new ArrayList<>();
    Block block = chain.getBlockByHash(best.getParentHash());
    while (block.getNumber() > 0) {
        expectedAll.add(0, ByteArrayWrapper.wrap(block.getHash()));
        block = chain.getBlockByHash(block.getParentHash());
    }
    expectedAll.add(0, ByteArrayWrapper.wrap(genesis.getHash()));
    int qty;
    // get one block
    qty = 1;
    assertStartFromBlock_expectedOne(chain, genesisHash, 0, qty);
    // get list of chain height from genesis block
    // chain height
    qty = (int) best.getNumber();
    assertStartFromBlock(chain, 0, qty, expectedAll);
    // get list of half from genesis block
    qty = (int) (best.getNumber() / 2);
    List<ByteArrayWrapper> expectedHalf = new ArrayList<>();
    block = chain.getBlockByNumber(qty - 1);
    for (int i = 0; i < qty; i++) {
        expectedHalf.add(0, ByteArrayWrapper.wrap(block.getHash()));
        block = chain.getBlockByHash(block.getParentHash());
    }
    assertStartFromBlock(chain, 0, qty, expectedHalf);
    // get list of 20 from genesis
    // larger than block height
    qty = 24;
    expectedAll.add(ByteArrayWrapper.wrap(best.getHash()));
    assertStartFromBlock(chain, 0, qty, expectedAll);
    // get list of -10 from genesis block
    // negative value
    qty = -10;
    assertStartFromBlock_expectedOne(chain, genesisHash, 0, qty);
    // get list of 0 from genesis block
    // zero blocks requested
    qty = 0;
    assertStartFromBlock_expectedOne(chain, genesisHash, 0, qty);
}
Also used : ByteArrayWrapper(org.aion.util.types.ByteArrayWrapper) ArrayList(java.util.ArrayList) MiningBlock(org.aion.zero.impl.types.MiningBlock) Block(org.aion.zero.impl.types.Block) MiningBlock(org.aion.zero.impl.types.MiningBlock) Test(org.junit.Test)

Aggregations

ByteArrayWrapper (org.aion.util.types.ByteArrayWrapper)130 Test (org.junit.Test)51 HashMap (java.util.HashMap)39 ArrayList (java.util.ArrayList)33 AionAddress (org.aion.types.AionAddress)26 Block (org.aion.zero.impl.types.Block)24 Map (java.util.Map)20 BigInteger (java.math.BigInteger)14 MiningBlock (org.aion.zero.impl.types.MiningBlock)14 IOException (java.io.IOException)13 MockDB (org.aion.db.impl.mockdb.MockDB)13 DataWord (org.aion.util.types.DataWord)13 PooledTransaction (org.aion.base.PooledTransaction)11 List (java.util.List)10 AionTransaction (org.aion.base.AionTransaction)10 Properties (java.util.Properties)8 HashSet (java.util.HashSet)5 Optional (java.util.Optional)5 ECKey (org.aion.crypto.ECKey)5 RLPElement (org.aion.rlp.RLPElement)5