use of co.rsk.crypto.Keccak256 in project rskj by rsksmart.
the class NodeBlockProcessorTest method processStatusHavingBestBlockAsBestBlockInBlockchain.
@Test
@Ignore("Ignored when Process status deleted on block processor")
public void processStatusHavingBestBlockAsBestBlockInBlockchain() throws UnknownHostException {
final BlockStore store = new BlockStore();
final Blockchain blockchain = BlockChainBuilder.ofSize(2);
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();
final Block block = blockchain.getBestBlock();
final Keccak256 blockHash = block.getHash();
// final Status status = new Status(block.getNumber(), block.getHash());
// processor.processStatus(sender, status);
Assert.assertTrue(processor.getNodeInformation().getNodesByBlock(block.getHash().getBytes()).size() == 1);
Assert.assertTrue(nodeInformation.getBlocksByNode(sender.getPeerNodeID()).contains(blockHash));
Assert.assertEquals(0, sender.getGetBlockMessages().size());
Assert.assertEquals(0, store.size());
}
use of co.rsk.crypto.Keccak256 in project rskj by rsksmart.
the class NodeMessageHandlerTest method processNewBlockHashesMessage.
@Test
public void processNewBlockHashesMessage() throws UnknownHostException {
final World world = new World();
final Blockchain blockchain = world.getBlockChain();
final BlockStore store = new BlockStore();
final List<Block> blocks = new BlockGenerator().getBlockChain(blockchain.getBestBlock(), 15);
final List<Block> bcBlocks = blocks.subList(0, 10);
for (Block b : bcBlocks) blockchain.tryToConnect(b);
BlockNodeInformation nodeInformation = new BlockNodeInformation();
SyncConfiguration syncConfiguration = SyncConfiguration.IMMEDIATE_FOR_TESTING;
BlockSyncService blockSyncService = new BlockSyncService(config, store, blockchain, nodeInformation, syncConfiguration);
final NodeBlockProcessor bp = new NodeBlockProcessor(store, blockchain, nodeInformation, blockSyncService, syncConfiguration);
final NodeMessageHandler handler = new NodeMessageHandler(config, bp, null, null, null, null, null, new ProofOfWorkRule(config).setFallbackMiningEnabled(false));
class TestCase {
protected final NewBlockHashesMessage message;
protected final List<Block> expected;
public TestCase(@Nonnull final NewBlockHashesMessage message, final List<Block> expected) {
this.message = message;
this.expected = expected;
}
}
TestCase[] testCases = { new TestCase(new NewBlockHashesMessage(new LinkedList<>()), null), new TestCase(new NewBlockHashesMessage(Arrays.asList(new BlockIdentifier(blocks.get(5).getHash().getBytes(), blocks.get(5).getNumber()))), null), new TestCase(new NewBlockHashesMessage(Arrays.asList(new BlockIdentifier(blocks.get(11).getHash().getBytes(), blocks.get(5).getNumber()))), Arrays.asList(blocks.get(11))), new TestCase(new NewBlockHashesMessage(Arrays.asList(new BlockIdentifier(blocks.get(11).getHash().getBytes(), blocks.get(5).getNumber()), new BlockIdentifier(blocks.get(5).getHash().getBytes(), blocks.get(5).getNumber()))), Arrays.asList(blocks.get(11))), new TestCase(new NewBlockHashesMessage(Arrays.asList(new BlockIdentifier(blocks.get(11).getHash().getBytes(), blocks.get(5).getNumber()), new BlockIdentifier(blocks.get(12).getHash().getBytes(), blocks.get(5).getNumber()))), Arrays.asList(blocks.get(11), blocks.get(12))), new TestCase(new NewBlockHashesMessage(Arrays.asList(new BlockIdentifier(blocks.get(11).getHash().getBytes(), blocks.get(5).getNumber()), new BlockIdentifier(blocks.get(11).getHash().getBytes(), blocks.get(5).getNumber()))), Arrays.asList(blocks.get(11))) };
for (int i = 0; i < testCases.length; i += 1) {
final TestCase testCase = testCases[i];
final SimpleMessageChannel sender = new SimpleMessageChannel();
handler.processMessage(sender, testCase.message);
if (testCase.expected == null) {
Assert.assertTrue(sender.getMessages().isEmpty());
continue;
}
Assert.assertEquals(testCase.expected.size(), sender.getMessages().size());
Assert.assertTrue(sender.getMessages().stream().allMatch(m -> m.getMessageType() == MessageType.GET_BLOCK_MESSAGE));
List<Keccak256> msgs = sender.getMessages().stream().map(m -> (GetBlockMessage) m).map(m -> m.getBlockHash()).map(h -> new Keccak256(h)).collect(Collectors.toList());
Set<Keccak256> expected = testCase.expected.stream().map(b -> b.getHash().getBytes()).map(h -> new Keccak256(h)).collect(Collectors.toSet());
for (Keccak256 h : msgs) {
Assert.assertTrue(expected.contains(h));
}
for (Keccak256 h : expected) {
Assert.assertTrue(msgs.stream().filter(h1 -> h.equals(h1)).count() == 1);
}
}
}
use of co.rsk.crypto.Keccak256 in project rskj by rsksmart.
the class TransactionSet method addTransaction.
public void addTransaction(Transaction transaction) {
Keccak256 txhash = transaction.getHash();
if (this.transactionsByHash.containsKey(txhash)) {
return;
}
this.transactionsByHash.put(txhash, transaction);
RskAddress senderAddress = transaction.getSender();
List<Transaction> txs = this.transactionsByAddress.get(senderAddress);
if (txs == null) {
txs = new ArrayList<>();
this.transactionsByAddress.put(senderAddress, txs);
}
txs.add(transaction);
}
use of co.rsk.crypto.Keccak256 in project rskj by rsksmart.
the class IndexedBlockStore method getBlockByHash.
@Override
public synchronized Block getBlockByHash(byte[] hash) {
Block block = this.blockCache.getBlockByHash(hash);
if (block != null) {
return block;
}
byte[] blockRlp = blocks.get(hash);
if (blockRlp == null) {
return null;
}
block = new Block(blockRlp);
this.blockCache.put(new Keccak256(hash), block);
return block;
}
use of co.rsk.crypto.Keccak256 in project rskj by rsksmart.
the class TransactionNodeInformationTest method twoNodesTwoTransactions.
@Test
public void twoNodesTwoTransactions() {
final TransactionNodeInformation nodeInformation = new TransactionNodeInformation();
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.addTransactionToNode(hash1, nodeID1);
nodeInformation.addTransactionToNode(hash2, nodeID1);
nodeInformation.addTransactionToNode(hash2, nodeID2);
Set<NodeID> nodes1 = nodeInformation.getNodesByTransaction(hash1);
Set<NodeID> nodes2 = nodeInformation.getNodesByTransaction(hash2);
Assert.assertTrue(nodes1.size() == 1);
Assert.assertTrue(nodes2.size() == 2);
Assert.assertTrue(nodes1.contains(nodeID1));
Assert.assertTrue(nodes2.contains(nodeID1));
Assert.assertTrue(nodes2.contains(nodeID2));
}
Aggregations