use of co.rsk.core.BlockDifficulty in project rskj by rsksmart.
the class DownloadingBackwardsBodiesSyncStateTest method onEnter_connectGenesis.
@Test
public void onEnter_connectGenesis() {
List<BlockHeader> toRequest = new LinkedList<>();
DownloadingBackwardsBodiesSyncState target = new DownloadingBackwardsBodiesSyncState(syncConfiguration, syncEventsHandler, peersInformation, genesis, blockFactory, blockStore, child, toRequest, peer);
Keccak256 childHash = new Keccak256(new byte[32]);
when(child.getHash()).thenReturn(childHash);
when(child.getNumber()).thenReturn(1L);
when(genesis.isParentOf(child)).thenReturn(true);
when(child.getCumulativeDifficulty()).thenReturn(new BlockDifficulty(BigInteger.valueOf(50)));
BlockDifficulty cumulativeDifficulty = new BlockDifficulty(BigInteger.valueOf(50));
when(genesis.getCumulativeDifficulty()).thenReturn(cumulativeDifficulty);
when(blockStore.getTotalDifficultyForHash(eq(childHash.getBytes()))).thenReturn(new BlockDifficulty(BigInteger.valueOf(100)));
target.onEnter();
verify(blockStore).saveBlock(eq(genesis), eq(cumulativeDifficulty), eq(true));
verify(blockStore).flush();
verify(syncEventsHandler).stopSyncing();
}
use of co.rsk.core.BlockDifficulty in project rskj by rsksmart.
the class DownloadingBackwardsBodiesSyncStateTest method connectingUntilGenesis.
@Test
public void connectingUntilGenesis() {
LinkedList<BlockHeader> toRequest = new LinkedList<>();
LinkedList<BodyResponseMessage> responses = new LinkedList<>();
LinkedList<Block> expectedBlocks = new LinkedList<>();
Function<Long, BlockDifficulty> difficultyForBlockNumber = (n) -> new BlockDifficulty(BigInteger.valueOf(n * (n + 1) / 2));
// their indexes and each one is the children of the previous block.
for (long i = 1; i <= 10; i++) {
BlockHeader headerToRequest = mock(BlockHeader.class);
when(headerToRequest.getNumber()).thenReturn(i);
Keccak256 headerHash = new Keccak256(ByteUtil.leftPadBytes(ByteUtil.longToBytes(i), 32));
when(headerToRequest.getHash()).thenReturn(headerHash);
toRequest.addFirst(headerToRequest);
when(syncEventsHandler.sendBodyRequest(any(), eq(headerToRequest))).thenReturn(i);
BodyResponseMessage response = new BodyResponseMessage(i, new LinkedList<>(), new LinkedList<>());
responses.addFirst(response);
Block block = mock(Block.class);
expectedBlocks.addFirst(block);
when(block.getNumber()).thenReturn(i);
when(block.getHash()).thenReturn(headerHash);
when(blockFactory.newBlock(headerToRequest, response.getTransactions(), response.getUncles())).thenReturn(block);
when(block.isParentOf(any())).thenReturn(true);
when(blockStore.getTotalDifficultyForHash(headerHash.getBytes())).thenReturn(difficultyForBlockNumber.apply(i));
when(block.getCumulativeDifficulty()).thenReturn(new BlockDifficulty(BigInteger.valueOf(i)));
}
when(genesis.isParentOf(expectedBlocks.getLast())).thenReturn(true);
when(genesis.getCumulativeDifficulty()).thenReturn(new BlockDifficulty(BigInteger.valueOf(0L)));
Keccak256 childHash = new Keccak256(ByteUtil.leftPadBytes(ByteUtil.intToBytes(11), 32));
when(child.getHash()).thenReturn(childHash);
when(blockStore.getTotalDifficultyForHash(childHash.getBytes())).thenReturn(difficultyForBlockNumber.apply(11L));
when(child.getCumulativeDifficulty()).thenReturn(new BlockDifficulty(BigInteger.valueOf(11L)));
when(child.getNumber()).thenReturn(11L);
DownloadingBackwardsBodiesSyncState target = new DownloadingBackwardsBodiesSyncState(syncConfiguration, syncEventsHandler, peersInformation, genesis, blockFactory, blockStore, child, toRequest, peer);
while (!responses.isEmpty()) {
target.onEnter();
target.newBody(responses.pop(), mock(Peer.class));
Block block = expectedBlocks.pop();
BlockDifficulty expectedDifficulty = difficultyForBlockNumber.apply(block.getNumber());
verify(blockStore).saveBlock(eq(block), eq(expectedDifficulty), eq(true));
}
}
use of co.rsk.core.BlockDifficulty in project rskj by rsksmart.
the class RemascTestRunner method createBlock.
public static Block createBlock(Block genesis, Block parentBlock, Keccak256 blockHash, RskAddress coinbase, List<BlockHeader> uncles, Long difficulty, Transaction... txsToInlcude) {
List<Transaction> txs = new ArrayList<>();
if (txsToInlcude != null) {
for (Transaction tx : txsToInlcude) {
txs.add(tx);
}
}
Transaction remascTx = new RemascTransaction(parentBlock.getNumber() + 1);
txs.add(remascTx);
BigInteger difficultyAsBI = difficulty == null ? parentBlock.getDifficulty().asBigInteger() : BigInteger.valueOf(difficulty);
if (difficultyAsBI.equals(BigInteger.ZERO)) {
difficultyAsBI = BigInteger.ONE;
}
BlockDifficulty difficultyAsBD = new BlockDifficulty(difficultyAsBI);
Coin paidFees = Coin.ZERO;
for (Transaction tx : txs) {
BigInteger gasLimit = new BigInteger(1, tx.getGasLimit());
Coin gasPrice = tx.getGasPrice();
paidFees = paidFees.add(gasPrice.multiply(gasLimit));
}
return new Block(new HardcodedHashBlockHeader(parentBlock, coinbase, genesis, txs, difficultyAsBD, paidFees, uncles, blockHash), txs, uncles, true, false);
}
use of co.rsk.core.BlockDifficulty in project rskj by rsksmart.
the class MainNetMinerTest method submitBitcoinBlockProofOfWorkNotGoodEnough.
/*
* This test is probabilistic, but it has a really high chance to pass. We will generate
* a random block that it is unlikely to pass the Long.MAX_VALUE difficulty, though
* it may happen once. Twice would be suspicious.
*/
@Test
public void submitBitcoinBlockProofOfWorkNotGoodEnough() {
/* We need a low target */
BlockChainBuilder blockChainBuilder = new BlockChainBuilder();
BlockChainImpl blockchain = blockChainBuilder.build();
Genesis gen = (Genesis) BlockChainImplTest.getGenesisBlock(blockChainBuilder.getTrieStore());
gen.getHeader().setDifficulty(new BlockDifficulty(BigInteger.valueOf(Long.MAX_VALUE)));
blockchain.setStatus(gen, gen.getCumulativeDifficulty());
EthereumImpl ethereumImpl = Mockito.mock(EthereumImpl.class);
MinerClock clock = new MinerClock(true, Clock.systemUTC());
MinerServer minerServer = new MinerServerImpl(config, ethereumImpl, mainchainView, null, new ProofOfWorkRule(config).setFallbackMiningEnabled(false), blockToMineBuilder(), clock, blockFactory, new BuildInfo("cb7f28e", "master"), ConfigUtils.getDefaultMiningConfig());
try {
minerServer.start();
MinerWork work = minerServer.getWork();
co.rsk.bitcoinj.core.BtcBlock bitcoinMergedMiningBlock = getMergedMiningBlock(work);
bitcoinMergedMiningBlock.setNonce(2);
SubmitBlockResult result = minerServer.submitBitcoinBlock(work.getBlockHashForMergedMining(), bitcoinMergedMiningBlock);
Assert.assertEquals("ERROR", result.getStatus());
Assert.assertNull(result.getBlockInfo());
Mockito.verify(ethereumImpl, Mockito.times(0)).addNewMinedBlock(Mockito.any());
} finally {
minerServer.stop();
}
}
use of co.rsk.core.BlockDifficulty in project rskj by rsksmart.
the class StatusTest method createWithCompleteArguments.
@Test
public void createWithCompleteArguments() {
byte[] hash = HashUtil.randomHash();
byte[] parentHash = HashUtil.randomHash();
Status status = new Status(42, hash, parentHash, new BlockDifficulty(BigInteger.TEN));
Assert.assertEquals(42, status.getBestBlockNumber());
Assert.assertNotNull(status.getBestBlockHash());
Assert.assertArrayEquals(hash, status.getBestBlockHash());
Assert.assertNotNull(status.getBestBlockParentHash());
Assert.assertArrayEquals(parentHash, status.getBestBlockParentHash());
Assert.assertNotNull(status.getTotalDifficulty());
Assert.assertEquals(new BlockDifficulty(BigInteger.TEN), status.getTotalDifficulty());
}
Aggregations