use of co.rsk.validators.ProofOfWorkRule in project rskj by rsksmart.
the class NodeMessageHandlerTest method processRejectedTransactionsMessage.
@Test
public void processRejectedTransactionsMessage() throws UnknownHostException {
PeerScoringManager scoring = createPeerScoringManager();
final SimpleChannelManager channelManager = new SimpleChannelManager();
TxHandler txmock = mock(TxHandler.class);
TransactionPool state = mock(TransactionPool.class);
BlockProcessor blockProcessor = mock(BlockProcessor.class);
Mockito.when(blockProcessor.hasBetterBlockToSync()).thenReturn(false);
final NodeMessageHandler handler = new NodeMessageHandler(config, blockProcessor, null, channelManager, state, txmock, scoring, new ProofOfWorkRule(config).setFallbackMiningEnabled(false));
final SimpleMessageChannel sender = new SimpleMessageChannel();
final List<Transaction> txs = TransactionUtils.getTransactions(0);
Mockito.when(txmock.retrieveValidTxs(any(List.class))).thenReturn(txs);
final TransactionsMessage message = new TransactionsMessage(txs);
handler.processMessage(sender, message);
Assert.assertNotNull(channelManager.getTransactions());
Assert.assertEquals(0, channelManager.getTransactions().size());
Assert.assertTrue(scoring.isEmpty());
PeerScoring pscoring = scoring.getPeerScoring(sender.getPeerNodeID());
Assert.assertNotNull(pscoring);
Assert.assertTrue(pscoring.isEmpty());
Assert.assertEquals(0, pscoring.getTotalEventCounter());
Assert.assertEquals(0, pscoring.getEventCounter(EventType.INVALID_TRANSACTION));
}
use of co.rsk.validators.ProofOfWorkRule in project rskj by rsksmart.
the class NodeMessageHandlerUtil method createHandlerWithSyncProcessor.
public static NodeMessageHandler createHandlerWithSyncProcessor(Blockchain blockchain, SyncConfiguration syncConfiguration, ChannelManager channelManager) {
final BlockStore store = new BlockStore();
BlockNodeInformation nodeInformation = new BlockNodeInformation();
BlockSyncService blockSyncService = new BlockSyncService(config, store, blockchain, nodeInformation, syncConfiguration);
NodeBlockProcessor processor = new NodeBlockProcessor(store, blockchain, nodeInformation, blockSyncService, syncConfiguration);
ProofOfWorkRule blockValidationRule = new ProofOfWorkRule(config);
PeerScoringManager peerScoringManager = mock(PeerScoringManager.class);
Mockito.when(peerScoringManager.hasGoodReputation(isA(NodeID.class))).thenReturn(true);
SyncProcessor syncProcessor = new SyncProcessor(config, blockchain, blockSyncService, peerScoringManager, channelManager, syncConfiguration, blockValidationRule, DIFFICULTY_CALCULATOR);
return new NodeMessageHandler(config, processor, syncProcessor, channelManager, null, null, null, blockValidationRule);
}
use of co.rsk.validators.ProofOfWorkRule in project rskj by rsksmart.
the class SyncProcessorTest method processBodyResponseWithTransactionAddsToBlockchain.
@Test
public void processBodyResponseWithTransactionAddsToBlockchain() {
Account senderAccount = createAccount("sender");
Account receiverAccount = createAccount("receiver");
List<Account> accounts = new ArrayList<Account>();
List<Coin> balances = new ArrayList<>();
accounts.add(senderAccount);
balances.add(Coin.valueOf(20000000));
accounts.add(receiverAccount);
balances.add(Coin.ZERO);
final BlockStore store = new BlockStore();
Blockchain blockchain = BlockChainBuilder.ofSize(0, false, accounts, balances);
Block genesis = blockchain.getBestBlock();
SimpleMessageChannel sender = new SimpleMessageChannel(new byte[] { 0x01 });
Assert.assertEquals(0, blockchain.getBestBlock().getNumber());
Transaction tx = createTransaction(senderAccount, receiverAccount, BigInteger.valueOf(1000000), BigInteger.ZERO);
List<Transaction> txs = new ArrayList<>();
txs.add(tx);
Block block = new BlockGenerator().createChildBlock(genesis, txs, blockchain.getRepository().getRoot());
BlockExecutor blockExecutor = new BlockExecutor(config, blockchain.getRepository(), null, blockchain.getBlockStore(), null);
Assert.assertEquals(1, block.getTransactionsList().size());
blockExecutor.executeAndFillAll(block, genesis);
Assert.assertEquals(21000, block.getFeesPaidToMiner().asBigInteger().intValueExact());
Assert.assertEquals(1, block.getTransactionsList().size());
Assert.assertEquals(1, block.getNumber());
Assert.assertArrayEquals(blockchain.getBestBlockHash(), block.getParentHash().getBytes());
BlockNodeInformation nodeInformation = new BlockNodeInformation();
RskSystemProperties config = new RskSystemProperties();
BlockSyncService blockSyncService = new BlockSyncService(config, store, blockchain, nodeInformation, SyncConfiguration.IMMEDIATE_FOR_TESTING);
SyncProcessor processor = new SyncProcessor(config, blockchain, blockSyncService, RskMockFactory.getPeerScoringManager(), getChannelManager(), SyncConfiguration.IMMEDIATE_FOR_TESTING, new ProofOfWorkRule(config).setFallbackMiningEnabled(false), DIFFICULTY_CALCULATOR);
processor.setSelectedPeer(sender, StatusUtils.getFakeStatus(), 0);
List<Transaction> transactions = block.getTransactionsList();
List<BlockHeader> uncles = block.getUncleList();
long lastRequestId = new Random().nextLong();
BodyResponseMessage response = new BodyResponseMessage(lastRequestId, transactions, uncles);
processor.registerExpectedMessage(response);
Deque<BlockHeader> headerStack = new ArrayDeque<>();
headerStack.add(block.getHeader());
List<Deque<BlockHeader>> headers = new ArrayList<>();
headers.add(headerStack);
List<BlockIdentifier> bids = new ArrayList<>();
bids.add(new BlockIdentifier(blockchain.getBlockByNumber(0).getHash().getBytes(), 0));
bids.add(new BlockIdentifier(block.getHash().getBytes(), 1));
processor.startDownloadingBodies(headers, Collections.singletonMap(sender.getPeerNodeID(), bids));
((DownloadingBodiesSyncState) processor.getSyncState()).expectBodyResponseFor(lastRequestId, sender.getPeerNodeID(), block.getHeader());
processor.processBodyResponse(sender, response);
Assert.assertEquals(1, blockchain.getBestBlock().getNumber());
Assert.assertArrayEquals(block.getHash().getBytes(), blockchain.getBestBlockHash());
Assert.assertTrue(processor.getExpectedResponses().isEmpty());
}
use of co.rsk.validators.ProofOfWorkRule in project rskj by rsksmart.
the class SyncProcessorTest method sendSkeletonRequest.
@Test
public void sendSkeletonRequest() {
Blockchain blockchain = BlockChainBuilder.ofSize(100);
SimpleMessageChannel sender = new SimpleMessageChannel(new byte[] { 0x01 });
final ChannelManager channelManager = mock(ChannelManager.class);
Channel channel = mock(Channel.class);
when(channel.getNodeId()).thenReturn(sender.getPeerNodeID());
when(channelManager.getActivePeers()).thenReturn(Collections.singletonList(channel));
final Message[] msg = new Message[1];
when(channelManager.sendMessageTo(eq(sender.getPeerNodeID()), any())).then((InvocationOnMock invocation) -> {
msg[0] = invocation.getArgumentAt(1, Message.class);
return true;
});
SyncProcessor processor = new SyncProcessor(config, blockchain, null, RskMockFactory.getPeerScoringManager(), channelManager, SyncConfiguration.IMMEDIATE_FOR_TESTING, new ProofOfWorkRule(config).setFallbackMiningEnabled(false), DIFFICULTY_CALCULATOR);
processor.setSelectedPeer(sender, StatusUtils.getFakeStatus(), 0);
processor.sendSkeletonRequest(sender.getPeerNodeID(), 0);
Message message = msg[0];
Assert.assertNotNull(message);
Assert.assertEquals(MessageType.SKELETON_REQUEST_MESSAGE, message.getMessageType());
SkeletonRequestMessage request = (SkeletonRequestMessage) message;
Assert.assertNotEquals(0, request.getId());
Assert.assertEquals(0, request.getStartNumber());
Assert.assertEquals(1, processor.getExpectedResponses().size());
}
use of co.rsk.validators.ProofOfWorkRule in project rskj by rsksmart.
the class SyncProcessorTest method processBlockResponseAddsToBlockchain.
@Test
public void processBlockResponseAddsToBlockchain() {
final BlockStore store = new BlockStore();
Blockchain blockchain = BlockChainBuilder.ofSize(10);
SimpleMessageChannel sender = new SimpleMessageChannel(new byte[] { 0x01 });
Assert.assertEquals(10, blockchain.getBestBlock().getNumber());
Block block = new BlockGenerator().createChildBlock(blockchain.getBlockByNumber(10));
Assert.assertEquals(11, block.getNumber());
Assert.assertArrayEquals(blockchain.getBestBlockHash(), block.getParentHash().getBytes());
BlockNodeInformation nodeInformation = new BlockNodeInformation();
RskSystemProperties config = new RskSystemProperties();
BlockSyncService blockSyncService = new BlockSyncService(config, store, blockchain, nodeInformation, SyncConfiguration.IMMEDIATE_FOR_TESTING);
SyncProcessor processor = new SyncProcessor(config, blockchain, blockSyncService, RskMockFactory.getPeerScoringManager(), getChannelManager(), SyncConfiguration.IMMEDIATE_FOR_TESTING, new ProofOfWorkRule(config).setFallbackMiningEnabled(false), DIFFICULTY_CALCULATOR);
processor.setSelectedPeer(sender, StatusUtils.getFakeStatus(), 0);
BlockResponseMessage response = new BlockResponseMessage(new Random().nextLong(), block);
processor.registerExpectedMessage(response);
processor.processBlockResponse(sender, response);
Assert.assertEquals(11, blockchain.getBestBlock().getNumber());
Assert.assertArrayEquals(block.getHash().getBytes(), blockchain.getBestBlockHash());
Assert.assertTrue(processor.getExpectedResponses().isEmpty());
}
Aggregations