Search in sources :

Example 16 with MiningBlockHeader

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

the class PendingBlockStoreTest method testDropPendingQueues_wSingleQueue.

@Test
public void testDropPendingQueues_wSingleQueue() {
    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);
    // test drop functionality
    Map<ByteArrayWrapper, List<Block>> actual = pb.loadBlockRange(first.getNumber());
    List<ByteArrayWrapper> queues = new ArrayList<>();
    queues.add(ByteArrayWrapper.wrap(first.getHash()));
    pb.dropPendingQueues(first.getNumber(), queues, actual);
    // check storage after drop functionality
    assertThat(pb.getIndexSize()).isEqualTo(1);
    assertThat(pb.getLevelSize()).isEqualTo(1);
    assertThat(pb.getQueueSize()).isEqualTo(1);
}
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 17 with MiningBlockHeader

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

the class PendingBlockStoreTest method testDropPendingQueues.

@Test
public void testDropPendingQueues() {
    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);
    // test drop functionality
    Map<ByteArrayWrapper, List<Block>> actual = pb.loadBlockRange(first.getNumber());
    pb.dropPendingQueues(first.getNumber(), actual.keySet(), actual);
    // check storage after drop functionality
    assertThat(pb.getIndexSize()).isEqualTo(0);
    assertThat(pb.getLevelSize()).isEqualTo(0);
    assertThat(pb.getQueueSize()).isEqualTo(0);
}
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 18 with MiningBlockHeader

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

the class FvmExploitsTest method testAddInvalidAndThenValidBlocks.

