2021-05-11 07:37:33 +00:00
|
|
|
# nim-eth - Whisper
|
|
|
|
# Copyright (c) 2018-2021 Status Research & Development GmbH
|
|
|
|
# Licensed and distributed under either of
|
|
|
|
# * MIT license (license terms in the root directory or at https://opensource.org/licenses/MIT).
|
|
|
|
# * Apache v2 license (license terms in the root directory or at https://www.apache.org/licenses/LICENSE-2.0).
|
|
|
|
# at your option. This file may not be copied, modified, or distributed except according to those terms.
|
2019-10-03 13:42:35 +00:00
|
|
|
|
2019-02-05 15:40:29 +00:00
|
|
|
## Whisper
|
2019-10-03 13:42:35 +00:00
|
|
|
## *******
|
2019-02-05 15:40:29 +00:00
|
|
|
##
|
|
|
|
## Whisper is a gossip protocol that synchronizes a set of messages across nodes
|
|
|
|
## with attention given to sender and recipient anonymitiy. Messages are
|
|
|
|
## categorized by a topic and stay alive in the network based on a time-to-live
|
|
|
|
## measured in seconds. Spam prevention is based on proof-of-work, where large
|
|
|
|
## or long-lived messages must spend more work.
|
2019-10-03 13:42:35 +00:00
|
|
|
##
|
|
|
|
## Example usage
|
|
|
|
## ----------
|
|
|
|
## First an `EthereumNode` needs to be created, either with all capabilities set
|
|
|
|
## or with specifically the Whisper capability set.
|
|
|
|
## The latter can be done like this:
|
|
|
|
##
|
|
|
|
## .. code-block::nim
|
|
|
|
## var node = newEthereumNode(keypair, address, netId, nil,
|
|
|
|
## addAllCapabilities = false)
|
|
|
|
## node.addCapability Whisper
|
|
|
|
##
|
|
|
|
## Now calls such as ``postMessage`` and ``subscribeFilter`` can be done.
|
|
|
|
## However, they only make real sense after ``connectToNetwork`` was started. As
|
|
|
|
## else there will be no peers to send and receive messages from.
|
2019-02-05 15:40:29 +00:00
|
|
|
|
2021-07-16 19:44:30 +00:00
|
|
|
{.push raises: [Defect].}
|
|
|
|
|
2019-02-05 15:40:29 +00:00
|
|
|
import
|
2021-04-06 11:33:24 +00:00
|
|
|
std/[options, tables, times],
|
|
|
|
chronos, chronicles, metrics,
|
|
|
|
".."/../[keys, async_utils, p2p],
|
|
|
|
./whisper/whisper_types
|
2019-11-19 09:24:20 +00:00
|
|
|
|
|
|
|
export
|
|
|
|
whisper_types
|
2019-02-05 15:40:29 +00:00
|
|
|
|
2019-04-26 13:36:54 +00:00
|
|
|
logScope:
|
|
|
|
topics = "whisper"
|
|
|
|
|
2019-02-05 15:40:29 +00:00
|
|
|
const
|
2020-01-14 17:17:37 +00:00
|
|
|
defaultQueueCapacity = 2048
|
2019-10-03 13:42:35 +00:00
|
|
|
whisperVersion* = 6 ## Whisper version.
|
|
|
|
whisperVersionStr* = $whisperVersion ## Whisper version.
|
|
|
|
defaultMinPow* = 0.2'f64 ## The default minimum PoW requirement for this node.
|
|
|
|
defaultMaxMsgSize* = 1024'u32 * 1024'u32 ## The current default and max
|
|
|
|
## message size. This can never be larger than the maximum RLPx message size.
|
|
|
|
messageInterval* = chronos.milliseconds(300) ## Interval at which messages are
|
|
|
|
## send to peers, in ms.
|
|
|
|
pruneInterval* = chronos.milliseconds(1000) ## Interval at which message
|
|
|
|
## queue is pruned, in ms.
|
2019-02-05 15:40:29 +00:00
|
|
|
|
|
|
|
type
|
|
|
|
WhisperConfig* = object
|
|
|
|
powRequirement*: float64
|
|
|
|
bloom*: Bloom
|
|
|
|
isLightNode*: bool
|
|
|
|
maxMsgSize*: uint32
|
|
|
|
|
2019-11-19 09:24:20 +00:00
|
|
|
WhisperPeer = ref object
|
|
|
|
initialized: bool # when successfully completed the handshake
|
|
|
|
powRequirement*: float64
|
|
|
|
bloom*: Bloom
|
|
|
|
isLightNode*: bool
|
|
|
|
trusted*: bool
|
2020-01-23 12:27:15 +00:00
|
|
|
received: HashSet[Hash]
|
2019-02-05 15:40:29 +00:00
|
|
|
|
2019-11-19 09:24:20 +00:00
|
|
|
WhisperNetwork = ref object
|
2019-11-19 16:22:35 +00:00
|
|
|
queue*: ref Queue
|
2019-11-19 09:24:20 +00:00
|
|
|
filters*: Filters
|
|
|
|
config*: WhisperConfig
|
2019-02-05 15:40:29 +00:00
|
|
|
|
|
|
|
proc allowed*(msg: Message, config: WhisperConfig): bool =
|
|
|
|
# Check max msg size, already happens in RLPx but there is a specific shh
|
|
|
|
# max msg size which should always be < RLPx max msg size
|
|
|
|
if msg.size > config.maxMsgSize:
|
2020-06-09 08:29:16 +00:00
|
|
|
envelopes_dropped.inc(labelValues = ["too_large"])
|
2019-02-05 15:40:29 +00:00
|
|
|
warn "Message size too large", size = msg.size
|
|
|
|
return false
|
|
|
|
|
|
|
|
if msg.pow < config.powRequirement:
|
2020-06-09 08:29:16 +00:00
|
|
|
envelopes_dropped.inc(labelValues = ["low_pow"])
|
2019-02-05 15:40:29 +00:00
|
|
|
warn "Message PoW too low", pow = msg.pow, minPow = config.powRequirement
|
|
|
|
return false
|
|
|
|
|
|
|
|
if not bloomFilterMatch(config.bloom, msg.bloom):
|
2020-06-09 08:29:16 +00:00
|
|
|
envelopes_dropped.inc(labelValues = ["bloom_filter_mismatch"])
|
2019-02-05 15:40:29 +00:00
|
|
|
warn "Message does not match node bloom filter"
|
|
|
|
return false
|
|
|
|
|
|
|
|
return true
|
|
|
|
|
2021-05-06 15:20:54 +00:00
|
|
|
proc run(peer: Peer) {.gcsafe, async, raises: [Defect].}
|
|
|
|
proc run(node: EthereumNode, network: WhisperNetwork)
|
|
|
|
{.gcsafe, async, raises: [Defect].}
|
2019-02-05 15:40:29 +00:00
|
|
|
|
|
|
|
proc initProtocolState*(network: WhisperNetwork, node: EthereumNode) {.gcsafe.} =
|
2019-11-19 16:22:35 +00:00
|
|
|
new(network.queue)
|
|
|
|
network.queue[] = initQueue(defaultQueueCapacity)
|
2019-02-05 15:40:29 +00:00
|
|
|
network.filters = initTable[string, Filter]()
|
|
|
|
network.config.bloom = fullBloom()
|
|
|
|
network.config.powRequirement = defaultMinPow
|
|
|
|
network.config.isLightNode = false
|
|
|
|
network.config.maxMsgSize = defaultMaxMsgSize
|
2021-05-06 15:20:54 +00:00
|
|
|
asyncSpawn node.run(network)
|
2019-02-05 15:40:29 +00:00
|
|
|
|
|
|
|
p2pProtocol Whisper(version = whisperVersion,
|
2019-08-05 13:31:51 +00:00
|
|
|
rlpxName = "shh",
|
2019-02-05 15:40:29 +00:00
|
|
|
peerState = WhisperPeer,
|
|
|
|
networkState = WhisperNetwork):
|
|
|
|
|
|
|
|
onPeerConnected do (peer: Peer):
|
2019-04-26 13:36:54 +00:00
|
|
|
trace "onPeerConnected Whisper"
|
2019-02-05 15:40:29 +00:00
|
|
|
let
|
|
|
|
whisperNet = peer.networkState
|
|
|
|
whisperPeer = peer.state
|
|
|
|
|
2019-05-19 18:05:02 +00:00
|
|
|
let m = await peer.status(whisperVersion,
|
2019-12-17 15:53:39 +00:00
|
|
|
cast[uint64](whisperNet.config.powRequirement),
|
2019-05-19 18:05:02 +00:00
|
|
|
@(whisperNet.config.bloom),
|
|
|
|
whisperNet.config.isLightNode,
|
2020-02-06 18:19:54 +00:00
|
|
|
timeout = chronos.milliseconds(5000))
|
2019-02-05 15:40:29 +00:00
|
|
|
|
|
|
|
if m.protocolVersion == whisperVersion:
|
|
|
|
debug "Whisper peer", peer, whisperVersion
|
|
|
|
else:
|
|
|
|
raise newException(UselessPeerError, "Incompatible Whisper version")
|
|
|
|
|
|
|
|
whisperPeer.powRequirement = cast[float64](m.powConverted)
|
|
|
|
|
|
|
|
if m.bloom.len > 0:
|
|
|
|
if m.bloom.len != bloomSize:
|
|
|
|
raise newException(UselessPeerError, "Bloomfilter size mismatch")
|
|
|
|
else:
|
|
|
|
whisperPeer.bloom.bytesCopy(m.bloom)
|
|
|
|
else:
|
|
|
|
# If no bloom filter is send we allow all
|
|
|
|
whisperPeer.bloom = fullBloom()
|
|
|
|
|
|
|
|
whisperPeer.isLightNode = m.isLightNode
|
|
|
|
if whisperPeer.isLightNode and whisperNet.config.isLightNode:
|
|
|
|
# No sense in connecting two light nodes so we disconnect
|
|
|
|
raise newException(UselessPeerError, "Two light nodes connected")
|
|
|
|
|
|
|
|
whisperPeer.received.init()
|
|
|
|
whisperPeer.trusted = false
|
|
|
|
whisperPeer.initialized = true
|
|
|
|
|
|
|
|
if not whisperNet.config.isLightNode:
|
2021-05-06 15:20:54 +00:00
|
|
|
asyncSpawn peer.run()
|
2019-02-05 15:40:29 +00:00
|
|
|
|
2019-06-12 13:20:47 +00:00
|
|
|
debug "Whisper peer initialized", peer
|
2019-02-05 15:40:29 +00:00
|
|
|
|
2019-05-19 18:05:02 +00:00
|
|
|
handshake:
|
|
|
|
proc status(peer: Peer,
|
|
|
|
protocolVersion: uint,
|
2019-12-17 15:53:39 +00:00
|
|
|
powConverted: uint64,
|
2020-04-20 18:14:39 +00:00
|
|
|
bloom: seq[byte],
|
2019-05-19 18:05:02 +00:00
|
|
|
isLightNode: bool)
|
2019-02-05 15:40:29 +00:00
|
|
|
|
|
|
|
proc messages(peer: Peer, envelopes: openarray[Envelope]) =
|
|
|
|
if not peer.state.initialized:
|
|
|
|
warn "Handshake not completed yet, discarding messages"
|
|
|
|
return
|
|
|
|
|
|
|
|
for envelope in envelopes:
|
|
|
|
# check if expired or in future, or ttl not 0
|
|
|
|
if not envelope.valid():
|
2019-04-26 13:36:54 +00:00
|
|
|
warn "Expired or future timed envelope", peer
|
2019-02-05 15:40:29 +00:00
|
|
|
# disconnect from peers sending bad envelopes
|
|
|
|
# await peer.disconnect(SubprotocolReason)
|
|
|
|
continue
|
|
|
|
|
|
|
|
let msg = initMessage(envelope)
|
|
|
|
if not msg.allowed(peer.networkState.config):
|
|
|
|
# disconnect from peers sending bad envelopes
|
|
|
|
# await peer.disconnect(SubprotocolReason)
|
|
|
|
continue
|
|
|
|
|
|
|
|
# This peer send this message thus should not receive it again.
|
|
|
|
# If this peer has the message in the `received` set already, this means
|
|
|
|
# it was either already received here from this peer or send to this peer.
|
|
|
|
# Either way it will be in our queue already (and the peer should know
|
|
|
|
# this) and this peer is sending duplicates.
|
2019-04-26 13:36:54 +00:00
|
|
|
# Note: geth does not check if a peer has send a message to them before
|
|
|
|
# broadcasting this message. This too is seen here as a duplicate message
|
|
|
|
# (see above comment). If we want to seperate these cases (e.g. when peer
|
|
|
|
# rating), then we have to add a "peer.state.send" HashSet.
|
2020-01-23 14:28:24 +00:00
|
|
|
# Note: it could also be a race between the arrival of a message send by
|
|
|
|
# this node to a peer and that same message arriving from that peer (after
|
|
|
|
# it was received from another peer) here.
|
2020-01-23 12:27:15 +00:00
|
|
|
if peer.state.received.containsOrIncl(msg.hash):
|
2020-06-09 08:29:16 +00:00
|
|
|
envelopes_dropped.inc(labelValues = ["duplicate"])
|
2020-01-23 12:27:15 +00:00
|
|
|
trace "Peer sending duplicate messages", peer, hash = $msg.hash
|
2019-02-05 15:40:29 +00:00
|
|
|
# await peer.disconnect(SubprotocolReason)
|
|
|
|
continue
|
|
|
|
|
|
|
|
# This can still be a duplicate message, but from another peer than
|
|
|
|
# the peer who send the message.
|
2019-11-19 16:22:35 +00:00
|
|
|
if peer.networkState.queue[].add(msg):
|
2019-02-05 15:40:29 +00:00
|
|
|
# notify filters of this message
|
|
|
|
peer.networkState.filters.notify(msg)
|
|
|
|
|
2019-12-17 15:53:39 +00:00
|
|
|
proc powRequirement(peer: Peer, value: uint64) =
|
2019-02-05 15:40:29 +00:00
|
|
|
if not peer.state.initialized:
|
|
|
|
warn "Handshake not completed yet, discarding powRequirement"
|
|
|
|
return
|
|
|
|
|
|
|
|
peer.state.powRequirement = cast[float64](value)
|
|
|
|
|
2020-04-20 18:14:39 +00:00
|
|
|
proc bloomFilterExchange(peer: Peer, bloom: openArray[byte]) =
|
2019-02-05 15:40:29 +00:00
|
|
|
if not peer.state.initialized:
|
|
|
|
warn "Handshake not completed yet, discarding bloomFilterExchange"
|
|
|
|
return
|
|
|
|
|
2019-10-03 10:09:00 +00:00
|
|
|
if bloom.len == bloomSize:
|
|
|
|
peer.state.bloom.bytesCopy(bloom)
|
2019-02-05 15:40:29 +00:00
|
|
|
|
|
|
|
nextID 126
|
|
|
|
|
|
|
|
proc p2pRequest(peer: Peer, envelope: Envelope) =
|
|
|
|
# TODO: here we would have to allow to insert some specific implementation
|
|
|
|
# such as e.g. Whisper Mail Server
|
|
|
|
discard
|
|
|
|
|
|
|
|
proc p2pMessage(peer: Peer, envelope: Envelope) =
|
|
|
|
if peer.state.trusted:
|
|
|
|
# when trusted we can bypass any checks on envelope
|
|
|
|
let msg = Message(env: envelope, isP2P: true)
|
|
|
|
peer.networkState.filters.notify(msg)
|
|
|
|
|
2019-04-26 13:36:54 +00:00
|
|
|
# Following message IDs are not part of EIP-627, but are added and used by
|
|
|
|
# the Status application, we ignore them for now.
|
|
|
|
nextID 11
|
|
|
|
proc batchAcknowledged(peer: Peer) = discard
|
|
|
|
proc messageResponse(peer: Peer) = discard
|
2019-05-30 15:11:23 +00:00
|
|
|
|
2019-04-26 13:36:54 +00:00
|
|
|
nextID 123
|
|
|
|
requestResponse:
|
|
|
|
proc p2pSyncRequest(peer: Peer) = discard
|
|
|
|
proc p2pSyncResponse(peer: Peer) = discard
|
2019-05-30 15:11:23 +00:00
|
|
|
|
2019-04-26 13:36:54 +00:00
|
|
|
proc p2pRequestComplete(peer: Peer) = discard
|
|
|
|
|
2019-02-05 15:40:29 +00:00
|
|
|
# 'Runner' calls ---------------------------------------------------------------
|
|
|
|
|
2021-07-16 19:44:30 +00:00
|
|
|
proc processQueue(peer: Peer) =
|
2019-05-15 08:45:15 +00:00
|
|
|
# Send to peer all valid and previously not send envelopes in the queue.
|
2019-02-05 15:40:29 +00:00
|
|
|
var
|
|
|
|
envelopes: seq[Envelope] = @[]
|
|
|
|
whisperPeer = peer.state(Whisper)
|
|
|
|
whisperNet = peer.networkState(Whisper)
|
|
|
|
|
|
|
|
for message in whisperNet.queue.items:
|
2020-01-23 12:27:15 +00:00
|
|
|
if whisperPeer.received.contains(message.hash):
|
|
|
|
# trace "message was already send to peer", hash = $message.hash, peer
|
2019-02-05 15:40:29 +00:00
|
|
|
continue
|
|
|
|
|
|
|
|
if message.pow < whisperPeer.powRequirement:
|
2020-01-14 18:02:34 +00:00
|
|
|
trace "Message PoW too low for peer", pow = message.pow,
|
2019-04-26 13:36:54 +00:00
|
|
|
powReq = whisperPeer.powRequirement
|
2019-02-05 15:40:29 +00:00
|
|
|
continue
|
|
|
|
|
|
|
|
if not bloomFilterMatch(whisperPeer.bloom, message.bloom):
|
2020-01-14 18:02:34 +00:00
|
|
|
trace "Message does not match peer bloom filter"
|
2019-02-05 15:40:29 +00:00
|
|
|
continue
|
|
|
|
|
2019-04-26 13:36:54 +00:00
|
|
|
trace "Adding envelope"
|
2019-02-05 15:40:29 +00:00
|
|
|
envelopes.add(message.env)
|
2020-01-23 12:27:15 +00:00
|
|
|
whisperPeer.received.incl(message.hash)
|
2019-02-05 15:40:29 +00:00
|
|
|
|
2019-12-03 15:49:20 +00:00
|
|
|
if envelopes.len() > 0:
|
|
|
|
trace "Sending envelopes", amount=envelopes.len
|
|
|
|
# Ignore failure of sending messages, this could occur when the connection
|
|
|
|
# gets dropped
|
|
|
|
traceAsyncErrors peer.messages(envelopes)
|
2019-02-05 15:40:29 +00:00
|
|
|
|
2021-07-16 19:44:30 +00:00
|
|
|
proc run(peer: Peer) {.async.} =
|
2019-06-12 13:20:47 +00:00
|
|
|
while peer.connectionState notin {Disconnecting, Disconnected}:
|
2019-02-05 15:40:29 +00:00
|
|
|
peer.processQueue()
|
|
|
|
await sleepAsync(messageInterval)
|
|
|
|
|
2021-07-16 19:44:30 +00:00
|
|
|
proc pruneReceived(node: EthereumNode) =
|
2019-02-05 15:40:29 +00:00
|
|
|
if node.peerPool != nil: # XXX: a bit dirty to need to check for this here ...
|
|
|
|
var whisperNet = node.protocolState(Whisper)
|
|
|
|
|
|
|
|
for peer in node.protocolPeers(Whisper):
|
|
|
|
if not peer.initialized:
|
|
|
|
continue
|
|
|
|
|
|
|
|
# NOTE: Perhaps alter the queue prune call to keep track of a HashSet
|
|
|
|
# of pruned messages (as these should be smaller), and diff this with
|
|
|
|
# the received sets.
|
|
|
|
peer.received = intersection(peer.received, whisperNet.queue.itemHashes)
|
|
|
|
|
2021-07-16 19:44:30 +00:00
|
|
|
proc run(node: EthereumNode, network: WhisperNetwork) {.async.} =
|
2019-02-05 15:40:29 +00:00
|
|
|
while true:
|
|
|
|
# prune message queue every second
|
|
|
|
# TTL unit is in seconds, so this should be sufficient?
|
2019-11-19 16:22:35 +00:00
|
|
|
network.queue[].prune()
|
2019-02-05 15:40:29 +00:00
|
|
|
# pruning the received sets is not necessary for correct workings
|
|
|
|
# but simply from keeping the sets growing indefinitely
|
|
|
|
node.pruneReceived()
|
|
|
|
await sleepAsync(pruneInterval)
|
|
|
|
|
2019-04-26 13:36:54 +00:00
|
|
|
# Private EthereumNode calls ---------------------------------------------------
|
2019-02-05 15:40:29 +00:00
|
|
|
|
2019-04-26 13:36:54 +00:00
|
|
|
proc sendP2PMessage(node: EthereumNode, peerId: NodeId, env: Envelope): bool =
|
2019-02-05 15:40:29 +00:00
|
|
|
for peer in node.peers(Whisper):
|
|
|
|
if peer.remote.id == peerId:
|
2021-05-11 07:24:10 +00:00
|
|
|
let f = peer.p2pMessage(env)
|
|
|
|
# Can't make p2pMessage not raise so this is the "best" option I can think
|
|
|
|
# of instead of using asyncSpawn and still keeping the call not async.
|
|
|
|
f.callback = proc(data: pointer) {.gcsafe, raises: [Defect].} =
|
|
|
|
if f.failed:
|
|
|
|
warn "P2PMessage send failed", msg = f.readError.msg
|
|
|
|
|
2019-02-05 15:40:29 +00:00
|
|
|
return true
|
|
|
|
|
2019-03-23 20:53:03 +00:00
|
|
|
proc queueMessage(node: EthereumNode, msg: Message): bool =
|
2019-02-05 15:40:29 +00:00
|
|
|
|
|
|
|
var whisperNet = node.protocolState(Whisper)
|
|
|
|
# We have to do the same checks here as in the messages proc not to leak
|
|
|
|
# any information that the message originates from this node.
|
|
|
|
if not msg.allowed(whisperNet.config):
|
|
|
|
return false
|
|
|
|
|
2020-01-23 12:27:15 +00:00
|
|
|
trace "Adding message to queue", hash = $msg.hash
|
2019-11-19 16:22:35 +00:00
|
|
|
if whisperNet.queue[].add(msg):
|
2019-02-05 15:40:29 +00:00
|
|
|
# Also notify our own filters of the message we are sending,
|
|
|
|
# e.g. msg from local Dapp to Dapp
|
|
|
|
whisperNet.filters.notify(msg)
|
|
|
|
|
|
|
|
return true
|
|
|
|
|
2019-04-26 13:36:54 +00:00
|
|
|
# Public EthereumNode calls ----------------------------------------------------
|
|
|
|
|
2019-02-05 15:40:29 +00:00
|
|
|
proc postMessage*(node: EthereumNode, pubKey = none[PublicKey](),
|
|
|
|
symKey = none[SymKey](), src = none[PrivateKey](),
|
2020-04-20 18:14:39 +00:00
|
|
|
ttl: uint32, topic: Topic, payload: seq[byte],
|
|
|
|
padding = none[seq[byte]](), powTime = 1'f,
|
2019-03-23 20:53:03 +00:00
|
|
|
powTarget = defaultMinPow,
|
2019-02-05 15:40:29 +00:00
|
|
|
targetPeer = none[NodeId]()): bool =
|
2019-04-26 13:36:54 +00:00
|
|
|
## Post a message on the message queue which will be processed at the
|
|
|
|
## next `messageInterval`.
|
2019-10-03 13:42:35 +00:00
|
|
|
##
|
2019-04-26 13:36:54 +00:00
|
|
|
## NOTE: This call allows a post without encryption. If encryption is
|
|
|
|
## mandatory it should be enforced a layer up
|
2020-07-07 08:56:26 +00:00
|
|
|
let payload = encode(node.rng[], Payload(
|
|
|
|
payload: payload, src: src, dst: pubKey, symKey: symKey, padding: padding))
|
2019-02-05 15:40:29 +00:00
|
|
|
if payload.isSome():
|
2019-03-23 20:53:03 +00:00
|
|
|
var env = Envelope(expiry:epochTime().uint32 + ttl,
|
2019-02-05 15:40:29 +00:00
|
|
|
ttl: ttl, topic: topic, data: payload.get(), nonce: 0)
|
|
|
|
|
|
|
|
# Allow lightnode to post only direct p2p messages
|
|
|
|
if targetPeer.isSome():
|
|
|
|
return node.sendP2PMessage(targetPeer.get(), env)
|
|
|
|
elif not node.protocolState(Whisper).config.isLightNode:
|
2019-03-23 20:53:03 +00:00
|
|
|
# non direct p2p message can not have ttl of 0
|
|
|
|
if env.ttl == 0:
|
|
|
|
return false
|
|
|
|
var msg = initMessage(env, powCalc = false)
|
2019-02-05 15:40:29 +00:00
|
|
|
# XXX: make this non blocking or not?
|
|
|
|
# In its current blocking state, it could be noticed by a peer that no
|
|
|
|
# messages are send for a while, and thus that mining PoW is done, and
|
|
|
|
# that next messages contains a message originated from this peer
|
|
|
|
# zah: It would be hard to execute this in a background thread at the
|
|
|
|
# moment. We'll need a way to send custom "tasks" to the async message
|
|
|
|
# loop (e.g. AD2 support for AsyncChannels).
|
2019-03-23 20:53:03 +00:00
|
|
|
if not msg.sealEnvelope(powTime, powTarget):
|
|
|
|
return false
|
|
|
|
|
|
|
|
# need to check expiry after mining PoW
|
|
|
|
if not msg.env.valid():
|
|
|
|
return false
|
|
|
|
|
|
|
|
return node.queueMessage(msg)
|
2019-02-05 15:40:29 +00:00
|
|
|
else:
|
2019-04-26 13:36:54 +00:00
|
|
|
warn "Light node not allowed to post messages"
|
2019-02-05 15:40:29 +00:00
|
|
|
return false
|
|
|
|
else:
|
|
|
|
error "Encoding of payload failed"
|
|
|
|
return false
|
|
|
|
|
|
|
|
proc subscribeFilter*(node: EthereumNode, filter: Filter,
|
|
|
|
handler:FilterMsgHandler = nil): string =
|
2019-04-26 13:36:54 +00:00
|
|
|
## Initiate a filter for incoming/outgoing messages. Messages can be
|
|
|
|
## retrieved with the `getFilterMessages` call or with a provided
|
|
|
|
## `FilterMsgHandler`.
|
2019-10-03 13:42:35 +00:00
|
|
|
##
|
2019-04-26 13:36:54 +00:00
|
|
|
## NOTE: This call allows for a filter without decryption. If encryption is
|
2019-10-03 13:42:35 +00:00
|
|
|
## mandatory it should be enforced a layer up.
|
2020-07-07 08:56:26 +00:00
|
|
|
return subscribeFilter(
|
|
|
|
node.rng[], node.protocolState(Whisper).filters, filter, handler)
|
2019-02-05 15:40:29 +00:00
|
|
|
|
|
|
|
proc unsubscribeFilter*(node: EthereumNode, filterId: string): bool =
|
2019-04-26 13:36:54 +00:00
|
|
|
## Remove a previously subscribed filter.
|
2019-02-05 15:40:29 +00:00
|
|
|
var filter: Filter
|
|
|
|
return node.protocolState(Whisper).filters.take(filterId, filter)
|
|
|
|
|
2021-07-16 19:44:30 +00:00
|
|
|
proc getFilterMessages*(node: EthereumNode, filterId: string):
|
|
|
|
seq[ReceivedMessage] {.raises: [KeyError, Defect].} =
|
2019-04-26 13:36:54 +00:00
|
|
|
## Get all the messages currently in the filter queue. This will reset the
|
2019-10-03 13:42:35 +00:00
|
|
|
## filter message queue.
|
2019-02-05 15:40:29 +00:00
|
|
|
return node.protocolState(Whisper).filters.getFilterMessages(filterId)
|
|
|
|
|
|
|
|
proc filtersToBloom*(node: EthereumNode): Bloom =
|
2019-10-03 13:42:35 +00:00
|
|
|
## Returns the bloom filter of all topics of all subscribed filters.
|
2019-02-05 15:40:29 +00:00
|
|
|
return node.protocolState(Whisper).filters.toBloom()
|
|
|
|
|
|
|
|
proc setPowRequirement*(node: EthereumNode, powReq: float64) {.async.} =
|
2019-04-26 13:36:54 +00:00
|
|
|
## Sets the PoW requirement for this node, will also send
|
2019-10-03 13:42:35 +00:00
|
|
|
## this new PoW requirement to all connected peers.
|
2019-10-03 10:09:00 +00:00
|
|
|
##
|
|
|
|
## Failures when sending messages to peers will not be reported.
|
2019-02-05 15:40:29 +00:00
|
|
|
# NOTE: do we need a tolerance of old PoW for some time?
|
|
|
|
node.protocolState(Whisper).config.powRequirement = powReq
|
|
|
|
var futures: seq[Future[void]] = @[]
|
|
|
|
for peer in node.peers(Whisper):
|
2019-12-17 15:53:39 +00:00
|
|
|
futures.add(peer.powRequirement(cast[uint64](powReq)))
|
2019-02-05 15:40:29 +00:00
|
|
|
|
2019-10-03 10:09:00 +00:00
|
|
|
# Exceptions from sendMsg will not be raised
|
|
|
|
await allFutures(futures)
|
2019-02-05 15:40:29 +00:00
|
|
|
|
|
|
|
proc setBloomFilter*(node: EthereumNode, bloom: Bloom) {.async.} =
|
2019-04-26 13:36:54 +00:00
|
|
|
## Sets the bloom filter for this node, will also send
|
2019-10-03 13:42:35 +00:00
|
|
|
## this new bloom filter to all connected peers.
|
2019-10-03 10:09:00 +00:00
|
|
|
##
|
|
|
|
## Failures when sending messages to peers will not be reported.
|
2019-02-05 15:40:29 +00:00
|
|
|
# NOTE: do we need a tolerance of old bloom filter for some time?
|
|
|
|
node.protocolState(Whisper).config.bloom = bloom
|
|
|
|
var futures: seq[Future[void]] = @[]
|
|
|
|
for peer in node.peers(Whisper):
|
|
|
|
futures.add(peer.bloomFilterExchange(@bloom))
|
|
|
|
|
2019-10-03 10:09:00 +00:00
|
|
|
# Exceptions from sendMsg will not be raised
|
|
|
|
await allFutures(futures)
|
2019-02-05 15:40:29 +00:00
|
|
|
|
|
|
|
proc setMaxMessageSize*(node: EthereumNode, size: uint32): bool =
|
2019-10-03 13:42:35 +00:00
|
|
|
## Set the maximum allowed message size.
|
|
|
|
## Can not be set higher than ``defaultMaxMsgSize``.
|
2019-02-05 15:40:29 +00:00
|
|
|
if size > defaultMaxMsgSize:
|
2019-04-26 13:36:54 +00:00
|
|
|
warn "size > defaultMaxMsgSize"
|
2019-02-05 15:40:29 +00:00
|
|
|
return false
|
|
|
|
node.protocolState(Whisper).config.maxMsgSize = size
|
|
|
|
return true
|
|
|
|
|
|
|
|
proc setPeerTrusted*(node: EthereumNode, peerId: NodeId): bool =
|
2019-10-03 13:42:35 +00:00
|
|
|
## Set a connected peer as trusted.
|
2019-02-05 15:40:29 +00:00
|
|
|
for peer in node.peers(Whisper):
|
|
|
|
if peer.remote.id == peerId:
|
|
|
|
peer.state(Whisper).trusted = true
|
|
|
|
return true
|
|
|
|
|
|
|
|
proc setLightNode*(node: EthereumNode, isLightNode: bool) =
|
2019-10-03 13:42:35 +00:00
|
|
|
## Set this node as a Whisper light node.
|
|
|
|
##
|
2019-04-26 13:36:54 +00:00
|
|
|
## NOTE: Should be run before connection is made with peers as this
|
2019-10-03 13:42:35 +00:00
|
|
|
## setting is only communicated at peer handshake.
|
2019-02-05 15:40:29 +00:00
|
|
|
node.protocolState(Whisper).config.isLightNode = isLightNode
|
|
|
|
|
|
|
|
proc configureWhisper*(node: EthereumNode, config: WhisperConfig) =
|
2019-10-03 13:42:35 +00:00
|
|
|
## Apply a Whisper configuration.
|
|
|
|
##
|
2019-04-26 13:36:54 +00:00
|
|
|
## NOTE: Should be run before connection is made with peers as some
|
2019-10-03 13:42:35 +00:00
|
|
|
## of the settings are only communicated at peer handshake.
|
2019-02-05 15:40:29 +00:00
|
|
|
node.protocolState(Whisper).config = config
|
|
|
|
|
|
|
|
proc resetMessageQueue*(node: EthereumNode) =
|
2019-10-03 13:42:35 +00:00
|
|
|
## Full reset of the message queue.
|
|
|
|
##
|
|
|
|
## NOTE: Not something that should be run in normal circumstances.
|
2019-11-19 16:22:35 +00:00
|
|
|
node.protocolState(Whisper).queue[] = initQueue(defaultQueueCapacity)
|