Search in sources :

Example 56 with BlockDifficulty

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));
}
Also used : IndexedBlockStore(org.ethereum.db.IndexedBlockStore) BlockStore(org.ethereum.db.BlockStore) IndexedBlockStore(org.ethereum.db.IndexedBlockStore) ArrayList(java.util.ArrayList) HashMapDB(org.ethereum.datasource.HashMapDB) BlockGenerator(co.rsk.blockchain.utils.BlockGenerator) BlockDifficulty(co.rsk.core.BlockDifficulty) Block(org.ethereum.core.Block) HashMapBlocksIndex(co.rsk.db.HashMapBlocksIndex) BlockHeader(org.ethereum.core.BlockHeader) Test(org.junit.Test)

Example 57 with BlockDifficulty

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));
}
Also used : BlockDifficulty(co.rsk.core.BlockDifficulty) BigInteger(java.math.BigInteger)

Example 58 with BlockDifficulty

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);
}
Also used : BlockDifficulty(co.rsk.core.BlockDifficulty) BigInteger(java.math.BigInteger)

Example 59 with BlockDifficulty

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());
}
Also used : SimpleBtcTransaction(co.rsk.peg.bitcoin.SimpleBtcTransaction) ECKey(org.ethereum.crypto.ECKey) World(co.rsk.test.World) BlockGenerator(co.rsk.blockchain.utils.BlockGenerator) BlockDifficulty(co.rsk.core.BlockDifficulty) SimpleBtcTransaction(co.rsk.peg.bitcoin.SimpleBtcTransaction) RepositoryImpl(co.rsk.db.RepositoryImpl) Test(org.junit.Test)

Example 60 with BlockDifficulty

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));
}
Also used : BlockDifficulty(co.rsk.core.BlockDifficulty) SimpleBlock(co.rsk.peg.simples.SimpleBlock) BlockGenerator(co.rsk.blockchain.utils.BlockGenerator) Test(org.junit.Test)

Aggregations

BlockDifficulty (co.rsk.core.BlockDifficulty)60 Test (org.junit.Test)32 Block (org.ethereum.core.Block)23 BigInteger (java.math.BigInteger)14 HashMapDB (org.ethereum.datasource.HashMapDB)11 Keccak256 (co.rsk.crypto.Keccak256)9 Ignore (org.junit.Ignore)9 BlockGenerator (co.rsk.blockchain.utils.BlockGenerator)8 BlockHeader (org.ethereum.core.BlockHeader)7 ActivationConfigsForTest (org.ethereum.config.blockchain.upgrades.ActivationConfigsForTest)6 KeyValueDataSource (org.ethereum.datasource.KeyValueDataSource)6 Coin (co.rsk.core.Coin)5 MapDBBlocksIndex (co.rsk.db.MapDBBlocksIndex)5 LevelDbDataSource (org.ethereum.datasource.LevelDbDataSource)5 BlockStore (org.ethereum.db.BlockStore)5 DifficultyCalculator (co.rsk.core.DifficultyCalculator)4 BlockChainImpl (co.rsk.core.bc.BlockChainImpl)4 HashMapBlocksIndex (co.rsk.db.HashMapBlocksIndex)4 BlockChainBuilder (co.rsk.test.builders.BlockChainBuilder)4 ArrayList (java.util.ArrayList)4