Search in sources :

Example 1 with EthMessage

use of org.ethereum.net.eth.message.EthMessage in project rskj by rsksmart.

the class RskMessageTest method encodeDecodeGetBlockMessage.

@Test
public void encodeDecodeGetBlockMessage() {
    Block block = new BlockGenerator().getBlock(1);
    GetBlockMessage message = new GetBlockMessage(block.getHash().getBytes());
    RskMessage rskmessage = new RskMessage(config, message);
    byte[] encoded = rskmessage.getEncoded();
    Eth62MessageFactory factory = new Eth62MessageFactory(config);
    EthMessage ethmessage = (EthMessage) factory.create((byte) 0x08, encoded);
    Assert.assertNotNull(ethmessage);
    Assert.assertEquals(EthMessageCodes.RSK_MESSAGE, ethmessage.getCommand());
    RskMessage result = (RskMessage) ethmessage;
    Assert.assertNotNull(result.getMessage());
    Message resultMessage = result.getMessage();
    Assert.assertEquals(MessageType.GET_BLOCK_MESSAGE, resultMessage.getMessageType());
    Assert.assertArrayEquals(block.getHash().getBytes(), ((GetBlockMessage) resultMessage).getBlockHash());
}
Also used : EthMessage(org.ethereum.net.eth.message.EthMessage) EthMessage(org.ethereum.net.eth.message.EthMessage) Eth62MessageFactory(org.ethereum.net.eth.message.Eth62MessageFactory) Block(org.ethereum.core.Block) BlockGenerator(co.rsk.blockchain.utils.BlockGenerator) Test(org.junit.Test)

Example 2 with EthMessage

use of org.ethereum.net.eth.message.EthMessage in project rskj by rsksmart.

the class MessageQueue method receivedMessage.

public void receivedMessage(Message msg) throws InterruptedException {
    MessageRoundtrip messageRoundtrip = requestQueue.peek();
    if (messageRoundtrip != null) {
        Message waitingMessage = messageRoundtrip.getMsg();
        if (waitingMessage instanceof PingMessage) {
            hasPing = false;
        }
        if (waitingMessage.getAnswerMessage() != null && msg.getClass() == waitingMessage.getAnswerMessage()) {
            messageRoundtrip.answer();
            if (waitingMessage instanceof EthMessage) {
                channel.getPeerStats().pong(messageRoundtrip.lastTimestamp);
            }
            logger.trace("Message round trip covered: [{}] ", messageRoundtrip.getMsg().getClass());
        }
    }
}
Also used : Message(org.ethereum.net.message.Message) DisconnectMessage(org.ethereum.net.p2p.DisconnectMessage) PingMessage(org.ethereum.net.p2p.PingMessage) EthMessage(org.ethereum.net.eth.message.EthMessage) EthMessage(org.ethereum.net.eth.message.EthMessage) PingMessage(org.ethereum.net.p2p.PingMessage)

Example 3 with EthMessage

use of org.ethereum.net.eth.message.EthMessage in project rskj by rsksmart.

the class ChannelManagerImpl method broadcastBlock.

/**
 * broadcastBlock Propagates a block message across active peers with exclusion of
 * the peers with an id belonging to the skip set.
 *
 * @param block new Block to be sent
 * @param skip  the set of peers to avoid sending the message.
 * @return a set containing the ids of the peers that received the block.
 */
