Search in sources :

Example 86 with MiningBlock

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

the class AionChainHolder method submitBlock.

@Override
public boolean submitBlock(byte[] nonce, byte[] solution, byte[] headerHash) {
    MiningBlock bestPowBlock = this.chain.getBlockchain().getCachingMiningBlockTemplate(headerHash);
    if (bestPowBlock == null) {
        // cannot seal a block that does not exist
        return false;
    } else {
        bestPowBlock.seal(nonce, solution);
        // AKI-648 reject the block add into the kernel if the timestamp of the block is in the future.
        boolean isValidTimestamp = futureBlockRule.validate(bestPowBlock.getHeader(), new ArrayList<>());
        final boolean sealedSuccessfully = isValidTimestamp && addNewBlock(bestPowBlock);
        if (sealedSuccessfully) {
            logSealedBlock(bestPowBlock);
        } else {
            logFailedSealedBlock(bestPowBlock);
        }
        return sealedSuccessfully;
    }
}
Also used : MiningBlock(org.aion.zero.impl.types.MiningBlock)

Example 87 with MiningBlock

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

the class ConsensusTest method testConsensus.

@Test
public void testConsensus() {
    Bundle bundle = new Builder().withDefaultAccounts(Collections.singletonList(ECKeyFac.inst().fromPrivate(PRIVATE_KEY))).withValidatorConfiguration("simple").build();
    StandaloneBlockchain blockchain = bundle.bc;
    AionTransaction deployTransaction = getDeployTransaction();
    assertEquals(CONTRACT, TxUtil.calculateContractAddress(deployTransaction));
    // Place the transaction in a block alone.
    Block parentBlock = blockchain.getRepository().getBestBlock();
    List<AionTransaction> transactions = Collections.singletonList(deployTransaction);
    // Run the transaction.
    MiningBlock block = blockchain.createNewMiningBlock(parentBlock, transactions, false);
    Pair<ImportResult, AionBlockSummary> results = blockchain.tryToConnectAndFetchSummary(block);
    assertEquals(ImportResult.IMPORTED_BEST, results.getLeft());
    assertEquals(1, results.getRight().getSummaries().size());
    // Extract the results.
    AionBlockSummary blockSummary = results.getRight();
    AionTxReceipt receipt = blockSummary.getSummaries().get(0).getReceipt();
    byte[] stateRoot = blockSummary.getBlock().getStateRoot();
    byte[] blockReceiptsRoot = blockSummary.getBlock().getReceiptsRoot();
    byte[] receiptPostTransactionState = receipt.getPostTxState();
    byte[] receiptTrieEncoded = receipt.getReceiptTrieEncoded();
    parentBlock = blockchain.getRepository().getBestBlock();
    // Place the transactions all in a block.
    transactions = new ArrayList<>();
    // success
    transactions.add(getTransactionThatCallsAddOwner());
    // success
    transactions.add(getTransactionThatCallsIsOwner(OWNER, 2));
    // success
    transactions.add(getTransactionThatCallsIsOwner(OTHER, 3));
    // rejected: invalid nonce
    transactions.add(getTransactionThatCallsIsOwner(OWNER, 3));
    transactions.add(// failed: out of energy
    getTransactionThatCallsIsOwnerAndRunOutOfEnergy());
    transactions.add(getTransactionThatCallsNumOwners());
    // success
    transactions.add(getTransactionThatCallsIsOwner(OWNER, 6));
    transactions.add(// success
    getTransactionThatOnlyTransfersValue(BigInteger.valueOf(1_000_000_000)));
    // Runs the block of transactions.
    block = blockchain.createNewMiningBlock(parentBlock, transactions, false);
    results = blockchain.tryToConnectAndFetchSummary(block);
    assertEquals(ImportResult.IMPORTED_BEST, results.getLeft());
    assertEquals(7, results.getRight().getSummaries().size());
    // Extract results.
    blockSummary = results.getRight();
    AionTxReceipt receipt1 = blockSummary.getSummaries().get(0).getReceipt();
    AionTxReceipt receipt2 = blockSummary.getSummaries().get(1).getReceipt();
    AionTxReceipt receipt3 = blockSummary.getSummaries().get(2).getReceipt();
    AionTxReceipt receipt4 = blockSummary.getSummaries().get(3).getReceipt();
    AionTxReceipt receipt5 = blockSummary.getSummaries().get(4).getReceipt();
    AionTxReceipt receipt6 = blockSummary.getSummaries().get(5).getReceipt();
    AionTxReceipt receipt7 = blockSummary.getSummaries().get(6).getReceipt();
    byte[] stateRoot2 = blockSummary.getBlock().getStateRoot();
    byte[] blockReceiptsRoot2 = blockSummary.getBlock().getReceiptsRoot();
    byte[] receiptPostTransactionState1 = receipt1.getPostTxState();
    byte[] receiptPostTransactionState2 = receipt2.getPostTxState();
    byte[] receiptPostTransactionState3 = receipt3.getPostTxState();
    byte[] receiptPostTransactionState4 = receipt4.getPostTxState();
    byte[] receiptPostTransactionState5 = receipt5.getPostTxState();
    byte[] receiptPostTransactionState6 = receipt6.getPostTxState();
    byte[] receiptPostTransactionState7 = receipt7.getPostTxState();
    byte[] receiptTrieEncoded1 = receipt1.getReceiptTrieEncoded();
    byte[] receiptTrieEncoded2 = receipt2.getReceiptTrieEncoded();
    byte[] receiptTrieEncoded3 = receipt3.getReceiptTrieEncoded();
    byte[] receiptTrieEncoded4 = receipt4.getReceiptTrieEncoded();
    byte[] receiptTrieEncoded5 = receipt5.getReceiptTrieEncoded();
    byte[] receiptTrieEncoded6 = receipt6.getReceiptTrieEncoded();
    byte[] receiptTrieEncoded7 = receipt7.getReceiptTrieEncoded();
    System.out.println("Transaction: " + receipt.getTransaction());
    System.out.println();
    System.out.println("BLOCK RECEIPT ROOT: " + Hex.toHexString(blockReceiptsRoot));
    System.out.println("RECEIPT TRIE ENCODING: " + Hex.toHexString(receiptTrieEncoded));
    System.out.println("RECEIPT POST-TRANSACTION STATE: " + Hex.toHexString(receiptPostTransactionState));
    System.out.println("Receipt: " + receipt);
    System.out.println();
    System.out.println("STATE ROOT: " + Hex.toHexString(stateRoot));
    System.out.println("----------------------------------------------------------------------");
    System.out.println("Transaction (CALL #1): " + receipt1.getTransaction());
    System.out.println("Transaction (CALL #2): " + receipt2.getTransaction());
    System.out.println();
    System.out.println("BLOCK RECEIPT ROOT: " + Hex.toHexString(blockReceiptsRoot2));
    System.out.println("RECEIPT TRIE ENCODING (CALL #1): " + Hex.toHexString(receiptTrieEncoded1));
    System.out.println("RECEIPT POST-TRANSACTION STATE (CALL #1): " + Hex.toHexString(receiptPostTransactionState1));
    System.out.println("Receipt (CALL #1): " + receipt1);
    System.out.println();
    System.out.println("RECEIPT TRIE ENCODING (CALL #2): " + Hex.toHexString(receiptTrieEncoded2));
    System.out.println("RECEIPT POST-TRANSACTION STATE (CALL #2): " + Hex.toHexString(receiptPostTransactionState2));
    System.out.println("Receipt (CALL #2): " + receipt2);
    System.out.println();
    System.out.println("RECEIPT TRIE ENCODING (CALL #3): " + Hex.toHexString(receiptTrieEncoded3));
    System.out.println("RECEIPT POST-TRANSACTION STATE (CALL #3): " + Hex.toHexString(receiptPostTransactionState3));
    System.out.println("Receipt (CALL #3): " + receipt3);
    System.out.println();
    System.out.println("RECEIPT TRIE ENCODING (CALL #4): " + Hex.toHexString(receiptTrieEncoded4));
    System.out.println("RECEIPT POST-TRANSACTION STATE (CALL #4): " + Hex.toHexString(receiptPostTransactionState4));
    System.out.println("Receipt (CALL #4): " + receipt4);
    System.out.println();
    System.out.println("RECEIPT TRIE ENCODING (CALL #5): " + Hex.toHexString(receiptTrieEncoded5));
    System.out.println("RECEIPT POST-TRANSACTION STATE (CALL #5): " + Hex.toHexString(receiptPostTransactionState5));
    System.out.println("Receipt (CALL #5): " + receipt5);
    System.out.println();
    System.out.println("RECEIPT TRIE ENCODING (CALL #6): " + Hex.toHexString(receiptTrieEncoded6));
    System.out.println("RECEIPT POST-TRANSACTION STATE (CALL #6): " + Hex.toHexString(receiptPostTransactionState6));
    System.out.println("Receipt (CALL #6): " + receipt6);
    System.out.println();
    System.out.println("RECEIPT TRIE ENCODING (CALL #7): " + Hex.toHexString(receiptTrieEncoded7));
    System.out.println("RECEIPT POST-TRANSACTION STATE (CALL #7): " + Hex.toHexString(receiptPostTransactionState7));
    System.out.println("Receipt (CALL #7): " + receipt7);
    System.out.println();
    System.out.println("STATE ROOT: " + Hex.toHexString(stateRoot2));
    // Check first block.
    assertEquals(RECEIPT, receipt.toString());
    assertArrayEquals(RECEIPT_TRIE_ENCODING, receiptTrieEncoded);
    assertArrayEquals(RECEIPT_POST_TX_STATE, receiptPostTransactionState);
    assertArrayEquals(BLOCK_RECEIPT_ROOT1, blockReceiptsRoot);
    assertArrayEquals(STATE_ROOT1, stateRoot);
    // Check second block.
    assertEquals(RECEIPT1, receipt1.toString());
    assertEquals(RECEIPT2, receipt2.toString());
    assertEquals(RECEIPT3, receipt3.toString());
    assertEquals(RECEIPT4, receipt4.toString());
    assertEquals(RECEIPT5, receipt5.toString());
    assertEquals(RECEIPT6, receipt6.toString());
    assertEquals(RECEIPT7, receipt7.toString());
    assertArrayEquals(RECEIPT_TRIE_ENCODING1, receiptTrieEncoded1);
    assertArrayEquals(RECEIPT_POST_TX_STATE1, receiptPostTransactionState1);
    assertArrayEquals(RECEIPT_TRIE_ENCODING2, receiptTrieEncoded2);
    assertArrayEquals(RECEIPT_POST_TX_STATE2, receiptPostTransactionState2);
    assertArrayEquals(RECEIPT_TRIE_ENCODING3, receiptTrieEncoded3);
    assertArrayEquals(RECEIPT_POST_TX_STATE3, receiptPostTransactionState3);
    assertArrayEquals(RECEIPT_TRIE_ENCODING4, receiptTrieEncoded4);
    assertArrayEquals(RECEIPT_POST_TX_STATE4, receiptPostTransactionState4);
    assertArrayEquals(RECEIPT_TRIE_ENCODING5, receiptTrieEncoded5);
    assertArrayEquals(RECEIPT_POST_TX_STATE5, receiptPostTransactionState5);
    assertArrayEquals(RECEIPT_TRIE_ENCODING6, receiptTrieEncoded6);
    assertArrayEquals(RECEIPT_POST_TX_STATE6, receiptPostTransactionState6);
    assertArrayEquals(RECEIPT_TRIE_ENCODING7, receiptTrieEncoded7);
    assertArrayEquals(RECEIPT_POST_TX_STATE7, receiptPostTransactionState7);
    assertArrayEquals(BLOCK_RECEIPT_ROOT2, blockReceiptsRoot2);
    assertArrayEquals(STATE_ROOT2, stateRoot2);
}
Also used : ImportResult(org.aion.zero.impl.core.ImportResult) Bundle(org.aion.zero.impl.blockchain.StandaloneBlockchain.Bundle) Builder(org.aion.zero.impl.blockchain.StandaloneBlockchain.Builder) StandaloneBlockchain(org.aion.zero.impl.blockchain.StandaloneBlockchain) AionTransaction(org.aion.base.AionTransaction) MiningBlock(org.aion.zero.impl.types.MiningBlock) AionBlockSummary(org.aion.zero.impl.types.AionBlockSummary) MiningBlock(org.aion.zero.impl.types.MiningBlock) Block(org.aion.zero.impl.types.Block) AionTxReceipt(org.aion.base.AionTxReceipt) Test(org.junit.Test)

