Search in sources :

Example 46 with RskSystemProperties

use of co.rsk.config.RskSystemProperties in project rskj by rsksmart.

the class BlockchainLoaderTest method testLoadBlockchainEmptyBlockchain.

@Test
public void testLoadBlockchainEmptyBlockchain() throws IOException {
    String jsonFile = "blockchain_loader_genesis.json";
    RskSystemProperties systemProperties = Mockito.mock(RskSystemProperties.class);
    Constants constants = Mockito.mock(Constants.class);
    Mockito.when(constants.getInitialNonce()).thenReturn(BigInteger.ZERO);
    BlockchainNetConfig blockchainNetConfig = Mockito.mock(BlockchainNetConfig.class);
    Mockito.when(blockchainNetConfig.getCommonConstants()).thenReturn(constants);
    Mockito.when(systemProperties.databaseDir()).thenReturn(new RskSystemProperties().databaseDir());
    Mockito.when(systemProperties.getBlockchainConfig()).thenReturn(blockchainNetConfig);
    Mockito.when(systemProperties.genesisInfo()).thenReturn(jsonFile);
    BlockStore blockStore = Mockito.mock(BlockStore.class);
    Mockito.when(blockStore.getBestBlock()).thenReturn(null);
    EthereumListener ethereumListener = Mockito.mock(EthereumListener.class);
    Repository repository = new RepositoryImpl(systemProperties, new TrieStoreImpl(new HashMapDB().setClearOnClose(false)));
    ;
    BlockChainLoader blockChainLoader = new BlockChainLoader(systemProperties, repository, blockStore, null, null, ethereumListener, null, null);
    blockChainLoader.loadBlockchain();
    Assert.assertEquals(5, repository.getAccountsKeys().size());
    Assert.assertEquals(Coin.valueOf(2000), repository.getBalance(new RskAddress("dabadabadabadabadabadabadabadabadaba0001")));
    Assert.assertEquals(BigInteger.valueOf(24), repository.getNonce(new RskAddress("dabadabadabadabadabadabadabadabadaba0001")));
    Assert.assertEquals(Coin.valueOf(1000), repository.getBalance(new RskAddress("dabadabadabadabadabadabadabadabadaba0002")));
    Assert.assertEquals(BigInteger.ZERO, repository.getNonce(new RskAddress("dabadabadabadabadabadabadabadabadaba0002")));
    Assert.assertEquals(Coin.valueOf(10), repository.getBalance(new RskAddress("77045e71a7a2c50903d88e564cd72fab11e82051")));
    Assert.assertEquals(BigInteger.valueOf(25), repository.getNonce(new RskAddress("77045e71a7a2c50903d88e564cd72fab11e82051")));
    Assert.assertEquals(DataWord.ONE, repository.getContractDetails(new RskAddress("77045e71a7a2c50903d88e564cd72fab11e82051")).get(DataWord.ZERO));
    Assert.assertEquals(new DataWord(3), repository.getContractDetails(new RskAddress("77045e71a7a2c50903d88e564cd72fab11e82051")).get(DataWord.ONE));
    Assert.assertEquals(274, repository.getContractDetails(new RskAddress("77045e71a7a2c50903d88e564cd72fab11e82051")).getCode().length);
}
Also used : TrieStoreImpl(co.rsk.trie.TrieStoreImpl) EthereumListener(org.ethereum.listener.EthereumListener) BlockStore(org.ethereum.db.BlockStore) Constants(org.ethereum.config.Constants) DataWord(org.ethereum.vm.DataWord) HashMapDB(org.ethereum.datasource.HashMapDB) Repository(org.ethereum.core.Repository) RepositoryImpl(co.rsk.db.RepositoryImpl) RskAddress(co.rsk.core.RskAddress) BlockchainNetConfig(org.ethereum.config.BlockchainNetConfig) RskSystemProperties(co.rsk.config.RskSystemProperties) Test(org.junit.Test) BlockChainImplTest(co.rsk.core.bc.BlockChainImplTest)

Example 47 with RskSystemProperties

use of co.rsk.config.RskSystemProperties in project rskj by rsksmart.

the class MainNetMinerTest method generateFallbackMinedBlock.