@Nonnull
public Set<NodeID> broadcastBlock(@Nonnull final Block block, @Nullable final Set<NodeID> skip) {
    Metrics.broadcastBlock(block);
    final Set<NodeID> res = new HashSet<>();
    final BlockIdentifier bi = new BlockIdentifier(block.getHash().getBytes(), block.getNumber());
    final EthMessage newBlock = new RskMessage(config, new BlockMessage(block));
    final EthMessage newBlockHashes = new RskMessage(config, new NewBlockHashesMessage(Arrays.asList(bi)));
    synchronized (activePeers) {
        // Get a randomized list with all the peers that don't have the block yet.
        activePeers.values().forEach(c -> logger.trace("RSK activePeers: {}", c));
        final Vector<Channel> peers = activePeers.values().stream().filter(p -> skip == null || !skip.contains(p.getNodeId())).collect(Collectors.toCollection(Vector::new));
        Collections.shuffle(peers);
        int sqrt = (int) Math.floor(Math.sqrt(peers.size()));
        for (int i = 0; i < sqrt; i++) {
            Channel peer = peers.get(i);
            res.add(peer.getNodeId());
            logger.trace("RSK propagate: {}", peer);
            peer.sendMessage(newBlock);
        }
        for (int i = sqrt; i < peers.size(); i++) {
            Channel peer = peers.get(i);
            logger.trace("RSK announce: {}", peer);
            peer.sendMessage(newBlockHashes);
        }
    }
    return res;
}
Also used : BlockIdentifier(org.ethereum.core.BlockIdentifier) NodeFilter(org.ethereum.config.NodeFilter) java.util(java.util) LoggerFactory(org.slf4j.LoggerFactory) LRUMap(org.apache.commons.collections4.map.LRUMap) Autowired(org.springframework.beans.factory.annotation.Autowired) CollectionUtils(org.apache.commons.collections4.CollectionUtils) Block(org.ethereum.core.Block) InetAddress(java.net.InetAddress) EthMessage(org.ethereum.net.eth.message.EthMessage) Status(co.rsk.net.Status) SyncPool(org.ethereum.sync.SyncPool) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) Metrics(co.rsk.net.Metrics) Nonnull(javax.annotation.Nonnull) Nullable(javax.annotation.Nullable) BlockIdentifier(org.ethereum.core.BlockIdentifier) RskMessage(co.rsk.net.eth.RskMessage) Logger(org.slf4j.Logger) co.rsk.net.messages(co.rsk.net.messages) ReasonCode(org.ethereum.net.message.ReasonCode) Collectors(java.util.stream.Collectors) Executors(java.util.concurrent.Executors) TimeUnit(java.util.concurrent.TimeUnit) Component(org.springframework.stereotype.Component) NodeID(co.rsk.net.NodeID) VisibleForTesting(com.google.common.annotations.VisibleForTesting) RskSystemProperties(co.rsk.config.RskSystemProperties) Transaction(org.ethereum.core.Transaction) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) EthMessage(org.ethereum.net.eth.message.EthMessage) NodeID(co.rsk.net.NodeID) RskMessage(co.rsk.net.eth.RskMessage) Nonnull(javax.annotation.Nonnull)

Example 4 with EthMessage

use of org.ethereum.net.eth.message.EthMessage in project rskj by rsksmart.

the class ChannelManagerImpl method broadcastBlockHash.

@Nonnull
public Set<NodeID> broadcastBlockHash(@Nonnull final List<BlockIdentifier> identifiers, @Nullable final Set<NodeID> targets) {
    final Set<NodeID> res = new HashSet<>();
    final EthMessage newBlockHash = new RskMessage(config, new NewBlockHashesMessage(identifiers));
    synchronized (activePeers) {
        activePeers.values().forEach(c -> logger.trace("RSK activePeers: {}", c));
        activePeers.values().stream().filter(p -> targets == null || targets.contains(p.getNodeId())).forEach(peer -> {
            logger.trace("RSK announce hash: {}", peer);
            peer.sendMessage(newBlockHash);
        });
    }
    return res;
}
Also used : NodeFilter(org.ethereum.config.NodeFilter) java.util(java.util) LoggerFactory(org.slf4j.LoggerFactory) LRUMap(org.apache.commons.collections4.map.LRUMap) Autowired(org.springframework.beans.factory.annotation.Autowired) CollectionUtils(org.apache.commons.collections4.CollectionUtils) Block(org.ethereum.core.Block) InetAddress(java.net.InetAddress) EthMessage(org.ethereum.net.eth.message.EthMessage) Status(co.rsk.net.Status) SyncPool(org.ethereum.sync.SyncPool) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) Metrics(co.rsk.net.Metrics) Nonnull(javax.annotation.Nonnull) Nullable(javax.annotation.Nullable) BlockIdentifier(org.ethereum.core.BlockIdentifier) RskMessage(co.rsk.net.eth.RskMessage) Logger(org.slf4j.Logger) co.rsk.net.messages(co.rsk.net.messages) ReasonCode(org.ethereum.net.message.ReasonCode) Collectors(java.util.stream.Collectors) Executors(java.util.concurrent.Executors) TimeUnit(java.util.concurrent.TimeUnit) Component(org.springframework.stereotype.Component) NodeID(co.rsk.net.NodeID) VisibleForTesting(com.google.common.annotations.VisibleForTesting) RskSystemProperties(co.rsk.config.RskSystemProperties) Transaction(org.ethereum.core.Transaction) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) EthMessage(org.ethereum.net.eth.message.EthMessage) NodeID(co.rsk.net.NodeID) RskMessage(co.rsk.net.eth.RskMessage) Nonnull(javax.annotation.Nonnull)