Example 88 with MiningBlock

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

the class BalanceTransferConsensusTest method testTransactionTypeBeforeTheFork.

@Test
@Ignore
public void testTransactionTypeBeforeTheFork() {
    // ensure that the fork was not triggered
    TransactionTypeRule.disallowAVMContractTransaction();
    BigInteger amount = BigInteger.TEN.pow(12).add(BigInteger.valueOf(293_865));
    BigInteger initialBalance = getBalance(new AionAddress(SENDER_ADDR));
    assertThat(initialBalance).isEqualTo(SENDER_BALANCE);
    assertThat(this.blockchain.getMinerCoinbase().toByteArray()).isEqualTo(MINER);
    // get contract address from precompiled factory
    AionAddress to = AddressUtils.wrapAddress("a0123456a89a6ffbfdc45782771fba3f5e9da36baa69444f8f95e325430463e7");
    // Make balance transfer transaction to precompiled contract.
    ECKey key = org.aion.crypto.ECKeyFac.inst().fromPrivate(SENDER_KEY);
    AionTransaction transaction = AionTransaction.create(key, BigInteger.ZERO.toByteArray(), to, amount.toByteArray(), new byte[0], 2_000_000, ENERGY_PRICE, // legal type before the fork
    (byte) 11, null);
    // check that the transaction is valid
    assertThat(TransactionTypeValidator.isValid(transaction)).isTrue();
    // Process the transaction.
    Block parentBlock = this.blockchain.getRepository().getBestBlock();
    MiningBlock block = this.blockchain.createNewMiningBlock(parentBlock, Collections.singletonList(transaction), true);
    Pair<ImportResult, AionBlockSummary> results = this.blockchain.tryToConnectAndFetchSummary(block);
    assertThat(results.getLeft()).isEqualTo(ImportResult.IMPORTED_BEST);
    // ensure transaction and block were valid
    AionBlockSummary blockSummary = results.getRight();
    AionTxReceipt receipt = blockSummary.getSummaries().get(0).getReceipt();
    assertThat(receipt.isSuccessful()).isTrue();
    assertThat(receipt.getEnergyUsed()).isEqualTo(21000);
}
Also used : AionAddress(org.aion.types.AionAddress) ImportResult(org.aion.zero.impl.core.ImportResult) AionBlockSummary(org.aion.zero.impl.types.AionBlockSummary) BigInteger(java.math.BigInteger) MiningBlock(org.aion.zero.impl.types.MiningBlock) Block(org.aion.zero.impl.types.Block) ECKey(org.aion.crypto.ECKey) AionTransaction(org.aion.base.AionTransaction) AionTxReceipt(org.aion.base.AionTxReceipt) MiningBlock(org.aion.zero.impl.types.MiningBlock) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 89 with MiningBlock

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

