Search in sources :

Example 26 with MutableRepository

use of org.ethereum.db.MutableRepository in project rskj by rsksmart.

the class BlockExecutorTest method generateBlockWithOneStrangeTransaction.

public TestObjects generateBlockWithOneStrangeTransaction(int strangeTransactionType) {
    TrieStore trieStore = new TrieStoreImpl(new HashMapDB());
    Repository repository = new MutableRepository(trieStore, new Trie(trieStore));
    Repository track = repository.startTracking();
    Account account = createAccount("acctest1", track, Coin.valueOf(30000));
    Account account2 = createAccount("acctest2", track, Coin.valueOf(10L));
    track.commit();
    Assert.assertFalse(Arrays.equals(EMPTY_TRIE_HASH, repository.getRoot()));
    BlockExecutor executor = buildBlockExecutor(trieStore);
    List<Transaction> txs = new ArrayList<>();
    Transaction tx = createStrangeTransaction(account, account2, BigInteger.TEN, repository.getNonce(account.getAddress()), strangeTransactionType);
    txs.add(tx);
    List<BlockHeader> uncles = new ArrayList<>();
    Block genesis = BlockChainImplTest.getGenesisBlock(trieStore);
    genesis.setStateRoot(repository.getRoot());
    Block block = new BlockGenerator().createChildBlock(genesis, txs, uncles, 1, null);
    // Forces all transactions included
    executor.executeAndFillReal(block, genesis.getHeader());
    repository.save();
    return new TestObjects(trieStore, block, genesis, tx, account);
}
Also used : TrieStoreImpl(co.rsk.trie.TrieStoreImpl) ArrayList(java.util.ArrayList) HashMapDB(org.ethereum.datasource.HashMapDB) TrieStore(co.rsk.trie.TrieStore) BlockGenerator(co.rsk.blockchain.utils.BlockGenerator) MutableRepository(org.ethereum.db.MutableRepository) MutableRepository(org.ethereum.db.MutableRepository) Trie(co.rsk.trie.Trie)

Example 27 with MutableRepository

use of org.ethereum.db.MutableRepository in project rskj by rsksmart.

the class NetworkStateExporterTest method setup.

@Before
public void setup() {
    TrieStore trieStore = new TrieStoreImpl(new HashMapDB());
    MutableTrieImpl mutableTrie = new MutableTrieImpl(trieStore, new Trie(trieStore));
    repository = new MutableRepository(mutableTrie);
    blockchain = mock(Blockchain.class);
    block = mock(Block.class);
    when(blockchain.getBestBlock()).thenReturn(block);
    BlockHeader blockHeader = mock(BlockHeader.class);
    when(block.getHeader()).thenReturn(blockHeader);
    RepositoryLocator repositoryLocator = mock(RepositoryLocator.class);
    when(repositoryLocator.snapshotAt(block.getHeader())).thenReturn(new MutableRepository(mutableTrie));
    this.nse = new NetworkStateExporter(repositoryLocator, blockchain);
}
Also used : TrieStoreImpl(co.rsk.trie.TrieStoreImpl) RepositoryLocator(co.rsk.db.RepositoryLocator) MutableRepository(org.ethereum.db.MutableRepository) Blockchain(org.ethereum.core.Blockchain) Block(org.ethereum.core.Block) MutableTrieImpl(co.rsk.db.MutableTrieImpl) HashMapDB(org.ethereum.datasource.HashMapDB) BlockHeader(org.ethereum.core.BlockHeader) TrieStore(co.rsk.trie.TrieStore) Trie(co.rsk.trie.Trie) Before(org.junit.Before)

Example 28 with MutableRepository

use of org.ethereum.db.MutableRepository in project rskj by rsksmart.

the class GenesisLoaderImpl method loadGenesisTrie.

