use of co.rsk.core.BlockDifficulty in project rskj by rsksmart.
the class BlockGenerator method createChildBlock.
public Block createChildBlock(Block parent, List<Transaction> txs, List<BlockHeader> uncles, long difficulty, BigInteger minGasPrice, byte[] gasLimit, RskAddress coinbase) {
if (txs == null) {
txs = new ArrayList<>();
}
if (uncles == null) {
uncles = new ArrayList<>();
}
byte[] unclesListHash = HashUtil.keccak256(BlockHeader.getUnclesEncodedEx(uncles));
byte[] miningForkDetectionData = parent.getNumber() + 1 > MiningConfig.REQUIRED_NUMBER_OF_BLOCKS_FOR_FORK_DETECTION_CALCULATION ? new byte[12] : new byte[0];
long blockNumber = parent.getNumber() + 1;
byte[] ummRoot = activationConfig.isActive(ConsensusRule.RSKIPUMM, blockNumber) ? new byte[0] : null;
Coin coinMinGasPrice = (minGasPrice != null) ? new Coin(minGasPrice) : null;
BlockHeader newHeader = blockFactory.getBlockHeaderBuilder().setParentHash(parent.getHash().getBytes()).setUnclesHash(unclesListHash).setCoinbase(coinbase).setLogsBloom(ByteUtils.clone(new Bloom().getData())).setDifficulty(BlockDifficulty.ONE).setNumber(parent.getNumber() + 1).setGasLimit(gasLimit).setGasUsed(0).setTimestamp(parent.getTimestamp() + ++count).setMergedMiningForkDetectionData(miningForkDetectionData).setMinimumGasPrice(coinMinGasPrice).setUncleCount(uncles.size()).setUmmRoot(ummRoot).build();
if (difficulty == 0) {
newHeader.setDifficulty(difficultyCalculator.calcDifficulty(newHeader, parent.getHeader()));
} else {
newHeader.setDifficulty(new BlockDifficulty(BigInteger.valueOf(difficulty)));
}
boolean isRskip126Enabled = activationConfig.isActive(ConsensusRule.RSKIP126, 0);
newHeader.setTransactionsRoot(BlockHashesHelper.getTxTrieRoot(txs, isRskip126Enabled));
newHeader.setStateRoot(ByteUtils.clone(parent.getStateRoot()));
return blockFactory.newBlock(newHeader, txs, uncles, false);
}
use of co.rsk.core.BlockDifficulty in project rskj by rsksmart.
the class AbstractConfig method calcDifficultyWithTimeStamps.
public static BlockDifficulty calcDifficultyWithTimeStamps(long curBlockTS, long parentBlockTS, BlockDifficulty pd, int uncleCount, int duration, BigInteger difDivisor, BlockDifficulty minDif) {
long delta = curBlockTS - parentBlockTS;
if (delta < 0) {
return pd;
}
int calcDur = (1 + uncleCount) * duration;
int sign = 0;
if (calcDur > delta) {
sign = 1;
}
if (calcDur < delta) {
sign = -1;
}
if (sign == 0) {
return pd;
}
BigInteger pdValue = pd.asBigInteger();
BigInteger quotient = pdValue.divide(difDivisor);
BigInteger fromParent;
if (sign == 1) {
fromParent = pdValue.add(quotient);
} else {
fromParent = pdValue.subtract(quotient);
}
// Note that we have to apply max() first in case fromParent ended up being negative.
return new BlockDifficulty(max(minDif.asBigInteger(), fromParent));
}
use of co.rsk.core.BlockDifficulty in project rskj by rsksmart.
the class AbstractConfig method calcDifficultyFortConstants.
public static BlockDifficulty calcDifficultyFortConstants(Constants constants, long curBlockTS, long parentBlockTS, BlockDifficulty pd, int uncleCount) {
int duration = constants.getDurationLimit();
BigInteger difDivisor = constants.getDifficultyBoundDivisor();
BlockDifficulty minDif = constants.getMinimumDifficulty();
return calcDifficultyWithTimeStamps(curBlockTS, parentBlockTS, pd, uncleCount, duration, difDivisor, minDif);
}
use of co.rsk.core.BlockDifficulty in project rskj by rsksmart.
the class BridgeTest method callUpdateCollectionsWithTransactionsWaitingForConfirmationWithEnoughConfirmations.
@Test
public void callUpdateCollectionsWithTransactionsWaitingForConfirmationWithEnoughConfirmations() throws IOException {
BtcTransaction tx1 = createTransaction();
BtcTransaction tx2 = createTransaction();
BtcTransaction tx3 = createTransaction();
Repository repository = new RepositoryImpl(config);
Repository track = repository.startTracking();
BridgeStorageProvider provider0 = new BridgeStorageProvider(track, PrecompiledContracts.BRIDGE_ADDR, config.getBlockchainConfig().getCommonConstants().getBridgeConstants());
provider0.getReleaseTransactionSet().add(tx1, 1L);
provider0.getReleaseTransactionSet().add(tx2, 2L);
provider0.getReleaseTransactionSet().add(tx3, 3L);
provider0.save();
track.commit();
track = repository.startTracking();
World world = new World();
List<Block> blocks = new BlockGenerator().getSimpleBlockChain(world.getBlockChain().getBestBlock(), 10);
Transaction rskTx = Transaction.create(config, PrecompiledContracts.BRIDGE_ADDR_STR, AMOUNT, NONCE, GAS_PRICE, GAS_LIMIT, DATA);
rskTx.sign(new ECKey().getPrivKeyBytes());
world.getBlockChain().getBlockStore().saveBlock(blocks.get(1), new BlockDifficulty(BigInteger.ONE), true);
Bridge bridge = new Bridge(config, PrecompiledContracts.BRIDGE_ADDR);
bridge.init(rskTx, blocks.get(9), track, world.getBlockChain().getBlockStore(), null, new LinkedList<>());
bridge.execute(Bridge.UPDATE_COLLECTIONS.encode());
track.commit();
BridgeStorageProvider provider = new BridgeStorageProvider(repository, PrecompiledContracts.BRIDGE_ADDR, config.getBlockchainConfig().getCommonConstants().getBridgeConstants());
Assert.assertEquals(2, provider.getReleaseTransactionSet().getEntries().size());
Assert.assertEquals(1, provider.getRskTxsWaitingForSignatures().size());
}
use of co.rsk.core.BlockDifficulty in project rskj by rsksmart.
the class BlockValidatorTest method invalidChildBlockBadDifficulty.
@Test
public void invalidChildBlockBadDifficulty() {
Block genesis = new BlockGenerator().getGenesisBlock();
Block block = new BlockGenerator().createChildBlock(genesis);
block.getHeader().setDifficulty(new BlockDifficulty(new byte[] { 0x00 }));
BlockValidatorImpl validator = new BlockValidatorBuilder().addDifficultyRule().blockStore(new SimpleBlockStore(block)).build();
// If the parent difficulty is zero, the child difficulty will always be zero
// because the child difficulty is always the parent diff multiplied by a factor.
// However, the calcDifficulty will put the minimum configured difficulty, so that the child
// difficulty can never be zero.
Assert.assertFalse(validator.isValid(block));
}
Aggregations