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

View file

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

View file

@ -204,22 +204,22 @@ func TestUnmarshalFilterArgs(t *testing.T) {
} }
i := 0 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]) t.Fatalf("wrong topic[%d]: %x.", i, f.Topics[i])
} }
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]) t.Fatalf("wrong topic[%d]: %x.", i, f.Topics[i])
} }
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]) t.Fatalf("wrong topic[%d]: %x.", i, f.Topics[i])
} }
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]) t.Fatalf("wrong topic[%d]: %x.", i, f.Topics[i])
} }
} }
@ -347,7 +347,9 @@ func TestIntegrationAsym(t *testing.T) {
f.Symmetric = false f.Symmetric = false
f.Key = key f.Key = key
f.SignedWith = sigPubKey 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.MinPoW = DefaultMinimumPoW / 2
f.AllowP2P = true f.AllowP2P = true
@ -442,7 +444,9 @@ func TestIntegrationSym(t *testing.T) {
var f WhisperFilterArgs var f WhisperFilterArgs
f.Symmetric = true f.Symmetric = true
f.Key = symKeyID 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.MinPoW = 0.324
f.SignedWith = sigPubKey f.SignedWith = sigPubKey
f.AllowP2P = false f.AllowP2P = false
@ -538,7 +542,9 @@ func TestIntegrationSymWithFilter(t *testing.T) {
var f WhisperFilterArgs var f WhisperFilterArgs
f.Symmetric = true f.Symmetric = true
f.Key = symKeyID 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.MinPoW = 0.324
f.SignedWith = sigPubKey f.SignedWith = sigPubKey
f.AllowP2P = false f.AllowP2P = false

View file

@ -29,7 +29,7 @@ type Filter struct {
Src *ecdsa.PublicKey // Sender of the message Src *ecdsa.PublicKey // Sender of the message
KeyAsym *ecdsa.PrivateKey // Private Key of recipient KeyAsym *ecdsa.PrivateKey // Private Key of recipient
KeySym []byte // Key associated with the Topic 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 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 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 SymKeyHash common.Hash // The Keccak256Hash of the symmetric key, needed for optimization
@ -201,12 +201,14 @@ func (f *Filter) MatchTopic(topic TopicType) bool {
return true return true
} }
for _, t := range f.Topics { for _, bt := range f.Topics {
if t == topic { for j, b := range bt {
return true if topic[j] != b {
}
}
return false return false
}
}
}
return true
} }
func IsPubKeyEqual(a, b *ecdsa.PublicKey) bool { 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) f.Messages = make(map[common.Hash]*ReceivedMessage)
const topicNum = 8 const topicNum = 8
f.Topics = make([]TopicType, topicNum) f.Topics = make([][]byte, topicNum)
for i := 0; i < topicNum; i++ { for i := 0; i < topicNum; i++ {
f.Topics[i] = make([]byte, 4)
randomize(f.Topics[i][:]) randomize(f.Topics[i][:])
f.Topics[i][0] = 0x01 f.Topics[i][0] = 0x01
} }
@ -194,8 +195,8 @@ func TestMatchEnvelope(t *testing.T) {
// encrypt symmetrically // encrypt symmetrically
i := rand.Int() % 4 i := rand.Int() % 4
fsym.Topics[i] = params.Topic fsym.Topics[i] = params.Topic[:]
fasym.Topics[i] = params.Topic fasym.Topics[i] = params.Topic[:]
msg = NewSentMessage(params) msg = NewSentMessage(params)
env, err = msg.Wrap(params) env, err = msg.Wrap(params)
if err != nil { if err != nil {
@ -320,7 +321,7 @@ func TestMatchMessageSym(t *testing.T) {
const index = 1 const index = 1
params.KeySym = f.KeySym params.KeySym = f.KeySym
params.Topic = f.Topics[index] params.Topic = BytesToTopic(f.Topics[index])
sentMessage := NewSentMessage(params) sentMessage := NewSentMessage(params)
env, err := sentMessage.Wrap(params) env, err := sentMessage.Wrap(params)
@ -413,7 +414,7 @@ func TestMatchMessageAsym(t *testing.T) {
} }
const index = 1 const index = 1
params.Topic = f.Topics[index] params.Topic = BytesToTopic(f.Topics[index])
params.Dst = &f.KeyAsym.PublicKey params.Dst = &f.KeyAsym.PublicKey
keySymOrig := params.KeySym keySymOrig := params.KeySym
params.KeySym = nil params.KeySym = nil
@ -504,7 +505,7 @@ func generateCompatibeEnvelope(t *testing.T, f *Filter) *Envelope {
} }
params.KeySym = f.KeySym params.KeySym = f.KeySym
params.Topic = f.Topics[2] params.Topic = BytesToTopic(f.Topics[2])
sentMessage := NewSentMessage(params) sentMessage := NewSentMessage(params)
env, err := sentMessage.Wrap(params) env, err := sentMessage.Wrap(params)
if err != nil { if err != nil {

View file

@ -118,7 +118,9 @@ func initialize(t *testing.T) {
node.shh.Start(nil) node.shh.Start(nil)
topics := make([]TopicType, 0) topics := make([]TopicType, 0)
topics = append(topics, sharedTopic) 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) node.filerId, err = node.shh.Watch(&f)
if err != nil { if err != nil {
t.Fatalf("failed to install the filter: %s.", err) 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.KeySym = f.KeySym
params.Topic = f.Topics[2] params.Topic = BytesToTopic(f.Topics[2])
params.PoW = smallPoW params.PoW = smallPoW
params.TTL = 3600 * 24 // one day params.TTL = 3600 * 24 // one day
msg := NewSentMessage(params) msg := NewSentMessage(params)