use of co.rsk.crypto.Keccak256 in project rskj by rsksmart.
the class PendingFederationTest method getHash.
@PrepareForTest({ BridgeSerializationUtils.class })
@Test
public void getHash() {
PowerMockito.mockStatic(BridgeSerializationUtils.class);
PowerMockito.when(BridgeSerializationUtils.serializePendingFederation(pendingFederation)).thenReturn(new byte[] { (byte) 0xaa });
Keccak256 expectedHash = new Keccak256(HashUtil.keccak256(new byte[] { (byte) 0xaa }));
Assert.assertEquals(expectedHash, pendingFederation.getHash());
}
use of co.rsk.crypto.Keccak256 in project rskj by rsksmart.
the class StateForFederatorTest method serialize.
@Test
public void serialize() {
Keccak256 hash1 = new Keccak256(SHA3_1);
Keccak256 hash2 = new Keccak256(SHA3_2);
Keccak256 hash3 = new Keccak256(SHA3_3);
Keccak256 hash4 = new Keccak256(SHA3_4);
BtcTransaction tx1 = new BtcTransaction(NETWORK_PARAMETERS);
BtcTransaction tx2 = new BtcTransaction(NETWORK_PARAMETERS);
BtcTransaction tx3 = new BtcTransaction(NETWORK_PARAMETERS);
BtcTransaction tx4 = new BtcTransaction(NETWORK_PARAMETERS);
SortedMap<Keccak256, BtcTransaction> rskTxsWaitingForSignatures = new TreeMap<>();
rskTxsWaitingForSignatures.put(hash1, tx1);
rskTxsWaitingForSignatures.put(hash2, tx2);
SortedMap<Keccak256, Pair<BtcTransaction, Long>> rskTxsWaitingForBroadcasting = new TreeMap<>();
rskTxsWaitingForBroadcasting.put(hash3, Pair.of(tx3, 3L));
rskTxsWaitingForBroadcasting.put(hash4, Pair.of(tx4, 4L));
StateForFederator stateForFederator = new StateForFederator(rskTxsWaitingForSignatures);
byte[] encoded = stateForFederator.getEncoded();
Assert.assertTrue(encoded.length > 0);
StateForFederator reverseResult = new StateForFederator(encoded, NETWORK_PARAMETERS);
Assert.assertNotNull(reverseResult);
Assert.assertEquals(2, reverseResult.getRskTxsWaitingForSignatures().size());
Assert.assertEquals(tx1, reverseResult.getRskTxsWaitingForSignatures().get(hash1));
Assert.assertEquals(tx2, reverseResult.getRskTxsWaitingForSignatures().get(hash2));
Assert.assertTrue(checkKeys(reverseResult.getRskTxsWaitingForSignatures().keySet(), hash1, hash2));
}
use of co.rsk.crypto.Keccak256 in project rskj by rsksmart.
the class BlockStore method saveBlock.
public synchronized void saveBlock(Block block) {
Keccak256 key = block.getHash();
Keccak256 pkey = block.getParentHash();
Long nkey = Long.valueOf(block.getNumber());
this.blocks.put(key, block);
Set<Block> bsbynumber = this.blocksbynumber.get(nkey);
if (bsbynumber == null) {
bsbynumber = new HashSet<>();
this.blocksbynumber.put(nkey, bsbynumber);
}
bsbynumber.add(block);
Set<Block> bsbyphash = this.blocksbyparent.get(pkey);
if (bsbyphash == null) {
bsbyphash = new HashSet<>();
this.blocksbyparent.put(pkey, bsbyphash);
}
bsbyphash.add(block);
}
use of co.rsk.crypto.Keccak256 in project rskj by rsksmart.
the class BlockSyncService method getConnectedBlocks.
private Set<Block> getConnectedBlocks(List<Block> remainingBlocks, MessageChannel sender, Map<Keccak256, ImportResult> connectionsResult, boolean ignoreMissingHashes) {
Set<Block> connected = new HashSet<>();
for (Block block : remainingBlocks) {
logger.trace("Trying to add block {} {}", block.getNumber(), block.getShortHash());
Set<Keccak256> missingHashes = BlockUtils.unknownDirectAncestorsHashes(block, blockchain, store);
if (!missingHashes.isEmpty()) {
if (!ignoreMissingHashes) {
logger.trace("Missing hashes for block in process {} {}", block.getNumber(), block.getShortHash());
requestMissingHashes(sender, missingHashes);
}
continue;
}
connectionsResult.put(block.getHash(), blockchain.tryToConnect(block));
if (BlockUtils.blockInSomeBlockChain(block, blockchain)) {
this.store.removeBlock(block);
connected.add(block);
}
}
return connected;
}
use of co.rsk.crypto.Keccak256 in project rskj by rsksmart.
the class NodeBlockProcessor method processBlockRequest.
/**
* processBlockRequest sends a requested block to a peer if the block is available.
*
* @param sender the sender of the BlockRequest message.
* @param requestId the id of the request
* @param hash the requested block's hash.
*/
@Override
public void processBlockRequest(@Nonnull final MessageChannel sender, long requestId, @Nonnull final byte[] hash) {
logger.trace("Processing get block by hash {} {} from {}", requestId, Hex.toHexString(hash).substring(0, 10), sender.getPeerNodeID().toString());
final Block block = blockSyncService.getBlockFromStoreOrBlockchain(hash);
if (block == null) {
return;
}
nodeInformation.addBlockToNode(new Keccak256(hash), sender.getPeerNodeID());
sender.sendMessage(new BlockResponseMessage(requestId, block));
}
Aggregations