Search in sources :

Example 6 with TestGenesisLoader

use of co.rsk.core.genesis.TestGenesisLoader in project rskj by rsksmart.

the class BlockchainLoaderTest method testLoadBlockchainEmptyBlockchain.

@Test
public void testLoadBlockchainEmptyBlockchain() {
    RskTestFactory objects = new RskTestFactory() {

        @Override
        protected GenesisLoader buildGenesisLoader() {
            return new TestGenesisLoader(getTrieStore(), "blockchain_loader_genesis.json", BigInteger.ZERO, true, true, true);
        }
    };
    // calls loadBlockchain
    Blockchain blockchain = objects.getBlockchain();
    RepositorySnapshot repository = objects.getRepositoryLocator().snapshotAt(blockchain.getBestBlock().getHeader());
    TestSystemProperties testSystemProperties = new TestSystemProperties();
    ActivationConfig.ForBlock activations = testSystemProperties.getActivationConfig().forBlock(0);
    int enabledPCCs = PrecompiledContracts.GENESIS_ADDRESSES.size();
    for (ConsensusRule consensusRule : PrecompiledContracts.CONSENSUS_ENABLED_ADDRESSES.values()) {
        if (activations.isActive(consensusRule)) {
            enabledPCCs++;
        }
    }
    int testAccountsSize = 3;
    // PCCs + test accounts in blockchain_loader_genesis.json
    int genesisAccountKeysSize = enabledPCCs + testAccountsSize;
    Assert.assertEquals(genesisAccountKeysSize, repository.getAccountsKeys().size());
    RskAddress daba01 = new RskAddress("dabadabadabadabadabadabadabadabadaba0001");
    Assert.assertEquals(Coin.valueOf(2000), repository.getBalance(daba01));
    Assert.assertEquals(BigInteger.valueOf(24), repository.getNonce(daba01));
    RskAddress daba02 = new RskAddress("dabadabadabadabadabadabadabadabadaba0002");
    Assert.assertEquals(Coin.valueOf(1000), repository.getBalance(daba02));
    Assert.assertEquals(BigInteger.ZERO, repository.getNonce(daba02));
    RskAddress address = new RskAddress("77045e71a7a2c50903d88e564cd72fab11e82051");
    Assert.assertEquals(Coin.valueOf(10), repository.getBalance(address));
    Assert.assertEquals(BigInteger.valueOf(25), repository.getNonce(address));
    Assert.assertEquals(DataWord.ONE, repository.getStorageValue(address, DataWord.ZERO));
    Assert.assertEquals(DataWord.valueOf(3), repository.getStorageValue(address, DataWord.ONE));
    Assert.assertEquals(274, Objects.requireNonNull(repository.getCode(address)).length);
}
Also used : RepositorySnapshot(co.rsk.db.RepositorySnapshot) Blockchain(org.ethereum.core.Blockchain) ConsensusRule(org.ethereum.config.blockchain.upgrades.ConsensusRule) RskAddress(co.rsk.core.RskAddress) RskTestFactory(org.ethereum.util.RskTestFactory) TestGenesisLoader(co.rsk.core.genesis.TestGenesisLoader) TestSystemProperties(co.rsk.config.TestSystemProperties) ActivationConfig(org.ethereum.config.blockchain.upgrades.ActivationConfig) Test(org.junit.Test)

Example 7 with TestGenesisLoader

use of co.rsk.core.genesis.TestGenesisLoader in project rskj by rsksmart.

the class BlockResultDTOTest method buildBlockWithTransactions.

private Block buildBlockWithTransactions(List<Transaction> transactions) {
    RskTestFactory objects = new RskTestFactory() {

        @Override
        protected GenesisLoader buildGenesisLoader() {
            return new TestGenesisLoader(getTrieStore(), "rsk-unittests.json", BigInteger.ZERO, true, true, true);
        }
    };
    Blockchain blockChain = objects.getBlockchain();
    // Build block with remasc and normal txs
    BlockBuilder builder = new BlockBuilder(null, null, null).parent(blockChain.getBestBlock());
    return builder.transactions(transactions).build();
}
Also used : Blockchain(org.ethereum.core.Blockchain) RskTestFactory(org.ethereum.util.RskTestFactory) TestGenesisLoader(co.rsk.core.genesis.TestGenesisLoader) BlockBuilder(co.rsk.test.builders.BlockBuilder)

