diff options
author | gluk256 <gluk256@users.noreply.github.com> | 2018-01-12 19:11:22 +0800 |
---|---|---|
committer | Felix Lange <fjl@users.noreply.github.com> | 2018-01-12 19:11:22 +0800 |
commit | fd869dc839e2b3696e130224a43b9b25455ceb46 (patch) | |
tree | f19c5db8acac542f451cceac216333f7a75a2ef5 /whisper/whisperv6/envelope.go | |
parent | 56152b31ac251d1cc68fcddbdad159ba5234c415 (diff) | |
download | go-tangerine-fd869dc839e2b3696e130224a43b9b25455ceb46.tar go-tangerine-fd869dc839e2b3696e130224a43b9b25455ceb46.tar.gz go-tangerine-fd869dc839e2b3696e130224a43b9b25455ceb46.tar.bz2 go-tangerine-fd869dc839e2b3696e130224a43b9b25455ceb46.tar.lz go-tangerine-fd869dc839e2b3696e130224a43b9b25455ceb46.tar.xz go-tangerine-fd869dc839e2b3696e130224a43b9b25455ceb46.tar.zst go-tangerine-fd869dc839e2b3696e130224a43b9b25455ceb46.zip |
whisper/whisperv6: implement pow/bloom exchange protocol (#15802)
This is the main feature of v6.
Diffstat (limited to 'whisper/whisperv6/envelope.go')
-rw-r--r-- | whisper/whisperv6/envelope.go | 35 |
1 files changed, 32 insertions, 3 deletions
diff --git a/whisper/whisperv6/envelope.go b/whisper/whisperv6/envelope.go index 676df669b..9ed712b93 100644 --- a/whisper/whisperv6/envelope.go +++ b/whisper/whisperv6/envelope.go @@ -42,9 +42,11 @@ type Envelope struct { Data []byte Nonce uint64 - pow float64 // Message-specific PoW as described in the Whisper specification. - hash common.Hash // Cached hash of the envelope to avoid rehashing every time. - // Don't access hash directly, use Hash() function instead. + pow float64 // Message-specific PoW as described in the Whisper specification. + + // the following variables should not be accessed directly, use the corresponding function instead: Hash(), Bloom() + hash common.Hash // Cached hash of the envelope to avoid rehashing every time. + bloom []byte } // size returns the size of envelope as it is sent (i.e. public fields only) @@ -227,3 +229,30 @@ func (e *Envelope) Open(watcher *Filter) (msg *ReceivedMessage) { } return msg } + +// Bloom maps 4-bytes Topic into 64-byte bloom filter with 3 bits set (at most). +func (e *Envelope) Bloom() []byte { + if e.bloom == nil { + e.bloom = TopicToBloom(e.Topic) + } + return e.bloom +} + +// TopicToBloom converts the topic (4 bytes) to the bloom filter (64 bytes) +func TopicToBloom(topic TopicType) []byte { + b := make([]byte, bloomFilterSize) + var index [3]int + for j := 0; j < 3; j++ { + index[j] = int(topic[j]) + if (topic[3] & (1 << uint(j))) != 0 { + index[j] += 256 + } + } + + for j := 0; j < 3; j++ { + byteIndex := index[j] / 8 + bitIndex := index[j] % 8 + b[byteIndex] = (1 << uint(bitIndex)) + } + return b +} |