Search in sources :

Example 1 with Constants

use of org.ethereum.config.Constants 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 2 with Constants

use of org.ethereum.config.Constants 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 3 with Constants

use of org.ethereum.config.Constants in project rskj by rsksmart.

the class GasLimitCalculator method calculateBlockGasLimit.

// At the end of this algorithm it will increase the gas limit if and only if the previous block gas used
// is above 2/3 of the block gas limit, and decrease otherwise, by small amounts
// The idea is to increase the gas limit when there are more transactions on the network while reduce it when
// there are no or almost no transaction on it
public BigInteger calculateBlockGasLimit(BigInteger parentGasLimit, BigInteger parentGasUsed, BigInteger minGasLimit, BigInteger targetGasLimit, boolean forceTarget) {
    Constants constants = config.getBlockchainConfig().getCommonConstants();
    BigInteger newGasLimit = parentGasLimit;
    // deltaMax = parentGasLimit / 1024
    // current Eth implementation substracts parentGasLimit / 1024 - 1
    // parent - deltaMax or parent + deltaMax are the limits
    // that should be accepted by consensus rules
    BigInteger deltaMax = parentGasLimit.divide(BigInteger.valueOf(constants.getGasLimitBoundDivisor()));
    // TODO: we should assert this before reaching this point
    if (targetGasLimit.compareTo(minGasLimit) < 0) {
        targetGasLimit = minGasLimit;
    }
    // the gas used
    if (forceTarget) {
        if (targetGasLimit.compareTo(parentGasLimit) < 0) {
            newGasLimit = newGasLimit.subtract(deltaMax);
            if (targetGasLimit.compareTo(newGasLimit) > 0) {
                newGasLimit = targetGasLimit;
            }
            return newGasLimit;
        } else {
            newGasLimit = newGasLimit.add(deltaMax);
            if (targetGasLimit.compareTo(newGasLimit) < 0) {
                newGasLimit = targetGasLimit;
            }
            return newGasLimit;
        }
    }
    // contrib = (parentGasUsed * 3 / 2) / 1024
    BigInteger contrib = parentGasUsed.multiply(BigInteger.valueOf(3));
    contrib = contrib.divide(BigInteger.valueOf(2));
    contrib = contrib.divide(BigInteger.valueOf(constants.getGasLimitBoundDivisor()));
    newGasLimit = newGasLimit.subtract(deltaMax);
    newGasLimit = newGasLimit.add(contrib);
    // Gas limit can never be lesser than a certain threshold
    if (newGasLimit.compareTo(minGasLimit) < 0) {
        newGasLimit = minGasLimit;
    }
    if (newGasLimit.compareTo(targetGasLimit) > 0) {
        newGasLimit = targetGasLimit;
    }
    // I've never done enough calculations, but neither of these two should ever happen
    if (newGasLimit.compareTo(parentGasLimit.subtract(deltaMax)) < 0) {
        newGasLimit = parentGasLimit;
    }
    if (newGasLimit.compareTo(parentGasLimit.add(deltaMax)) > 0) {
        newGasLimit = parentGasLimit;
    }
    return newGasLimit;
}
Also used : Constants(org.ethereum.config.Constants) BigInteger(java.math.BigInteger)

Aggregations

Constants (org.ethereum.config.Constants)3 RskSystemProperties (co.rsk.config.RskSystemProperties)2 BlockChainImplTest (co.rsk.core.bc.BlockChainImplTest)2 BlockchainNetConfig (org.ethereum.config.BlockchainNetConfig)2 Test (org.junit.Test)2 RskAddress (co.rsk.core.RskAddress)1 RepositoryImpl (co.rsk.db.RepositoryImpl)1 TrieStoreImpl (co.rsk.trie.TrieStoreImpl)1 ProofOfWorkRule (co.rsk.validators.ProofOfWorkRule)1 File (java.io.File)1 IOException (java.io.IOException)1 BigInteger (java.math.BigInteger)1 Repository (org.ethereum.core.Repository)1 ECKey (org.ethereum.crypto.ECKey)1 HashMapDB (org.ethereum.datasource.HashMapDB)1 BlockStore (org.ethereum.db.BlockStore)1 EthereumImpl (org.ethereum.facade.EthereumImpl)1 EthereumListener (org.ethereum.listener.EthereumListener)1 DataWord (org.ethereum.vm.DataWord)1