whisper: variable topic size allowed for a filter

This commit is contained in:
Vlad 2017-03-13 20:56:22 +01:00
parent 812a2d5a05
commit 58c8121a22
7 changed files with 44 additions and 31 deletions

View file

@ -65,7 +65,7 @@ var (
pub *ecdsa.PublicKey
asymKey *ecdsa.PrivateKey
nodeid *ecdsa.PrivateKey
topic whisper.TopicType
topic []byte
asymKeyID string
filterID string
symPass string
@ -129,7 +129,7 @@ func processArgs() {
if err != nil {
utils.Fatalf("Failed to parse the topic: %s", err)
}
topic = whisper.BytesToTopic(x)
topic = x
}
if *asymmetricMode && len(*argPub) > 0 {
@ -355,7 +355,7 @@ func configureNode() {
filter := whisper.Filter{
KeySym: symKey,
KeyAsym: asymKey,
Topics: []whisper.TopicType{topic},
Topics: [][]byte{topic},
AllowP2P: p2pAccept,
}
filterID, err = shh.Watch(&filter)
@ -486,7 +486,7 @@ func sendMsg(payload []byte) common.Hash {
Dst: pub,
KeySym: symKey,
Payload: payload,
Topic: topic,
Topic: whisper.BytesToTopic(topic),
TTL: uint32(*argTTL),
PoW: *argPoW,
WorkTime: uint32(*argWorkTime),

View file

@ -233,7 +233,9 @@ func (api *PublicWhisperAPI) Subscribe(args WhisperFilterArgs) (string, error) {
AllowP2P: args.AllowP2P,
}
filter.Topics = append(filter.Topics, args.Topics...)
for _, bt := range args.Topics {
filter.Topics = append(filter.Topics, bt)
}
err := ValidateKeyID(args.Key)
if err != nil {
@ -441,7 +443,7 @@ type WhisperFilterArgs struct {
Key string
SignedWith string
MinPoW float64
Topics []TopicType
Topics [][]byte
AllowP2P bool
}
@ -487,13 +489,13 @@ func (args *WhisperFilterArgs) UnmarshalJSON(b []byte) (err error) {
return fmt.Errorf("topic[%d] is not a string", i)
}
}
topicsDecoded := make([]TopicType, len(topics))
topicsDecoded := make([][]byte, len(topics))
for j, s := range topics {
x := common.FromHex(s)
if x == nil || len(x) != TopicLength {
if x == nil || len(x) > TopicLength {
return fmt.Errorf("topic[%d] is invalid", j)
}
topicsDecoded[j] = BytesToTopic(x)
topicsDecoded[j] = x
}
args.Topics = topicsDecoded
}

View file

@ -204,22 +204,22 @@ func TestUnmarshalFilterArgs(t *testing.T) {
}
i := 0
if f.Topics[i] != (TopicType{0x00, 0x00, 0x00, 0x00}) {
if !bytes.Equal(f.Topics[i], []byte{0x00, 0x00, 0x00, 0x00}) {
t.Fatalf("wrong topic[%d]: %x.", i, f.Topics[i])
}
i++
if f.Topics[i] != (TopicType{0x00, 0x7f, 0x80, 0xff}) {
if !bytes.Equal(f.Topics[i], []byte{0x00, 0x7f, 0x80, 0xff}) {
t.Fatalf("wrong topic[%d]: %x.", i, f.Topics[i])
}
i++
if f.Topics[i] != (TopicType{0xff, 0x80, 0x7f, 0x00}) {
if !bytes.Equal(f.Topics[i], []byte{0xff, 0x80, 0x7f, 0x00}) {
t.Fatalf("wrong topic[%d]: %x.", i, f.Topics[i])
}
i++
if f.Topics[i] != (TopicType{0xf2, 0x6e, 0x77, 0x79}) {
if !bytes.Equal(f.Topics[i], []byte{0xf2, 0x6e, 0x77, 0x79}) {
t.Fatalf("wrong topic[%d]: %x.", i, f.Topics[i])
}
}
@ -347,7 +347,9 @@ func TestIntegrationAsym(t *testing.T) {
f.Symmetric = false
f.Key = key
f.SignedWith = sigPubKey
f.Topics = topics[:]
f.Topics = make([][]byte, 2)
f.Topics[0] = topics[0][:]
f.Topics[1] = topics[1][:]
f.MinPoW = DefaultMinimumPoW / 2
f.AllowP2P = true
@ -442,7 +444,9 @@ func TestIntegrationSym(t *testing.T) {
var f WhisperFilterArgs
f.Symmetric = true
f.Key = symKeyID
f.Topics = topics[:]
f.Topics = make([][]byte, 2)
f.Topics[0] = topics[0][:]
f.Topics[1] = topics[1][:]
f.MinPoW = 0.324
f.SignedWith = sigPubKey
f.AllowP2P = false
@ -538,7 +542,9 @@ func TestIntegrationSymWithFilter(t *testing.T) {
var f WhisperFilterArgs
f.Symmetric = true
f.Key = symKeyID
f.Topics = topics[:]
f.Topics = make([][]byte, 2)
f.Topics[0] = topics[0][:]
f.Topics[1] = topics[1][:]
f.MinPoW = 0.324
f.SignedWith = sigPubKey
f.AllowP2P = false

View file

@ -29,13 +29,13 @@ type Filter struct {
Src *ecdsa.PublicKey // Sender of the message
KeyAsym *ecdsa.PrivateKey // Private Key of recipient
KeySym []byte // Key associated with the Topic
Topics []TopicType // Topics to filter messages with
Topics [][]byte // Topics to filter messages with
PoW float64 // Proof of work as described in the Whisper spec
AllowP2P bool // Indicates whether this filter is interested in direct peer-to-peer messages
SymKeyHash common.Hash // The Keccak256Hash of the symmetric key, needed for optimization
Messages map[common.Hash]*ReceivedMessage
mutex sync.RWMutex
Messages map[common.Hash]*ReceivedMessage
mutex sync.RWMutex
}
type Filters struct {
@ -201,12 +201,14 @@ func (f *Filter) MatchTopic(topic TopicType) bool {
return true
}
for _, t := range f.Topics {
if t == topic {
return true
for _, bt := range f.Topics {
for j, b := range bt {
if topic[j] != b {
return false
}
}
}
return false
return true
}
func IsPubKeyEqual(a, b *ecdsa.PublicKey) bool {

View file

@ -53,8 +53,9 @@ func generateFilter(t *testing.T, symmetric bool) (*Filter, error) {
f.Messages = make(map[common.Hash]*ReceivedMessage)
const topicNum = 8
f.Topics = make([]TopicType, topicNum)
f.Topics = make([][]byte, topicNum)
for i := 0; i < topicNum; i++ {
f.Topics[i] = make([]byte, 4)
randomize(f.Topics[i][:])
f.Topics[i][0] = 0x01
}
@ -194,8 +195,8 @@ func TestMatchEnvelope(t *testing.T) {
// encrypt symmetrically
i := rand.Int() % 4
fsym.Topics[i] = params.Topic
fasym.Topics[i] = params.Topic
fsym.Topics[i] = params.Topic[:]
fasym.Topics[i] = params.Topic[:]
msg = NewSentMessage(params)
env, err = msg.Wrap(params)
if err != nil {
@ -320,7 +321,7 @@ func TestMatchMessageSym(t *testing.T) {
const index = 1
params.KeySym = f.KeySym
params.Topic = f.Topics[index]
params.Topic = BytesToTopic(f.Topics[index])
sentMessage := NewSentMessage(params)
env, err := sentMessage.Wrap(params)
@ -413,7 +414,7 @@ func TestMatchMessageAsym(t *testing.T) {
}
const index = 1
params.Topic = f.Topics[index]
params.Topic = BytesToTopic(f.Topics[index])
params.Dst = &f.KeyAsym.PublicKey
keySymOrig := params.KeySym
params.KeySym = nil
@ -504,7 +505,7 @@ func generateCompatibeEnvelope(t *testing.T, f *Filter) *Envelope {
}
params.KeySym = f.KeySym
params.Topic = f.Topics[2]
params.Topic = BytesToTopic(f.Topics[2])
sentMessage := NewSentMessage(params)
env, err := sentMessage.Wrap(params)
if err != nil {

View file

@ -118,7 +118,9 @@ func initialize(t *testing.T) {
node.shh.Start(nil)
topics := make([]TopicType, 0)
topics = append(topics, sharedTopic)
f := Filter{KeySym: sharedKey, Topics: topics}
f := Filter{KeySym: sharedKey}
f.Topics = make([][]byte, 1)
f.Topics[0] = topics[0][:]
node.filerId, err = node.shh.Watch(&f)
if err != nil {
t.Fatalf("failed to install the filter: %s.", err)

View file

@ -510,7 +510,7 @@ func TestCustomization(t *testing.T) {
}
params.KeySym = f.KeySym
params.Topic = f.Topics[2]
params.Topic = BytesToTopic(f.Topics[2])
params.PoW = smallPoW
params.TTL = 3600 * 24 // one day
msg := NewSentMessage(params)