github.com/yinchengtsinghua/golang-Eos-dpos-Ethereum@v0.0.0-20190121132951-92cc4225ed8e/whisper/whisperv6/api_test.go (about)

     1  
     2  //此源码被清华学神尹成大魔王专业翻译分析并修改
     3  //尹成QQ77025077
     4  //尹成微信18510341407
     5  //尹成所在QQ群721929980
     6  //尹成邮箱 yinc13@mails.tsinghua.edu.cn
     7  //尹成毕业于清华大学,微软区块链领域全球最有价值专家
     8  //https://mvp.microsoft.com/zh-cn/PublicProfile/4033620
     9  //
    10  //
    11  //
    12  //
    13  //
    14  //
    15  //
    16  //
    17  //
    18  //
    19  //
    20  //
    21  //
    22  //
    23  //
    24  
    25  package whisperv6
    26  
    27  import (
    28  	"bytes"
    29  	"crypto/ecdsa"
    30  	"testing"
    31  	"time"
    32  
    33  	mapset "github.com/deckarep/golang-set"
    34  	"github.com/ethereum/go-ethereum/common"
    35  )
    36  
    37  func TestMultipleTopicCopyInNewMessageFilter(t *testing.T) {
    38  	w := &Whisper{
    39  		privateKeys:   make(map[string]*ecdsa.PrivateKey),
    40  		symKeys:       make(map[string][]byte),
    41  		envelopes:     make(map[common.Hash]*Envelope),
    42  		expirations:   make(map[uint32]mapset.Set),
    43  		peers:         make(map[*Peer]struct{}),
    44  		messageQueue:  make(chan *Envelope, messageQueueLimit),
    45  		p2pMsgQueue:   make(chan *Envelope, messageQueueLimit),
    46  		quit:          make(chan struct{}),
    47  		syncAllowance: DefaultSyncAllowance,
    48  	}
    49  	w.filters = NewFilters(w)
    50  
    51  	keyID, err := w.GenerateSymKey()
    52  	if err != nil {
    53  		t.Fatalf("Error generating symmetric key: %v", err)
    54  	}
    55  	api := PublicWhisperAPI{
    56  		w:        w,
    57  		lastUsed: make(map[string]time.Time),
    58  	}
    59  
    60  	t1 := [4]byte{0xde, 0xea, 0xbe, 0xef}
    61  	t2 := [4]byte{0xca, 0xfe, 0xde, 0xca}
    62  
    63  	crit := Criteria{
    64  		SymKeyID: keyID,
    65  		Topics:   []TopicType{TopicType(t1), TopicType(t2)},
    66  	}
    67  
    68  	_, err = api.NewMessageFilter(crit)
    69  	if err != nil {
    70  		t.Fatalf("Error creating the filter: %v", err)
    71  	}
    72  
    73  	found := false
    74  	candidates := w.filters.getWatchersByTopic(TopicType(t1))
    75  	for _, f := range candidates {
    76  		if len(f.Topics) == 2 {
    77  			if bytes.Equal(f.Topics[0], t1[:]) && bytes.Equal(f.Topics[1], t2[:]) {
    78  				found = true
    79  			}
    80  		}
    81  	}
    82  
    83  	if !found {
    84  		t.Fatalf("Could not find filter with both topics")
    85  	}
    86  }