|
| 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 | +} |
0 commit comments