the class PendingBlockStoreTest method testDropPendingQueues_wException.

@Test
public void testDropPendingQueues_wException() {
    Properties props = new Properties();
    props.setProperty(Props.DB_TYPE, DBVendor.MOCKDB.toValue());
    PendingBlockStore pb = null;
    try {
        pb = new PendingBlockStore(props);
    } catch (IOException e) {
        e.printStackTrace();
    }
    assertThat(pb.isOpen()).isTrue();
    // add first queue
    List<Block> blocks = TestResources.consecutiveBlocks(6);
    Block first = blocks.get(0);
    pb.addBlockRange(blocks);
    // add second queue
    MiningBlock altBlock = (MiningBlock) BlockUtil.newBlockFromRlp(first.getEncoded());
    MiningBlockHeader newHeader = MiningBlockHeader.Builder.newInstance().withHeader(altBlock.getHeader()).withExtraData("random".getBytes()).build();
    altBlock.updateHeader(newHeader);
    List<Block> sideChain = new ArrayList<>();
    sideChain.add(altBlock);
    pb.addBlockRange(sideChain);
    // check storage updates
    assertThat(pb.getIndexSize()).isEqualTo(7);
    assertThat(pb.getLevelSize()).isEqualTo(1);
    assertThat(pb.getQueueSize()).isEqualTo(2);
    // closing the pending block store to cause exception
    pb.close();
    // test drop functionality
    Map<ByteArrayWrapper, List<Block>> actual = pb.loadBlockRange(first.getNumber());
    pb.dropPendingQueues(first.getNumber(), actual.keySet(), actual);
}
Also used : MiningBlockHeader(org.aion.zero.impl.types.MiningBlockHeader) ByteArrayWrapper(org.aion.util.types.ByteArrayWrapper) ArrayList(java.util.ArrayList) MiningBlock(org.aion.zero.impl.types.MiningBlock) Block(org.aion.zero.impl.types.Block) ArrayList(java.util.ArrayList) List(java.util.List) IOException(java.io.IOException) Properties(java.util.Properties) MiningBlock(org.aion.zero.impl.types.MiningBlock) Test(org.junit.Test)

