Skip to content

Commit e1692e8

Browse files
authored
Adding portal_HistoryRoutingTable and tests (#148)
1 parent e027775 commit e1692e8

15 files changed

Lines changed: 426 additions & 1 deletion

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ View logs output and results:
125125
```shell script
126126
./hiveview --serve --logdir ./workspace/logs
127127
```
128-
## [JSON-RPC API (20)](https://samba-portal-node.postman.co/workspace/Samba-Portal-Node-Workspace~8bf54719-5e6d-4476-8b33-6434dc57d833/request/33150235-eb63c4bf-82ff-477e-a17d-616657e9cdbc?action=share&creator=33150235&ctx=documentation&active-environment=33150235-5c222146-bd60-431b-bb15-f3f9dc8fc9cc)
128+
## [JSON-RPC API (23)](https://samba-portal-node.postman.co/workspace/Samba-Portal-Node-Workspace~8bf54719-5e6d-4476-8b33-6434dc57d833/request/33150235-eb63c4bf-82ff-477e-a17d-616657e9cdbc?action=share&creator=33150235&ctx=documentation&active-environment=33150235-5c222146-bd60-431b-bb15-f3f9dc8fc9cc)
129129

130130
#### History
131131
- portal_historyAddEnr
@@ -140,6 +140,7 @@ View logs output and results:
140140
- portal_historyPing
141141
- portal_historyStore
142142
- portal_historyPutContent
143+
- portal_historyRoutingTableInfo
143144

144145
#### Discv5
145146
- discv5_getEnr,

core/src/main/java/samba/api/HistoryAPI.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@ Optional<Bytes> offer(
4848

4949
Optional<RecursiveFindNodesResult> recursiveFindNodes(final String nodeId);
5050

51+
Optional<List<List<String>>> getRoutingTable();
52+
5153
// For Besu
5254

5355
Optional<BlockHeader> getBlockHeaderByBlockHash(Hash blockHash);

core/src/main/java/samba/api/HistoryAPIClient.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,11 @@ public Optional<RecursiveFindNodesResult> recursiveFindNodes(String nodeId) {
9393
return RecursiveFindNodes.execute(this.historyNetworkInternalAPI, nodeId);
9494
}
9595

96+
@Override
97+
public Optional<List<List<String>>> getRoutingTable() {
98+
return GetRoutingTable.execute(this.historyNetworkInternalAPI);
99+
}
100+
96101
@Override
97102
public Optional<BlockHeader> getBlockHeaderByBlockHash(Hash blockHash) {
98103
return GetBlockHeaderByBlockHash.execute(this.historyNetworkInternalAPI, blockHash);
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package samba.api.jsonrpc;
2+
3+
import samba.api.Discv5API;
4+
import samba.api.HistoryAPI;
5+
import samba.api.jsonrpc.results.RoutingTableInfoResult;
6+
import samba.jsonrpc.config.RpcMethod;
7+
import samba.jsonrpc.reponse.JsonRpcMethod;
8+
import samba.jsonrpc.reponse.JsonRpcRequestContext;
9+
import samba.jsonrpc.reponse.JsonRpcResponse;
10+
import samba.jsonrpc.reponse.RpcErrorType;
11+
12+
public class PortalHistoryRoutingTableInfo implements JsonRpcMethod {
13+
14+
private final HistoryAPI historyAPI;
15+
private final Discv5API discv5API;
16+
17+
public PortalHistoryRoutingTableInfo(final HistoryAPI historyAPI, final Discv5API discv5API) {
18+
this.historyAPI = historyAPI;
19+
this.discv5API = discv5API;
20+
}
21+
22+
@Override
23+
public String getName() {
24+
return RpcMethod.PORTAL_HISTORY_ROUTING_TABLE_INFO.getMethodName();
25+
}
26+
27+
@Override
28+
public JsonRpcResponse response(JsonRpcRequestContext requestContext) {
29+
return this.discv5API
30+
.getNodeInfo()
31+
.flatMap(
32+
info ->
33+
this.historyAPI
34+
.getRoutingTable()
35+
.map(
36+
table -> {
37+
RoutingTableInfoResult result =
38+
new RoutingTableInfoResult(info.getNodeId(), table);
39+
return createSuccessResponse(requestContext, result);
40+
}))
41+
.orElseGet(
42+
() ->
43+
createJsonRpcInvalidRequestResponse(requestContext, RpcErrorType.INVALID_REQUEST));
44+
}
45+
}

core/src/main/java/samba/domain/dht/NodeTable.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import java.time.Clock;
44
import java.util.Comparator;
55
import java.util.HashMap;
6+
import java.util.List;
67
import java.util.Map;
78
import java.util.Optional;
89
import java.util.Spliterator;
@@ -95,4 +96,8 @@ private Optional<KBucket> getBucket(final int distance) {
9596
public boolean isNodeIgnored(NodeRecord nodeRecord) {
9697
return this.livenessManager.isABadPeer(nodeRecord);
9798
}
99+
100+
public List<List<NodeRecord>> getNodeRecordBuckets() {
101+
return this.buckets.values().stream().map(KBucket::getAllNodes).toList();
102+
}
98103
}

core/src/main/java/samba/network/history/HistoryNetwork.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -816,6 +816,11 @@ public Optional<TraceGetContentResult> traceGetContent(
816816
}
817817
}
818818

819+
@Override
820+
public List<List<NodeRecord>> getRoutingTable() {
821+
return this.routingTable.getNodeRecordBuckets();
822+
}
823+
819824
@Override
820825
public Optional<RecursiveFindNodesResult> recursiveFindNodes(
821826
final String nodeId, Set<NodeRecord> excludedNodes, final int timeout) {

core/src/main/java/samba/network/history/api/HistoryNetworkInternalAPI.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,4 +61,6 @@ Optional<RecursiveFindNodesResult> recursiveFindNodes(
6161

6262
Optional<TraceGetContentResult> traceGetContent(
6363
ContentKey contentKey, int timeout, long startTime);
64+
65+
List<List<NodeRecord>> getRoutingTable();
6466
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package samba.network.history.api.methods;
2+
3+
import samba.network.history.api.HistoryNetworkInternalAPI;
4+
5+
import java.util.Collections;
6+
import java.util.List;
7+
import java.util.Optional;
8+
import java.util.stream.Collectors;
9+
10+
import org.apache.tuweni.bytes.Bytes;
11+
import org.ethereum.beacon.discovery.schema.NodeRecord;
12+
import org.slf4j.Logger;
13+
import org.slf4j.LoggerFactory;
14+
15+
public class GetRoutingTable {
16+
17+
private static final Logger LOG = LoggerFactory.getLogger(GetRoutingTable.class);
18+
public static final int MAX_ROUTING_TABLE_SIZE = 16;
19+
20+
private final HistoryNetworkInternalAPI historyNetworkInternalAPI;
21+
22+
public GetRoutingTable(final HistoryNetworkInternalAPI historyNetworkInternalAPI) {
23+
this.historyNetworkInternalAPI = historyNetworkInternalAPI;
24+
}
25+
26+
private Optional<List<List<String>>> execute() {
27+
List<List<NodeRecord>> routingTable = historyNetworkInternalAPI.getRoutingTable();
28+
return Optional.of(
29+
routingTable.stream()
30+
.map(
31+
innerList -> {
32+
List<String> reversed =
33+
innerList.stream()
34+
.map(NodeRecord::getNodeId)
35+
.map(Bytes::toHexString)
36+
.limit(MAX_ROUTING_TABLE_SIZE)
37+
.collect(Collectors.toList());
38+
Collections.reverse(reversed); // ordered from least-recently to most-recently
39+
return reversed;
40+
})
41+
.collect(Collectors.toList()));
42+
}
43+
44+
public static Optional<List<List<String>>> execute(
45+
final HistoryNetworkInternalAPI historyNetworkInternalAPI) {
46+
LOG.debug("Executing GetRoutingTable");
47+
return new GetRoutingTable(historyNetworkInternalAPI).execute();
48+
}
49+
}

core/src/main/java/samba/network/history/routingtable/HistoryRoutingTable.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
import java.util.Comparator;
88
import java.util.LinkedHashSet;
9+
import java.util.List;
910
import java.util.Map;
1011
import java.util.Objects;
1112
import java.util.Optional;
@@ -103,6 +104,11 @@ public boolean isNodeIgnored(NodeRecord nodeRecord) {
103104
return this.nodeTable.isNodeIgnored(nodeRecord);
104105
}
105106

107+
@Override
108+
public List<List<NodeRecord>> getNodeRecordBuckets() {
109+
return this.nodeTable.getNodeRecordBuckets();
110+
}
111+
106112
@Override
107113
public Optional<NodeRecord> findClosestNodeToKey(Bytes key) {
108114
return radiusMap.entrySet().stream()

core/src/main/java/samba/network/history/routingtable/RoutingTable.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package samba.network.history.routingtable;
22

3+
import java.util.List;
34
import java.util.Optional;
45
import java.util.Set;
56
import java.util.stream.Stream;
@@ -33,4 +34,7 @@ public interface RoutingTable {
3334
boolean isNodeConnected(Bytes nodeId);
3435

3536
boolean isNodeIgnored(NodeRecord nodeRecord);
37+
38+
/** Recently nodes are at the start of the list with older at the end. */
39+
List<List<NodeRecord>> getNodeRecordBuckets();
3640
}

0 commit comments

Comments
 (0)