diff --git a/rskj-core/src/main/java/co/rsk/config/RskSystemProperties.java b/rskj-core/src/main/java/co/rsk/config/RskSystemProperties.java index d846110e988..5f06a5d089f 100644 --- a/rskj-core/src/main/java/co/rsk/config/RskSystemProperties.java +++ b/rskj-core/src/main/java/co/rsk/config/RskSystemProperties.java @@ -364,7 +364,7 @@ public long getBlocksForPeers() { public long getTargetGasLimit() { return getLongProperty("targetgaslimit", - getBlockchainConfig().getCommonConstants().getTargetGasLimit()); + 6_800_000L); } public boolean getForceTargetGasLimit() { diff --git a/rskj-core/src/main/java/co/rsk/mine/MinerServerImpl.java b/rskj-core/src/main/java/co/rsk/mine/MinerServerImpl.java index 8231f01ebbb..d3f142a5567 100644 --- a/rskj-core/src/main/java/co/rsk/mine/MinerServerImpl.java +++ b/rskj-core/src/main/java/co/rsk/mine/MinerServerImpl.java @@ -131,6 +131,7 @@ public class MinerServerImpl implements MinerServer { private long minimumAcceptableTime; private boolean autoSwitchBetweenNormalAndFallbackMining; private boolean fallbackMiningScheduled; + private final RskSystemProperties rskSystemProperties; @Autowired public MinerServerImpl(Ethereum ethereum, @@ -144,6 +145,21 @@ public MinerServerImpl(Ethereum ethereum, DifficultyCalculator difficultyCalculator, GasLimitCalculator gasLimitCalculator, ProofOfWorkRule powRule) { + this(ethereum, blockchain, blockStore, pendingState, repository, miningConfig, validationRules, nodeBlockProcessor, difficultyCalculator, gasLimitCalculator, powRule, RskSystemProperties.CONFIG); + } + + public MinerServerImpl(Ethereum ethereum, + Blockchain blockchain, + BlockStore blockStore, + PendingState pendingState, + Repository repository, + MiningConfig miningConfig, + @Qualifier("minerServerBlockValidation") BlockValidationRule validationRules, + BlockProcessor nodeBlockProcessor, + DifficultyCalculator difficultyCalculator, + GasLimitCalculator gasLimitCalculator, + ProofOfWorkRule powRule, + RskSystemProperties rskSystemProperties) { this.ethereum = ethereum; this.blockchain = blockchain; this.blockStore = blockStore; @@ -174,9 +190,9 @@ public MinerServerImpl(Ethereum ethereum, RskSystemProperties.CONFIG.getAverageFallbackMiningTime(); // default if (secsBetweenFallbackMinedBlocks==0) - secsBetweenFallbackMinedBlocks = (RskSystemProperties.CONFIG.getBlockchainConfig().getCommonConstants().getDurationLimit()); - - autoSwitchBetweenNormalAndFallbackMining = !RskSystemProperties.CONFIG.getBlockchainConfig().getCommonConstants().getFallbackMiningDifficulty().equals(BigInteger.ZERO); + secsBetweenFallbackMinedBlocks = (rskSystemProperties.getBlockchainConfig().getCommonConstants().getDurationLimit()); + this.rskSystemProperties = rskSystemProperties; + autoSwitchBetweenNormalAndFallbackMining = !rskSystemProperties.getBlockchainConfig().getCommonConstants().getFallbackMiningDifficulty().equals(BigInteger.ZERO); } // This method is used for tests @@ -333,7 +349,7 @@ public boolean generateFallbackBlock() { boolean isEvenBlockNumber = (newBlock.getNumber() % 2) == 0; - String path = RskSystemProperties.CONFIG.fallbackMiningKeysDir(); + String path = rskSystemProperties.fallbackMiningKeysDir(); if (privKey0==null) privKey0= readFromFile(new File(path,"privkey0.bin")); if (privKey1==null) privKey1 = readFromFile(new File(path,"privkey1.bin")); @@ -622,7 +638,7 @@ public void buildBlockToMine(@Nonnull Block newBlockParent, boolean createCompet if (autoSwitchBetweenNormalAndFallbackMining) { if (ProofOfWorkRule.isFallbackMiningPossible( - RskSystemProperties.CONFIG.getBlockchainConfig().getCommonConstants(), + rskSystemProperties.getBlockchainConfig().getCommonConstants(), newBlock.getHeader())) { setFallbackMining(true); diff --git a/rskj-core/src/main/java/org/ethereum/config/Constants.java b/rskj-core/src/main/java/org/ethereum/config/Constants.java index 19a9b16a034..d151565167f 100644 --- a/rskj-core/src/main/java/org/ethereum/config/Constants.java +++ b/rskj-core/src/main/java/org/ethereum/config/Constants.java @@ -41,7 +41,6 @@ public class Constants { private int maximumExtraDataSize = 32; private int minGasLimit = 3000000; private int gasLimitBoundDivisor = 1024; - private int targetGasLimit = 5000000; // Private mining is allowed if difficulty is lower or equal than this value private BigInteger fallbackMiningDifficulty = BigInteger.valueOf((long) 14E15); // 14 peta evert 14 secs = 1 peta/s. @@ -152,10 +151,6 @@ public static BigInteger getSECP256K1N() { public BridgeConstants getBridgeConstants() { return BridgeTestNetConstants.getInstance(); } - public long getTargetGasLimit() { - return targetGasLimit; - } - public int getNewBlockMaxSecondsInTheFuture() { return this.newBlockMaxSecondsInTheFuture; } diff --git a/rskj-core/src/main/java/org/ethereum/config/DefaultConfig.java b/rskj-core/src/main/java/org/ethereum/config/DefaultConfig.java index 296dc1811b8..13258a96c54 100644 --- a/rskj-core/src/main/java/org/ethereum/config/DefaultConfig.java +++ b/rskj-core/src/main/java/org/ethereum/config/DefaultConfig.java @@ -141,7 +141,7 @@ public MiningConfig miningConfig(RskSystemProperties rskSystemProperties) { rskSystemProperties.getBlockchainConfig().getCommonConstants().getUncleGenerationLimit(), new GasLimitConfig( rskSystemProperties.getBlockchainConfig().getCommonConstants().getMinGasLimit(), - rskSystemProperties.getBlockchainConfig().getCommonConstants().getTargetGasLimit(), + rskSystemProperties.getTargetGasLimit(), rskSystemProperties.getForceTargetGasLimit() ) ); diff --git a/rskj-core/src/test/java/co/rsk/mine/GasLimitCalculatorTest.java b/rskj-core/src/test/java/co/rsk/mine/GasLimitCalculatorTest.java index bfa078ad793..c6871274628 100644 --- a/rskj-core/src/test/java/co/rsk/mine/GasLimitCalculatorTest.java +++ b/rskj-core/src/test/java/co/rsk/mine/GasLimitCalculatorTest.java @@ -42,7 +42,7 @@ public void NextBlockGasLimitIsDecreasedByAFactor() { GasLimitCalculator calc = new GasLimitCalculator(RskSystemProperties.CONFIG); BigInteger minGasLimit = BigInteger.valueOf(constants.getMinGasLimit()); BigInteger parentGasLimit = minGasLimit.add(BigInteger.valueOf(21000)); - BigInteger targetGasLimit = BigInteger.valueOf(constants.getTargetGasLimit()); + BigInteger targetGasLimit = BigInteger.valueOf(RskSystemProperties.CONFIG.getTargetGasLimit()); BigInteger newGasLimit = calc.calculateBlockGasLimit(parentGasLimit, BigInteger.ZERO, minGasLimit, targetGasLimit, false); @@ -55,7 +55,7 @@ public void NextBlockGasLimitIsDecreasedByAFactor() { public void NextBlockGasLimitIsNotDecreasedLowerThanMinGasLimit() { GasLimitCalculator calc = new GasLimitCalculator(RskSystemProperties.CONFIG); BigInteger minGasLimit = BigInteger.valueOf(constants.getMinGasLimit()); - BigInteger targetGasLimit = BigInteger.valueOf(constants.getTargetGasLimit()); + BigInteger targetGasLimit = BigInteger.valueOf(RskSystemProperties.CONFIG.getTargetGasLimit()); BigInteger newGasLimit = calc.calculateBlockGasLimit(minGasLimit, BigInteger.ZERO, minGasLimit, targetGasLimit, false); Assert.assertTrue(newGasLimit.compareTo(minGasLimit) == 0); @@ -66,7 +66,7 @@ public void NextBlockGasLimitIsIncreasedBasedOnGasUsed() { GasLimitCalculator calc = new GasLimitCalculator(RskSystemProperties.CONFIG); BigInteger parentGas = BigInteger.valueOf(3500000); BigInteger gasUsed = BigInteger.valueOf(3000000); - BigInteger targetGasLimit = BigInteger.valueOf(constants.getTargetGasLimit()); + BigInteger targetGasLimit = BigInteger.valueOf(RskSystemProperties.CONFIG.getTargetGasLimit()); BigInteger newGasLimit = calc.calculateBlockGasLimit(parentGas, gasUsed, BigInteger.ZERO, targetGasLimit, false); @@ -80,7 +80,7 @@ public void NextBlockGasLimitIsIncreasedBasedOnFullGasUsed() { GasLimitCalculator calc = new GasLimitCalculator(RskSystemProperties.CONFIG); BigInteger parentGas = BigInteger.valueOf(3500000); BigInteger gasUsed = BigInteger.valueOf(3500000); - BigInteger targetGasLimit = BigInteger.valueOf(constants.getTargetGasLimit()); + BigInteger targetGasLimit = BigInteger.valueOf(RskSystemProperties.CONFIG.getTargetGasLimit()); BigInteger newGasLimit = calc.calculateBlockGasLimit(parentGas, gasUsed, BigInteger.ZERO, targetGasLimit, false); @@ -91,7 +91,7 @@ public void NextBlockGasLimitIsIncreasedBasedOnFullGasUsed() { @Test public void NextBlockGasLimitIsNotIncreasedMoreThanTargetGasLimit() { GasLimitCalculator calc = new GasLimitCalculator(RskSystemProperties.CONFIG); - BigInteger targetGasLimit = BigInteger.valueOf(constants.getTargetGasLimit()); + BigInteger targetGasLimit = BigInteger.valueOf(RskSystemProperties.CONFIG.getTargetGasLimit()); BigInteger gasUsed = targetGasLimit; BigInteger newGasLimit = calc.calculateBlockGasLimit(targetGasLimit, gasUsed, BigInteger.ZERO, targetGasLimit, false); @@ -102,8 +102,8 @@ public void NextBlockGasLimitIsNotIncreasedMoreThanTargetGasLimit() { @Test public void NextBlockGasLimitRemainsTheSame() { GasLimitCalculator calc = new GasLimitCalculator(RskSystemProperties.CONFIG); - BigInteger minGasLimit = BigInteger.valueOf(constants.getTargetGasLimit()); - BigInteger targetGasLimit = BigInteger.valueOf(constants.getTargetGasLimit()); + BigInteger minGasLimit = BigInteger.valueOf(RskSystemProperties.CONFIG.getTargetGasLimit()); + BigInteger targetGasLimit = BigInteger.valueOf(RskSystemProperties.CONFIG.getTargetGasLimit()); BigInteger newGasLimit = calc.calculateBlockGasLimit(targetGasLimit, BigInteger.ZERO, minGasLimit, targetGasLimit, true); Assert.assertTrue(newGasLimit.compareTo(targetGasLimit) == 0); Assert.assertTrue(validByConsensus(newGasLimit, targetGasLimit)); @@ -113,7 +113,7 @@ public void NextBlockGasLimitRemainsTheSame() { public void NextBlockGasLimitIsIncreasedByMaximumValue() { GasLimitCalculator calc = new GasLimitCalculator(RskSystemProperties.CONFIG); BigInteger minGasLimit = BigInteger.valueOf(constants.getMinGasLimit()); - BigInteger targetGasLimit = BigInteger.valueOf(constants.getTargetGasLimit()); + BigInteger targetGasLimit = BigInteger.valueOf(RskSystemProperties.CONFIG.getTargetGasLimit()); BigInteger newGasLimit = calc.calculateBlockGasLimit(minGasLimit, BigInteger.ZERO, minGasLimit, targetGasLimit, true); BigInteger newGasLimit2 = calc.calculateBlockGasLimit(minGasLimit, minGasLimit, minGasLimit, targetGasLimit, true); Assert.assertTrue(newGasLimit.compareTo(newGasLimit2) == 0); @@ -167,7 +167,7 @@ public void NextBlockGasLimitIsDecreasedToMinimum() { public void NextBlockGasLimitIsDecreasedByMaximumValue() { GasLimitCalculator calc = new GasLimitCalculator(RskSystemProperties.CONFIG); BigInteger minGasLimit = BigInteger.valueOf(constants.getMinGasLimit()); - BigInteger targetGasLimit = BigInteger.valueOf(constants.getTargetGasLimit()); + BigInteger targetGasLimit = BigInteger.valueOf(RskSystemProperties.CONFIG.getTargetGasLimit()); BigInteger usedGas = targetGasLimit.multiply(BigInteger.valueOf(2)); BigInteger newGasLimit = calc.calculateBlockGasLimit(usedGas, BigInteger.ZERO, minGasLimit, targetGasLimit, true); BigInteger newGasLimit2 = calc.calculateBlockGasLimit(usedGas, usedGas, minGasLimit, targetGasLimit, true); diff --git a/rskj-core/src/test/java/co/rsk/mine/MainNetMinerTest.java b/rskj-core/src/test/java/co/rsk/mine/MainNetMinerTest.java index 53ee9d3629d..774e881b2b1 100644 --- a/rskj-core/src/test/java/co/rsk/mine/MainNetMinerTest.java +++ b/rskj-core/src/test/java/co/rsk/mine/MainNetMinerTest.java @@ -11,7 +11,9 @@ import co.rsk.test.builders.BlockChainBuilder; import co.rsk.validators.BlockUnclesValidationRule; import co.rsk.validators.ProofOfWorkRule; +import org.ethereum.config.BlockchainConfig; import org.ethereum.config.BlockchainNetConfig; +import org.ethereum.config.Constants; import org.ethereum.config.blockchain.FallbackMainNetConfig; import org.ethereum.core.Block; import org.ethereum.core.Genesis; @@ -20,6 +22,7 @@ import org.ethereum.facade.EthereumImpl; import org.ethereum.rpc.TypeConverter; import org.junit.*; +import org.junit.rules.TemporaryFolder; import org.mockito.Mockito; import java.io.File; @@ -35,6 +38,8 @@ public class MainNetMinerTest { private BlockChainImpl blockchain; public static DifficultyCalculator DIFFICULTY_CALCULATOR ; + @Rule + public TemporaryFolder folder = new TemporaryFolder(); @Before public void setup() { @@ -104,18 +109,53 @@ public static void saveToFile(byte[] array, File f) { } @Test - @Ignore("This doesn't run in multiple platforms") - public void generateFallbackMinedBlock() throws InterruptedException { + 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)); - String path = RskSystemProperties.CONFIG.fallbackMiningKeysDir(); - byte[] privKey0 = privateMiningKey0.getPrivKeyBytes(); - saveToFile(privKey0,new File(path ,"privkey0.bin")); + saveToFile(privKey0, new File(folder.getRoot().getCanonicalPath(), "privkey0.bin")); byte[] privKey1 = privateMiningKey1.getPrivKeyBytes(); - saveToFile(privKey1,new File(path ,"privkey1.bin")); + saveToFile(privKey1, new File(folder.getRoot().getCanonicalPath(), "privkey1.bin")); + + RskSystemProperties tempConfig = new RskSystemProperties() { + + BlockchainNetConfig blockchainNetConfig = RskSystemProperties.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); @@ -127,7 +167,8 @@ public void generateFallbackMinedBlock() throws InterruptedException { blockchain.getPendingState(), blockchain.getRepository(), ConfigUtils.getDefaultMiningConfig(), unclesValidationRule, null, DIFFICULTY_CALCULATOR, new GasLimitCalculator(RskSystemProperties.CONFIG), - new ProofOfWorkRule(RskSystemProperties.CONFIG).setFallbackMiningEnabled(true)); + new ProofOfWorkRule(tempConfig).setFallbackMiningEnabled(true), + tempConfig); try { minerServer.setFallbackMining(true); diff --git a/rskj-core/src/test/java/co/rsk/vm/MinerHelper.java b/rskj-core/src/test/java/co/rsk/vm/MinerHelper.java index 757664b24a0..e07796169e9 100644 --- a/rskj-core/src/test/java/co/rsk/vm/MinerHelper.java +++ b/rskj-core/src/test/java/co/rsk/vm/MinerHelper.java @@ -145,7 +145,7 @@ public void completeBlock(Block newBlock, Block parent) { newBlock.getHeader().setLogsBloom(logBloom.getData()); BigInteger minGasLimit = BigInteger.valueOf(RskSystemProperties.CONFIG.getBlockchainConfig().getCommonConstants().getMinGasLimit()); - BigInteger targetGasLimit = BigInteger.valueOf(RskSystemProperties.CONFIG.getBlockchainConfig().getCommonConstants().getTargetGasLimit()); + BigInteger targetGasLimit = BigInteger.valueOf(RskSystemProperties.CONFIG.getTargetGasLimit()); BigInteger parentGasLimit = new BigInteger(1, parent.getGasLimit()); BigInteger gasLimit = gasLimitCalculator.calculateBlockGasLimit(parentGasLimit, BigInteger.valueOf(totalGasUsed), minGasLimit, targetGasLimit, false);