Search in sources :

Example 51 with AionBlock

use of org.aion.zero.impl.types.AionBlock in project aion by aionnetwork.

the class BlockchainIntegrationTest method testSimpleFailedTransactionInsufficientBalance.

@Test
public void testSimpleFailedTransactionInsufficientBalance() {
    // generate a recipient
    final Address receiverAddress = Address.wrap(ByteUtil.hexStringToBytes("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)
    AionTransaction tx = new AionTransaction(BigInteger.valueOf(1).toByteArray(), receiverAddress, BigInteger.valueOf(100).toByteArray(), ByteUtil.EMPTY_BYTE_ARRAY, 1L, 1L);
    tx.sign(bundle.privateKeys.get(0));
    AionBlock block = bc.createNewBlock(bc.getBestBlock(), Collections.singletonList(tx), true);
    assertThat(block.getTransactionsList()).isEmpty();
    assertThat(block.getTxTrieRoot()).isEqualTo(HashUtil.EMPTY_TRIE_HASH);
    ImportResult connection = bc.tryToConnect(block);
    assertThat(connection).isEqualTo(ImportResult.IMPORTED_BEST);
}
Also used : ImportResult(org.aion.mcf.core.ImportResult) Address(org.aion.base.type.Address) AionTransaction(org.aion.zero.types.AionTransaction) AionBlock(org.aion.zero.impl.types.AionBlock) Test(org.junit.Test)

Example 52 with AionBlock

use of org.aion.zero.impl.types.AionBlock in project aion by aionnetwork.

the class BlockchainIntegrationTest method testCreateNewEmptyBlock.

@Test
public void testCreateNewEmptyBlock() {
    StandaloneBlockchain.Bundle bundle = (new StandaloneBlockchain.Builder()).withDefaultAccounts().build();
    StandaloneBlockchain bc = bundle.bc;
    AionBlock block = bc.createNewBlock(bc.getBestBlock(), Collections.EMPTY_LIST, true);
    assertThat(block.getParentHash()).isEqualTo(bc.getGenesis().getHash());
}
Also used : AionBlock(org.aion.zero.impl.types.AionBlock) Test(org.junit.Test)

Example 53 with AionBlock

use of org.aion.zero.impl.types.AionBlock in project aion by aionnetwork.

the class BlockPropagationTest method testIgnoreSameBlock.

@Test
public void testIgnoreSameBlock() {
    List<ECKey> accounts = generateDefaultAccounts();
    StandaloneBlockchain.Bundle bundle = new StandaloneBlockchain.Builder().withValidatorConfiguration("simple").withDefaultAccounts(accounts).build();
    AionBlock block = bundle.bc.createNewBlock(bundle.bc.getGenesis(), Collections.EMPTY_LIST, true);
    assertThat(block.getNumber()).isEqualTo(1);
    byte[] sender = HashUtil.h256("node1".getBytes());
    byte[] receiver = HashUtil.h256("receiver".getBytes());
    NodeMock senderMock = new NodeMock(sender, 1);
    NodeMock receiverMock = new NodeMock(receiver, 0);
    Map<Integer, INode> node = new HashMap<>();
    node.put(1, senderMock);
    node.put(2, receiverMock);
    AtomicInteger times = new AtomicInteger();
    P2pMock p2pMock = new P2pMock(node) {

        @Override
        public void send(int _nodeId, Msg _msg) {
            if (_nodeId != receiverMock.getIdHash())
                throw new RuntimeException("should only send to receiver");
            times.getAndIncrement();
        }
    };
    StandaloneBlockchain.Bundle anotherBundle = new StandaloneBlockchain.Builder().withValidatorConfiguration("simple").withDefaultAccounts(accounts).build();
    assertThat(bundle.bc.genesis.getHash()).isEqualTo(anotherBundle.bc.genesis.getHash());
    BlockPropagationHandler handler = new BlockPropagationHandler(1024, // NOTE: not the same blockchain that generated the block
    anotherBundle.bc, p2pMock, anotherBundle.bc.getBlockHeaderValidator());
    // block is processed
    assertThat(handler.processIncomingBlock(senderMock.getIdHash(), block)).isEqualTo(BlockPropagationHandler.PropStatus.PROP_CONNECTED);
    assertThat(handler.processIncomingBlock(senderMock.getIdHash(), block)).isEqualTo(BlockPropagationHandler.PropStatus.DROPPED);
    assertThat(times.get()).isEqualTo(1);
}
Also used : BlockPropagationHandler(org.aion.zero.impl.sync.handler.BlockPropagationHandler) ECKey(org.aion.crypto.ECKey) StandaloneBlockchain(org.aion.zero.impl.StandaloneBlockchain) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) BigInteger(java.math.BigInteger) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AionBlock(org.aion.zero.impl.types.AionBlock) Test(org.junit.Test)

Example 54 with AionBlock

use of org.aion.zero.impl.types.AionBlock in project aion by aionnetwork.

the class ApiAion method getTransactionCount.

public long getTransactionCount(Address addr, long blkNr) {
    AionBlock pBlk = this.getBlock(blkNr);
    if (pBlk == null) {
        LOG.error("ApiAion.getTransactionByBlockNumberAndIndex - can't find the block by the block number");
        return -1;
    }
    long cnt = 0;
    List<AionTransaction> txList = pBlk.getTransactionsList();
    for (AionTransaction tx : txList) {
        if (addr.equals(tx.getFrom())) {
            cnt++;
        }
    }
    return cnt;
}
Also used : AionTransaction(org.aion.zero.types.AionTransaction) AionBlock(org.aion.zero.impl.types.AionBlock)

Example 55 with AionBlock

use of org.aion.zero.impl.types.AionBlock in project aion by aionnetwork.

the class ApiAion method getBlockTemplate.

public AionBlock getBlockTemplate() {
    // TODO: Change to follow onBlockTemplate event mode defined in internal
    // miner
    // TODO: Track multiple block templates
    AionBlock bestPendingState = ((AionPendingStateImpl) ac.getAionHub().getPendingState()).getBestBlock();
    AionPendingStateImpl.TransactionSortedSet ret = new AionPendingStateImpl.TransactionSortedSet();
    ret.addAll(ac.getAionHub().getPendingState().getPendingTransactions());
    return ac.getAionHub().getBlockchain().createNewBlock(bestPendingState, new ArrayList<>(ret), false);
}
Also used : AionPendingStateImpl(org.aion.zero.impl.blockchain.AionPendingStateImpl) AionBlock(org.aion.zero.impl.types.AionBlock)

Aggregations

AionBlock (org.aion.zero.impl.types.AionBlock)64 Test (org.junit.Test)20 AionTransaction (org.aion.zero.types.AionTransaction)16 BigInteger (java.math.BigInteger)15 ImportResult (org.aion.mcf.core.ImportResult)15 ArrayList (java.util.ArrayList)8 Address (org.aion.base.type.Address)6 ECKey (org.aion.crypto.ECKey)6 AionTxInfo (org.aion.zero.impl.types.AionTxInfo)5 IAionBlock (org.aion.zero.types.IAionBlock)5 JSONObject (org.json.JSONObject)5 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)4 ByteArrayWrapper (org.aion.base.util.ByteArrayWrapper)4 StandaloneBlockchain (org.aion.zero.impl.StandaloneBlockchain)4 BlockPropagationHandler (org.aion.zero.impl.sync.handler.BlockPropagationHandler)4 A0BlockHeader (org.aion.zero.types.A0BlockHeader)4 Map (java.util.Map)3 TxRecpt (org.aion.api.server.types.TxRecpt)3 IByteArrayKeyValueDatabase (org.aion.base.db.IByteArrayKeyValueDatabase)3 LRUMap (org.apache.commons.collections4.map.LRUMap)3