Search in sources :

Example 11 with ProofOfWorkRule

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));
}
Also used : SimpleTransactionPool(co.rsk.net.simples.SimpleTransactionPool) TxHandler(co.rsk.net.handler.TxHandler) SimpleMessageChannel(co.rsk.net.simples.SimpleMessageChannel) ProofOfWorkRule(co.rsk.validators.ProofOfWorkRule) PeerScoring(co.rsk.scoring.PeerScoring) PeerScoringManager(co.rsk.scoring.PeerScoringManager) SimpleChannelManager(org.ethereum.rpc.Simples.SimpleChannelManager) SimpleBlockProcessor(co.rsk.net.simples.SimpleBlockProcessor) Test(org.junit.Test)

Example 12 with ProofOfWorkRule

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);
}
Also used : PeerScoringManager(co.rsk.scoring.PeerScoringManager) ProofOfWorkRule(co.rsk.validators.ProofOfWorkRule)

Example 13 with ProofOfWorkRule

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());
}
Also used : BlockGenerator(co.rsk.blockchain.utils.BlockGenerator) ProofOfWorkRule(co.rsk.validators.ProofOfWorkRule) Coin(co.rsk.core.Coin) BlockExecutor(co.rsk.core.bc.BlockExecutor) DownloadingBodiesSyncState(co.rsk.net.sync.DownloadingBodiesSyncState) SimpleMessageChannel(co.rsk.net.simples.SimpleMessageChannel) RskSystemProperties(co.rsk.config.RskSystemProperties) Test(org.junit.Test)

Example 14 with ProofOfWorkRule

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());
}
Also used : SimpleChannelManager(org.ethereum.rpc.Simples.SimpleChannelManager) ChannelManager(org.ethereum.net.server.ChannelManager) SimpleMessageChannel(co.rsk.net.simples.SimpleMessageChannel) InvocationOnMock(org.mockito.invocation.InvocationOnMock) SimpleMessageChannel(co.rsk.net.simples.SimpleMessageChannel) Channel(org.ethereum.net.server.Channel) ProofOfWorkRule(co.rsk.validators.ProofOfWorkRule) Test(org.junit.Test)

Example 15 with ProofOfWorkRule

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());
}
Also used : SimpleMessageChannel(co.rsk.net.simples.SimpleMessageChannel) BlockGenerator(co.rsk.blockchain.utils.BlockGenerator) RskSystemProperties(co.rsk.config.RskSystemProperties) ProofOfWorkRule(co.rsk.validators.ProofOfWorkRule) Test(org.junit.Test)

Aggregations

ProofOfWorkRule (co.rsk.validators.ProofOfWorkRule)61 Test (org.junit.Test)57 SimpleMessageChannel (co.rsk.net.simples.SimpleMessageChannel)40 RskSystemProperties (co.rsk.config.RskSystemProperties)21 BlockGenerator (co.rsk.blockchain.utils.BlockGenerator)16 SyncConfiguration (co.rsk.net.sync.SyncConfiguration)16 SimpleBlockProcessor (co.rsk.net.simples.SimpleBlockProcessor)14 EthereumImpl (org.ethereum.facade.EthereumImpl)14 SimpleChannelManager (org.ethereum.rpc.Simples.SimpleChannelManager)13 World (co.rsk.test.World)11 BlockUnclesValidationRule (co.rsk.validators.BlockUnclesValidationRule)11 PeerScoringManager (co.rsk.scoring.PeerScoringManager)9 ChannelManager (org.ethereum.net.server.ChannelManager)9 Channel (org.ethereum.net.server.Channel)8 InvocationOnMock (org.mockito.invocation.InvocationOnMock)8 TxHandler (co.rsk.net.handler.TxHandler)7 PeerScoring (co.rsk.scoring.PeerScoring)7 BlockDifficulty (co.rsk.core.BlockDifficulty)6 SimpleTransactionPool (co.rsk.net.simples.SimpleTransactionPool)5 DownloadingBodiesSyncState (co.rsk.net.sync.DownloadingBodiesSyncState)4