use of co.rsk.crypto.Keccak256 in project rskj by rsksmart.
the class MinerUtilsTest method getAllTransactionsTest.
@Test
public void getAllTransactionsTest() {
TransactionPool transactionPool = Mockito.mock(TransactionPool.class);
Transaction tx1 = Mockito.mock(Transaction.class);
Transaction tx2 = Mockito.mock(Transaction.class);
byte[] s1 = new byte[32];
byte[] s2 = new byte[32];
s1[0] = 0;
s2[0] = 1;
Mockito.when(tx1.getHash()).thenReturn(new Keccak256(s1));
Mockito.when(tx2.getHash()).thenReturn(new Keccak256(s2));
List<Transaction> txs = new LinkedList<>();
txs.add(tx1);
txs.add(tx2);
Mockito.when(transactionPool.getPendingTransactions()).thenReturn(txs);
List<Transaction> res = new MinerUtils().getAllTransactions(transactionPool);
Assert.assertEquals(2, res.size());
}
use of co.rsk.crypto.Keccak256 in project rskj by rsksmart.
the class BlockCacheTest method repeatingValueAtEndPreventsCleanup.
@Test
public void repeatingValueAtEndPreventsCleanup() {
BlockCache store = getSubject();
store.put(new Keccak256(HASH_1), Mockito.mock(Block.class));
store.put(new Keccak256(HASH_2), Mockito.mock(Block.class));
store.put(new Keccak256(HASH_3), Mockito.mock(Block.class));
store.put(new Keccak256(HASH_4), Mockito.mock(Block.class));
store.put(new Keccak256(HASH_1), Mockito.mock(Block.class));
store.put(new Keccak256(HASH_5), Mockito.mock(Block.class));
assertThat(store.getBlockByHash(HASH_1), notNullValue());
assertThat(store.getBlockByHash(HASH_2), nullValue());
assertThat(store.getBlockByHash(HASH_3), notNullValue());
assertThat(store.getBlockByHash(HASH_4), notNullValue());
assertThat(store.getBlockByHash(HASH_5), notNullValue());
}
use of co.rsk.crypto.Keccak256 in project rskj by rsksmart.
the class BlockNodeInformationTest method getIsNotEmptyIfPresent.
@Test
public void getIsNotEmptyIfPresent() {
final BlockNodeInformation nodeInformation = new BlockNodeInformation();
final Keccak256 hash1 = createBlockHash(1);
final NodeID nodeID1 = new NodeID(new byte[] { 2 });
final Keccak256 badHash = createBlockHash(3);
final NodeID badNode = new NodeID(new byte[] { 4 });
nodeInformation.addBlockToNode(hash1, nodeID1);
Set<Keccak256> blocks = nodeInformation.getBlocksByNode(nodeID1);
Assert.assertTrue(blocks.size() == 1);
Assert.assertTrue(blocks.contains(hash1));
Assert.assertFalse(blocks.contains(badHash));
blocks = nodeInformation.getBlocksByNode(badNode);
Assert.assertTrue(blocks.size() == 0);
Set<NodeID> nodes = nodeInformation.getNodesByBlock(hash1);
Assert.assertTrue(nodes.size() == 1);
Assert.assertTrue(nodes.contains(nodeID1));
Assert.assertFalse(nodes.contains(badNode));
nodes = nodeInformation.getNodesByBlock(badHash);
Assert.assertTrue(nodes.size() == 0);
}
use of co.rsk.crypto.Keccak256 in project rskj by rsksmart.
the class BlockNodeInformationTest method twoNodesTwoBlocks.
@Test
public void twoNodesTwoBlocks() {
final BlockNodeInformation nodeInformation = new BlockNodeInformation();
final Keccak256 hash1 = createBlockHash(1);
final NodeID nodeID1 = new NodeID(new byte[] { 2 });
final Keccak256 hash2 = createBlockHash(3);
final NodeID nodeID2 = new NodeID(new byte[] { 4 });
nodeInformation.addBlockToNode(hash1, nodeID1);
nodeInformation.addBlockToNode(hash2, nodeID1);
nodeInformation.addBlockToNode(hash2, nodeID2);
Set<Keccak256> blocks1 = nodeInformation.getBlocksByNode(nodeID1);
Set<Keccak256> blocks2 = nodeInformation.getBlocksByNode(nodeID2);
Set<NodeID> nodes1 = nodeInformation.getNodesByBlock(hash1);
Set<NodeID> nodes2 = nodeInformation.getNodesByBlock(hash2);
Assert.assertTrue(blocks1.size() == 2);
Assert.assertTrue(blocks2.size() == 1);
Assert.assertTrue(nodes1.size() == 1);
Assert.assertTrue(nodes2.size() == 2);
Assert.assertTrue(blocks1.contains(hash1));
Assert.assertTrue(blocks1.contains(hash2));
Assert.assertTrue(blocks2.contains(hash2));
Assert.assertTrue(nodes1.contains(nodeID1));
Assert.assertTrue(nodes2.contains(nodeID1));
Assert.assertTrue(nodes2.contains(nodeID2));
}
use of co.rsk.crypto.Keccak256 in project rskj by rsksmart.
the class NodeBlockProcessorTest method processBlockRequestMessageUsingBlockInStore.
@Test
public void processBlockRequestMessageUsingBlockInStore() throws UnknownHostException {
final Block block = new BlockGenerator().getBlock(3);
final Keccak256 blockHash = block.getHash();
final BlockStore store = new BlockStore();
store.saveBlock(block);
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.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());
}
Aggregations