Search in sources :

Example 6 with HashMapBlocksIndex

use of co.rsk.db.HashMapBlocksIndex in project rskj by rsksmart.

the class TransactionTest method multiSuicideTest.

@Test
public void multiSuicideTest() throws IOException, InterruptedException {
    /*
        Original contract

        pragma solidity ^0.4.3;

        contract PsychoKiller {
            function () payable {}

            function homicide() {
                suicide(msg.sender);
            }

            function multipleHomicide() {
                PsychoKiller k  = this;
                k.homicide();
                k.homicide();
                k.homicide();
                k.homicide();
            }
        }

         */
    BigInteger nonce = config.getNetworkConstants().getInitialNonce();
    TrieStore trieStore = new TrieStoreImpl(new HashMapDB());
    MutableRepository repository = new MutableRepository(new MutableTrieImpl(trieStore, new Trie(trieStore)));
    IndexedBlockStore blockStore = new IndexedBlockStore(blockFactory, new HashMapDB(), new HashMapBlocksIndex());
    BlockTxSignatureCache blockTxSignatureCache = new BlockTxSignatureCache(new ReceivedTxSignatureCache());
    Blockchain blockchain = ImportLightTest.createBlockchain(new TestGenesisLoader(trieStore, getClass().getResourceAsStream("/genesis/genesis-light.json"), nonce, false, true, true).load(), config, repository, blockStore, trieStore);
    ECKey sender = ECKey.fromPrivate(Hex.decode("3ec771c31cac8c0dba77a69e503765701d3c2bb62435888d4ffa38fed60c445c"));
    System.out.println("address: " + ByteUtil.toHexString(sender.getAddress()));
    String code = "6060604052341561000c57fe5b5b6102938061001c6000396000f3006060604052361561004a576000357c0100000000000000000000000000000000000000000000000000000000900463ffffffff16806309e587a514610053578063de990da914610065575b6100515b5b565b005b341561005b57fe5b610063610077565b005b341561006d57fe5b610075610092565b005b3373ffffffffffffffffffffffffffffffffffffffff16ff5b565b60003090508073ffffffffffffffffffffffffffffffffffffffff166309e587a56040518163ffffffff167c0100000000000000000000000000000000000000000000000000000000028152600401809050600060405180830381600087803b15156100fa57fe5b60325a03f1151561010757fe5b5050508073ffffffffffffffffffffffffffffffffffffffff166309e587a56040518163ffffffff167c0100000000000000000000000000000000000000000000000000000000028152600401809050600060405180830381600087803b151561016d57fe5b60325a03f1151561017a57fe5b5050508073ffffffffffffffffffffffffffffffffffffffff166309e587a56040518163ffffffff167c0100000000000000000000000000000000000000000000000000000000028152600401809050600060405180830381600087803b15156101e057fe5b60325a03f115156101ed57fe5b5050508073ffffffffffffffffffffffffffffffffffffffff166309e587a56040518163ffffffff167c0100000000000000000000000000000000000000000000000000000000028152600401809050600060405180830381600087803b151561025357fe5b60325a03f1151561026057fe5b5050505b505600a165627a7a72305820084e74021c556522723b6725354378df2fb4b6732f82dd33f5daa29e2820b37c0029";
    String abi = "[{\"constant\":false,\"inputs\":[],\"name\":\"homicide\",\"outputs\":[],\"payable\":false,\"type\":\"function\"},{\"constant\":false,\"inputs\":[],\"name\":\"multipleHomicide\",\"outputs\":[],\"payable\":false,\"type\":\"function\"},{\"payable\":true,\"type\":\"fallback\"}]";
    Transaction tx = createTx(sender, new byte[0], Hex.decode(code), repository);
    executeTransaction(blockchain, blockStore, tx, repository, blockTxSignatureCache);
    byte[] contractAddress = tx.getContractAddress().getBytes();
    CallTransaction.Contract contract1 = new CallTransaction.Contract(abi);
    byte[] callData = contract1.getByName("multipleHomicide").encode();
    Assert.assertNull(contract1.getConstructor());
    Assert.assertNotNull(contract1.parseInvocation(callData));
    Assert.assertNotNull(contract1.parseInvocation(callData).toString());
    try {
        contract1.parseInvocation(new byte[32]);
        Assert.fail();
    } catch (RuntimeException ex) {
    }
    try {
        contract1.parseInvocation(new byte[2]);
        Assert.fail();
    } catch (RuntimeException ex) {
    }
    Transaction tx1 = createTx(sender, contractAddress, callData, repository);
    ProgramResult programResult = executeTransaction(blockchain, blockStore, tx1, repository, blockTxSignatureCache).getResult();
    // suicide of a single account should be counted only once
    Assert.assertEquals(24000, programResult.getFutureRefund());
}
Also used : TrieStoreImpl(co.rsk.trie.TrieStoreImpl) IndexedBlockStore(org.ethereum.db.IndexedBlockStore) ProgramResult(org.ethereum.vm.program.ProgramResult) ECKey(org.ethereum.crypto.ECKey) HashMapDB(org.ethereum.datasource.HashMapDB) TrieStore(co.rsk.trie.TrieStore) TestGenesisLoader(co.rsk.core.genesis.TestGenesisLoader) MutableRepository(org.ethereum.db.MutableRepository) BigInteger(java.math.BigInteger) MutableTrieImpl(co.rsk.db.MutableTrieImpl) HashMapBlocksIndex(co.rsk.db.HashMapBlocksIndex) Trie(co.rsk.trie.Trie) Test(org.junit.Test)

Example 7 with HashMapBlocksIndex

use of co.rsk.db.HashMapBlocksIndex in project rskj by rsksmart.

the class BlockUnclesValidationRuleTest method rejectBlockWithSiblingUncle.

@Test
public void rejectBlockWithSiblingUncle() {
    BlockGenerator blockGenerator = new BlockGenerator();
    Block genesis = blockGenerator.getGenesisBlock();
    Block block1 = blockGenerator.createChildBlock(genesis);
    Block uncle = blockGenerator.createChildBlock(block1);
    List<BlockHeader> uncles = new ArrayList<>();
    uncles.add(uncle.getHeader());
    Block block = blockGenerator.createChildBlock(block1, null, uncles, 1, null);
    BlockStore blockStore = new IndexedBlockStore(null, new HashMapDB(), new HashMapBlocksIndex());
    blockStore.saveBlock(genesis, new BlockDifficulty(BigInteger.valueOf(1)), true);
    blockStore.saveBlock(block1, new BlockDifficulty(BigInteger.valueOf(2)), true);
    BlockUnclesValidationRule rule = new BlockUnclesValidationRule(blockStore, 10, 10, new BlockHeaderCompositeRule(), new BlockHeaderParentCompositeRule());
    Assert.assertFalse(rule.isValid(block));
}
Also used : IndexedBlockStore(org.ethereum.db.IndexedBlockStore) BlockStore(org.ethereum.db.BlockStore) IndexedBlockStore(org.ethereum.db.IndexedBlockStore) ArrayList(java.util.ArrayList) HashMapDB(org.ethereum.datasource.HashMapDB) BlockGenerator(co.rsk.blockchain.utils.BlockGenerator) BlockDifficulty(co.rsk.core.BlockDifficulty) Block(org.ethereum.core.Block) HashMapBlocksIndex(co.rsk.db.HashMapBlocksIndex) BlockHeader(org.ethereum.core.BlockHeader) Test(org.junit.Test)

Example 8 with HashMapBlocksIndex

use of co.rsk.db.HashMapBlocksIndex in project rskj by rsksmart.

the class CliToolsTest method rewindBlocks.

