Skip to content

Commit

Permalink
Add additional RPC methods and data structures (#528)
Browse files Browse the repository at this point in the history
  • Loading branch information
atoulme authored and jrhea committed Mar 27, 2019
1 parent 109ecf8 commit 3c00f55
Show file tree
Hide file tree
Showing 9 changed files with 408 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -119,21 +119,22 @@ private void connectStaticPeers() {
private void receiveMessage(NetSocket netSocket) {
URI peerURI =
URI.create(
"hobs://" + netSocket.remoteAddress().host() + ":" + netSocket.remoteAddress().port());
"hob+tcp://"
+ netSocket.remoteAddress().host()
+ ":"
+ netSocket.remoteAddress().port());
handlersMap.computeIfAbsent(
peerURI,
uri -> {
Peer peer = new Peer(peerURI);
HobbitsSocketHandler handler =
new HobbitsSocketHandler(netSocket, userAgent, peer, chainData);
return handler;
return new HobbitsSocketHandler(netSocket, userAgent, peer, chainData);
});
}

@Override
public Collection<?> getPeers() {
return handlersMap.values().stream()
.map(handler -> handler.peer())
.map(HobbitsSocketHandler::peer)
.collect(Collectors.toList());
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
/*
* Copyright 2019 ConsenSys AG.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*/

package tech.pegasys.artemis.networking.p2p.hobbits;

import com.fasterxml.jackson.annotation.JsonValue;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.fasterxml.jackson.databind.deser.std.StdDeserializer;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

@JsonDeserialize(using = BlockBodies.BlockBodiesDeserializer.class)
final class BlockBodies {

static class BlockBodiesDeserializer extends StdDeserializer<BlockBodies> {

protected BlockBodiesDeserializer() {
super(BlockBodies.class);
}

@Override
public BlockBodies deserialize(JsonParser jp, DeserializationContext ctxt)
throws IOException, JsonProcessingException {
JsonNode node = jp.getCodec().readTree(jp);
Iterator<JsonNode> iterator = node.iterator();
List<BlockBody> elts = new ArrayList<>();
while (iterator.hasNext()) {
JsonNode child = iterator.next();
elts.add(new BlockBody());
}

return new BlockBodies(elts);
}
}

static class BlockBody {

/*
'randao_reveal': 'bytes96',
'eth1_data': Eth1Data,
'proposer_slashings': [ProposerSlashing],
'attester_slashings': [AttesterSlashing],
'attestations': [Attestation],
'deposits': [Deposit],
'voluntary_exits': [VoluntaryExit],
'transfers': [Transfer],
*/

BlockBody() {
// TODO fill body with the right info
}
}

private final List<BlockBody> bodies;

BlockBodies(List<BlockBody> rootsAndSlots) {
this.bodies = rootsAndSlots;
}

@JsonValue
public List<BlockBody> bodies() {
return bodies;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
/*
* Copyright 2019 ConsenSys AG.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*/

package tech.pegasys.artemis.networking.p2p.hobbits;

import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonValue;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.fasterxml.jackson.databind.deser.std.StdDeserializer;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import net.consensys.cava.bytes.Bytes;
import net.consensys.cava.bytes.Bytes32;

@JsonDeserialize(using = BlockHeaders.BlockHeadersDeserializer.class)
final class BlockHeaders {

static class BlockHeadersDeserializer extends StdDeserializer<BlockHeaders> {

protected BlockHeadersDeserializer() {
super(BlockHeaders.class);
}

@Override
public BlockHeaders deserialize(JsonParser jp, DeserializationContext ctxt)
throws IOException, JsonProcessingException {
JsonNode node = jp.getCodec().readTree(jp);
Iterator<JsonNode> iterator = node.iterator();
List<BlockHeader> elts = new ArrayList<>();
while (iterator.hasNext()) {
JsonNode child = iterator.next();
elts.add(
new BlockHeader(
child.get("slot").asLong(),
Bytes32.fromHexString(child.get("previous_block_root").asText()),
Bytes32.fromHexString(child.get("state_root").asText()),
Bytes32.fromHexString(child.get("block_body_root").asText()),
Bytes.fromHexString(child.get("signature").asText())));
}

return new BlockHeaders(elts);
}
}

static class BlockHeader {

private final long slot;
private final Bytes32 previousBlockRoot;
private final Bytes32 stateRoot;
private final Bytes32 blockBodyRoot;
private final Bytes signature;

BlockHeader(
long slot,
Bytes32 previousBlockRoot,
Bytes32 stateRoot,
Bytes32 blockBodyRoot,
Bytes signature) {
this.slot = slot;
this.previousBlockRoot = previousBlockRoot;
this.stateRoot = stateRoot;
this.blockBodyRoot = blockBodyRoot;
this.signature = signature;
}

@JsonProperty("slot")
public long slot() {
return slot;
}

@JsonProperty("previous_block_root")
public Bytes32 previousBlockRoot() {
return previousBlockRoot;
}

@JsonProperty("state_root")
public Bytes32 stateRoot() {
return stateRoot;
}

@JsonProperty("block_body_root")
public Bytes32 blockBodyRoot() {
return blockBodyRoot;
}

@JsonProperty("signature")
public Bytes signature() {
return signature;
}
}

private final List<BlockHeader> headers;

BlockHeaders(List<BlockHeader> headers) {
this.headers = headers;
}

@JsonValue
public List<BlockHeader> headers() {
return headers;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
/*
* Copyright 2019 ConsenSys AG.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*/

package tech.pegasys.artemis.networking.p2p.hobbits;

import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonValue;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.fasterxml.jackson.databind.deser.std.StdDeserializer;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import net.consensys.cava.bytes.Bytes32;

@JsonDeserialize(using = BlockRoots.BlockRootsDeserializer.class)
final class BlockRoots {

static class BlockRootsDeserializer extends StdDeserializer<BlockRoots> {

protected BlockRootsDeserializer() {
super(BlockRoots.class);
}

@Override
public BlockRoots deserialize(JsonParser jp, DeserializationContext ctxt)
throws IOException, JsonProcessingException {
JsonNode node = jp.getCodec().readTree(jp);
Iterator<JsonNode> iterator = node.iterator();
List<BlockRootAndSlot> elts = new ArrayList<>();
while (iterator.hasNext()) {
JsonNode child = iterator.next();
elts.add(
new BlockRootAndSlot(
Bytes32.fromHexString(child.get("block_root").asText()),
child.get("slot").asLong()));
}

return new BlockRoots(elts);
}
}

static class BlockRootAndSlot {

private final Bytes32 blockRoot;
private final long slot;

BlockRootAndSlot(Bytes32 blockRoot, long slot) {
this.blockRoot = blockRoot;
this.slot = slot;
}

@JsonProperty("block_root")
public Bytes32 blockRoot() {
return blockRoot;
}

@JsonProperty("slot")
public long slot() {
return slot;
}
}

private final List<BlockRootAndSlot> rootsAndSlots;

BlockRoots(List<BlockRootAndSlot> rootsAndSlots) {
this.rootsAndSlots = rootsAndSlots;
}

@JsonValue
public List<BlockRootAndSlot> rootsAndSlots() {
return rootsAndSlots;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import io.vertx.core.buffer.Buffer;
import io.vertx.core.net.NetSocket;
import java.time.Instant;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Set;
import java.util.concurrent.atomic.AtomicBoolean;
Expand Down Expand Up @@ -80,6 +81,15 @@ private void handleRPCMessage(RPCMessage rpcMessage) {
replyStatus(rpcMessage.requestId());
}
peer.setPeerStatus(rpcMessage.bodyAs(GetStatus.class));
} else if (RPCMethod.REQUEST_BLOCK_ROOTS.equals(rpcMessage.method())) {
// TODO provide data
sendReply(RPCMethod.BLOCK_ROOTS, new BlockRoots(new ArrayList<>()), rpcMessage.requestId());
} else if (RPCMethod.REQUEST_BLOCK_HEADERS.equals(rpcMessage.method())) {
// TODO provide data
sendReply(RPCMethod.BLOCK_HEADERS, null, rpcMessage.requestId());
} else if (RPCMethod.REQUEST_BLOCK_BODIES.equals(rpcMessage.method())) {
// TODO provide data
sendReply(RPCMethod.BLOCK_BODIES, null, rpcMessage.requestId());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ public Bytes deserialize(JsonParser p, DeserializationContext ctxt)
}
}

private static final class BytesModule extends SimpleModule {
static final class BytesModule extends SimpleModule {

BytesModule() {
super("bytes");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,11 @@ public enum RPCMethod {
GOODBYE(1),
GET_STATUS(2),
REQUEST_BLOCK_ROOTS(10),
REQUEST_BLOCK_HEADERS(11),
REQUEST_BLOCK_BODIES(12),
REQUEST_STATE(13);
BLOCK_ROOTS(11),
REQUEST_BLOCK_HEADERS(12),
BLOCK_HEADERS(13),
REQUEST_BLOCK_BODIES(14),
BLOCK_BODIES(15);

private int code;

Expand Down Expand Up @@ -52,11 +54,15 @@ static RPCMethod valueOf(int code) {
case 10:
return REQUEST_BLOCK_ROOTS;
case 11:
return REQUEST_BLOCK_HEADERS;
return BLOCK_ROOTS;
case 12:
return REQUEST_BLOCK_BODIES;
return REQUEST_BLOCK_HEADERS;
case 13:
return REQUEST_STATE;
return BLOCK_HEADERS;
case 14:
return REQUEST_BLOCK_BODIES;
case 15:
return BLOCK_BODIES;
default:
throw new IllegalArgumentException("Unsupported code " + code);
}
Expand Down
Loading

0 comments on commit 3c00f55

Please sign in to comment.