Search in sources :

Example 96 with MiningBlock

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

the class AionBlockStoreTest method testRollback.

@Test
public void testRollback() {
    MiningBlock blk1 = new MiningBlock(new byte[32], AddressUtils.ZERO_ADDRESS, new byte[256], BigInteger.TEN.toByteArray(), 1, 1, new byte[32], new byte[32], new byte[32], new byte[32], new byte[32], new ArrayList<>(), new byte[1408], 1, 1);
    MiningBlock blk2 = new MiningBlock(new byte[32], AddressUtils.ZERO_ADDRESS, new byte[256], BigInteger.TWO.toByteArray(), 2, 2, new byte[32], new byte[32], new byte[32], new byte[32], new byte[32], new ArrayList<>(), new byte[1408], 1, 1);
    AionBlockStore store = new AionBlockStore(index, blocks, false);
    store.saveBlock(blk1, BigInteger.TEN, true);
    store.saveBlock(blk2, BigInteger.TEN.add(BigInteger.ONE), true);
    store.rollback(1);
    Block storedBlk = store.getBestBlock();
    assertThat(storedBlk.getNumber() == 1);
    assertThat(storedBlk.getDifficulty().equals(BigInteger.TEN.toByteArray()));
}
Also used : 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)

Example 97 with MiningBlock

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

the class PendingBlockStoreTest method testLoadBlockRange.

@Test
public void testLoadBlockRange() {
    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();
    List<Block> allBlocks = TestResources.consecutiveBlocks(8);
    // 1. test with empty storage
    assertThat(pb.loadBlockRange(100)).isEmpty();
    // 2. test with valid range
    List<Block> blocks = allBlocks.subList(0, 6);
    Block first = blocks.get(0);
    assertThat(blocks.size()).isEqualTo(6);
    assertThat(pb.addBlockRange(blocks)).isEqualTo(6);
    Map<ByteArrayWrapper, List<Block>> actual = pb.loadBlockRange(first.getNumber());
    assertThat(actual.size()).isEqualTo(1);
    assertThat(actual.get(ByteArrayWrapper.wrap(first.getHash()))).isEqualTo(blocks);
    // 3. test with multiple queues
    // create side chain
    MiningBlock altBlock = (MiningBlock) BlockUtil.newBlockFromRlp(first.getEncoded());
    MiningBlockHeader newHeader = MiningBlockHeader.Builder.newInstance().withHeader(altBlock.getHeader()).withExtraData("random".getBytes()).build();
    altBlock.updateHeader(newHeader);
    assertThat(altBlock.equals(first)).isFalse();
    List<Block> sideChain = new ArrayList<>();
    sideChain.add(altBlock);
    assertThat(pb.addBlockRange(sideChain)).isEqualTo(1);
    // check functionality
    actual = pb.loadBlockRange(first.getNumber());
    assertThat(actual.size()).isEqualTo(2);
    assertThat(actual.get(ByteArrayWrapper.wrap(first.getHash()))).isEqualTo(blocks);
    assertThat(actual.get(ByteArrayWrapper.wrap(altBlock.getHash()))).isEqualTo(sideChain);
    // 4. test with empty level
    long level = first.getNumber();
    assertThat(pb.loadBlockRange(level - 1)).isEmpty();
    assertThat(pb.loadBlockRange(level + 1)).isEmpty();
}
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 98 with MiningBlock

use of org.aion.zero.impl.types.MiningBlock 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 99 with MiningBlock

use of org.aion.zero.impl.types.MiningBlock 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 100 with MiningBlock

use of org.aion.zero.impl.types.MiningBlock 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)

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