|
1 | | -/* |
2 | | - * SPDX-License-Identifier: Apache-2.0 |
3 | | - */ |
4 | 1 | package org.ethereum.beacon.discovery; |
5 | 2 |
|
| 3 | +import org.apache.tuweni.bytes.Bytes; |
| 4 | +import org.ethereum.beacon.discovery.schema.NodeRecord; |
| 5 | +import org.ethereum.beacon.discovery.storage.BucketStats; |
| 6 | + |
6 | 7 | import java.util.Collection; |
7 | 8 | import java.util.List; |
8 | 9 | import java.util.Optional; |
9 | 10 | import java.util.concurrent.CompletableFuture; |
10 | 11 | import java.util.stream.Stream; |
11 | | -import org.apache.logging.log4j.LogManager; |
12 | | -import org.apache.logging.log4j.Logger; |
13 | | -import org.apache.tuweni.bytes.Bytes; |
14 | | -import org.apache.tuweni.bytes.Bytes32; |
15 | | -import org.ethereum.beacon.discovery.scheduler.ExpirationSchedulerFactory; |
16 | | -import org.ethereum.beacon.discovery.schema.NodeRecord; |
17 | | -import org.ethereum.beacon.discovery.storage.BucketStats; |
18 | | -import org.ethereum.beacon.discovery.storage.KBuckets; |
19 | | -import org.ethereum.beacon.discovery.task.DiscoveryTaskManager; |
20 | | - |
21 | | -public class DiscoverySystem { |
22 | | - private static final Logger LOG = LogManager.getLogger(); |
23 | | - private final DiscoveryManager discoveryManager; |
24 | | - private final DiscoveryTaskManager taskManager; |
25 | | - private final ExpirationSchedulerFactory expirationSchedulerFactory; |
26 | | - private final KBuckets buckets; |
27 | | - private final List<NodeRecord> bootnodes; |
28 | | - |
29 | | - DiscoverySystem( |
30 | | - final DiscoveryManager discoveryManager, |
31 | | - final DiscoveryTaskManager taskManager, |
32 | | - final ExpirationSchedulerFactory expirationSchedulerFactory, |
33 | | - final KBuckets buckets, |
34 | | - final List<NodeRecord> bootnodes) { |
35 | | - this.discoveryManager = discoveryManager; |
36 | | - this.taskManager = taskManager; |
37 | | - this.expirationSchedulerFactory = expirationSchedulerFactory; |
38 | | - this.buckets = buckets; |
39 | | - this.bootnodes = bootnodes; |
40 | | - } |
41 | | - |
42 | | - public CompletableFuture<Void> start() { |
43 | | - return discoveryManager.start().thenRun(taskManager::start).thenRun(this::pingBootnodes); |
44 | | - } |
45 | | - |
46 | | - private void pingBootnodes() { |
47 | | - bootnodes.forEach( |
48 | | - bootnode -> |
49 | | - discoveryManager |
50 | | - .ping(bootnode) |
51 | | - .exceptionally( |
52 | | - e -> { |
53 | | - LOG.debug("Failed to ping bootnode: " + bootnode); |
54 | | - return null; |
55 | | - })); |
56 | | - } |
57 | | - |
58 | | - public void stop() { |
59 | | - taskManager.stop(); |
60 | | - discoveryManager.stop(); |
61 | | - expirationSchedulerFactory.stop(); |
62 | | - } |
63 | | - |
64 | | - public NodeRecord getLocalNodeRecord() { |
65 | | - return discoveryManager.getLocalNodeRecord(); |
66 | | - } |
67 | | - |
68 | | - public BucketStats getBucketStats() { |
69 | | - return buckets.getStats(); |
70 | | - } |
71 | | - |
72 | | - public void updateCustomFieldValue(final String fieldName, final Bytes value) { |
73 | | - discoveryManager.updateCustomFieldValue(fieldName, value); |
74 | | - } |
75 | | - |
76 | | - /** |
77 | | - * Initiates FINDNODE with node `nodeRecord` |
78 | | - * |
79 | | - * @param nodeRecord Ethereum Node record |
80 | | - * @param distances Distances to search for |
81 | | - * @return Future which is fired when reply is received or fails in timeout/not successful |
82 | | - * handshake/bad message exchange. Contains the collection of nodes returned by the peer. |
83 | | - */ |
84 | | - public CompletableFuture<Collection<NodeRecord>> findNodes( |
85 | | - NodeRecord nodeRecord, List<Integer> distances) { |
86 | | - return discoveryManager.findNodes(nodeRecord, distances); |
87 | | - } |
88 | | - |
89 | | - /** |
90 | | - * Initiates PING with node `nodeRecord` |
91 | | - * |
92 | | - * @param nodeRecord Ethereum Node record |
93 | | - * @return Future which is fired when reply is received or fails in timeout/not successful |
94 | | - * handshake/bad message exchange. |
95 | | - */ |
96 | | - public CompletableFuture<Void> ping(NodeRecord nodeRecord) { |
97 | | - return discoveryManager.ping(nodeRecord); |
98 | | - } |
99 | | - |
100 | | - /** |
101 | | - * Initiates TALK with node `nodeRecord` |
102 | | - * |
103 | | - * @param nodeRecord Ethereum Node record |
104 | | - * @return Promise of the node TALK response. |
105 | | - */ |
106 | | - public CompletableFuture<Bytes> talk(NodeRecord nodeRecord, Bytes protocol, Bytes request) { |
107 | | - return discoveryManager.talk(nodeRecord, protocol, request); |
108 | | - } |
109 | | - |
110 | | - public Stream<NodeRecord> streamLiveNodes() { |
111 | | - return Stream.concat( |
112 | | - buckets.streamClosestNodes(Bytes32.ZERO), discoveryManager.streamActiveSessions()); |
113 | | - } |
114 | | - |
115 | | - public CompletableFuture<Collection<NodeRecord>> searchForNewPeers() { |
116 | | - return taskManager.searchForNewPeers(); |
117 | | - } |
118 | | - |
119 | | - /** |
120 | | - * Lookup node in locally stored KBuckets by its nodeId. Allows lookup of local node record. |
121 | | - * |
122 | | - * @param nodeId NodeId, big endian UInt256 Node ID in bytes |
123 | | - * @return NodeRecord if any found |
124 | | - */ |
125 | | - public Optional<NodeRecord> lookupNode(final Bytes nodeId) { |
126 | | - if (nodeId.equals(getLocalNodeRecord().getNodeId())) { |
127 | | - return Optional.of(getLocalNodeRecord()); |
128 | | - } |
129 | | - return buckets |
130 | | - .streamClosestNodes(nodeId) |
131 | | - .findFirst() |
132 | | - .filter(node -> node.getNodeId().equals(nodeId)); |
133 | | - } |
134 | | - |
135 | | - /** |
136 | | - * Gets all the NodeRecords in the routing table, grouped by their bucket |
137 | | - * |
138 | | - * @return all the NodeRecords in the routing table, grouped by their bucket |
139 | | - */ |
140 | | - public List<List<NodeRecord>> getNodeRecordBuckets() { |
141 | | - return buckets.getNodeRecordBuckets(); |
142 | | - } |
143 | | - |
144 | | - /** |
145 | | - * Adds a NodeRecord to the routing table |
146 | | - * |
147 | | - * @param nodeRecord The NodeRecord to add to the routing table |
148 | | - */ |
149 | | - public void addNodeRecord(NodeRecord nodeRecord) { |
150 | | - buckets.offer(nodeRecord); |
151 | | - } |
152 | 12 |
|
153 | | - /** |
154 | | - * Deletes the NodeRecord identified by nodeId from the routing table |
155 | | - * |
156 | | - * @param nodeId The node ID to be deleted from the routing table |
157 | | - */ |
158 | | - public void deleteNode(Bytes nodeId) { |
159 | | - buckets.deleteNode(nodeId); |
160 | | - } |
| 13 | +public interface DiscoverySystem { |
| 14 | + CompletableFuture<Void> start(); |
| 15 | + void stop(); |
| 16 | + NodeRecord getLocalNodeRecord(); |
| 17 | + BucketStats getBucketStats(); |
| 18 | + void updateCustomFieldValue(final String fieldName, final Bytes value); |
| 19 | + /** |
| 20 | + * Initiates FINDNODE with node `nodeRecord` |
| 21 | + * |
| 22 | + * @param nodeRecord Ethereum Node record |
| 23 | + * @param distances Distances to search for |
| 24 | + * @return Future which is fired when reply is received or fails in timeout/not successful |
| 25 | + * handshake/bad message exchange. Contains the collection of nodes returned by the peer. |
| 26 | + */ |
| 27 | + CompletableFuture<Collection<NodeRecord>> findNodes( |
| 28 | + NodeRecord nodeRecord, List<Integer> distances); |
| 29 | + /** |
| 30 | + * Initiates PING with node `nodeRecord` |
| 31 | + * |
| 32 | + * @param nodeRecord Ethereum Node record |
| 33 | + * @return Future which is fired when reply is received or fails in timeout/not successful |
| 34 | + * handshake/bad message exchange. |
| 35 | + */ |
| 36 | + CompletableFuture<Void> ping(NodeRecord nodeRecord); |
| 37 | + /** |
| 38 | + * Initiates TALK with node `nodeRecord` |
| 39 | + * |
| 40 | + * @param nodeRecord Ethereum Node record |
| 41 | + * @return Promise of the node TALK response. |
| 42 | + */ |
| 43 | + CompletableFuture<Bytes> talk(NodeRecord nodeRecord, Bytes protocol, Bytes request); |
| 44 | + Stream<NodeRecord> streamLiveNodes(); |
| 45 | + CompletableFuture<Collection<NodeRecord>> searchForNewPeers(); |
| 46 | + /** |
| 47 | + * Lookup node in locally stored KBuckets by its nodeId. Allows lookup of local node record. |
| 48 | + * |
| 49 | + * @param nodeId NodeId, big endian UInt256 Node ID in bytes |
| 50 | + * @return NodeRecord if any found |
| 51 | + */ |
| 52 | + Optional<NodeRecord> lookupNode(final Bytes nodeId); |
| 53 | + /** |
| 54 | + * Gets all the NodeRecords in the routing table, grouped by their bucket |
| 55 | + * |
| 56 | + * @return all the NodeRecords in the routing table, grouped by their bucket |
| 57 | + */ |
| 58 | + List<List<NodeRecord>> getNodeRecordBuckets(); |
161 | 59 | } |
0 commit comments