whisper: minor refactoring

This commit is contained in:
Vlad 2018-01-07 21:12:05 +02:00
parent becaadd13d
commit 5d71ee3a2d
4 changed files with 43 additions and 27 deletions

View file

@ -51,12 +51,12 @@ const (
paddingMask = byte(3)
signatureFlag = byte(4)
TopicLength = 4
signatureLength = 65
aesKeyLength = 32
AESNonceLength = 12
keyIdSize = 32
bloomFilterSize = 64
TopicLength = 4 // in bytes
signatureLength = 65 // in bytes
aesKeyLength = 32 // in bytes
AESNonceLength = 12 // in bytes
keyIdSize = 32 // in bytes
bloomFilterSize = 64 // in bytes
MaxMessageSize = uint32(10 * 1024 * 1024) // maximum accepted size of a message.
DefaultMaxMessageSize = uint32(1024 * 1024)
@ -70,8 +70,6 @@ const (
DefaultTTL = 50 // seconds
DefaultSyncAllowance = 10 // seconds
EnvelopeHeaderLength = 20
)
type unknownVersionError uint64

View file

@ -51,6 +51,7 @@ type Envelope struct {
// size returns the size of envelope as it is sent (i.e. public fields only)
func (e *Envelope) size() int {
const EnvelopeHeaderLength = 20
return EnvelopeHeaderLength + len(e.Data)
}
@ -238,13 +239,13 @@ func (e *Envelope) Bloom() []byte {
return e.bloom
}
// TopicToBloom converts the topic (4 bytes) to the bloom filter (64 bytes)
func TopicToBloom(topic TopicType) []byte {
var powers = [...]byte{1, 2, 4, 8, 16, 32, 64, 128}
b := make([]byte, bloomFilterSize)
var index [3]int
for j := 0; j < 3; j++ {
index[j] = int(topic[j])
if (topic[3] & powers[j]) != 0 {
if (topic[3] & (1 << uint(j))) != 0 {
index[j] += 256
}
}
@ -252,7 +253,7 @@ func TopicToBloom(topic TopicType) []byte {
for j := 0; j < 3; j++ {
byteIndex := index[j] / 8
bitIndex := index[j] % 8
b[byteIndex] = powers[bitIndex]
b[byteIndex] = (1 << uint(bitIndex))
}
return b
}

View file

@ -428,12 +428,3 @@ func checkBloomFilterExchange(t *testing.T) {
}
}
}
func isBloomFilterEqual(a, b []byte) bool {
for i := 0; i < bloomFilterSize; i++ {
if a[i] != b[i] {
return false
}
}
return true
}

View file

@ -49,11 +49,11 @@ type Statistics struct {
const (
maxMsgSizeIdx = iota // Maximal message length allowed by the whisper node
overflowIdx = iota // Indicator of message queue overflow
minPowIdx = iota // Minimal PoW required by the whisper node
minPowToleranceIdx = iota // Minimal PoW tolerated by the whisper node for a limited time
bloomFilterIdx = iota // Bloom filter for topics of interest for this node
bloomFilterToleranceIdx = iota // Bloom filter tolerated by the whisper node for a limited time
overflowIdx // Indicator of message queue overflow
minPowIdx // Minimal PoW required by the whisper node
minPowToleranceIdx // Minimal PoW tolerated by the whisper node for a limited time
bloomFilterIdx // Bloom filter for topics of interest for this node
bloomFilterToleranceIdx // Bloom filter tolerated by the whisper node for a limited time
)
// Whisper represents a dark communication interface through the Ethereum
@ -129,14 +129,23 @@ func New(cfg *Config) *Whisper {
return whisper
}
// MinPow returns the PoW value required by this node.
func (w *Whisper) MinPow() float64 {
val, exist := w.settings.Load(minPowIdx)
if !exist || val == nil {
return DefaultMinimumPoW
}
return val.(float64)
v, ok := val.(float64)
if !ok {
log.Error("Error loading minPowIdx, using default")
return DefaultMinimumPoW
}
return v
}
// MinPowTolerance returns the value of minimum PoW which is tolerated for a limited
// time after PoW was changed. If sufficient time have elapsed or no change of PoW
// have ever occurred, the return value will be the same as return value of MinPow().
func (w *Whisper) MinPowTolerance() float64 {
val, exist := w.settings.Load(minPowToleranceIdx)
if !exist || val == nil {
@ -145,6 +154,10 @@ func (w *Whisper) MinPowTolerance() float64 {
return val.(float64)
}
// BloomFilter returns the aggregated bloom filter for all the topics of interest.
// The nodes are required to send only messages that match the advertised bloom filter.
// If a message does not match the bloom, it will tantamount to spam, and the peer will
// be disconnected.
func (w *Whisper) BloomFilter() []byte {
val, exist := w.settings.Load(bloomFilterIdx)
if !exist || val == nil {
@ -153,6 +166,10 @@ func (w *Whisper) BloomFilter() []byte {
return val.([]byte)
}
// BloomFilterTolerance returns the bloom filter which is tolerated for a limited
// time after new bloom was advertised to the peers. If sufficient time have elapsed
// or no change of bloom filter have ever occurred, the return value will be the same
// as return value of BloomFilter().
func (w *Whisper) BloomFilterTolerance() []byte {
val, exist := w.settings.Load(bloomFilterToleranceIdx)
if !exist || val == nil {
@ -1035,7 +1052,7 @@ func bloomFilterMatch(filter, sample []byte) bool {
for i := 0; i < bloomFilterSize; i++ {
f := filter[i]
s := sample[i]
if ((f | s) ^ f) != 0 {
if (f | s) != f {
return false
}
}
@ -1050,3 +1067,12 @@ func addBloom(a, b []byte) []byte {
}
return c
}
func isBloomFilterEqual(a, b []byte) bool {
for i := 0; i < bloomFilterSize; i++ {
if a[i] != b[i] {
return false
}
}
return true
}