2016-09-11 11:44:14 +00:00
|
|
|
package geth_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/ethereum/go-ethereum/common"
|
|
|
|
"github.com/ethereum/go-ethereum/crypto"
|
2017-03-21 16:27:59 +00:00
|
|
|
whisper "github.com/ethereum/go-ethereum/whisper/whisperv5"
|
2016-09-11 11:44:14 +00:00
|
|
|
"github.com/status-im/status-go/geth"
|
|
|
|
)
|
|
|
|
|
2017-03-21 16:27:59 +00:00
|
|
|
func TestWhisperFilterRace(t *testing.T) {
|
2016-09-11 11:44:14 +00:00
|
|
|
err := geth.PrepareTestNode()
|
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2016-12-07 21:07:08 +00:00
|
|
|
whisperService, err := geth.NodeManagerInstance().WhisperService()
|
2016-09-11 11:44:14 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("whisper service not running: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
whisperAPI := whisper.NewPublicWhisperAPI(whisperService)
|
|
|
|
|
2017-03-21 16:27:59 +00:00
|
|
|
// account1
|
|
|
|
_, accountKey1, err := geth.AddressToDecryptedAccount(testConfig.Account1.Address, testConfig.Account1.Password)
|
2016-09-11 11:44:14 +00:00
|
|
|
if err != nil {
|
2017-03-21 16:27:59 +00:00
|
|
|
t.Fatal(err)
|
2016-09-11 11:44:14 +00:00
|
|
|
}
|
2017-03-21 16:27:59 +00:00
|
|
|
accountKey1Hex := common.ToHex(crypto.FromECDSAPub(&accountKey1.PrivateKey.PublicKey))
|
2016-09-11 11:44:14 +00:00
|
|
|
|
2017-03-21 16:27:59 +00:00
|
|
|
whisperService.AddIdentity(accountKey1.PrivateKey)
|
|
|
|
if ok, err := whisperAPI.HasIdentity(accountKey1Hex); err != nil || !ok {
|
|
|
|
t.Fatalf("identity not injected: %v", accountKey1Hex)
|
2016-09-11 11:44:14 +00:00
|
|
|
}
|
|
|
|
|
2017-03-21 16:27:59 +00:00
|
|
|
// account2
|
|
|
|
_, accountKey2, err := geth.AddressToDecryptedAccount(testConfig.Account2.Address, testConfig.Account2.Password)
|
2016-09-11 11:44:14 +00:00
|
|
|
if err != nil {
|
2017-03-21 16:27:59 +00:00
|
|
|
t.Fatal(err)
|
2016-09-11 11:44:14 +00:00
|
|
|
}
|
2017-03-21 16:27:59 +00:00
|
|
|
accountKey2Hex := common.ToHex(crypto.FromECDSAPub(&accountKey2.PrivateKey.PublicKey))
|
2016-09-11 11:44:14 +00:00
|
|
|
|
2017-03-21 16:27:59 +00:00
|
|
|
whisperService.AddIdentity(accountKey2.PrivateKey)
|
|
|
|
if ok, err := whisperAPI.HasIdentity(accountKey2Hex); err != nil || !ok {
|
|
|
|
t.Fatalf("identity not injected: %v", accountKey2Hex)
|
2016-09-11 11:44:14 +00:00
|
|
|
}
|
|
|
|
|
2017-03-21 16:27:59 +00:00
|
|
|
// race filter addition
|
|
|
|
filterAdded := make(chan struct{})
|
|
|
|
allFiltersAdded := make(chan struct{})
|
2016-09-11 11:44:14 +00:00
|
|
|
|
2017-03-21 16:27:59 +00:00
|
|
|
go func() {
|
|
|
|
counter := 10
|
|
|
|
for range filterAdded {
|
|
|
|
counter--
|
|
|
|
if counter <= 0 {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
2016-09-11 11:44:14 +00:00
|
|
|
|
2017-03-21 16:27:59 +00:00
|
|
|
close(allFiltersAdded)
|
|
|
|
}()
|
2016-09-11 11:44:14 +00:00
|
|
|
|
2017-03-21 16:27:59 +00:00
|
|
|
for i := 0; i < 10; i++ {
|
|
|
|
go func() {
|
|
|
|
whisperAPI.NewFilter(whisper.WhisperFilterArgs{
|
|
|
|
From: accountKey1Hex,
|
|
|
|
To: accountKey2Hex,
|
|
|
|
Topics: []whisper.TopicType{
|
|
|
|
{0x4e, 0x03, 0x65, 0x7a}, {0x34, 0x60, 0x7c, 0x9b}, {0x21, 0x41, 0x7d, 0xf9},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
filterAdded <- struct{}{}
|
|
|
|
}()
|
2016-09-11 11:44:14 +00:00
|
|
|
}
|
|
|
|
|
2017-03-21 16:27:59 +00:00
|
|
|
<-allFiltersAdded
|
2016-09-11 11:44:14 +00:00
|
|
|
}
|