Example 90 with MiningBlock

use of org.aion.zero.impl.types.MiningBlock 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();
    MiningBlock block = bundle.bc.createNewMiningBlock(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, String s, 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).withEventManger(this.loadEventMgr()).build();
    assertThat(bundle.bc.genesis.getHash()).isEqualTo(anotherBundle.bc.genesis.getHash());
    SyncStats syncStats = new SyncStats(bundle.bc.getBestBlock().getNumber(), true);
    BlockPropagationHandler handler = new BlockPropagationHandler(1024, // NOTE: not the same blockchain that generated the block
    anotherBundle.bc, syncStats, p2pMock, anotherBundle.bc.getBlockHeaderValidator(), false, (byte) 2, new AionPendingStateImpl(anotherBundle.bc, blockEnergyUpperBound, pendingTransactionTimeout, enablePoolBackup, enableSeedMode, enablePoolDump, new PendingTxCallback(new ArrayList<>()), new NetworkBestBlockCallback(AionImpl.inst()), new TransactionBroadcastCallback(AionImpl.inst()), true));
    // block is processed
    assertThat(handler.processIncomingBlock(senderMock.getIdHash(), "test", block)).isEqualTo(BlockPropagationHandler.PropStatus.PROP_CONNECTED);
    assertThat(handler.processIncomingBlock(senderMock.getIdHash(), "test", block)).isEqualTo(BlockPropagationHandler.PropStatus.DROPPED);
    assertThat(times.get()).isEqualTo(1);
}
Also used : INode(org.aion.p2p.INode) TransactionBroadcastCallback(org.aion.zero.impl.blockchain.AionImpl.TransactionBroadcastCallback) HashMap(java.util.HashMap) ECKey(org.aion.crypto.ECKey) MiningBlock(org.aion.zero.impl.types.MiningBlock) Msg(org.aion.p2p.Msg) AionPendingStateImpl(org.aion.zero.impl.pendingState.AionPendingStateImpl) BlockPropagationHandler(org.aion.zero.impl.sync.handler.BlockPropagationHandler) NetworkBestBlockCallback(org.aion.zero.impl.blockchain.AionImpl.NetworkBestBlockCallback) PendingTxCallback(org.aion.zero.impl.blockchain.AionImpl.PendingTxCallback) StandaloneBlockchain(org.aion.zero.impl.blockchain.StandaloneBlockchain) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) BigInteger(java.math.BigInteger) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Test(org.junit.Test)

