Simplified gossipsub.broadcast further
This commit is contained in:
parent
dd72218f48
commit
6d4470a8c8
|
@ -1,6 +1,5 @@
|
||||||
import
|
import
|
||||||
options, chronos, json_serialization, strutils,
|
options, chronos, json_serialization, strutils,
|
||||||
chronicles,
|
|
||||||
spec/digest, version, conf
|
spec/digest, version, conf
|
||||||
|
|
||||||
const
|
const
|
||||||
|
|
|
@ -64,30 +64,18 @@ p2pProtocol GossipSub(version = 1,
|
||||||
if handler != nil:
|
if handler != nil:
|
||||||
handler(msg)
|
handler(msg)
|
||||||
|
|
||||||
proc broadcastIMPL(node: EthereumNode, topic: string, msg: string): seq[Future[void]] {.gcsafe.} =
|
|
||||||
var randBytes: array[10, byte];
|
|
||||||
if randomBytes(randBytes) != 10:
|
|
||||||
warn "Failed to generate random message id"
|
|
||||||
|
|
||||||
let msgId = base64.encode(randBytes)
|
|
||||||
trace "Sending GossipSub message", msgId
|
|
||||||
|
|
||||||
for peer in node.peers(GossipSub):
|
|
||||||
if topic in peer.state(GossipSub).subscribedFor:
|
|
||||||
result.add peer.tryEmitting(topic, msgId, msg)
|
|
||||||
|
|
||||||
proc trySubscribing(peer: Peer, topic: string) =
|
proc trySubscribing(peer: Peer, topic: string) =
|
||||||
var fut = peer.subscribeFor(topic)
|
var fut = peer.subscribeFor(topic)
|
||||||
fut.addCallback do (arg: pointer):
|
fut.addCallback do (arg: pointer):
|
||||||
if fut.failed:
|
if fut.failed:
|
||||||
warn "Failed to subscribe to topic with GossipSub peer", topic, peer
|
debug "Failed to subscribe to topic with GossipSub peer", topic, peer
|
||||||
|
|
||||||
proc tryEmitting(peer: Peer, topic: string,
|
proc tryEmitting(peer: Peer, topic: string,
|
||||||
msgId: string, msg: string): Future[void] =
|
msgId: string, msg: string): Future[void] =
|
||||||
var fut = peer.emit(topic, msgId, msg)
|
var fut = peer.emit(topic, msgId, msg)
|
||||||
fut.addCallback do (arg: pointer):
|
fut.addCallback do (arg: pointer):
|
||||||
if fut.failed:
|
if fut.failed:
|
||||||
warn "GossipSub message not delivered to Peer", peer
|
debug "GossipSub message not delivered to Peer", peer
|
||||||
return fut
|
return fut
|
||||||
|
|
||||||
proc subscribe*[MsgType](node: EthereumNode,
|
proc subscribe*[MsgType](node: EthereumNode,
|
||||||
|
@ -101,8 +89,15 @@ proc subscribe*[MsgType](node: EthereumNode,
|
||||||
peer.trySubscribing(topic)
|
peer.trySubscribing(topic)
|
||||||
|
|
||||||
proc broadcast*(node: EthereumNode, topic: string, msg: auto) =
|
proc broadcast*(node: EthereumNode, topic: string, msg: auto) =
|
||||||
# We are intentionally using `yield` here, so the broadcast call can
|
var randBytes: array[10, byte];
|
||||||
# never fail. Please note that errors are logged through a callback
|
if randomBytes(randBytes) != 10:
|
||||||
# set in `tryEmitting`
|
warn "Failed to generate random message id"
|
||||||
traceAsyncErrors all(node.broadcastIMPL(topic, Json.encode(msg)))
|
|
||||||
|
let msg = Json.encode(msg)
|
||||||
|
let msgId = base64.encode(randBytes)
|
||||||
|
trace "Sending GossipSub message", msgId
|
||||||
|
|
||||||
|
for peer in node.peers(GossipSub):
|
||||||
|
if topic in peer.state(GossipSub).subscribedFor:
|
||||||
|
traceAsyncErrors peer.tryEmitting(topic, msgId, msg)
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue