mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-08-17 17:33:47 +00:00
whisper: minor refactoring
This commit is contained in:
parent
becaadd13d
commit
5d71ee3a2d
4 changed files with 43 additions and 27 deletions
|
|
@ -51,12 +51,12 @@ const (
|
||||||
paddingMask = byte(3)
|
paddingMask = byte(3)
|
||||||
signatureFlag = byte(4)
|
signatureFlag = byte(4)
|
||||||
|
|
||||||
TopicLength = 4
|
TopicLength = 4 // in bytes
|
||||||
signatureLength = 65
|
signatureLength = 65 // in bytes
|
||||||
aesKeyLength = 32
|
aesKeyLength = 32 // in bytes
|
||||||
AESNonceLength = 12
|
AESNonceLength = 12 // in bytes
|
||||||
keyIdSize = 32
|
keyIdSize = 32 // in bytes
|
||||||
bloomFilterSize = 64
|
bloomFilterSize = 64 // in bytes
|
||||||
|
|
||||||
MaxMessageSize = uint32(10 * 1024 * 1024) // maximum accepted size of a message.
|
MaxMessageSize = uint32(10 * 1024 * 1024) // maximum accepted size of a message.
|
||||||
DefaultMaxMessageSize = uint32(1024 * 1024)
|
DefaultMaxMessageSize = uint32(1024 * 1024)
|
||||||
|
|
@ -70,8 +70,6 @@ const (
|
||||||
|
|
||||||
DefaultTTL = 50 // seconds
|
DefaultTTL = 50 // seconds
|
||||||
DefaultSyncAllowance = 10 // seconds
|
DefaultSyncAllowance = 10 // seconds
|
||||||
|
|
||||||
EnvelopeHeaderLength = 20
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type unknownVersionError uint64
|
type unknownVersionError uint64
|
||||||
|
|
|
||||||
|
|
@ -51,6 +51,7 @@ type Envelope struct {
|
||||||
|
|
||||||
// size returns the size of envelope as it is sent (i.e. public fields only)
|
// size returns the size of envelope as it is sent (i.e. public fields only)
|
||||||
func (e *Envelope) size() int {
|
func (e *Envelope) size() int {
|
||||||
|
const EnvelopeHeaderLength = 20
|
||||||
return EnvelopeHeaderLength + len(e.Data)
|
return EnvelopeHeaderLength + len(e.Data)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -238,13 +239,13 @@ func (e *Envelope) Bloom() []byte {
|
||||||
return e.bloom
|
return e.bloom
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// TopicToBloom converts the topic (4 bytes) to the bloom filter (64 bytes)
|
||||||
func TopicToBloom(topic TopicType) []byte {
|
func TopicToBloom(topic TopicType) []byte {
|
||||||
var powers = [...]byte{1, 2, 4, 8, 16, 32, 64, 128}
|
|
||||||
b := make([]byte, bloomFilterSize)
|
b := make([]byte, bloomFilterSize)
|
||||||
var index [3]int
|
var index [3]int
|
||||||
for j := 0; j < 3; j++ {
|
for j := 0; j < 3; j++ {
|
||||||
index[j] = int(topic[j])
|
index[j] = int(topic[j])
|
||||||
if (topic[3] & powers[j]) != 0 {
|
if (topic[3] & (1 << uint(j))) != 0 {
|
||||||
index[j] += 256
|
index[j] += 256
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -252,7 +253,7 @@ func TopicToBloom(topic TopicType) []byte {
|
||||||
for j := 0; j < 3; j++ {
|
for j := 0; j < 3; j++ {
|
||||||
byteIndex := index[j] / 8
|
byteIndex := index[j] / 8
|
||||||
bitIndex := index[j] % 8
|
bitIndex := index[j] % 8
|
||||||
b[byteIndex] = powers[bitIndex]
|
b[byteIndex] = (1 << uint(bitIndex))
|
||||||
}
|
}
|
||||||
return b
|
return b
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -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
|
|
||||||
}
|
|
||||||
|
|
|
||||||
|
|
@ -49,11 +49,11 @@ type Statistics struct {
|
||||||
|
|
||||||
const (
|
const (
|
||||||
maxMsgSizeIdx = iota // Maximal message length allowed by the whisper node
|
maxMsgSizeIdx = iota // Maximal message length allowed by the whisper node
|
||||||
overflowIdx = iota // Indicator of message queue overflow
|
overflowIdx // Indicator of message queue overflow
|
||||||
minPowIdx = iota // Minimal PoW required by the whisper node
|
minPowIdx // Minimal PoW required by the whisper node
|
||||||
minPowToleranceIdx = iota // Minimal PoW tolerated by the whisper node for a limited time
|
minPowToleranceIdx // Minimal PoW tolerated by the whisper node for a limited time
|
||||||
bloomFilterIdx = iota // Bloom filter for topics of interest for this node
|
bloomFilterIdx // Bloom filter for topics of interest for this node
|
||||||
bloomFilterToleranceIdx = iota // Bloom filter tolerated by the whisper node for a limited time
|
bloomFilterToleranceIdx // Bloom filter tolerated by the whisper node for a limited time
|
||||||
)
|
)
|
||||||
|
|
||||||
// Whisper represents a dark communication interface through the Ethereum
|
// Whisper represents a dark communication interface through the Ethereum
|
||||||
|
|
@ -129,14 +129,23 @@ func New(cfg *Config) *Whisper {
|
||||||
return whisper
|
return whisper
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// MinPow returns the PoW value required by this node.
|
||||||
func (w *Whisper) MinPow() float64 {
|
func (w *Whisper) MinPow() float64 {
|
||||||
val, exist := w.settings.Load(minPowIdx)
|
val, exist := w.settings.Load(minPowIdx)
|
||||||
if !exist || val == nil {
|
if !exist || val == nil {
|
||||||
return DefaultMinimumPoW
|
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 {
|
func (w *Whisper) MinPowTolerance() float64 {
|
||||||
val, exist := w.settings.Load(minPowToleranceIdx)
|
val, exist := w.settings.Load(minPowToleranceIdx)
|
||||||
if !exist || val == nil {
|
if !exist || val == nil {
|
||||||
|
|
@ -145,6 +154,10 @@ func (w *Whisper) MinPowTolerance() float64 {
|
||||||
return val.(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 {
|
func (w *Whisper) BloomFilter() []byte {
|
||||||
val, exist := w.settings.Load(bloomFilterIdx)
|
val, exist := w.settings.Load(bloomFilterIdx)
|
||||||
if !exist || val == nil {
|
if !exist || val == nil {
|
||||||
|
|
@ -153,6 +166,10 @@ func (w *Whisper) BloomFilter() []byte {
|
||||||
return val.([]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 {
|
func (w *Whisper) BloomFilterTolerance() []byte {
|
||||||
val, exist := w.settings.Load(bloomFilterToleranceIdx)
|
val, exist := w.settings.Load(bloomFilterToleranceIdx)
|
||||||
if !exist || val == nil {
|
if !exist || val == nil {
|
||||||
|
|
@ -1035,7 +1052,7 @@ func bloomFilterMatch(filter, sample []byte) bool {
|
||||||
for i := 0; i < bloomFilterSize; i++ {
|
for i := 0; i < bloomFilterSize; i++ {
|
||||||
f := filter[i]
|
f := filter[i]
|
||||||
s := sample[i]
|
s := sample[i]
|
||||||
if ((f | s) ^ f) != 0 {
|
if (f | s) != f {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -1050,3 +1067,12 @@ func addBloom(a, b []byte) []byte {
|
||||||
}
|
}
|
||||||
return c
|
return c
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func isBloomFilterEqual(a, b []byte) bool {
|
||||||
|
for i := 0; i < bloomFilterSize; i++ {
|
||||||
|
if a[i] != b[i] {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue