From 9b119887e4bed24090cd7e95ccd657b5c97d4079 Mon Sep 17 00:00:00 2001 From: Vlad Date: Sat, 18 Feb 2017 20:21:13 +0100 Subject: [PATCH] whisper: POW updated --- whisper/whisperv5/api_test.go | 6 +++--- whisper/whisperv5/benchmarks_test.go | 31 +++++++++++++++++++++++++++- whisper/whisperv5/doc.go | 4 ++-- whisper/whisperv5/envelope.go | 8 +++++-- 4 files changed, 41 insertions(+), 8 deletions(-) diff --git a/whisper/whisperv5/api_test.go b/whisper/whisperv5/api_test.go index 79285559f3..48536b84ba 100644 --- a/whisper/whisperv5/api_test.go +++ b/whisper/whisperv5/api_test.go @@ -152,7 +152,7 @@ func TestUnmarshalFilterArgs(t *testing.T) { "keyname":"testname", "pow":2.34, "topics":["0x00000000", "0x007f80ff", "0xff807f00", "0xf26e7779"], - "acceptP2P":true + "p2p":true }`) var f WhisperFilterArgs @@ -212,8 +212,8 @@ func TestUnmarshalPostArgs(t *testing.T) { "payload":"0x7061796C6F61642073686F756C642062652070736575646F72616E646F6D", "worktime":777, "pow":3.1416, - "filterID":"test-filter-id", - "peerID":"0xf26e7779" + "filterid":"test-filter-id", + "peerid":"0xf26e7779" }`) var a PostArgs diff --git a/whisper/whisperv5/benchmarks_test.go b/whisper/whisperv5/benchmarks_test.go index f2eef3c471..417b2881b8 100644 --- a/whisper/whisperv5/benchmarks_test.go +++ b/whisper/whisperv5/benchmarks_test.go @@ -34,7 +34,6 @@ func BenchmarkDeriveOneTimeKey(b *testing.B) { } } -//func TestEncryptionSym(b *testing.T) { func BenchmarkEncryptionSym(b *testing.B) { 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) + } + } +} diff --git a/whisper/whisperv5/doc.go b/whisper/whisperv5/doc.go index b82a82468b..70c7008a73 100644 --- a/whisper/whisperv5/doc.go +++ b/whisper/whisperv5/doc.go @@ -55,8 +55,8 @@ const ( saltLength = 12 AESNonceMaxLength = 12 - MaxMessageLength = 0xFFFF // todo: remove this restriction after testing. this should be regulated by PoW. - MinimumPoW = 1.0 // todo: review after testing. + MaxMessageLength = 0x0FFFFF // todo: remove this restriction after testing. this should be regulated by PoW. + MinimumPoW = 10.0 // todo: review after testing. 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 diff --git a/whisper/whisperv5/envelope.go b/whisper/whisperv5/envelope.go index 1b976705d4..c89238b7a2 100644 --- a/whisper/whisperv5/envelope.go +++ b/whisper/whisperv5/envelope.go @@ -122,6 +122,10 @@ func (e *Envelope) Seal(options *MessageParams) error { 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 { if e.pow == 0 { e.calculatePoW(0) @@ -137,14 +141,14 @@ func (e *Envelope) calculatePoW(diff uint32) { h = crypto.Keccak256(buf) firstBit := common.FirstBitSet(common.BigD(h)) 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) e.pow = x } func (e *Envelope) powToFirstBit(pow float64) int { x := pow - x *= float64(len(e.Data)) + x *= float64(e.size()) x *= float64(e.TTL) bits := math.Log2(x) bits = math.Ceil(bits)