use of co.rsk.core.BlockDifficulty in project rskj by rsksmart.
the class BlockUnclesValidationRuleTest method rejectBlockWithUncleHavingHigherNumber.
@Test
public void rejectBlockWithUncleHavingHigherNumber() {
BlockGenerator blockGenerator = new BlockGenerator();
Block genesis = blockGenerator.getGenesisBlock();
Block block1 = blockGenerator.createChildBlock(genesis);
Block uncle1 = blockGenerator.createChildBlock(block1);
Block uncle2 = blockGenerator.createChildBlock(uncle1);
List<BlockHeader> uncles = new ArrayList<>();
uncles.add(uncle2.getHeader());
Block block = blockGenerator.createChildBlock(block1, null, uncles, 1, null);
BlockStore blockStore = new IndexedBlockStore(null, new HashMapDB(), new HashMapBlocksIndex());
blockStore.saveBlock(genesis, new BlockDifficulty(BigInteger.valueOf(1)), true);
blockStore.saveBlock(block1, new BlockDifficulty(BigInteger.valueOf(2)), true);
BlockUnclesValidationRule rule = new BlockUnclesValidationRule(blockStore, 10, 10, new BlockHeaderCompositeRule(), new BlockHeaderParentCompositeRule());
Assert.assertFalse(rule.isValid(block));
}
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