whisper: powRequirementCode introduced

This commit is contained in:
Vlad 2017-12-15 10:27:00 +02:00
parent 3654aeaa4f
commit fac0300f16
3 changed files with 30 additions and 6 deletions

View file

@ -40,10 +40,13 @@ const (
ProtocolVersionStr = "6.0" ProtocolVersionStr = "6.0"
ProtocolName = "shh" ProtocolName = "shh"
statusCode = 0 // used by whisper protocol // whisper protocol message codes, according to EIP-627
messagesCode = 1 // normal whisper message statusCode = 0 // used by whisper protocol
p2pCode = 2 // peer-to-peer message (to be consumed by the peer, but not forwarded any further) messagesCode = 1 // normal whisper message
p2pRequestCode = 3 // peer-to-peer message, used by Dapp protocol 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 NumberOfMessageCodes = 128
paddingMask = byte(3) paddingMask = byte(3)

View file

@ -172,3 +172,8 @@ func (p *Peer) ID() []byte {
id := p.peer.ID() id := p.peer.ID()
return id[:] return id[:]
} }
func (p *Peer) notifyAboutPowRequirementChange(pow float64) error {
val := float32(pow)
return p2p.Send(p.ws, powRequirementCode, val)
}

View file

@ -181,9 +181,25 @@ func (w *Whisper) SetMinimumPoW(val float64) error {
return fmt.Errorf("invalid PoW: %f", val) return fmt.Errorf("invalid PoW: %f", val)
} }
w.settings.Store(minPowIdx, val) w.settings.Store(minPowIdx, val)
w.notifyPeersAboutPowRequirementChange(val)
return nil 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 // getPeer retrieves peer by ID
func (w *Whisper) getPeer(peerID []byte) (*Peer, error) { func (w *Whisper) getPeer(peerID []byte) (*Peer, error) {
w.peerMu.Lock() 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. // SendP2PDirect sends a peer-to-peer message to a specific peer.
func (w *Whisper) SendP2PDirect(peer *Peer, envelope *Envelope) error { 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 // 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 { if cached {
p.mark(&envelope) p.mark(&envelope)
} }
case p2pCode: case p2pMessageCode:
// peer-to-peer message, sent directly to peer bypassing PoW checks, etc. // peer-to-peer message, sent directly to peer bypassing PoW checks, etc.
// this message is not supposed to be forwarded to other peers, and // this message is not supposed to be forwarded to other peers, and
// therefore might not satisfy the PoW, expiry and other requirements. // therefore might not satisfy the PoW, expiry and other requirements.