diff options
author | gluk256 <gluk256@users.noreply.github.com> | 2018-02-09 23:25:23 +0800 |
---|---|---|
committer | Péter Szilágyi <peterke@gmail.com> | 2018-02-09 23:25:23 +0800 |
commit | 42628ba7eda25830653763ced060f702861d0887 (patch) | |
tree | cf0fc13c7fc9fa005c58e6f87841e210fac4a40d /whisper | |
parent | ccf808353794f835422e02446384bd627f045f1a (diff) | |
download | dexon-42628ba7eda25830653763ced060f702861d0887.tar dexon-42628ba7eda25830653763ced060f702861d0887.tar.gz dexon-42628ba7eda25830653763ced060f702861d0887.tar.bz2 dexon-42628ba7eda25830653763ced060f702861d0887.tar.lz dexon-42628ba7eda25830653763ced060f702861d0887.tar.xz dexon-42628ba7eda25830653763ced060f702861d0887.tar.zst dexon-42628ba7eda25830653763ced060f702861d0887.zip |
whisper: bloom filter refactoring (#16046)
* whisper: bloom filter refactoring
* whisper: fixed full node
Diffstat (limited to 'whisper')
-rw-r--r-- | whisper/whisperv6/peer.go | 30 | ||||
-rw-r--r-- | whisper/whisperv6/whisper.go | 7 |
2 files changed, 21 insertions, 16 deletions
diff --git a/whisper/whisperv6/peer.go b/whisper/whisperv6/peer.go index 4f9a7c378..4ef0f3c43 100644 --- a/whisper/whisperv6/peer.go +++ b/whisper/whisperv6/peer.go @@ -36,7 +36,8 @@ type Peer struct { trusted bool powRequirement float64 - bloomFilter []byte // may contain nil in case of full node + bloomFilter []byte + fullNode bool known *set.Set // Messages already known by the peer to avoid wasting bandwidth @@ -53,6 +54,8 @@ func newPeer(host *Whisper, remote *p2p.Peer, rw p2p.MsgReadWriter) *Peer { powRequirement: 0.0, known: set.New(), quit: make(chan struct{}), + bloomFilter: makeFullNodeBloom(), + fullNode: true, } } @@ -118,11 +121,7 @@ func (peer *Peer) handshake() error { if sz != bloomFilterSize && sz != 0 { return fmt.Errorf("peer [%x] sent bad status message: wrong bloom filter size %d", peer.ID(), sz) } - if isFullNode(bloom) { - peer.bloomFilter = nil - } else { - peer.bloomFilter = bloom - } + peer.setBloomFilter(bloom) } } @@ -226,10 +225,21 @@ func (peer *Peer) notifyAboutBloomFilterChange(bloom []byte) error { } func (peer *Peer) bloomMatch(env *Envelope) bool { - if peer.bloomFilter == nil { - // no filter - full node, accepts all envelops - return true + return peer.fullNode || bloomFilterMatch(peer.bloomFilter, env.Bloom()) +} + +func (peer *Peer) setBloomFilter(bloom []byte) { + peer.bloomFilter = bloom + peer.fullNode = isFullNode(bloom) + if peer.fullNode && peer.bloomFilter == nil { + peer.bloomFilter = makeFullNodeBloom() } +} - return bloomFilterMatch(peer.bloomFilter, env.Bloom()) +func makeFullNodeBloom() []byte { + bloom := make([]byte, bloomFilterSize) + for i := 0; i < bloomFilterSize; i++ { + bloom[i] = 0xFF + } + return bloom } diff --git a/whisper/whisperv6/whisper.go b/whisper/whisperv6/whisper.go index d75ad04ac..600f9cb28 100644 --- a/whisper/whisperv6/whisper.go +++ b/whisper/whisperv6/whisper.go @@ -710,11 +710,7 @@ func (whisper *Whisper) runMessageLoop(p *Peer, rw p2p.MsgReadWriter) error { log.Warn("failed to decode bloom filter exchange message, peer will be disconnected", "peer", p.peer.ID(), "err", err) return errors.New("invalid bloom filter exchange message") } - if isFullNode(bloom) { - p.bloomFilter = nil - } else { - p.bloomFilter = bloom - } + p.setBloomFilter(bloom) 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 @@ -1049,7 +1045,6 @@ func isFullNode(bloom []byte) bool { func bloomFilterMatch(filter, sample []byte) bool { if filter == nil { - // full node, accepts all messages return true } |