Search in sources :

Example 6 with Capability

use of org.ethereum.net.client.Capability in project rskj by rsksmart.

the class ConfigCapabilitiesImpl method getConfigCapabilities.

/**
 * Gets the capabilities listed in 'peer.capabilities' config property
 * sorted by their names.
 */
public List<Capability> getConfigCapabilities() {
    List<Capability> ret = new ArrayList<>();
    List<String> caps = config.peerCapabilities();
    for (Capability capability : allCaps) {
        if (caps.contains(capability.getName())) {
            ret.add(capability);
        }
    }
    return ret;
}
Also used : Capability(org.ethereum.net.client.Capability) ArrayList(java.util.ArrayList)

Example 7 with Capability

use of org.ethereum.net.client.Capability in project rskj by rsksmart.

the class HelloMessage method parse.

private void parse() {
    RLPList paramsList = (RLPList) RLP.decode2(encoded).get(0);
    byte[] p2pVersionBytes = paramsList.get(0).getRLPData();
    this.p2pVersion = p2pVersionBytes != null ? p2pVersionBytes[0] : 0;
    byte[] clientIdBytes = paramsList.get(1).getRLPData();
    this.clientId = new String(clientIdBytes != null ? clientIdBytes : EMPTY_BYTE_ARRAY);
    RLPList capabilityList = (RLPList) paramsList.get(2);
    this.capabilities = new ArrayList<>();
    for (Object aCapabilityList : capabilityList) {
        RLPElement capId = ((RLPList) aCapabilityList).get(0);
        RLPElement capVersion = ((RLPList) aCapabilityList).get(1);
        String name = new String(capId.getRLPData());
        byte version = capVersion.getRLPData() == null ? 0 : capVersion.getRLPData()[0];
        Capability cap = new Capability(name, version);
        this.capabilities.add(cap);
    }
    byte[] peerPortBytes = paramsList.get(3).getRLPData();
    this.listenPort = ByteUtil.byteArrayToInt(peerPortBytes);
    byte[] peerIdBytes = paramsList.get(4).getRLPData();
    this.peerId = Hex.toHexString(peerIdBytes);
    this.parsed = true;
}
Also used : Capability(org.ethereum.net.client.Capability) RLPElement(org.ethereum.util.RLPElement) RLPList(org.ethereum.util.RLPList)

Example 8 with Capability

use of org.ethereum.net.client.Capability in project rskj by rsksmart.

the class HelloMessage method encode.

private void encode() {
    byte[] p2pVersion = RLP.encodeByte(this.p2pVersion);
    byte[] clientId = RLP.encodeString(this.clientId);
    byte[][] capabilities = new byte[this.capabilities.size()][];
    for (int i = 0; i < this.capabilities.size(); i++) {
        Capability capability = this.capabilities.get(i);
        capabilities[i] = RLP.encodeList(RLP.encodeElement(capability.getName().getBytes(StandardCharsets.UTF_8)), RLP.encodeInt(capability.getVersion()));
    }
    byte[] capabilityList = RLP.encodeList(capabilities);
    byte[] peerPort = RLP.encodeInt(this.listenPort);
    byte[] peerId = RLP.encodeElement(Hex.decode(this.peerId));
    this.encoded = RLP.encodeList(p2pVersion, clientId, capabilityList, peerPort, peerId);
}
Also used : Capability(org.ethereum.net.client.Capability)

Example 9 with Capability

use of org.ethereum.net.client.Capability in project rskj by rsksmart.

the class P2pHandler method setHandshake.

public void setHandshake(HelloMessage msg, ChannelHandlerContext ctx) {
    channel.getNodeStatistics().setClientId(msg.getClientId());
    this.ethInbound = channel.getNodeStatistics().ethInbound.get();
    this.ethOutbound = channel.getNodeStatistics().ethOutbound.get();
    this.handshakeHelloMessage = msg;
    if (!isProtocolVersionSupported(msg.getP2PVersion())) {
        disconnect(ReasonCode.INCOMPATIBLE_PROTOCOL);
    } else {
        List<Capability> capInCommon = getSupportedCapabilities(msg);
        channel.initMessageCodes(capInCommon);
        for (Capability capability : capInCommon) {
            if (capability.getName().equals(Capability.RSK)) {
                // Activate EthHandler for this peer
                channel.activateEth(ctx, fromCode(capability.getVersion()));
            }
        }
        ethereumListener.onHandShakePeer(channel, msg);
    }
}
Also used : Capability(org.ethereum.net.client.Capability)

Example 10 with Capability

use of org.ethereum.net.client.Capability in project rskj by rsksmart.

the class P2pHandler method getSupportedCapabilities.

public List<Capability> getSupportedCapabilities(HelloMessage hello) {
    List<Capability> configCaps = configCapabilities.getConfigCapabilities();
    List<Capability> supported = new ArrayList<>();
    List<Capability> eths = new ArrayList<>();
    for (Capability cap : hello.getCapabilities()) {
        if (configCaps.contains(cap)) {
            if (cap.isRSK()) {
                eths.add(cap);
            } else {
                supported.add(cap);
            }
        }
    }
    if (eths.isEmpty()) {
        return supported;
    }
    // we need to pick up
    // the most recent Eth version
    Capability highest = null;
    for (Capability eth : eths) {
        if (highest == null || highest.getVersion() < eth.getVersion()) {
            highest = eth;
        }
    }
    supported.add(highest);
    return supported;
}
Also used : Capability(org.ethereum.net.client.Capability) ArrayList(java.util.ArrayList)

Aggregations

Capability (org.ethereum.net.client.Capability)15 Test (org.junit.Test)6 ArrayList (java.util.ArrayList)4 HelloMessage (org.ethereum.net.p2p.HelloMessage)3 InetAddress (java.net.InetAddress)1 ECKey (org.ethereum.crypto.ECKey)1 EthVersion (org.ethereum.net.eth.EthVersion)1 Peer (org.ethereum.net.p2p.Peer)1 RLPElement (org.ethereum.util.RLPElement)1 RLPList (org.ethereum.util.RLPList)1 Before (org.junit.Before)1