Search in sources :

Example 16 with MutableRepository

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

the class BridgeUtilsTest method isFreeBridgeTx.

private void isFreeBridgeTx(boolean expected, RskAddress destinationAddress, byte[] privKeyBytes) {
    BridgeSupportFactory bridgeSupportFactory = new BridgeSupportFactory(new RepositoryBtcBlockStoreWithCache.Factory(constants.getBridgeConstants().getBtcParams()), constants.getBridgeConstants(), activationConfig);
    Bridge bridge = new Bridge(PrecompiledContracts.BRIDGE_ADDR, constants, activationConfig, bridgeSupportFactory);
    org.ethereum.core.Transaction rskTx = CallTransaction.createCallTransaction(0, 1, 1, destinationAddress, 0, Bridge.UPDATE_COLLECTIONS, constants.getChainId());
    rskTx.sign(privKeyBytes);
    TrieStore trieStore = new TrieStoreImpl(new HashMapDB());
    Repository repository = new MutableRepository(new MutableTrieCache(new MutableTrieImpl(trieStore, new Trie())));
    Block rskExecutionBlock = new BlockGenerator().createChildBlock(getGenesisInstance(trieStore));
    bridge.init(rskTx, rskExecutionBlock, repository.startTracking(), null, null, null);
    Assert.assertEquals(expected, BridgeUtils.isFreeBridgeTx(rskTx, constants, activationConfig.forBlock(rskExecutionBlock.getNumber())));
}
Also used : TrieStoreImpl(co.rsk.trie.TrieStoreImpl) MutableTrieCache(co.rsk.db.MutableTrieCache) HashMapDB(org.ethereum.datasource.HashMapDB) TrieStore(co.rsk.trie.TrieStore) BlockGenerator(co.rsk.blockchain.utils.BlockGenerator) Repository(org.ethereum.core.Repository) MutableRepository(org.ethereum.db.MutableRepository) MutableRepository(org.ethereum.db.MutableRepository) Block(org.ethereum.core.Block) MutableTrieImpl(co.rsk.db.MutableTrieImpl) Transaction(org.ethereum.core.Transaction) Trie(co.rsk.trie.Trie)

Example 17 with MutableRepository

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

the class DslFilesTest method getBalance.

private static Coin getBalance(World world, String address) {
    Block bestBlock = world.getBlockChain().getBestBlock();
    Repository repo = new MutableRepository(world.getTrieStore(), world.getTrieStore().retrieve(bestBlock.getStateRoot()).get());
    RskAddress rskAddress = new RskAddress(address);
    return repo.getBalance(rskAddress);
}
Also used : Repository(org.ethereum.core.Repository) MutableRepository(org.ethereum.db.MutableRepository) MutableRepository(org.ethereum.db.MutableRepository) RskAddress(co.rsk.core.RskAddress) Block(org.ethereum.core.Block)

Example 18 with MutableRepository

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

the class TransactionExecutorTest method firstTxIsRemovedWhenTheCacheLimitSizeIsExceeded.

@Test
public void firstTxIsRemovedWhenTheCacheLimitSizeIsExceeded() {
    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);
    assertTrue(executeValidTransaction(transaction, blockTxSignatureCache));
    for (int i = 0; i < MAX_CACHE_SIZE; i++) {
        if (i == MAX_CACHE_SIZE - 1) {
            assertTrue(blockTxSignatureCache.containsTx(transaction));
        }
        sender = new RskAddress(TestUtils.randomAddress().getBytes());
        when(repository.getNonce(sender)).thenReturn(BigInteger.valueOf(1L));
        when(repository.getBalance(sender)).thenReturn(new Coin(BigInteger.valueOf(68000L)));
        Transaction transactionAux = getTransaction(sender, receiver, gasLimit, txNonce, gasPrice, value);
        assertTrue(executeValidTransaction(transactionAux, blockTxSignatureCache));
    }
    assertFalse(blockTxSignatureCache.containsTx(transaction));
}
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)

Example 19 with MutableRepository

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

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

the class TransactionExecutorTest method InvalidTxsIsInBlockAndShouldntBeInCache.

@Test
public void InvalidTxsIsInBlockAndShouldntBeInCache() {
    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));
    Transaction transaction = getTransaction(sender, receiver, gasLimit, txNonce, gasPrice, value);
    when(executionBlock.getGasLimit()).thenReturn(BigInteger.valueOf(6800000).toByteArray());
    when(repository.getNonce(sender)).thenReturn(BigInteger.valueOf(1L));
    when(repository.getBalance(sender)).thenReturn(new Coin(BigInteger.valueOf(0L)));
    TransactionExecutor txExecutor = new TransactionExecutor(constants, activationConfig, transaction, txIndex, rskAddress, repository, blockStore, receiptStore, blockFactory, programInvokeFactory, executionBlock, gasUsedInTheBlock, vmConfig, true, precompiledContracts, deletedAccounts, blockTxSignatureCache);
    assertEquals(0, transaction.transactionCost(constants, activationConfig.forBlock(executionBlock.getNumber())));
    assertFalse(txExecutor.executeTransaction());
    assertFalse(blockTxSignatureCache.containsTx(transaction));
}
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