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;
}
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;
}
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);
}
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);
}
}
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;
}
Aggregations