Example 8 with TestGenesisLoader

use of co.rsk.core.genesis.TestGenesisLoader in project rskj by rsksmart.

the class RskTestFactory method getGenesisInstance.

public static Genesis getGenesisInstance(RskSystemProperties config) {
    boolean useRskip92Encoding = config.getActivationConfig().isActive(ConsensusRule.RSKIP92, 0L);
    boolean isRskip126Enabled = config.getActivationConfig().isActive(ConsensusRule.RSKIP126, 0L);
    TrieStore trieStore = new TrieStoreImpl(new HashMapDB());
    return new TestGenesisLoader(trieStore, config.genesisInfo(), config.getNetworkConstants().getInitialNonce(), false, useRskip92Encoding, isRskip126Enabled).load();
}
Also used : TrieStoreImpl(co.rsk.trie.TrieStoreImpl) HashMapDB(org.ethereum.datasource.HashMapDB) TrieStore(co.rsk.trie.TrieStore) TestGenesisLoader(co.rsk.core.genesis.TestGenesisLoader)

Example 9 with TestGenesisLoader

use of co.rsk.core.genesis.TestGenesisLoader 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 TestGenesisLoader

use of co.rsk.core.genesis.TestGenesisLoader in project rskj by rsksmart.

the class TransactionPoolImplTest method setUp.

@Before
public void setUp() {
    rskTestContext = new RskTestContext(new String[] { "--regtest" }) {

        @Override
        protected GenesisLoader buildGenesisLoader() {
            return new TestGenesisLoader(getTrieStore(), "rsk-unittests.json", BigInteger.ZERO, true, true, true);
        }

        @Override
        protected RepositoryLocator buildRepositoryLocator() {
            return spy(super.buildRepositoryLocator());
        }
    };
    blockChain = rskTestContext.getBlockchain();
    RepositoryLocator repositoryLocator = rskTestContext.getRepositoryLocator();
    repository = repositoryLocator.startTrackingAt(blockChain.getBestBlock().getHeader());
    signatureCache = spy(rskTestContext.getReceivedTxSignatureCache());
    transactionPool = new TransactionPoolImpl(rskTestContext.getRskSystemProperties(), repositoryLocator, rskTestContext.getBlockStore(), rskTestContext.getBlockFactory(), rskTestContext.getCompositeEthereumListener(), rskTestContext.getTransactionExecutorFactory(), signatureCache, 10, 100);
    // don't call start to avoid creating threads
    transactionPool.processBest(blockChain.getBestBlock());
    // this is to workaround the current test structure, which abuses the Repository by
    // modifying it in place
    doReturn(repository).when(repositoryLocator).snapshotAt(any());
}
Also used : GenesisLoader(org.ethereum.core.genesis.GenesisLoader) TestGenesisLoader(co.rsk.core.genesis.TestGenesisLoader) RepositoryLocator(co.rsk.db.RepositoryLocator) RskTestContext(org.ethereum.util.RskTestContext) TestGenesisLoader(co.rsk.core.genesis.TestGenesisLoader) Before(org.junit.Before)

Aggregations

TestGenesisLoader (co.rsk.core.genesis.TestGenesisLoader)10 RskTestFactory (org.ethereum.util.RskTestFactory)5 Before (org.junit.Before)4 Test (org.junit.Test)4 TrieStore (co.rsk.trie.TrieStore)3 TrieStoreImpl (co.rsk.trie.TrieStoreImpl)3 GenesisLoader (org.ethereum.core.genesis.GenesisLoader)3 HashMapDB (org.ethereum.datasource.HashMapDB)3 TestSystemProperties (co.rsk.config.TestSystemProperties)2 HashMapBlocksIndex (co.rsk.db.HashMapBlocksIndex)2 MutableTrieImpl (co.rsk.db.MutableTrieImpl)2 RepositoryLocator (co.rsk.db.RepositoryLocator)2 Trie (co.rsk.trie.Trie)2 BigInteger (java.math.BigInteger)2 ActivationConfig (org.ethereum.config.blockchain.upgrades.ActivationConfig)2 Blockchain (org.ethereum.core.Blockchain)2 ECKey (org.ethereum.crypto.ECKey)2 IndexedBlockStore (org.ethereum.db.IndexedBlockStore)2 MutableRepository (org.ethereum.db.MutableRepository)2 RskTestContext (org.ethereum.util.RskTestContext)2