@Test
public void testAddInvalidAndThenValidBlocks() throws InterruptedException {
    String testerByteCode = "0x60506040525b3360006000508282909180600101839055555050505b610020565b6107ab8061002f6000396000f30060506040523615610096576000356c01000000000000000000000000900463ffffffff16806306c8dcde1461009a578063590e1ae3146100c657806367a3914e146100dc5780636aee4cac1461010857806379ba5097146101525780638135ceea146101685780638da5cb5b146101d9578063a6f9dae11461020a578063b22fce4014610236578063d4ee1d901461027a57610096565b5b5b005b34156100a65760006000fd5b6100c4600480808060100135903590916020019091929050506102ab565b005b34156100d25760006000fd5b6100da610308565b005b34156100e85760006000fd5b6101066004808080601001359035909160200190919290505061035a565b005b34156101145760006000fd5b610138600480808060100135903590600019169090916020019091929050506103b7565b604051808215151515815260100191505060405180910390f35b341561015e5760006000fd5b6101666103e3565b005b34156101745760006000fd5b6101d760048080806010013590359060001916909091602001909192908080601001359035906000191690909160200190919290803590601001908201803590601001919091929080359060100190820180359060100191909192905050610470565b005b34156101e55760006000fd5b6101ed61062b565b604051808383825281601001526020019250505060405180910390f35b34156102165760006000fd5b6102346004808080601001359035909160200190919290505061063a565b005b34156102425760006000fd5b61026060048080806010013590359091602001909192905050610676565b604051808215151515815260100191505060405180910390f35b34156102865760006000fd5b61028e6106a2565b604051808383825281601001526020019250505060405180910390f35b600060005080600101549054339091149190141615156102cb5760006000fd5b600160046000506000848482528160100152602001908152601001600020905060006101000a81548160ff0219169083151502179055505b5b5050565b600060005080600101549054339091149190141615156103285760006000fd5b336108fc3031908115029060405160006040518083038185898989f194505050505015156103565760006000fd5b5b5b565b6000600050806001015490543390911491901416151561037a5760006000fd5b600060046000506000848482528160100152602001908152601001600020905060006101000a81548160ff0219169083151502179055505b5b5050565b60056000506020528181600052601052603060002090506000915091509054906101000a900460ff1681565b60026000508060010154905433909114919014161561046d5760026000508060010154905460006000508282909180600101839055555050506000600060026000508282909180600101839055555050506000600050806001015490547fa701229f4b9ddf00aa1c7228d248e6320ee7c581d856ddfba036e73947cd0d1360405160405180910390a25b5b565b60006000600050806001015490543390911491901416806104b85750600460005060003382528160100152602001908152601001600020905060009054906101000a900460ff165b15156104c45760006000fd5b868660056000506000838390600019169090600019169082528160100152602001908152601001600020905060009054906101000a900460ff1615151561050b5760006000fd5b600092506000925082505b8686905083101561057157610563878785818110151561053257fe5b905090906020020180601001359035878787818110151561054f57fe5b9050909060100201356106b163ffffffff16565b5b8280600101935050610516565b6001600560005060008b8b90600019169090600019169082528160100152602001908152601001600020905060006101000a81548160ff0219169083151502179055507f1fa305c7f8521af161de570532762ed7a60199cde79e18e1d259af34595625218c8c8c8c6040518085859060001916909060001916908252816010015260200183839060001916909060001916908252816010015260200194505050505060405180910390a15b5b50505b505050505050505050565b60006000508060010154905482565b6000600050806001015490543390911491901416151561065a5760006000fd5b818160026000508282909180600101839055555050505b5b5050565b60046000506020528181600052601052603060002090506000915091509054906101000a900460ff1681565b60026000508060010154905482565b6000600050806001015490543390911491901416806106f75750600460005060003382528160100152602001908152601001600020905060009054906101000a900460ff165b15156107035760006000fd5b82826108fc83908115029060405160006040518083038185898989f194505050505015156107315760006000fd5b7fdc3b8ebc415c945740a70187f1d472ad2d64a9e7a87047f38023aec56516976b84848460405180848482528160100152602001828152601001935050505060405180910390a15b5b5050505600a165627a7a7230582042b26e68ff40177f10fcfc05c70e102d5d14648a2134ffeec77c7138bf8d27dd0029";
    StandaloneBlockchain.Bundle bundle = (new StandaloneBlockchain.Builder()).withValidatorConfiguration("simple").withDefaultAccounts().build();
    StandaloneBlockchain bc = bundle.bc;
    ECKey deployerAccount = bundle.privateKeys.get(0);
    // =======================================================================
    BigInteger nonce = BigInteger.ZERO;
    AionTransaction tx = AionTransaction.create(deployerAccount, nonce.toByteArray(), null, new byte[0], ByteUtil.hexStringToBytes(testerByteCode), 1_000_000L, energyPrice, TransactionTypes.DEFAULT, null);
    assertThat(tx.isContractCreationTransaction()).isTrue();
    BlockContext context = bc.createNewMiningBlockContext(bc.getBestBlock(), Collections.singletonList(tx), false);
    ImportResult result = bc.tryToConnect(context.block);
    assertThat(result).isEqualTo(ImportResult.IMPORTED_BEST);
    AionAddress contractAddress = TxUtil.calculateContractAddress(tx);
    Thread.sleep(1000L);
    // =======================================================================
    nonce = nonce.add(BigInteger.ONE);
    AionTransaction tx2 = AionTransaction.create(deployerAccount, nonce.toByteArray(), contractAddress, new BigInteger("100000000000000000000").toByteArray(), new byte[0], 1_000_000L, energyPrice, TransactionTypes.DEFAULT, null);
    BlockContext context2 = bc.createNewMiningBlockContext(bc.getBestBlock(), Collections.singletonList(tx2), false);
    MiningBlock block = context2.block;
    byte[] originalReceiptsHash = block.getReceiptsRoot();
    // try importing an invalid block
    MiningBlockHeader newHeader = MiningBlockHeader.Builder.newInstance().withHeader(block.getHeader()).withReceiptTrieRoot(new byte[32]).build();
    block.updateHeader(newHeader);
    ImportResult result2 = bc.tryToConnect(context2.block);
    assertThat(result2).isEqualTo(ImportResult.INVALID_BLOCK);
    // try importing the correct block
    newHeader = MiningBlockHeader.Builder.newInstance().withHeader(block.getHeader()).withReceiptTrieRoot(originalReceiptsHash).build();
    block.updateHeader(newHeader);
    ImportResult result3 = bc.tryToConnect(context2.block);
    assertThat(result3).isEqualTo(ImportResult.IMPORTED_BEST);
}
Also used : ImportResult(org.aion.zero.impl.core.ImportResult) AionAddress(org.aion.types.AionAddress) BlockContext(org.aion.zero.impl.types.BlockContext) StandaloneBlockchain(org.aion.zero.impl.blockchain.StandaloneBlockchain) ECKey(org.aion.crypto.ECKey) AionTransaction(org.aion.base.AionTransaction) MiningBlock(org.aion.zero.impl.types.MiningBlock) MiningBlockHeader(org.aion.zero.impl.types.MiningBlockHeader) BigInteger(java.math.BigInteger) Test(org.junit.Test)

Example 19 with MiningBlockHeader

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

the class Equihash method mine.

/*
     * Mine for a single nonce
     */
