Skip to content

Commit

Permalink
Merge pull request #398 from rsksmart/fix_gas_limit
Browse files Browse the repository at this point in the history
Fix gas limit
  • Loading branch information
martinmedina authored Jan 4, 2018
2 parents ba2d72e + 7a4cf4e commit 987c3e4
Show file tree
Hide file tree
Showing 7 changed files with 81 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -364,7 +364,7 @@ public long getBlocksForPeers() {

public long getTargetGasLimit() {
return getLongProperty("targetgaslimit",
getBlockchainConfig().getCommonConstants().getTargetGasLimit());
6_800_000L);
}

public boolean getForceTargetGasLimit() {
Expand Down
26 changes: 21 additions & 5 deletions rskj-core/src/main/java/co/rsk/mine/MinerServerImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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;
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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"));
Expand Down Expand Up @@ -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);
Expand Down
5 changes: 0 additions & 5 deletions rskj-core/src/main/java/org/ethereum/config/Constants.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()
)
);
Expand Down
18 changes: 9 additions & 9 deletions rskj-core/src/test/java/co/rsk/mine/GasLimitCalculatorTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand All @@ -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);
Expand All @@ -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);

Expand All @@ -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);

Expand All @@ -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);
Expand All @@ -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));
Expand All @@ -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);
Expand Down Expand Up @@ -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);
Expand Down
55 changes: 48 additions & 7 deletions rskj-core/src/test/java/co/rsk/mine/MainNetMinerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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() {
Expand Down Expand Up @@ -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);
Expand All @@ -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);

Expand Down
2 changes: 1 addition & 1 deletion rskj-core/src/test/java/co/rsk/vm/MinerHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down

0 comments on commit 987c3e4

Please sign in to comment.