Example 5 with EthMessage

use of org.ethereum.net.eth.message.EthMessage in project rskj by rsksmart.

the class ChannelManagerImpl method broadcastTransaction.

/**
 * broadcastTransaction Propagates a transaction message across active peers with exclusion of
 * the peers with an id belonging to the skip set.
 *
 * @param transaction new Transaction to be sent
 * @param skip  the set of peers to avoid sending the message.
 * @return a set containing the ids of the peers that received the transaction.
 */
@Nonnull
public Set<NodeID> broadcastTransaction(@Nonnull final Transaction transaction, @Nullable final Set<NodeID> skip) {
    Metrics.broadcastTransaction(transaction);
    List<Transaction> transactions = new ArrayList<>();
    transactions.add(transaction);
    final Set<NodeID> res = new HashSet<>();
    final EthMessage newTransactions = new RskMessage(config, new TransactionsMessage(transactions));
    synchronized (activePeers) {
        final Vector<Channel> peers = activePeers.values().stream().filter(p -> skip == null || !skip.contains(p.getNodeId())).collect(Collectors.toCollection(Vector::new));
        for (Channel peer : peers) {
            res.add(peer.getNodeId());
            peer.sendMessage(newTransactions);
        }
    }
    return res;
}
Also used : NodeFilter(org.ethereum.config.NodeFilter) java.util(java.util) LoggerFactory(org.slf4j.LoggerFactory) LRUMap(org.apache.commons.collections4.map.LRUMap) Autowired(org.springframework.beans.factory.annotation.Autowired) CollectionUtils(org.apache.commons.collections4.CollectionUtils) Block(org.ethereum.core.Block) InetAddress(java.net.InetAddress) EthMessage(org.ethereum.net.eth.message.EthMessage) Status(co.rsk.net.Status) SyncPool(org.ethereum.sync.SyncPool) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) Metrics(co.rsk.net.Metrics) Nonnull(javax.annotation.Nonnull) Nullable(javax.annotation.Nullable) BlockIdentifier(org.ethereum.core.BlockIdentifier) RskMessage(co.rsk.net.eth.RskMessage) Logger(org.slf4j.Logger) co.rsk.net.messages(co.rsk.net.messages) ReasonCode(org.ethereum.net.message.ReasonCode) Collectors(java.util.stream.Collectors) Executors(java.util.concurrent.Executors) TimeUnit(java.util.concurrent.TimeUnit) Component(org.springframework.stereotype.Component) NodeID(co.rsk.net.NodeID) VisibleForTesting(com.google.common.annotations.VisibleForTesting) RskSystemProperties(co.rsk.config.RskSystemProperties) Transaction(org.ethereum.core.Transaction) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) Transaction(org.ethereum.core.Transaction) EthMessage(org.ethereum.net.eth.message.EthMessage) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) NodeID(co.rsk.net.NodeID) RskMessage(co.rsk.net.eth.RskMessage) Nonnull(javax.annotation.Nonnull)

Aggregations

EthMessage (org.ethereum.net.eth.message.EthMessage)11 RskMessage (co.rsk.net.eth.RskMessage)6 Block (org.ethereum.core.Block)6 Metrics (co.rsk.net.Metrics)4 Status (co.rsk.net.Status)4 CopyOnWriteArrayList (java.util.concurrent.CopyOnWriteArrayList)4 BlockGenerator (co.rsk.blockchain.utils.BlockGenerator)3 RskSystemProperties (co.rsk.config.RskSystemProperties)3 NodeID (co.rsk.net.NodeID)3 co.rsk.net.messages (co.rsk.net.messages)3 VisibleForTesting (com.google.common.annotations.VisibleForTesting)3 InetAddress (java.net.InetAddress)3 java.util (java.util)3 Executors (java.util.concurrent.Executors)3 ScheduledExecutorService (java.util.concurrent.ScheduledExecutorService)3 TimeUnit (java.util.concurrent.TimeUnit)3 Collectors (java.util.stream.Collectors)3 Nonnull (javax.annotation.Nonnull)3 Nullable (javax.annotation.Nullable)3 CollectionUtils (org.apache.commons.collections4.CollectionUtils)3