@Test
public void generateFallbackMinedBlock() throws InterruptedException, IOException {
    // generate private keys for testing now.
    ECKey privateMiningKey0 = ECKey.fromPrivate(BigInteger.TEN);
    ECKey privateMiningKey1 = ECKey.fromPrivate(BigInteger.TEN.add(BigInteger.ONE));
    byte[] privKey0 = privateMiningKey0.getPrivKeyBytes();
    saveToFile(privKey0, new File(folder.getRoot().getCanonicalPath(), "privkey0.bin"));
    byte[] privKey1 = privateMiningKey1.getPrivKeyBytes();
    saveToFile(privKey1, new File(folder.getRoot().getCanonicalPath(), "privkey1.bin"));
    RskSystemProperties tempConfig = new RskSystemProperties() {

        BlockchainNetConfig blockchainNetConfig = config.getBlockchainConfig();

        @Override
        public String fallbackMiningKeysDir() {
            try {
                return folder.getRoot().getCanonicalPath();
            } catch (Exception e) {
            }
            return null;
        }

        @Override
        public BlockchainNetConfig getBlockchainConfig() {
            return new BlockchainNetConfig() {

                @Override
                public BlockchainConfig getConfigForBlock(long blockNumber) {
                    return blockchainNetConfig.getConfigForBlock(blockNumber);
                }

                @Override
                public Constants getCommonConstants() {
                    return new Constants() {

                        @Override
                        public byte[] getFallbackMiningPubKey0() {
                            return privateMiningKey0.getPubKey();
                        }

                        @Override
                        public byte[] getFallbackMiningPubKey1() {
                            return privateMiningKey1.getPubKey();
                        }
                    };
                }
            };
        }
    };
    EthereumImpl ethereumImpl = Mockito.mock(EthereumImpl.class);
    Mockito.when(ethereumImpl.addNewMinedBlock(Mockito.any())).thenReturn(ImportResult.IMPORTED_BEST);
    MinerServer minerServer = new MinerServerImpl(tempConfig, ethereumImpl, blockchain, null, DIFFICULTY_CALCULATOR, new ProofOfWorkRule(tempConfig).setFallbackMiningEnabled(true), blockToMineBuilder(), ConfigUtils.getDefaultMiningConfig());
    try {
        minerServer.setFallbackMining(true);
        // Accelerate mining
        ((MinerServerImpl) minerServer).setSecsBetweenFallbackMinedBlocks(1);
        minerServer.start();
        // Blocks are generated auomatically
        // but we can call minerServer.generateFallbackBlock() to generate it manually
        // boolean result = minerServer.generateFallbackBlock();
        // Assert.assertTrue(result);
        long start = System.currentTimeMillis();
        while (((MinerServerImpl) minerServer).getFallbackBlocksGenerated() == 0) {
            if (System.currentTimeMillis() - start > 20 * 1000) {
                Assert.assertTrue(false);
            }
            // 
            Thread.sleep(1000);
        }
        Mockito.verify(ethereumImpl, Mockito.times(1)).addNewMinedBlock(Mockito.any());
        // mine another
        // NOTE that is NOT using the next block (parity change) because of the blockchain mockito
        // to mine a subsequent block, use a real blockchain, not the mockito.
        minerServer.buildBlockToMine(blockchain.getBestBlock(), false);
        // result = minerServer.generateFallbackBlock();
        // Assert.assertTrue(result);
        start = System.currentTimeMillis();
        while (((MinerServerImpl) minerServer).getFallbackBlocksGenerated() == 1) {
            if (System.currentTimeMillis() - start > 20 * 1000) {
                Assert.assertTrue(false);
            }
            // 
            Thread.sleep(1000);
        }
        Mockito.verify(ethereumImpl, Mockito.times(2)).addNewMinedBlock(Mockito.any());
    } finally {
        minerServer.stop();
    }
}
Also used : Constants(org.ethereum.config.Constants) ECKey(org.ethereum.crypto.ECKey) BlockchainNetConfig(org.ethereum.config.BlockchainNetConfig) EthereumImpl(org.ethereum.facade.EthereumImpl) File(java.io.File) RskSystemProperties(co.rsk.config.RskSystemProperties) IOException(java.io.IOException) ProofOfWorkRule(co.rsk.validators.ProofOfWorkRule) Test(org.junit.Test) BlockChainImplTest(co.rsk.core.bc.BlockChainImplTest)

Example 48 with RskSystemProperties

use of co.rsk.config.RskSystemProperties in project rskj by rsksmart.

the class BridgePerformanceTestCase method setUpBeforeClass.

@BeforeClass
public static void setUpBeforeClass() throws Exception {
    config = new RskSystemProperties();
    config.setBlockchainConfig(new RegTestConfig());
    bridgeConstants = config.getBlockchainConfig().getCommonConstants().getBridgeConstants();
    networkParameters = bridgeConstants.getBtcParams();
}
Also used : RegTestConfig(org.ethereum.config.blockchain.RegTestConfig) RskSystemProperties(co.rsk.config.RskSystemProperties) BeforeClass(org.junit.BeforeClass)

