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 StatusResolver method currentStatus.
/**
* Resolves the current status to broadcast and send to peers.
*/
public Status currentStatus() {
Status status;
if (blockStore.getMinNumber() != 0) {
status = new Status(genesis.getNumber(), genesis.getHash().getBytes(), genesis.getParentHash().getBytes(), genesis.getCumulativeDifficulty());
} else {
Block block = blockStore.getBestBlock();
BlockDifficulty totalDifficulty = blockStore.getTotalDifficultyForHash(block.getHash().getBytes());
status = new Status(block.getNumber(), block.getHash().getBytes(), block.getParentHash().getBytes(), totalDifficulty);
}
return status;
}
use of co.rsk.core.BlockDifficulty in project rskj by rsksmart.
the class NodeMessageHandlerTest method processStatusMessageUsingSyncProcessor.
@Test()
public void processStatusMessageUsingSyncProcessor() {
final SimplePeer sender = new SimplePeer();
final ChannelManager channelManager = mock(ChannelManager.class);
when(channelManager.getActivePeers()).thenReturn(Collections.singletonList(sender));
final NodeMessageHandler handler = NodeMessageHandlerUtil.createHandlerWithSyncProcessor(SyncConfiguration.IMMEDIATE_FOR_TESTING, channelManager);
BlockGenerator blockGenerator = new BlockGenerator();
final Block block = blockGenerator.createChildBlock(blockGenerator.getGenesisBlock());
final Status status = new Status(block.getNumber(), block.getHash().getBytes(), block.getParentHash().getBytes(), new BlockDifficulty(BigInteger.TEN));
final Message message = new StatusMessage(status);
handler.processMessage(sender, message);
Assert.assertFalse(sender.getMessages().isEmpty());
Assert.assertEquals(MessageType.BLOCK_HEADERS_REQUEST_MESSAGE, sender.getMessages().get(0).getMessageType());
}
use of co.rsk.core.BlockDifficulty in project rskj by rsksmart.
the class RskWireProtocolTest method activate_sendStatus.
@Test
public void activate_sendStatus() {
BlockDifficulty blockDifficulty = new BlockDifficulty(BigInteger.valueOf(20));
Status status = mock(Status.class);
when(status.getTotalDifficulty()).thenReturn(blockDifficulty);
when(statusResolver.currentStatus()).thenReturn(status);
when(genesis.getHash()).thenReturn(new Keccak256(new byte[32]));
NodeStatistics nodeStatistics = mock(NodeStatistics.class);
when(nodeStatistics.getEthOutbound()).thenReturn(mock(NodeStatistics.StatHandler.class));
when(channel.getNodeStatistics()).thenReturn(nodeStatistics);
target.activate();
verify(messageQueue, times(2)).sendMessage(any());
}
use of co.rsk.core.BlockDifficulty in project rskj by rsksmart.
the class BlockDifficultyValidationRuleTest method testDifficulty.
@Test
public void testDifficulty() {
DifficultyCalculator difficultyCalculator = new DifficultyCalculator(activationConfig, networkConstants);
BlockDifficultyRule validationRule = new BlockDifficultyRule(difficultyCalculator);
Block block = Mockito.mock(Block.class);
Block parent = Mockito.mock(Block.class);
long parentTimestamp = 0;
long blockTimeStamp = 10;
BlockDifficulty parentDifficulty = new BlockDifficulty(new BigInteger("2048"));
BlockDifficulty blockDifficulty = new BlockDifficulty(new BigInteger("2049"));
// blockDifficulty = blockDifficulty.add(AbstractConfig.getConstants().getDifficultyBoundDivisor());
Mockito.when(block.getDifficulty()).thenReturn(blockDifficulty);
BlockHeader blockHeader = getEmptyHeader(blockDifficulty, blockTimeStamp, 1);
BlockHeader parentHeader = Mockito.mock(BlockHeader.class);
Mockito.when(parentHeader.getDifficulty()).thenReturn(parentDifficulty);
Mockito.when(block.getHeader()).thenReturn(blockHeader);
Mockito.when(parent.getHeader()).thenReturn(parentHeader);
Mockito.when(parent.getDifficulty()).thenReturn(parentDifficulty);
Mockito.when(parent.getTimestamp()).thenReturn(parentTimestamp);
Assert.assertEquals(validationRule.isValid(block, parent), true);
}
Aggregations