Search in sources :

Example 16 with TrieStore

use of co.rsk.trie.TrieStore 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 17 with TrieStore

use of co.rsk.trie.TrieStore in project rskj by rsksmart.

the class RskContextTest method shouldBuildSimpleTrieStore.

@Test
public void shouldBuildSimpleTrieStore() throws IOException {
    doReturn(new GarbageCollectorConfig(false, 1000, 3)).when(testProperties).garbageCollectorConfig();
    TrieStore trieStore = rskContext.getTrieStore();
    assertThat(trieStore, is(instanceOf(TrieStoreImpl.class)));
    assertThat(Files.list(databaseDir.toPath()).count(), is(1L));
}
Also used : TrieStore(co.rsk.trie.TrieStore) MultiTrieStore(co.rsk.trie.MultiTrieStore) Test(org.junit.Test)

Example 18 with TrieStore

use of co.rsk.trie.TrieStore in project rskj by rsksmart.

the class RskContextTest method shouldBuildMultiTrieStoreFromExistingDirectories.

@Test
public void shouldBuildMultiTrieStoreFromExistingDirectories() throws IOException {
    int numberOfEpochs = 3;
    Path testDatabasesDirectory = databaseDir.toPath();
    doReturn(false).when(testProperties).databaseReset();
    doReturn(new GarbageCollectorConfig(true, 1000, numberOfEpochs)).when(testProperties).garbageCollectorConfig();
    int initialEpoch = 3;
    for (int i = initialEpoch; i < initialEpoch + numberOfEpochs; i++) {
        Files.createDirectory(testDatabasesDirectory.resolve(String.format("unitrie_%d", i)));
    }
    rskContext.close();
    rskContext = makeRskContext();
    TrieStore trieStore = rskContext.getTrieStore();
    assertThat(trieStore, is(instanceOf(MultiTrieStore.class)));
    assertThat(Files.list(testDatabasesDirectory).count(), is((long) numberOfEpochs));
    int[] directorySuffixes = Files.list(testDatabasesDirectory).map(Path::getFileName).map(Path::toString).map(fileName -> fileName.replaceAll("unitrie_", "")).mapToInt(Integer::valueOf).sorted().toArray();
    assertThat(directorySuffixes, is(IntStream.range(initialEpoch, initialEpoch + numberOfEpochs).toArray()));
}
Also used : Path(java.nio.file.Path) IntStream(java.util.stream.IntStream) NodeBlockProcessor(co.rsk.net.NodeBlockProcessor) co.rsk.config(co.rsk.config) HashSet(java.util.HashSet) ActivationConfig(org.ethereum.config.blockchain.upgrades.ActivationConfig) Method(java.lang.reflect.Method) Path(java.nio.file.Path) TrieStore(co.rsk.trie.TrieStore) Before(org.junit.Before) Files(java.nio.file.Files) AsyncNodeBlockProcessor(co.rsk.net.AsyncNodeBlockProcessor) MultiTrieStore(co.rsk.trie.MultiTrieStore) Set(java.util.Set) Matchers(org.hamcrest.Matchers) Test(org.junit.Test) IOException(java.io.IOException) BlockGenerator(co.rsk.blockchain.utils.BlockGenerator) File(java.io.File) InvocationTargetException(java.lang.reflect.InvocationTargetException) Mockito(org.mockito.Mockito) List(java.util.List) Rule(org.junit.Rule) RskTestContext(org.ethereum.util.RskTestContext) Paths(java.nio.file.Paths) Matchers.contains(org.hamcrest.Matchers.contains) TrieStoreImpl(co.rsk.trie.TrieStoreImpl) Modifier(java.lang.reflect.Modifier) Genesis(org.ethereum.core.Genesis) Assert(org.junit.Assert) ConsensusRule(org.ethereum.config.blockchain.upgrades.ConsensusRule) Collections(java.util.Collections) TemporaryFolder(org.junit.rules.TemporaryFolder) Constants(org.ethereum.config.Constants) ECKey(org.ethereum.crypto.ECKey) TrieStore(co.rsk.trie.TrieStore) MultiTrieStore(co.rsk.trie.MultiTrieStore) Test(org.junit.Test)

Example 19 with TrieStore

use of co.rsk.trie.TrieStore in project rskj by rsksmart.

the class RskContextTest method shouldBuildSimpleTrieStoreCleaningUpMultiTrieStore.

