whisper: POW updated

This commit is contained in:
Vlad 2017-02-18 20:21:13 +01:00
parent e85268fcd1
commit 9b119887e4
4 changed files with 41 additions and 8 deletions

View file

@ -152,7 +152,7 @@ func TestUnmarshalFilterArgs(t *testing.T) {
"keyname":"testname", "keyname":"testname",
"pow":2.34, "pow":2.34,
"topics":["0x00000000", "0x007f80ff", "0xff807f00", "0xf26e7779"], "topics":["0x00000000", "0x007f80ff", "0xff807f00", "0xf26e7779"],
"acceptP2P":true "p2p":true
}`) }`)
var f WhisperFilterArgs var f WhisperFilterArgs
@ -212,8 +212,8 @@ func TestUnmarshalPostArgs(t *testing.T) {
"payload":"0x7061796C6F61642073686F756C642062652070736575646F72616E646F6D", "payload":"0x7061796C6F61642073686F756C642062652070736575646F72616E646F6D",
"worktime":777, "worktime":777,
"pow":3.1416, "pow":3.1416,
"filterID":"test-filter-id", "filterid":"test-filter-id",
"peerID":"0xf26e7779" "peerid":"0xf26e7779"
}`) }`)
var a PostArgs var a PostArgs

View file

@ -34,7 +34,6 @@ func BenchmarkDeriveOneTimeKey(b *testing.B) {
} }
} }
//func TestEncryptionSym(b *testing.T) {
func BenchmarkEncryptionSym(b *testing.B) { func BenchmarkEncryptionSym(b *testing.B) {
InitSingleTest() InitSingleTest()
@ -181,3 +180,33 @@ func BenchmarkDecryptionAsymInvalid(b *testing.B) {
} }
} }
} }
func increment(x []byte) {
for i := 0; i < len(x); i++ {
x[i]++
if x[i] != 0 {
break
}
}
}
func BenchmarkPoW(b *testing.B) {
InitSingleTest()
params, err := generateMessageParams()
if err != nil {
b.Fatalf("failed generateMessageParams with seed %d: %s.", seed, err)
}
params.Payload = make([]byte, 32)
params.PoW = 10.0
params.TTL = 1
for i := 0; i < b.N; i++ {
increment(params.Payload)
msg := NewSentMessage(params)
_, err := msg.Wrap(params)
if err != nil {
b.Fatalf("failed Wrap with seed %d: %s.", seed, err)
}
}
}

View file

@ -55,8 +55,8 @@ const (
saltLength = 12 saltLength = 12
AESNonceMaxLength = 12 AESNonceMaxLength = 12
MaxMessageLength = 0xFFFF // todo: remove this restriction after testing. this should be regulated by PoW. MaxMessageLength = 0x0FFFFF // todo: remove this restriction after testing. this should be regulated by PoW.
MinimumPoW = 1.0 // todo: review after testing. MinimumPoW = 10.0 // todo: review after testing.
padSizeLimitLower = 128 // it can not be less - we don't want to reveal the absence of signature padSizeLimitLower = 128 // it can not be less - we don't want to reveal the absence of signature
padSizeLimitUpper = 256 // just an arbitrary number, could be changed without losing compatibility padSizeLimitUpper = 256 // just an arbitrary number, could be changed without losing compatibility

View file

@ -122,6 +122,10 @@ func (e *Envelope) Seal(options *MessageParams) error {
return nil return nil
} }
func (e *Envelope) size() int {
return len(e.Data) + len(e.Version) + len(e.AESNonce) + len(e.Salt) + 20
}
func (e *Envelope) PoW() float64 { func (e *Envelope) PoW() float64 {
if e.pow == 0 { if e.pow == 0 {
e.calculatePoW(0) e.calculatePoW(0)
@ -137,14 +141,14 @@ func (e *Envelope) calculatePoW(diff uint32) {
h = crypto.Keccak256(buf) h = crypto.Keccak256(buf)
firstBit := common.FirstBitSet(common.BigD(h)) firstBit := common.FirstBitSet(common.BigD(h))
x := math.Pow(2, float64(firstBit)) x := math.Pow(2, float64(firstBit))
x /= float64(len(e.Data)) // we only count e.Data, other variable-sized members are checked in Whisper.add() x /= float64(e.size())
x /= float64(e.TTL + diff) x /= float64(e.TTL + diff)
e.pow = x e.pow = x
} }
func (e *Envelope) powToFirstBit(pow float64) int { func (e *Envelope) powToFirstBit(pow float64) int {
x := pow x := pow
x *= float64(len(e.Data)) x *= float64(e.size())
x *= float64(e.TTL) x *= float64(e.TTL)
bits := math.Log2(x) bits := math.Log2(x)
bits = math.Ceil(bits) bits = math.Ceil(bits)