use of co.rsk.crypto.Keccak256 in project rskj by rsksmart.
the class BlockCacheTest method putAndGetValue.
@Test
public void putAndGetValue() {
BlockCache store = getSubject();
Block block = Mockito.mock(Block.class);
store.put(new Keccak256(HASH_1), block);
assertThat(store.getBlockByHash(HASH_1), is(block));
}
use of co.rsk.crypto.Keccak256 in project rskj by rsksmart.
the class BlockNodeInformationTest method nodeEvictionPolicy.
@Test
public void nodeEvictionPolicy() {
final BlockNodeInformation nodeInformation = new BlockNodeInformation();
final Keccak256 block = createBlockHash(10);
// These nodes should contain the block when we call getBlocksByNode
for (int i = 0; i < 30; i++) {
final NodeID node = createNodeID(i);
nodeInformation.addBlockToNode(block, node);
}
Assert.assertTrue(nodeInformation.getBlocksByNode(createNodeID(10)).contains(block));
Assert.assertTrue(nodeInformation.getBlocksByNode(createNodeID(20)).contains(block));
Assert.assertFalse(nodeInformation.getBlocksByNode(createNodeID(80)).contains(block));
// Except for node 10, which is being constantly accessed.
for (int i = 30; i < 100; i++) {
final NodeID node = createNodeID(i);
nodeInformation.addBlockToNode(block, node);
nodeInformation.getBlocksByNode(createNodeID(10));
}
Assert.assertTrue(nodeInformation.getBlocksByNode(createNodeID(10)).contains(block));
Assert.assertFalse(nodeInformation.getBlocksByNode(createNodeID(20)).contains(block));
Assert.assertFalse(nodeInformation.getBlocksByNode(createNodeID(210)).contains(block));
Assert.assertTrue(nodeInformation.getBlocksByNode(createNodeID(80)).contains(block));
}
use of co.rsk.crypto.Keccak256 in project rskj by rsksmart.
the class BlockNodeInformationTest method blockEvictionPolicy.
@Test
public void blockEvictionPolicy() {
final BlockNodeInformation nodeInformation = new BlockNodeInformation();
final NodeID nodeID1 = new NodeID(new byte[] { 2 });
// Add a few blocks, without exceeding the block limit. NodeID1 should contain them all.
for (int i = 0; i < 500; i++) {
final Keccak256 hash1 = createBlockHash(i);
nodeInformation.addBlockToNode(hash1, nodeID1);
}
Assert.assertTrue(nodeInformation.getNodesByBlock(createBlockHash(15)).contains(nodeID1));
Assert.assertTrue(nodeInformation.getNodesByBlock(createBlockHash(200)).contains(nodeID1));
Assert.assertTrue(nodeInformation.getBlocksByNode(nodeID1).contains(createBlockHash(15)));
Assert.assertTrue(nodeInformation.getBlocksByNode(nodeID1).contains(createBlockHash(300)));
// Except from block 10, which is being constantly accessed.
for (int i = 500; i < 2000; i++) {
final Keccak256 hash1 = createBlockHash(i);
nodeInformation.addBlockToNode(hash1, nodeID1);
nodeInformation.getNodesByBlock(createBlockHash(10));
}
Assert.assertFalse(nodeInformation.getNodesByBlock(createBlockHash(1)).contains(nodeID1));
Assert.assertFalse(nodeInformation.getNodesByBlock(createBlockHash(700)).contains(nodeID1));
Assert.assertFalse(nodeInformation.getNodesByBlock(createBlockHash(200)).contains(nodeID1));
Assert.assertTrue(nodeInformation.getNodesByBlock(createBlockHash(1900)).contains(nodeID1));
Assert.assertTrue(nodeInformation.getNodesByBlock(createBlockHash(10)).contains(nodeID1));
Assert.assertFalse(nodeInformation.getBlocksByNode(nodeID1).contains(createBlockHash(25)));
Assert.assertFalse(nodeInformation.getBlocksByNode(nodeID1).contains(createBlockHash(70)));
Assert.assertTrue(nodeInformation.getBlocksByNode(nodeID1).contains(createBlockHash(1901)));
}
use of co.rsk.crypto.Keccak256 in project rskj by rsksmart.
the class NodeBlockProcessorTest method processBlockHashRequestMessageUsingBlockInBlockchain.
@Test
public void processBlockHashRequestMessageUsingBlockInBlockchain() throws UnknownHostException {
final Blockchain blockchain = BlockChainBuilder.ofSize(10);
final Block block = blockchain.getBlockByNumber(5);
final Keccak256 blockHash = block.getHash();
final BlockStore store = new BlockStore();
BlockNodeInformation nodeInformation = new BlockNodeInformation();
SyncConfiguration syncConfiguration = SyncConfiguration.IMMEDIATE_FOR_TESTING;
RskSystemProperties config = new RskSystemProperties();
BlockSyncService blockSyncService = new BlockSyncService(config, store, blockchain, nodeInformation, syncConfiguration);
final NodeBlockProcessor processor = new NodeBlockProcessor(store, blockchain, nodeInformation, blockSyncService, syncConfiguration);
final SimpleMessageChannel sender = new SimpleMessageChannel();
Assert.assertTrue(nodeInformation.getBlocksByNode(sender.getPeerNodeID()).isEmpty());
processor.processBlockRequest(sender, 100, block.getHash().getBytes());
Assert.assertTrue(nodeInformation.getBlocksByNode(sender.getPeerNodeID()).contains(blockHash));
Assert.assertFalse(sender.getMessages().isEmpty());
Assert.assertEquals(1, sender.getMessages().size());
final Message message = sender.getMessages().get(0);
Assert.assertEquals(MessageType.BLOCK_RESPONSE_MESSAGE, message.getMessageType());
final BlockResponseMessage bMessage = (BlockResponseMessage) message;
Assert.assertEquals(100, bMessage.getId());
Assert.assertEquals(block.getHash(), bMessage.getBlock().getHash());
}
use of co.rsk.crypto.Keccak256 in project rskj by rsksmart.
the class NodeBlockProcessorTest method processBlockHashRequestMessageUsingEmptyStore.
@Test
public void processBlockHashRequestMessageUsingEmptyStore() throws UnknownHostException {
final Block block = new BlockGenerator().getBlock(3);
final Keccak256 blockHash = block.getHash();
final BlockStore store = new BlockStore();
final Blockchain blockchain = BlockChainBuilder.ofSize(0);
BlockNodeInformation nodeInformation = new BlockNodeInformation();
SyncConfiguration syncConfiguration = SyncConfiguration.IMMEDIATE_FOR_TESTING;
RskSystemProperties config = new RskSystemProperties();
BlockSyncService blockSyncService = new BlockSyncService(config, store, blockchain, nodeInformation, syncConfiguration);
final NodeBlockProcessor processor = new NodeBlockProcessor(store, blockchain, nodeInformation, blockSyncService, syncConfiguration);
final SimpleMessageChannel sender = new SimpleMessageChannel();
Assert.assertTrue(nodeInformation.getBlocksByNode(sender.getPeerNodeID()).isEmpty());
processor.processBlockRequest(sender, 100, block.getHash().getBytes());
Assert.assertFalse(nodeInformation.getBlocksByNode(sender.getPeerNodeID()).contains(blockHash));
Assert.assertTrue(sender.getMessages().isEmpty());
}
Aggregations