aboutsummaryrefslogtreecommitdiffstats
path: root/whisper/whisperv6/envelope.go
diff options
context:
space:
mode:
Diffstat (limited to 'whisper/whisperv6/envelope.go')
-rw-r--r--whisper/whisperv6/envelope.go35
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
+}