@Test
public void rewindBlocks() {
    TestSystemProperties config = new TestSystemProperties();
    BlockFactory blockFactory = new BlockFactory(config.getActivationConfig());
    KeyValueDataSource keyValueDataSource = new HashMapDB();
    IndexedBlockStore indexedBlockStore = new IndexedBlockStore(blockFactory, keyValueDataSource, new HashMapBlocksIndex());
    int blocksToGenerate = 14;
    Keccak256 parentHash = Keccak256.ZERO_HASH;
    for (long i = 0; i < blocksToGenerate; i++) {
        Block block = mock(Block.class);
        Keccak256 blockHash = randomHash();
        when(block.getHash()).thenReturn(blockHash);
        when(block.getParentHash()).thenReturn(parentHash);
        when(block.getNumber()).thenReturn(i);
        when(block.getEncoded()).thenReturn(TestUtils.randomBytes(128));
        indexedBlockStore.saveBlock(block, ZERO, true);
        parentHash = blockHash;
    }
    Block bestBlock = indexedBlockStore.getBestBlock();
    assertThat(bestBlock.getNumber(), is((long) blocksToGenerate - 1));
    RskContext rskContext = mock(RskContext.class);
    doReturn(indexedBlockStore).when(rskContext).getBlockStore();
    RepositoryLocator repositoryLocator = mock(RepositoryLocator.class);
    doReturn(Optional.of(mock(RepositorySnapshot.class))).when(repositoryLocator).findSnapshotAt(any());
    doReturn(repositoryLocator).when(rskContext).getRepositoryLocator();
    NodeStopper stopper = mock(NodeStopper.class);
    StringBuilder output = new StringBuilder();
    RewindBlocks rewindBlocksCliTool = new RewindBlocks(output::append);
    rewindBlocksCliTool.execute(new String[] { "fmi" }, () -> rskContext, stopper);
    String data = output.toString();
    Assert.assertTrue(data.contains("No inconsistent block has been found"));
    verify(stopper).stop(0);
    clearInvocations(stopper);
    long blockToRewind = blocksToGenerate / 2;
    output = new StringBuilder();
    rewindBlocksCliTool = new RewindBlocks(output::append);
    rewindBlocksCliTool.execute(new String[] { String.valueOf(blockToRewind) }, () -> rskContext, stopper);
    bestBlock = indexedBlockStore.getBestBlock();
    assertThat(bestBlock.getNumber(), is(blockToRewind));
    data = output.toString();
    Assert.assertTrue(data.contains("New highest block number stored in db: " + blockToRewind));
    verify(stopper).stop(0);
    clearInvocations(stopper);
    output = new StringBuilder();
    rewindBlocksCliTool = new RewindBlocks(output::append);
    rewindBlocksCliTool.execute(new String[] { String.valueOf(blocksToGenerate + 1) }, () -> rskContext, stopper);
    bestBlock = indexedBlockStore.getBestBlock();
    assertThat(bestBlock.getNumber(), is(blockToRewind));
    data = output.toString();
    Assert.assertTrue(data.contains("No need to rewind"));
    verify(stopper).stop(0);
    clearInvocations(stopper);
    doReturn(Optional.empty()).when(repositoryLocator).findSnapshotAt(any());
    output = new StringBuilder();
    rewindBlocksCliTool = new RewindBlocks(output::append);
    rewindBlocksCliTool.execute(new String[] { "fmi" }, () -> rskContext, stopper);
    data = output.toString();
    Assert.assertTrue(data.contains("Min inconsistent block number: 0"));
    verify(stopper).stop(0);
    clearInvocations(stopper);
    output = new StringBuilder();
    rewindBlocksCliTool = new RewindBlocks(output::append);
    rewindBlocksCliTool.execute(new String[] { "rbc" }, () -> rskContext, stopper);
    data = output.toString();
    Assert.assertTrue(data.contains("Min inconsistent block number: 0"));
    Assert.assertTrue(data.contains("New highest block number stored in db: -1"));
    verify(stopper).stop(0);
}
Also used : BlockFactory(org.ethereum.core.BlockFactory) IndexedBlockStore(org.ethereum.db.IndexedBlockStore) Keccak256(co.rsk.crypto.Keccak256) HashMapDB(org.ethereum.datasource.HashMapDB) RepositoryLocator(co.rsk.db.RepositoryLocator) RskContext(co.rsk.RskContext) KeyValueDataSource(org.ethereum.datasource.KeyValueDataSource) Block(org.ethereum.core.Block) NodeStopper(co.rsk.util.NodeStopper) HashMapBlocksIndex(co.rsk.db.HashMapBlocksIndex) TestSystemProperties(co.rsk.config.TestSystemProperties) Test(org.junit.Test) ActivationConfigsForTest(org.ethereum.config.blockchain.upgrades.ActivationConfigsForTest)

