2018-11-23 23:58:49 +00:00
|
|
|
import
|
2018-12-10 12:06:45 +00:00
|
|
|
tables, sets, macros, base64,
|
2018-12-28 16:51:40 +00:00
|
|
|
asyncdispatch2, nimcrypto/sysrand, chronicles, rlp, json_serialization,
|
|
|
|
eth_p2p, eth_p2p/[rlpx, peer_pool],
|
|
|
|
spec/[datatypes, crypto]
|
2018-11-23 23:58:49 +00:00
|
|
|
|
|
|
|
type
|
2018-12-28 16:51:40 +00:00
|
|
|
TopicMsgHandler = proc(msg: string): Future[void]
|
2018-11-26 13:33:06 +00:00
|
|
|
|
2018-12-28 16:51:40 +00:00
|
|
|
GossipSubPeer* = ref object
|
2018-11-29 01:08:34 +00:00
|
|
|
sentMessages: HashSet[string]
|
2018-12-10 12:06:45 +00:00
|
|
|
subscribedFor: HashSet[string]
|
2018-11-29 01:08:34 +00:00
|
|
|
|
2018-12-28 16:51:40 +00:00
|
|
|
GossipSubNetwork* = ref object
|
2018-12-10 12:06:45 +00:00
|
|
|
topicSubscribers: Table[string, TopicMsgHandler]
|
2018-12-28 16:51:40 +00:00
|
|
|
handledMessages: HashSet[string]
|
2018-12-10 12:06:45 +00:00
|
|
|
|
2018-12-28 16:51:40 +00:00
|
|
|
proc initProtocolState*(network: GossipSubNetwork, _: EthereumNode) =
|
2018-12-10 12:06:45 +00:00
|
|
|
network.topicSubscribers = initTable[string, TopicMsgHandler]()
|
2018-12-28 16:51:40 +00:00
|
|
|
network.handledMessages = initSet[string]()
|
2018-12-10 12:06:45 +00:00
|
|
|
|
2018-12-28 16:51:40 +00:00
|
|
|
proc initProtocolState*(peer: GossipSubPeer, _: Peer) =
|
2018-12-10 12:06:45 +00:00
|
|
|
peer.sentMessages = initSet[string]()
|
|
|
|
peer.subscribedFor = initSet[string]()
|
2018-11-23 23:58:49 +00:00
|
|
|
|
2018-11-29 01:08:34 +00:00
|
|
|
p2pProtocol GossipSub(version = 1,
|
|
|
|
shortName = "gss",
|
2018-12-28 16:51:40 +00:00
|
|
|
peerState = GossipSubPeer,
|
2018-11-29 01:08:34 +00:00
|
|
|
networkState = GossipSubNetwork):
|
2018-11-26 13:33:06 +00:00
|
|
|
# This is a very barebones emulation of the GossipSub protocol
|
|
|
|
# available in LibP2P:
|
|
|
|
|
2018-12-28 16:51:40 +00:00
|
|
|
onPeerConnected do (peer: Peer):
|
|
|
|
info "GossipSub Peer connecetd", peer
|
|
|
|
let gossipNet = peer.networkState
|
|
|
|
for topic, _ in gossipNet.topicSubscribers:
|
2019-01-05 21:01:26 +00:00
|
|
|
asyncCheck peer.subscribeFor(topic)
|
2018-12-28 16:51:40 +00:00
|
|
|
|
|
|
|
onPeerDisconnected do (peer: Peer, reason: DisconnectionReason):
|
2019-01-05 21:01:26 +00:00
|
|
|
info "GossipSub Peer disconnected", peer, reason
|
2018-12-28 16:51:40 +00:00
|
|
|
writeStackTrace()
|
|
|
|
|
2018-12-10 12:06:45 +00:00
|
|
|
proc subscribeFor(peer: Peer, topic: string) =
|
|
|
|
peer.state.subscribedFor.incl topic
|
|
|
|
|
2018-12-28 16:51:40 +00:00
|
|
|
proc emit(peer: Peer, topic: string, msgId: string, msg: string) =
|
|
|
|
if msgId in peer.networkState.handledMessages:
|
2019-01-05 21:01:26 +00:00
|
|
|
trace "Ignored previously handled message", msgId
|
2018-12-28 16:51:40 +00:00
|
|
|
return
|
|
|
|
|
|
|
|
peer.networkState.handledMessages.incl msgId
|
|
|
|
|
2018-12-10 12:06:45 +00:00
|
|
|
for p in peer.network.peers(GossipSub):
|
|
|
|
if msgId notin p.state.sentMessages and topic in p.state.subscribedFor:
|
2018-12-28 16:51:40 +00:00
|
|
|
p.state.sentMessages.incl msgId
|
|
|
|
asyncCheck p.emit(topic, msgId, msg)
|
2018-12-10 12:06:45 +00:00
|
|
|
|
|
|
|
let handler = peer.networkState.topicSubscribers.getOrDefault(topic)
|
|
|
|
if handler != nil:
|
2018-12-28 16:51:40 +00:00
|
|
|
await handler(msg)
|
2018-11-26 13:33:06 +00:00
|
|
|
|
|
|
|
proc subscribeImpl(node: EthereumNode,
|
|
|
|
topic: string,
|
|
|
|
subscriber: TopicMsgHandler) =
|
2018-12-10 12:06:45 +00:00
|
|
|
var gossipNet = node.protocolState(GossipSub)
|
|
|
|
gossipNet.topicSubscribers[topic] = subscriber
|
|
|
|
|
2018-12-28 16:51:40 +00:00
|
|
|
for peer in node.peers(GossipSub):
|
|
|
|
discard peer.subscribeFor(topic)
|
|
|
|
|
|
|
|
proc broadcastImpl(node: EthereumNode, topic: string, msg: string): seq[Future[void]] {.gcsafe.} =
|
2018-12-10 12:06:45 +00:00
|
|
|
var randBytes: array[10, byte];
|
|
|
|
if randomBytes(randBytes) != 10:
|
|
|
|
warn "Failed to generate random message id"
|
2018-12-28 16:51:40 +00:00
|
|
|
|
2018-12-10 12:06:45 +00:00
|
|
|
let msgId = base64.encode(randBytes)
|
2019-01-05 21:01:26 +00:00
|
|
|
trace "Sending GossipSub message", msgId
|
2018-12-10 12:06:45 +00:00
|
|
|
|
|
|
|
for peer in node.peers(GossipSub):
|
|
|
|
if topic in peer.state(GossipSub).subscribedFor:
|
2018-12-28 16:51:40 +00:00
|
|
|
result.add peer.emit(topic, msgId, msg)
|
2018-11-26 13:33:06 +00:00
|
|
|
|
2018-12-10 12:06:45 +00:00
|
|
|
proc makeMessageHandler[MsgType](userHandler: proc(msg: MsgType): Future[void]): TopicMsgHandler =
|
2018-12-28 16:51:40 +00:00
|
|
|
result = proc (msg: string): Future[void] =
|
|
|
|
userHandler Json.decode(msg, MsgType)
|
2018-11-26 13:33:06 +00:00
|
|
|
|
2018-11-29 01:08:34 +00:00
|
|
|
macro subscribe*(node: EthereumNode, topic: string, handler: untyped): untyped =
|
2018-12-10 12:06:45 +00:00
|
|
|
handler.addPragma ident"async"
|
|
|
|
result = newCall(bindSym"subscribeImpl",
|
|
|
|
node, topic, newCall(bindSym"makeMessageHandler", handler))
|
2018-11-26 13:33:06 +00:00
|
|
|
|
2018-12-28 16:51:40 +00:00
|
|
|
proc broadcast*(node: EthereumNode, topic: string, msg: auto) {.async.} =
|
|
|
|
await all(node.broadcastImpl(topic, Json.encode(msg)))
|
2018-11-23 23:58:49 +00:00
|
|
|
|