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;
}
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);
}
}
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;
}
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);
}
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;
}
Aggregations