Example 9 with HashMapBlocksIndex

use of co.rsk.db.HashMapBlocksIndex in project rskj by rsksmart.

the class TransactionTest method dontLogWhenReverting.

@Test
public void dontLogWhenReverting() throws IOException, InterruptedException {
    /*

        Original contracts

        pragma solidity ^0.4.0;
        contract TestEventInvoked {
            event internalEvent();

            function doIt() {
                internalEvent();
                throw;
            }
        }

        contract TestEventInvoker {
            event externalEvent();

            function doIt(address invokedAddress) {
                externalEvent();
                invokedAddress.call.gas(50000)(0xb29f0835);
            }
        }

         */
    BigInteger nonce = config.getNetworkConstants().getInitialNonce();
    TrieStore trieStore = new TrieStoreImpl(new HashMapDB());
    MutableRepository repository = new MutableRepository(new MutableTrieImpl(trieStore, new Trie(trieStore)));
    IndexedBlockStore blockStore = new IndexedBlockStore(blockFactory, new HashMapDB(), new HashMapBlocksIndex());
    BlockTxSignatureCache blockTxSignatureCache = new BlockTxSignatureCache(new ReceivedTxSignatureCache());
    Blockchain blockchain = ImportLightTest.createBlockchain(new TestGenesisLoader(trieStore, getClass().getResourceAsStream("/genesis/genesis-light.json"), nonce, false, true, true).load(), config, repository, blockStore, trieStore);
    ECKey sender = ECKey.fromPrivate(Hex.decode("3ec771c31cac8c0dba77a69e503765701d3c2bb62435888d4ffa38fed60c445c"));
    System.out.println("address: " + ByteUtil.toHexString(sender.getAddress()));
    // First contract code TestEventInvoked
    String code1 = "6060604052341561000f57600080fd5b5b60ae8061001e6000396000f30060606040526000357c0100000000000000000000000000000000000000000000000000000000900463ffffffff168063b29f083514603d575b600080fd5b3415604757600080fd5b604d604f565b005b7f95481a538d62f8458d3cecac82408d5ff2630d8335962b1cdbac16f1a9b910e760405160405180910390a1600080fd5b5600a165627a7a723058207d93861daff7f4a0479d7f3eb0ca7ef5cef7e2bbf2c4637ab4f021ecc5afa7ad0029";
    // Second contract code TestEventInvoker
    String code2 = "6060604052341561000f57600080fd5b5b6101358061001f6000396000f30060606040526000357c0100000000000000000000000000000000000000000000000000000000900463ffffffff168063e25fd8a71461003e575b600080fd5b341561004957600080fd5b610075600480803573ffffffffffffffffffffffffffffffffffffffff16906020019091905050610077565b005b7f4cd6f2e769273405c20f3a0c098c9045749deec145502c4838b54206ec5c542860405160405180910390a18073ffffffffffffffffffffffffffffffffffffffff1661c35063b29f0835906040518263ffffffff167c010000000000000000000000000000000000000000000000000000000002815260040160006040518083038160008887f19350505050505b505600a165627a7a7230582019096fd773ebc5581ba378acd64cb1acb450b4eb4866d710f3e3f4e33d635a4b0029";
    // Second contract ABI
    String abi2 = "[{\"constant\":false,\"inputs\":[{\"name\":\"invokedAddress\",\"type\":\"address\"}],\"name\":\"doIt\",\"outputs\":[],\"payable\":false,\"type\":\"function\"},{\"anonymous\":false,\"inputs\":[],\"name\":\"externalEvent\",\"type\":\"event\"}]";
    Transaction tx1 = createTx(sender, new byte[0], Hex.decode(code1), repository);
    executeTransaction(blockchain, blockStore, tx1, repository, blockTxSignatureCache);
    Transaction tx2 = createTx(sender, new byte[0], Hex.decode(code2), repository);
    executeTransaction(blockchain, blockStore, tx2, repository, blockTxSignatureCache);
    CallTransaction.Contract contract2 = new CallTransaction.Contract(abi2);
    byte[] data = contract2.getByName("doIt").encode(ByteUtil.toHexString(tx1.getContractAddress().getBytes()));
    Transaction tx3 = createTx(sender, tx2.getContractAddress().getBytes(), data, repository);
    TransactionExecutor executor = executeTransaction(blockchain, blockStore, tx3, repository, blockTxSignatureCache);
    Assert.assertEquals(1, executor.getResult().getLogInfoList().size());
    Assert.assertFalse(executor.getResult().getLogInfoList().get(0).isRejected());
    Assert.assertEquals(1, executor.getVMLogs().size());
}
Also used : TrieStoreImpl(co.rsk.trie.TrieStoreImpl) IndexedBlockStore(org.ethereum.db.IndexedBlockStore) ECKey(org.ethereum.crypto.ECKey) HashMapDB(org.ethereum.datasource.HashMapDB) TrieStore(co.rsk.trie.TrieStore) TestGenesisLoader(co.rsk.core.genesis.TestGenesisLoader) MutableRepository(org.ethereum.db.MutableRepository) BigInteger(java.math.BigInteger) MutableTrieImpl(co.rsk.db.MutableTrieImpl) HashMapBlocksIndex(co.rsk.db.HashMapBlocksIndex) Trie(co.rsk.trie.Trie) Test(org.junit.Test)