Example 49 with RskSystemProperties

use of co.rsk.config.RskSystemProperties in project rskj by rsksmart.

the class LockWhitelistTest method buildInitializer.

private BridgeStorageProviderInitializer buildInitializer() {
    final int minSize = 10;
    final int maxSize = 100;
    final int minBtcBlocks = 500;
    final int maxBtcBlocks = 1000;
    return (BridgeStorageProvider provider, Repository repository, int executionIndex) -> {
        BtcBlockStore btcBlockStore = new RepositoryBlockStore(new RskSystemProperties(), repository, PrecompiledContracts.BRIDGE_ADDR);
        Context btcContext = new Context(networkParameters);
        BtcBlockChain btcBlockChain;
        try {
            btcBlockChain = new BtcBlockChain(btcContext, btcBlockStore);
        } catch (BlockStoreException e) {
            throw new RuntimeException("Error initializing btc blockchain for tests");
        }
        int blocksToGenerate = Helper.randomInRange(minBtcBlocks, maxBtcBlocks);
        Helper.generateAndAddBlocks(btcBlockChain, blocksToGenerate);
        lockWhitelist = provider.getLockWhitelist();
        int size = Helper.randomInRange(minSize, maxSize);
        for (int i = 0; i < size; i++) {
            Address address = new BtcECKey().toAddress(networkParameters);
            Coin value = Helper.randomCoin(Coin.COIN, 1, 30);
            lockWhitelist.put(address, value);
        }
    };
}
Also used : BlockStoreException(co.rsk.bitcoinj.store.BlockStoreException) BridgeStorageProvider(co.rsk.peg.BridgeStorageProvider) BtcBlockStore(co.rsk.bitcoinj.store.BtcBlockStore) Repository(org.ethereum.core.Repository) RepositoryBlockStore(co.rsk.peg.RepositoryBlockStore) RskSystemProperties(co.rsk.config.RskSystemProperties)

Example 50 with RskSystemProperties

use of co.rsk.config.RskSystemProperties in project rskj by rsksmart.

the class RemascFederationProviderTest method getRemascFederationProvider.

private static RemascFederationProvider getRemascFederationProvider() throws IOException, BlockStoreException {
    Genesis genesisBlock = new BlockGenerator().getGenesisBlock();
    BlockChainBuilder builder = new BlockChainBuilder().setTesting(true).setGenesis(genesisBlock);
    Blockchain blockchain = builder.build();
    BridgeSupport bridgeSupport = new BridgeSupport(new RskSystemProperties(), blockchain.getRepository(), null, PrecompiledContracts.BRIDGE_ADDR, null);
    RemascFederationProvider provider = null;
    try {
        provider = new RemascFederationProvider(bridgeSupport);
    } catch (BlockStoreException | IOException e) {
        e.printStackTrace();
    }
    return provider;
}
Also used : BlockStoreException(co.rsk.bitcoinj.store.BlockStoreException) Blockchain(org.ethereum.core.Blockchain) Genesis(org.ethereum.core.Genesis) IOException(java.io.IOException) BlockGenerator(co.rsk.blockchain.utils.BlockGenerator) BlockChainBuilder(co.rsk.test.builders.BlockChainBuilder) BridgeSupport(co.rsk.peg.BridgeSupport) RskSystemProperties(co.rsk.config.RskSystemProperties)

Aggregations

RskSystemProperties (co.rsk.config.RskSystemProperties)112 Test (org.junit.Test)81 SyncConfiguration (co.rsk.net.sync.SyncConfiguration)48 SimpleMessageChannel (co.rsk.net.simples.SimpleMessageChannel)45 Blockchain (org.ethereum.core.Blockchain)45 Block (org.ethereum.core.Block)41 BlockGenerator (co.rsk.blockchain.utils.BlockGenerator)35 ProofOfWorkRule (co.rsk.validators.ProofOfWorkRule)20 Ignore (org.junit.Ignore)10 Keccak256 (co.rsk.crypto.Keccak256)8 RegTestConfig (org.ethereum.config.blockchain.RegTestConfig)8 Repository (org.ethereum.core.Repository)8 SimpleChannelManager (org.ethereum.rpc.Simples.SimpleChannelManager)8 Channel (org.ethereum.net.server.Channel)7 ChannelManager (org.ethereum.net.server.ChannelManager)7 InvocationOnMock (org.mockito.invocation.InvocationOnMock)7 BlockDifficulty (co.rsk.core.BlockDifficulty)6 RskAddress (co.rsk.core.RskAddress)6 PersonalModule (co.rsk.rpc.modules.personal.PersonalModule)6 BeforeClass (org.junit.BeforeClass)6