public AionPowSolution mine(MiningBlock block, byte[] nonce) {
    MiningBlockHeader updateHeader = MiningBlockHeader.Builder.newInstance().withHeader(block.getHeader()).build();
    byte[] inputBytes = updateHeader.getMineHash();
    BigInteger target = updateHeader.getPowBoundaryBI();
    int[][] generatedSolutions;
    // Convert byte to LE order (in place)
    toLEByteArray(nonce);
    // Get solutions for this nonce
    generatedSolutions = getSolutionsForNonce(inputBytes, nonce);
    // Increment number of solutions
    this.totalSolGenerated.addAndGet(generatedSolutions.length);
    if (LOG.isDebugEnabled()) {
        LOG.debug("Produced " + generatedSolutions.length + " solutions");
    }
    // Check each returned solution
    for (int[] generatedSolution : generatedSolutions) {
        // Verify if any of the solutions pass the difficulty filter, return if true.
        byte[] minimal = EquiUtils.getMinimalFromIndices(generatedSolution, cBitLen);
        byte[] validationBytes = merge(inputBytes, nonce, minimal);
        // Found a valid solution
        if (isValidBlock(validationBytes, target)) {
            return new AionPowSolution(block, nonce, minimal);
        }
    }
    return null;
}
Also used : MiningBlockHeader(org.aion.zero.impl.types.MiningBlockHeader) BigInteger(java.math.BigInteger)

Example 20 with MiningBlockHeader

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

the class ResBlocksHeadersTest method testHeader.

@Test
public void testHeader() {
    byte[] dd = bh1.getEncoded();
    MiningBlockHeader bh2;
    try {
        bh2 = MiningBlockHeader.Builder.newInstance().withRlpEncodedData(dd).build();
    } catch (Exception e) {
        System.out.println(e.toString());
        e.printStackTrace();
        return;
    }
    assertArrayEquals(bh1.getParentHash(), bh2.getParentHash());
    assertArrayEquals(bh1.getParentHash(), bh2.getParentHash());
    assertTrue(bh1.getCoinbase().equals(bh2.getCoinbase()));
    byte[] log1 = bh1.getLogsBloom();
    byte[] log2 = bh2.getLogsBloom();
    assertThat(log2).isEqualTo(log1);
    byte[] dif1 = bh1.getDifficulty();
    byte[] dif2 = bh2.getDifficulty();
    assertThat(dif2).isEqualTo(dif1);
    assertThat(bh2.getNumber()).isEqualTo(bh1.getNumber());
    assertThat(bh2.getTimestamp()).isEqualTo(bh1.getTimestamp());
    byte[] data1 = bh1.getExtraData();
    byte[] data2 = bh2.getExtraData();
    assertThat(data2).isEqualTo(data1);
    byte[] nonc1 = bh1.getNonce();
    byte[] nonc2 = bh2.getNonce();
    assertThat(nonc2).isEqualTo(nonc1);
    byte[] sol1 = bh1.getSolution();
    byte[] sol2 = bh2.getSolution();
    assertThat(sol2).isEqualTo(sol1);
    assertThat(bh2.getEnergyConsumed()).isEqualTo(bh1.getEnergyConsumed());
    assertThat(bh2.getEnergyLimit()).isEqualTo(bh1.getEnergyLimit());
}
Also used : MiningBlockHeader(org.aion.zero.impl.types.MiningBlockHeader) Test(org.junit.Test)

Aggregations

MiningBlockHeader (org.aion.zero.impl.types.MiningBlockHeader)29 MiningBlock (org.aion.zero.impl.types.MiningBlock)23 Test (org.junit.Test)20 Block (org.aion.zero.impl.types.Block)14 AionTransaction (org.aion.base.AionTransaction)9 ImportResult (org.aion.zero.impl.core.ImportResult)9 ArrayList (java.util.ArrayList)8 AionRepositoryImpl (org.aion.zero.impl.db.AionRepositoryImpl)8 List (java.util.List)5 IOException (java.io.IOException)4 Properties (java.util.Properties)4 ByteArrayWrapper (org.aion.util.types.ByteArrayWrapper)4 StakingBlock (org.aion.zero.impl.types.StakingBlock)4 BigInteger (java.math.BigInteger)3 ECKey (org.aion.crypto.ECKey)3 AionAddress (org.aion.types.AionAddress)3 BlockContext (org.aion.zero.impl.types.BlockContext)3 AionTxReceipt (org.aion.base.AionTxReceipt)2 MockDB (org.aion.db.impl.mockdb.MockDB)2 AionBlockStore (org.aion.zero.impl.db.AionBlockStore)2