Example 10 with HashMapBlocksIndex

use of co.rsk.db.HashMapBlocksIndex in project rskj by rsksmart.

the class IndexedBlockStoreTest method test1.

// save some load, and check it exist
@Test
@Ignore
public void test1() {
    IndexedBlockStore indexedBlockStore = new IndexedBlockStore(blockFactory, new HashMapDB(), new HashMapBlocksIndex());
    BlockDifficulty cummDiff = BlockDifficulty.ZERO;
    for (Block block : blocks) {
        cummDiff = cummDiff.add(block.getCumulativeDifficulty());
        indexedBlockStore.saveBlock(block, cummDiff, true);
    }
    // testing:   getTotalDifficultyForHash(byte[])
    // testing:   getMaxNumber()
    long bestIndex = blocks.get(blocks.size() - 1).getNumber();
    assertEquals(bestIndex, indexedBlockStore.getMaxNumber());
    assertEquals(cumDifficulty, indexedBlockStore.getTotalDifficultyForHash(blocks.get(blocks.size() - 1).getHash().getBytes()));
    // testing:  getBlockByHash(byte[])
    Block block = blocks.get(50);
    Block block_ = indexedBlockStore.getBlockByHash(block.getHash().getBytes());
    assertEquals(block.getNumber(), block_.getNumber());
    block = blocks.get(150);
    block_ = indexedBlockStore.getBlockByHash(block.getHash().getBytes());
    assertEquals(block.getNumber(), block_.getNumber());
    block = blocks.get(0);
    block_ = indexedBlockStore.getBlockByHash(block.getHash().getBytes());
    assertEquals(block.getNumber(), block_.getNumber());
    block = blocks.get(8003);
    block_ = indexedBlockStore.getBlockByHash(block.getHash().getBytes());
    assertEquals(block.getNumber(), block_.getNumber());
    block_ = indexedBlockStore.getBlockByHash(Hex.decode("00112233"));
    assertEquals(null, block_);
    // testing:  getChainBlockByNumber(long)
    block = blocks.get(50);
    block_ = indexedBlockStore.getChainBlockByNumber(block.getNumber());
    assertEquals(block.getNumber(), block_.getNumber());
    block = blocks.get(150);
    block_ = indexedBlockStore.getChainBlockByNumber(block.getNumber());
    assertEquals(block.getNumber(), block_.getNumber());
    block = blocks.get(0);
    block_ = indexedBlockStore.getChainBlockByNumber(block.getNumber());
    assertEquals(block.getNumber(), block_.getNumber());
    block = blocks.get(8003);
    block_ = indexedBlockStore.getChainBlockByNumber(block.getNumber());
    assertEquals(block.getNumber(), block_.getNumber());
    block_ = indexedBlockStore.getChainBlockByNumber(10000);
    assertEquals(null, block_);
    // testing: getBlocksInformationByNumber(long)
    block = blocks.get(50);
    BlockInformation blockInformation = indexedBlockStore.getBlocksInformationByNumber(block.getNumber()).get(0);
    Assert.assertArrayEquals(block.getHash().getBytes(), blockInformation.getHash());
    block = blocks.get(150);
    blockInformation = indexedBlockStore.getBlocksInformationByNumber(block.getNumber()).get(0);
    Assert.assertArrayEquals(block.getHash().getBytes(), blockInformation.getHash());
    block = blocks.get(0);
    blockInformation = indexedBlockStore.getBlocksInformationByNumber(block.getNumber()).get(0);
    Assert.assertArrayEquals(block.getHash().getBytes(), blockInformation.getHash());
    block = blocks.get(8003);
    blockInformation = indexedBlockStore.getBlocksInformationByNumber(block.getNumber()).get(0);
    Assert.assertArrayEquals(block.getHash().getBytes(), blockInformation.getHash());
    Assert.assertTrue(blockInformation.getTotalDifficulty().compareTo(ZERO) > 0);
    int blocksNum = indexedBlockStore.getBlocksInformationByNumber(10000).size();
    assertEquals(0, blocksNum);
    // testing: getListHashesEndWith(byte[], long)
    block = blocks.get(8003);
    List<byte[]> hashList = indexedBlockStore.getListHashesEndWith(block.getHash().getBytes(), 100);
    for (int i = 0; i < 100; ++i) {
        block = blocks.get(8003 - i);
        String hash = ByteUtil.toHexString(hashList.get(i));
        String hash_ = ByteUtil.toHexString(block.getHash().getBytes());
        assertEquals(hash_, hash);
    }
    // testing: getListHashesStartWith(long, long)
    block = blocks.get(7003);
    hashList = indexedBlockStore.getListHashesStartWith(block.getNumber(), 100);
    for (int i = 0; i < 100; ++i) {
        block = blocks.get(7003 + i);
        String hash = ByteUtil.toHexString(hashList.get(i));
        String hash_ = ByteUtil.toHexString(block.getHash().getBytes());
        assertEquals(hash_, hash);
    }
}
Also used : BlockDifficulty(co.rsk.core.BlockDifficulty) Block(org.ethereum.core.Block) HashMapBlocksIndex(co.rsk.db.HashMapBlocksIndex) HashMapDB(org.ethereum.datasource.HashMapDB) Ignore(org.junit.Ignore) Test(org.junit.Test)

Aggregations

HashMapBlocksIndex (co.rsk.db.HashMapBlocksIndex)24 HashMapDB (org.ethereum.datasource.HashMapDB)23 Test (org.junit.Test)23 IndexedBlockStore (org.ethereum.db.IndexedBlockStore)19 BlockGenerator (co.rsk.blockchain.utils.BlockGenerator)15 BtcBlock (co.rsk.bitcoinj.core.BtcBlock)13 Block (org.ethereum.core.Block)8 BlockDifficulty (co.rsk.core.BlockDifficulty)5 Keccak256 (co.rsk.crypto.Keccak256)5 Ignore (org.junit.Ignore)4 TrieStoreImpl (co.rsk.trie.TrieStoreImpl)3 ArrayList (java.util.ArrayList)3 BlockStore (org.ethereum.db.BlockStore)3 TestGenesisLoader (co.rsk.core.genesis.TestGenesisLoader)2 MutableTrieImpl (co.rsk.db.MutableTrieImpl)2 RepositoryLocator (co.rsk.db.RepositoryLocator)2 Trie (co.rsk.trie.Trie)2 TrieStore (co.rsk.trie.TrieStore)2 BigInteger (java.math.BigInteger)2 BlockFactory (org.ethereum.core.BlockFactory)2