@Test
public void shouldBuildSimpleTrieStoreCleaningUpMultiTrieStore() throws IOException {
    Path testDatabasesDirectory = databaseDir.toPath();
    doReturn(new GarbageCollectorConfig(false, 1000, 3)).when(testProperties).garbageCollectorConfig();
    long preExistingEpochs = 4;
    for (int i = 0; i < preExistingEpochs; i++) {
        Files.createDirectory(testDatabasesDirectory.resolve(String.format("unitrie_%d", i)));
    }
    assertThat(Files.list(testDatabasesDirectory).count(), is(preExistingEpochs));
    TrieStore trieStore = rskContext.getTrieStore();
    assertThat(trieStore, is(instanceOf(TrieStoreImpl.class)));
    assertThat(Files.list(testDatabasesDirectory).count(), is(1L));
}
Also used : Path(java.nio.file.Path) TrieStore(co.rsk.trie.TrieStore) MultiTrieStore(co.rsk.trie.MultiTrieStore) Test(org.junit.Test)

Example 20 with TrieStore

use of co.rsk.trie.TrieStore in project rskj by rsksmart.

the class TransactionModuleTest method sendRawTransactionWithAutoMining.

@Test
public void sendRawTransactionWithAutoMining() throws Exception {
    ReceiptStore receiptStore = new ReceiptStoreImpl(new HashMapDB());
    World world = new World(receiptStore);
    BlockChainImpl blockchain = world.getBlockChain();
    TrieStore trieStore = world.getTrieStore();
    RepositoryLocator repositoryLocator = world.getRepositoryLocator();
    BlockStore blockStore = world.getBlockStore();
    TransactionPool transactionPool = new TransactionPoolImpl(config, repositoryLocator, blockStore, blockFactory, null, buildTransactionExecutorFactory(blockStore, receiptStore, world.getBlockTxSignatureCache()), world.getReceivedTxSignatureCache(), 10, 100);
    TransactionGateway transactionGateway = new TransactionGateway(new SimpleChannelManager(), transactionPool);
    Web3Impl web3 = createEnvironment(blockchain, receiptStore, trieStore, transactionPool, blockStore, true, world.getBlockTxSignatureCache(), transactionGateway);
    String txHash = sendRawTransaction(web3);
    Assert.assertEquals(1, blockchain.getBestBlock().getNumber());
    Assert.assertEquals(2, blockchain.getBestBlock().getTransactionsList().size());
    Transaction txInBlock = getTransactionFromBlockWhichWasSend(blockchain, txHash);
    // Transaction tx must be in the block mined.
    Assert.assertEquals(txHash, txInBlock.getHash().toJsonString());
}
Also used : BlockStore(org.ethereum.db.BlockStore) HashMapDB(org.ethereum.datasource.HashMapDB) World(co.rsk.test.World) TransactionGateway(co.rsk.net.TransactionGateway) TrieStore(co.rsk.trie.TrieStore) ReceiptStoreImpl(org.ethereum.db.ReceiptStoreImpl) RepositoryLocator(co.rsk.db.RepositoryLocator) SimpleChannelManager(org.ethereum.rpc.Simples.SimpleChannelManager) ReceiptStore(org.ethereum.db.ReceiptStore) Test(org.junit.Test)

Aggregations

TrieStore (co.rsk.trie.TrieStore)43 Test (org.junit.Test)29 TrieStoreImpl (co.rsk.trie.TrieStoreImpl)28 HashMapDB (org.ethereum.datasource.HashMapDB)26 Trie (co.rsk.trie.Trie)18 MutableRepository (org.ethereum.db.MutableRepository)12 BlockStore (org.ethereum.db.BlockStore)11 BlockGenerator (co.rsk.blockchain.utils.BlockGenerator)9 RepositoryLocator (co.rsk.db.RepositoryLocator)6 MultiTrieStore (co.rsk.trie.MultiTrieStore)6 Path (java.nio.file.Path)6 Repository (org.ethereum.core.Repository)6 DataWord (org.ethereum.vm.DataWord)6 MutableTrieImpl (co.rsk.db.MutableTrieImpl)5 TransactionGateway (co.rsk.net.TransactionGateway)5 World (co.rsk.test.World)5 BigInteger (java.math.BigInteger)5 ArrayList (java.util.ArrayList)5 ECKey (org.ethereum.crypto.ECKey)5 SimpleChannelManager (org.ethereum.rpc.Simples.SimpleChannelManager)5