private Trie loadGenesisTrie(Genesis genesis) {
    Repository repository = new MutableRepository(trieStore, new Trie(trieStore));
    loadGenesisInitalState(repository, genesis);
    setupPrecompiledContractsStorage(repository);
    repository.commit();
    repository.save();
    return repository.getTrie();
}
Also used : Repository(org.ethereum.core.Repository) MutableRepository(org.ethereum.db.MutableRepository) MutableRepository(org.ethereum.db.MutableRepository) Trie(co.rsk.trie.Trie)

Example 29 with MutableRepository

use of org.ethereum.db.MutableRepository 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 30 with MutableRepository

use of org.ethereum.db.MutableRepository in project rskj by rsksmart.

the class TransactionExecutorTest method txInBlockIsReceivedAndShouldBeUsedInTxExecutorInsteadOfComputeSender.

@Test
public void txInBlockIsReceivedAndShouldBeUsedInTxExecutorInsteadOfComputeSender() {
    ReceivedTxSignatureCache receivedTxSignatureCache = mock(ReceivedTxSignatureCache.class);
    BlockTxSignatureCache blockTxSignatureCache = new BlockTxSignatureCache(receivedTxSignatureCache);
    MutableRepository cacheTrack = mock(MutableRepository.class);
    when(repository.startTracking()).thenReturn(cacheTrack);
    RskAddress sender = new RskAddress("0000000000000000000000000000000000000001");
    RskAddress receiver = new RskAddress("0000000000000000000000000000000000000002");
    byte[] gasLimit = BigInteger.valueOf(4000000).toByteArray();
    byte[] txNonce = BigInteger.valueOf(1L).toByteArray();
    Coin gasPrice = Coin.valueOf(1);
    Coin value = new Coin(BigInteger.valueOf(2));
    when(repository.getNonce(sender)).thenReturn(BigInteger.valueOf(1L));
    when(repository.getBalance(sender)).thenReturn(new Coin(BigInteger.valueOf(68000L)));
    Transaction transaction = getTransaction(sender, receiver, gasLimit, txNonce, gasPrice, value);
    when(receivedTxSignatureCache.getSender(transaction)).thenReturn(sender);
    when(receivedTxSignatureCache.containsTx(transaction)).thenReturn(true);
    assertTrue(executeValidTransaction(transaction, blockTxSignatureCache));
    // Execute two times the same tx
    assertTrue(executeValidTransaction(transaction, blockTxSignatureCache));
    verify(receivedTxSignatureCache, times(1)).getSender(transaction);
    assertArrayEquals(blockTxSignatureCache.getSender(transaction).getBytes(), sender.getBytes());
}
Also used : Coin(co.rsk.core.Coin) MutableRepository(org.ethereum.db.MutableRepository) RskAddress(co.rsk.core.RskAddress) Test(org.junit.Test) ActivationConfigsForTest(org.ethereum.config.blockchain.upgrades.ActivationConfigsForTest)

Aggregations

MutableRepository (org.ethereum.db.MutableRepository)40 Test (org.junit.Test)31 Trie (co.rsk.trie.Trie)26 RskAddress (co.rsk.core.RskAddress)21 Repository (org.ethereum.core.Repository)15 TrieStore (co.rsk.trie.TrieStore)12 TrieStoreImpl (co.rsk.trie.TrieStoreImpl)12 HashMapDB (org.ethereum.datasource.HashMapDB)12 ActivationConfigsForTest (org.ethereum.config.blockchain.upgrades.ActivationConfigsForTest)9 BigInteger (java.math.BigInteger)8 Coin (co.rsk.core.Coin)7 DataWord (org.ethereum.vm.DataWord)7 MutableTrieImpl (co.rsk.db.MutableTrieImpl)6 BlockGenerator (co.rsk.blockchain.utils.BlockGenerator)5 MutableTrie (co.rsk.trie.MutableTrie)4 ArrayList (java.util.ArrayList)4 Block (org.ethereum.core.Block)4 TestGenesisLoader (co.rsk.core.genesis.TestGenesisLoader)2 HashMapBlocksIndex (co.rsk.db.HashMapBlocksIndex)2 CountDownLatch (java.util.concurrent.CountDownLatch)2