Search in sources :

Example 1 with BlockDifficulty

use of co.rsk.core.BlockDifficulty in project rskj by rsksmart.

the class ImportLightTest method createBlockchain.

public static BlockChainImpl createBlockchain(Genesis genesis) {
    RskSystemProperties config = new RskSystemProperties();
    config.setBlockchainConfig(new GenesisConfig(new GenesisConfig.GenesisConstants() {

        @Override
        public BlockDifficulty getMinimumDifficulty() {
            return new BlockDifficulty(BigInteger.ONE);
        }
    }));
    IndexedBlockStore blockStore = new IndexedBlockStore(new HashMap<>(), new HashMapDB(), null);
    Repository repository = new RepositoryImpl(config, new TrieStoreImpl(new HashMapDB()));
    EthereumListenerAdapter listener = new EthereumListenerAdapter();
    KeyValueDataSource ds = new HashMapDB();
    ds.init();
    ReceiptStore receiptStore = new ReceiptStoreImpl(ds);
    BlockChainImpl blockchain = new BlockChainImpl(config, repository, blockStore, receiptStore, null, listener, new AdminInfo(), new DummyBlockValidator());
    blockchain.setNoValidation(true);
    TransactionPoolImpl transactionPool = new TransactionPoolImpl(config, repository, null, receiptStore, null, listener, 10, 100);
    blockchain.setTransactionPool(transactionPool);
    Repository track = repository.startTracking();
    for (RskAddress addr : genesis.getPremine().keySet()) {
        track.createAccount(addr);
        track.addBalance(addr, genesis.getPremine().get(addr).getAccountState().getBalance());
    }
    track.commit();
    genesis.setStateRoot(repository.getRoot());
    genesis.flushRLP();
    blockStore.saveBlock(genesis, genesis.getCumulativeDifficulty(), true);
    blockchain.setBestBlock(genesis);
    blockchain.setTotalDifficulty(genesis.getCumulativeDifficulty());
    return blockchain;
}
Also used : TrieStoreImpl(co.rsk.trie.TrieStoreImpl) IndexedBlockStore(org.ethereum.db.IndexedBlockStore) AdminInfo(org.ethereum.manager.AdminInfo) BlockChainImpl(co.rsk.core.bc.BlockChainImpl) HashMapDB(org.ethereum.datasource.HashMapDB) EthereumListenerAdapter(org.ethereum.listener.EthereumListenerAdapter) ReceiptStoreImpl(org.ethereum.db.ReceiptStoreImpl) DummyBlockValidator(co.rsk.validators.DummyBlockValidator) BlockDifficulty(co.rsk.core.BlockDifficulty) TransactionPoolImpl(co.rsk.core.bc.TransactionPoolImpl) RepositoryImpl(co.rsk.db.RepositoryImpl) RskAddress(co.rsk.core.RskAddress) KeyValueDataSource(org.ethereum.datasource.KeyValueDataSource) RskSystemProperties(co.rsk.config.RskSystemProperties) ReceiptStore(org.ethereum.db.ReceiptStore) GenesisConfig(org.ethereum.config.blockchain.GenesisConfig)

Example 2 with BlockDifficulty

use of co.rsk.core.BlockDifficulty in project rskj by rsksmart.

the class LocalBasicTest method runDifficultyTest.

@Test
public void runDifficultyTest() throws IOException, ParseException {
    config.setGenesisInfo("frontier.json");
    config.setBlockchainConfig(new MainNetConfig());
    String json = getJSON("difficulty");
    DifficultyTestSuite testSuite = new DifficultyTestSuite(json);
    for (DifficultyTestCase testCase : testSuite.getTestCases()) {
        logger.info("Running {}\n", testCase.getName());
        BlockHeader current = testCase.getCurrent();
        BlockHeader parent = testCase.getParent();
        BlockDifficulty calc = new DifficultyCalculator(config).calcDifficulty(current, parent);
        int c = calc.compareTo(parent.getDifficulty());
        if (c > 0)
            logger.info(" Difficulty increase test\n");
        else if (c < 0)
            logger.info(" Difficulty decrease test\n");
        else
            logger.info(" Difficulty without change test\n");
        assertEquals(testCase.getExpectedDifficulty(), calc);
    }
}
Also used : DifficultyTestCase(org.ethereum.jsontestsuite.DifficultyTestCase) BlockDifficulty(co.rsk.core.BlockDifficulty) DifficultyCalculator(co.rsk.core.DifficultyCalculator) MainNetConfig(org.ethereum.config.net.MainNetConfig) BlockHeader(org.ethereum.core.BlockHeader) DifficultyTestSuite(org.ethereum.jsontestsuite.DifficultyTestSuite) Test(org.junit.Test)

Example 3 with BlockDifficulty

use of co.rsk.core.BlockDifficulty in project rskj by rsksmart.

the class BlockDifficultyRule method isValid.

@Override
public boolean isValid(Block block, Block parent) {
    if (block == null || parent == null) {
        logger.warn("BlockDifficultyRule - block or parent are null");
        return false;
    }
    BlockHeader header = block.getHeader();
    BlockDifficulty calcDifficulty = difficultyCalculator.calcDifficulty(header, parent.getHeader());
    BlockDifficulty difficulty = header.getDifficulty();
    if (!difficulty.equals(calcDifficulty)) {
        logger.warn("#{}: difficulty != calcDifficulty", header.getNumber());
        return false;
    }
    return true;
}
Also used : BlockDifficulty(co.rsk.core.BlockDifficulty) BlockHeader(org.ethereum.core.BlockHeader)

Example 4 with BlockDifficulty

use of co.rsk.core.BlockDifficulty in project rskj by rsksmart.

the class AbstractConfig method calcDifficulty.

@Override
public BlockDifficulty calcDifficulty(BlockHeader curBlockHeader, BlockHeader parent) {
    BlockDifficulty pd = parent.getDifficulty();
    int uncleCount = curBlockHeader.getUncleCount();
    long curBlockTS = curBlockHeader.getTimestamp();
    long parentBlockTS = parent.getTimestamp();
    return calcDifficultyFortConstants(getConstants(), curBlockTS, parentBlockTS, pd, uncleCount);
}
Also used : BlockDifficulty(co.rsk.core.BlockDifficulty)

Example 5 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) {
    if (txs == null) {
        txs = new ArrayList<>();
    }
    if (uncles == null) {
        uncles = new ArrayList<>();
    }
    byte[] unclesListHash = HashUtil.keccak256(BlockHeader.getUnclesEncodedEx(uncles));
    BlockHeader newHeader = new BlockHeader(parent.getHash().getBytes(), unclesListHash, parent.getCoinbase().getBytes(), ByteUtils.clone(new Bloom().getData()), new byte[] { 1 }, parent.getNumber() + 1, gasLimit, 0, parent.getTimestamp() + ++count, new byte[] {}, new byte[] {}, new byte[] {}, new byte[] {}, (minGasPrice != null) ? minGasPrice.toByteArray() : null, CollectionUtils.size(uncles));
    if (difficulty == 0) {
        newHeader.setDifficulty(difficultyCalculator.calcDifficulty(newHeader, parent.getHeader()));
    } else {
        newHeader.setDifficulty(new BlockDifficulty(BigInteger.valueOf(difficulty)));
    }
    newHeader.setTransactionsRoot(Block.getTxTrie(txs).getHash().getBytes());
    newHeader.setStateRoot(ByteUtils.clone(parent.getStateRoot()));
    Block newBlock = new Block(newHeader, txs, uncles);
    return newBlock;
}
Also used : BlockDifficulty(co.rsk.core.BlockDifficulty) SimpleBlock(co.rsk.peg.simples.SimpleBlock)

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