Search in sources :

Example 61 with Keccak256

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());
}
Also used : Keccak256(co.rsk.crypto.Keccak256) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 62 with Keccak256

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));
}
Also used : BtcTransaction(co.rsk.bitcoinj.core.BtcTransaction) Keccak256(co.rsk.crypto.Keccak256) TreeMap(java.util.TreeMap) Pair(org.apache.commons.lang3.tuple.Pair) Test(org.junit.Test)

Example 63 with Keccak256

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);
}
Also used : Block(org.ethereum.core.Block) Keccak256(co.rsk.crypto.Keccak256)

Example 64 with Keccak256

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;
}
Also used : Block(org.ethereum.core.Block) Keccak256(co.rsk.crypto.Keccak256)

Example 65 with Keccak256

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));
}
Also used : Block(org.ethereum.core.Block) Keccak256(co.rsk.crypto.Keccak256)

Aggregations

Keccak256 (co.rsk.crypto.Keccak256)102 Test (org.junit.Test)53 Block (org.ethereum.core.Block)40 BlockGenerator (co.rsk.blockchain.utils.BlockGenerator)15 BigInteger (java.math.BigInteger)14 RskSystemProperties (co.rsk.config.RskSystemProperties)8 SimpleMessageChannel (co.rsk.net.simples.SimpleMessageChannel)8 SyncConfiguration (co.rsk.net.sync.SyncConfiguration)8 HashMapDB (org.ethereum.datasource.HashMapDB)8 RepositoryImpl (co.rsk.db.RepositoryImpl)7 ArrayList (java.util.ArrayList)7 Blockchain (org.ethereum.core.Blockchain)7 BlockStore (org.ethereum.db.BlockStore)7 IndexedBlockStore (org.ethereum.db.IndexedBlockStore)7 RLPList (org.ethereum.util.RLPList)6 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)6 Script (co.rsk.bitcoinj.script.Script)5 Coin (co.rsk.core.Coin)5 IOException (java.io.IOException)5 BlockHeader (org.ethereum.core.BlockHeader)5