Search in sources :

Example 31 with BlockDifficulty

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

Example 32 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 33 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 34 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 35 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