Aggregations

MiningBlock (org.aion.zero.impl.types.MiningBlock)185 Test (org.junit.Test)136 AionTransaction (org.aion.base.AionTransaction)128 ImportResult (org.aion.zero.impl.core.ImportResult)81 AionAddress (org.aion.types.AionAddress)79 BigInteger (java.math.BigInteger)64 Block (org.aion.zero.impl.types.Block)63 AionBlockSummary (org.aion.zero.impl.types.AionBlockSummary)59 RepositoryCache (org.aion.base.db.RepositoryCache)41 AionTxReceipt (org.aion.base.AionTxReceipt)37 ArrayList (java.util.ArrayList)36 ECKey (org.aion.crypto.ECKey)27 AionRepositoryImpl (org.aion.zero.impl.db.AionRepositoryImpl)27 AionTxExecSummary (org.aion.base.AionTxExecSummary)25 AionRepositoryCache (org.aion.zero.impl.db.AionRepositoryCache)24 MiningBlockHeader (org.aion.zero.impl.types.MiningBlockHeader)22 BlockContext (org.aion.zero.impl.types.BlockContext)18 DataWord (org.aion.util.types.DataWord)13 MockDB (org.aion.db.impl.mockdb.MockDB)12 StakingBlock (org.aion.zero.impl.types.StakingBlock)11