mirror of
https://github.com/logos-storage/logos-storage-nim.git
synced 2026-06-27 21:09:28 +00:00
Add spr method to compute with UDP and TCP records
This commit is contained in:
parent
9b3c79037a
commit
fac2113381
@ -51,12 +51,12 @@ proc getDebug(
|
||||
): Future[Result[string, string]] {.async: (raises: []).} =
|
||||
let node = storage[].node
|
||||
let table = RestRoutingTable.init(node.discovery.protocol.routingTable)
|
||||
let nodeSpr = node.discovery.getSpr()
|
||||
|
||||
let json = %*{
|
||||
"id": $node.switch.peerInfo.peerId,
|
||||
"addrs": node.switch.peerInfo.addrs.mapIt($it),
|
||||
"spr":
|
||||
if node.discovery.dhtRecord.isSome: node.discovery.dhtRecord.get.toURI else: "",
|
||||
"spr": if nodeSpr.isSome: nodeSpr.get.toURI else: "",
|
||||
"announceAddresses": node.discovery.announceAddrs,
|
||||
"table": table,
|
||||
"nat": {
|
||||
|
||||
@ -38,7 +38,7 @@ proc getRepo(
|
||||
proc getSpr(
|
||||
storage: ptr StorageServer
|
||||
): Future[Result[string, string]] {.async: (raises: []).} =
|
||||
let spr = storage[].node.discovery.dhtRecord
|
||||
let spr = storage[].node.discovery.getSpr()
|
||||
if spr.isNone:
|
||||
return err("Failed to get SPR: no SPR record found.")
|
||||
|
||||
|
||||
@ -176,6 +176,27 @@ method removeProvider*(
|
||||
warn "Error removing provider", peerId = peerId, exc = exc.msg
|
||||
raiseAssert("Unexpected Exception in removeProvider")
|
||||
|
||||
proc getSpr*(d: Discovery): ?SignedPeerRecord =
|
||||
## Combined TCP+UDP record for bootstrap use by connecting nodes.
|
||||
without providerRecord =? d.providerRecord:
|
||||
return none(SignedPeerRecord)
|
||||
|
||||
without dhtRecord =? d.dhtRecord:
|
||||
return none(SignedPeerRecord)
|
||||
|
||||
let tcpAddrs = providerRecord.data.addresses.mapIt(it.address)
|
||||
let udpAddrs = dhtRecord.data.addresses.mapIt(it.address)
|
||||
|
||||
SignedPeerRecord
|
||||
.init(d.key, PeerRecord.init(d.peerId, tcpAddrs & udpAddrs))
|
||||
.expect("Should construct signed record").some
|
||||
|
||||
proc updateSpr(d: Discovery) =
|
||||
if not d.protocol.isNil:
|
||||
let spr = d.getSpr()
|
||||
if spr.isSome:
|
||||
d.protocol.updateRecord(spr).expect("Should update SPR")
|
||||
|
||||
proc updateRecords*(
|
||||
d: Discovery, announceAddrs: openArray[MultiAddress], discoveryPort: Port
|
||||
) =
|
||||
@ -193,11 +214,10 @@ proc updateRecords*(
|
||||
.init(d.key, PeerRecord.init(d.peerId, tcpAddrs))
|
||||
.expect("Should construct signed record").some
|
||||
d.dhtRecord = SignedPeerRecord
|
||||
.init(d.key, PeerRecord.init(d.peerId, tcpAddrs & udpAddrs))
|
||||
.init(d.key, PeerRecord.init(d.peerId, udpAddrs))
|
||||
.expect("Should construct signed record").some
|
||||
|
||||
if not d.protocol.isNil:
|
||||
d.protocol.updateRecord(d.dhtRecord).expect("Should update SPR")
|
||||
d.updateSpr()
|
||||
|
||||
proc updateAnnounceRecord*(d: Discovery, addrs: openArray[MultiAddress]) =
|
||||
# Updates announce addresses only, not the DHT routing record.
|
||||
@ -207,8 +227,8 @@ proc updateAnnounceRecord*(d: Discovery, addrs: openArray[MultiAddress]) =
|
||||
d.providerRecord = SignedPeerRecord
|
||||
.init(d.key, PeerRecord.init(d.peerId, d.announceAddrs))
|
||||
.expect("Should construct signed record").some
|
||||
if not d.protocol.isNil:
|
||||
d.protocol.updateRecord(d.providerRecord).expect("Should update SPR")
|
||||
|
||||
d.updateSpr()
|
||||
|
||||
proc updateDhtRecord*(
|
||||
d: Discovery, addrs: openArray[MultiAddress]
|
||||
@ -217,8 +237,8 @@ proc updateDhtRecord*(
|
||||
d.dhtRecord = SignedPeerRecord
|
||||
.init(d.key, PeerRecord.init(d.peerId, @addrs))
|
||||
.expect("Should construct signed record").some
|
||||
if not d.protocol.isNil:
|
||||
d.protocol.updateRecord(d.dhtRecord).expect("Should update SPR")
|
||||
|
||||
d.updateSpr()
|
||||
|
||||
proc start*(d: Discovery) {.async: (raises: []).} =
|
||||
try:
|
||||
|
||||
@ -25,10 +25,11 @@ import pkg/libp2p
|
||||
import pkg/libp2p/routing_record
|
||||
import pkg/libp2p/protocols/connectivity/autonatv2/service
|
||||
import pkg/libp2p/services/autorelayservice
|
||||
import pkg/codexdht/discv5/spr as spr
|
||||
import pkg/codexdht/discv5/spr
|
||||
|
||||
import ../logutils
|
||||
import ../node
|
||||
import ../discovery
|
||||
import ../blocktype
|
||||
import ../storagetypes
|
||||
import ../conf
|
||||
@ -487,7 +488,7 @@ proc initNodeApi(node: StorageNodeRef, conf: StorageConf, router: var RestRouter
|
||||
var headers = buildCorsHeaders("GET", allowedOrigin)
|
||||
|
||||
try:
|
||||
without spr =? node.discovery.dhtRecord:
|
||||
without spr =? node.discovery.getSpr():
|
||||
return RestApiResponse.response(
|
||||
"", status = Http503, contentType = "application/json", headers = headers
|
||||
)
|
||||
@ -577,13 +578,13 @@ proc initDebugApi(
|
||||
|
||||
try:
|
||||
let table = RestRoutingTable.init(node.discovery.protocol.routingTable)
|
||||
let nodeSpr = node.discovery.getSpr()
|
||||
|
||||
let json = %*{
|
||||
"id": $node.switch.peerInfo.peerId,
|
||||
"addrs": node.switch.peerInfo.addrs.mapIt($it),
|
||||
"repo": $conf.dataDir,
|
||||
"spr":
|
||||
if node.discovery.dhtRecord.isSome: node.discovery.dhtRecord.get.toURI else: "",
|
||||
"spr": if nodeSpr.isSome: nodeSpr.get.toURI else: "",
|
||||
"announceAddresses": node.discovery.announceAddrs,
|
||||
"table": table,
|
||||
"storage": {"version": $storageVersion, "revision": $storageRevision},
|
||||
|
||||
@ -98,13 +98,8 @@ proc start*(s: StorageServer) {.async.} =
|
||||
)
|
||||
]
|
||||
else:
|
||||
# If extip is not set, we have 2 choices:
|
||||
# 1- Announce the peer addrs contains detected addresses on the machine.
|
||||
# 2- Wait for AutoNat
|
||||
# The problem with 1 is that you will certainly announce private addresses
|
||||
# and if you advertise a CID, you will advertise these private addresses.
|
||||
# Don't announce address and wait for AutoNat
|
||||
# TODO: DHT client mode
|
||||
#s.storageNode.switch.peerInfo.addrs
|
||||
@[]
|
||||
|
||||
s.storageNode.discovery.updateRecords(announceAddrs, s.config.discoveryPort)
|
||||
@ -114,6 +109,7 @@ proc start*(s: StorageServer) {.async.} =
|
||||
for spr in findReachableNodes(s.config.bootstrapNodes):
|
||||
try:
|
||||
let addrs = spr.data.addresses.mapIt(it.address)
|
||||
echo "addrs", addrs
|
||||
await s.storageNode.switch.connect(spr.data.peerId, addrs)
|
||||
except CatchableError as e:
|
||||
warn "Cannot connect to bootstrap node", error = e.msg
|
||||
|
||||
@ -225,8 +225,8 @@ proc generateNodes*(
|
||||
if config.enableBootstrap:
|
||||
waitFor switch.peerInfo.update()
|
||||
blockDiscovery.updateRecords(switch.peerInfo.addrs, bindPort.Port)
|
||||
if blockDiscovery.dhtRecord.isSome:
|
||||
bootstrapNodes.add !blockDiscovery.dhtRecord
|
||||
if blockDiscovery.getSpr().isSome:
|
||||
bootstrapNodes.add !blockDiscovery.getSpr()
|
||||
|
||||
fullNode
|
||||
else:
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user