Search in sources :

Example 41 with Keccak256

use of co.rsk.crypto.Keccak256 in project rskj by rsksmart.

the class NodeBlockProcessor method processGetBlock.

/**
 * processGetBlock sends a requested block to a peer if the block is available.
 *
 * @param sender the sender of the GetBlock message.
 * @param hash   the requested block's hash.
 */
@Override
public void processGetBlock(@Nonnull final MessageChannel sender, @Nonnull final byte[] hash) {
    logger.trace("Processing get block {} from {}", 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 BlockMessage(block));
}
Also used : Block(org.ethereum.core.Block) Keccak256(co.rsk.crypto.Keccak256)

Example 42 with Keccak256

use of co.rsk.crypto.Keccak256 in project rskj by rsksmart.

the class NodeMessageHandler method relayTransactions.

private void relayTransactions(@Nonnull MessageChannel sender, List<Transaction> acceptedTxs) {
    for (Transaction tx : acceptedTxs) {
        Keccak256 txHash = tx.getHash();
        transactionNodeInformation.addTransactionToNode(txHash, sender.getPeerNodeID());
        final Set<NodeID> nodesToSkip = new HashSet<>(transactionNodeInformation.getNodesByTransaction(txHash));
        final Set<NodeID> newNodes = channelManager.broadcastTransaction(tx, nodesToSkip);
        newNodes.forEach(nodeID -> transactionNodeInformation.addTransactionToNode(txHash, nodeID));
    }
}
Also used : Transaction(org.ethereum.core.Transaction) Keccak256(co.rsk.crypto.Keccak256)

Example 43 with Keccak256

use of co.rsk.crypto.Keccak256 in project rskj by rsksmart.

the class NodeMessageHandler method tryAddMessage.

private void tryAddMessage(MessageChannel sender, Message message) {
    Keccak256 encodedMessage = new Keccak256(HashUtil.keccak256(message.getEncoded()));
    if (!receivedMessages.contains(encodedMessage)) {
        if (message.getMessageType() == MessageType.BLOCK_MESSAGE || message.getMessageType() == MessageType.TRANSACTIONS) {
            if (this.receivedMessages.size() >= MAX_NUMBER_OF_MESSAGES_CACHED) {
                this.receivedMessages.clear();
            }
            this.receivedMessages.add(encodedMessage);
        }
        if (!this.queue.offer(new MessageTask(sender, message))) {
            logger.trace("Queue full, message not added to the queue");
        }
    } else {
        recordEvent(sender, EventType.REPEATED_MESSAGE);
        logger.trace("Received message already known, not added to the queue");
    }
}
Also used : Keccak256(co.rsk.crypto.Keccak256)

Example 44 with Keccak256

use of co.rsk.crypto.Keccak256 in project rskj by rsksmart.

the class BridgeSerializationUtils method deserializeMap.

public static SortedMap<Keccak256, BtcTransaction> deserializeMap(byte[] data, NetworkParameters networkParameters, boolean noInputsTxs) {
    SortedMap<Keccak256, BtcTransaction> map = new TreeMap<>();
    if (data == null || data.length == 0) {
        return map;
    }
    RLPList rlpList = (RLPList) RLP.decode2(data).get(0);
    int ntxs = rlpList.size() / 2;
    for (int k = 0; k < ntxs; k++) {
        Keccak256 hash = new Keccak256(rlpList.get(k * 2).getRLPData());
        byte[] payload = rlpList.get(k * 2 + 1).getRLPData();
        BtcTransaction tx;
        if (!noInputsTxs) {
            tx = new BtcTransaction(networkParameters, payload);
        } else {
            tx = new BtcTransaction(networkParameters);
            tx.parseNoInputs(payload);
        }
        map.put(hash, tx);
    }
    return map;
}
Also used : Keccak256(co.rsk.crypto.Keccak256) RLPList(org.ethereum.util.RLPList)

Example 45 with Keccak256

use of co.rsk.crypto.Keccak256 in project rskj by rsksmart.

the class BridgeSupport method addSignature.

/**
 * Adds a federator signature to a btc release tx.
 * The hash for the signature must be calculated with Transaction.SigHash.ALL and anyoneCanPay=false. The signature must be canonical.
 * If enough signatures were added, ask federators to broadcast the btc release tx.
 *
 * @param executionBlockNumber The block number of the block that is currently being procesed
 * @param federatorPublicKey   Federator who is signing
 * @param signatures           1 signature per btc tx input
 * @param rskTxHash            The id of the rsk tx
 */
public void addSignature(long executionBlockNumber, BtcECKey federatorPublicKey, List<byte[]> signatures, byte[] rskTxHash) throws Exception {
    Context.propagate(btcContext);
    Federation retiringFederation = getRetiringFederation();
    if (!getActiveFederation().getPublicKeys().contains(federatorPublicKey) && (retiringFederation == null || !retiringFederation.getPublicKeys().contains(federatorPublicKey))) {
        logger.warn("Supplied federator public key {} does not belong to any of the federators.", federatorPublicKey);
        return;
    }
    BtcTransaction btcTx = provider.getRskTxsWaitingForSignatures().get(new Keccak256(rskTxHash));
    if (btcTx == null) {
        logger.warn("No tx waiting for signature for hash {}. Probably fully signed already.", new Keccak256(rskTxHash));
        return;
    }
    if (btcTx.getInputs().size() != signatures.size()) {
        logger.warn("Expected {} signatures but received {}.", btcTx.getInputs().size(), signatures.size());
        return;
    }
    eventLogger.logAddSignature(federatorPublicKey, btcTx, rskTxHash);
    processSigning(executionBlockNumber, federatorPublicKey, signatures, rskTxHash, btcTx);
}
Also used : 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