From fac0300f168d5485e356770b65b0d0035a9cd6f9 Mon Sep 17 00:00:00 2001 From: Vlad Date: Fri, 15 Dec 2017 10:27:00 +0200 Subject: [PATCH] whisper: powRequirementCode introduced --- whisper/whisperv6/doc.go | 11 +++++++---- whisper/whisperv6/peer.go | 5 +++++ whisper/whisperv6/whisper.go | 20 ++++++++++++++++++-- 3 files changed, 30 insertions(+), 6 deletions(-) diff --git a/whisper/whisperv6/doc.go b/whisper/whisperv6/doc.go index 64925ba48b..2a4911d65a 100644 --- a/whisper/whisperv6/doc.go +++ b/whisper/whisperv6/doc.go @@ -40,10 +40,13 @@ const ( ProtocolVersionStr = "6.0" ProtocolName = "shh" - statusCode = 0 // used by whisper protocol - messagesCode = 1 // normal whisper message - p2pCode = 2 // peer-to-peer message (to be consumed by the peer, but not forwarded any further) - p2pRequestCode = 3 // peer-to-peer message, used by Dapp protocol + // whisper protocol message codes, according to EIP-627 + statusCode = 0 // used by whisper protocol + messagesCode = 1 // normal whisper message + powRequirementCode = 2 // PoW requirement + bloomFilterExCode = 3 // bloom filter exchange + p2pRequestCode = 126 // peer-to-peer message, used by Dapp protocol + p2pMessageCode = 127 // peer-to-peer message (to be consumed by the peer, but not forwarded any further) NumberOfMessageCodes = 128 paddingMask = byte(3) diff --git a/whisper/whisperv6/peer.go b/whisper/whisperv6/peer.go index ac7b3b12b6..c104720b37 100644 --- a/whisper/whisperv6/peer.go +++ b/whisper/whisperv6/peer.go @@ -172,3 +172,8 @@ func (p *Peer) ID() []byte { id := p.peer.ID() return id[:] } + +func (p *Peer) notifyAboutPowRequirementChange(pow float64) error { + val := float32(pow) + return p2p.Send(p.ws, powRequirementCode, val) +} diff --git a/whisper/whisperv6/whisper.go b/whisper/whisperv6/whisper.go index d09baab3fd..0aeba180f1 100644 --- a/whisper/whisperv6/whisper.go +++ b/whisper/whisperv6/whisper.go @@ -181,9 +181,25 @@ func (w *Whisper) SetMinimumPoW(val float64) error { return fmt.Errorf("invalid PoW: %f", val) } w.settings.Store(minPowIdx, val) + w.notifyPeersAboutPowRequirementChange(val) return nil } +func (w *Whisper) notifyPeersAboutPowRequirementChange(pow float64) { + w.peerMu.Lock() + defer w.peerMu.Unlock() + for p := range w.peers { + err := p.notifyAboutPowRequirementChange(pow) + if err != nil { + // allow one retry + err = p.notifyAboutPowRequirementChange(pow) + } + if err != nil { + fmt.Errorf("Error sending PoW notification to peer [%x]: %s", p.ID(), err) + } + } +} + // getPeer retrieves peer by ID func (w *Whisper) getPeer(peerID []byte) (*Peer, error) { w.peerMu.Lock() @@ -233,7 +249,7 @@ func (w *Whisper) SendP2PMessage(peerID []byte, envelope *Envelope) error { // SendP2PDirect sends a peer-to-peer message to a specific peer. func (w *Whisper) SendP2PDirect(peer *Peer, envelope *Envelope) error { - return p2p.Send(peer.ws, p2pCode, envelope) + return p2p.Send(peer.ws, p2pMessageCode, envelope) } // NewKeyPair generates a new cryptographic identity for the client, and injects @@ -528,7 +544,7 @@ func (wh *Whisper) runMessageLoop(p *Peer, rw p2p.MsgReadWriter) error { if cached { p.mark(&envelope) } - case p2pCode: + case p2pMessageCode: // peer-to-peer message, sent directly to peer bypassing PoW checks, etc. // this message is not supposed to be forwarded to other peers, and // therefore might not satisfy the PoW, expiry and other requirements.