Update selected mail servers that are used in envelope tracker
This commit is contained in:
parent
d61e838235
commit
e60dbe3c1b
|
@ -11,6 +11,7 @@ import (
|
||||||
"github.com/ethereum/go-ethereum/common/hexutil"
|
"github.com/ethereum/go-ethereum/common/hexutil"
|
||||||
"github.com/ethereum/go-ethereum/log"
|
"github.com/ethereum/go-ethereum/log"
|
||||||
gethnode "github.com/ethereum/go-ethereum/node"
|
gethnode "github.com/ethereum/go-ethereum/node"
|
||||||
|
"github.com/ethereum/go-ethereum/p2p/enode"
|
||||||
|
|
||||||
fcmlib "github.com/NaySoftware/go-fcm"
|
fcmlib "github.com/NaySoftware/go-fcm"
|
||||||
|
|
||||||
|
@ -571,3 +572,21 @@ func (b *StatusBackend) DisableInstallation(installationID string) error {
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// UpdateMailservers on ShhExtService.
|
||||||
|
func (b *StatusBackend) UpdateMailservers(enodes []string) error {
|
||||||
|
st, err := b.statusNode.ShhExtService()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
nodes := make([]*enode.Node, len(enodes))
|
||||||
|
for i, rawurl := range enodes {
|
||||||
|
node, err := enode.ParseV4(rawurl)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
nodes[i] = node
|
||||||
|
}
|
||||||
|
st.UpdateMailservers(nodes)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
|
@ -462,6 +462,18 @@ func NotifyUsers(message, payloadJSON, tokensArray *C.char) (outCBytes *C.char)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// UpdateMailservers updates mail servers in status backend.
|
||||||
|
//export UpdateMailservers
|
||||||
|
func UpdateMailservers(data *C.char) *C.char {
|
||||||
|
var enodes []string
|
||||||
|
err := json.Unmarshal([]byte(C.GoString(data)), &enodes)
|
||||||
|
if err != nil {
|
||||||
|
return makeJSONResponse(err)
|
||||||
|
}
|
||||||
|
err = statusBackend.UpdateMailservers(enodes)
|
||||||
|
return makeJSONResponse(err)
|
||||||
|
}
|
||||||
|
|
||||||
// AddPeer adds an enode as a peer.
|
// AddPeer adds an enode as a peer.
|
||||||
//export AddPeer
|
//export AddPeer
|
||||||
func AddPeer(enode *C.char) *C.char {
|
func AddPeer(enode *C.char) *C.char {
|
||||||
|
|
|
@ -13,6 +13,7 @@ import (
|
||||||
"github.com/ethereum/go-ethereum/log"
|
"github.com/ethereum/go-ethereum/log"
|
||||||
"github.com/ethereum/go-ethereum/node"
|
"github.com/ethereum/go-ethereum/node"
|
||||||
"github.com/ethereum/go-ethereum/p2p"
|
"github.com/ethereum/go-ethereum/p2p"
|
||||||
|
"github.com/ethereum/go-ethereum/p2p/enode"
|
||||||
"github.com/ethereum/go-ethereum/rpc"
|
"github.com/ethereum/go-ethereum/rpc"
|
||||||
"github.com/status-im/status-go/services/shhext/chat"
|
"github.com/status-im/status-go/services/shhext/chat"
|
||||||
"github.com/status-im/status-go/services/shhext/dedup"
|
"github.com/status-im/status-go/services/shhext/dedup"
|
||||||
|
@ -60,6 +61,7 @@ type ServiceConfig struct {
|
||||||
InstallationID string
|
InstallationID string
|
||||||
Debug bool
|
Debug bool
|
||||||
PFSEnabled bool
|
PFSEnabled bool
|
||||||
|
MailServerConfirmations bool
|
||||||
}
|
}
|
||||||
|
|
||||||
// Make sure that Service implements node.Service interface.
|
// Make sure that Service implements node.Service interface.
|
||||||
|
@ -72,6 +74,8 @@ func New(w *whisper.Whisper, handler EnvelopeEventsHandler, db *leveldb.DB, conf
|
||||||
handler: handler,
|
handler: handler,
|
||||||
cache: map[common.Hash]EnvelopeState{},
|
cache: map[common.Hash]EnvelopeState{},
|
||||||
batches: map[common.Hash]map[common.Hash]struct{}{},
|
batches: map[common.Hash]map[common.Hash]struct{}{},
|
||||||
|
mailservers: map[enode.ID]struct{}{},
|
||||||
|
mailServerConfirmation: config.MailServerConfirmations,
|
||||||
}
|
}
|
||||||
return &Service{
|
return &Service{
|
||||||
w: w,
|
w: w,
|
||||||
|
@ -84,6 +88,11 @@ func New(w *whisper.Whisper, handler EnvelopeEventsHandler, db *leveldb.DB, conf
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// UpdateMailservers updates information about selected mail servers.
|
||||||
|
func (s *Service) UpdateMailservers(nodes []*enode.Node) {
|
||||||
|
s.tracker.UpdateMailservers(nodes)
|
||||||
|
}
|
||||||
|
|
||||||
// Protocols returns a new protocols list. In this case, there are none.
|
// Protocols returns a new protocols list. In this case, there are none.
|
||||||
func (s *Service) Protocols() []p2p.Protocol {
|
func (s *Service) Protocols() []p2p.Protocol {
|
||||||
return []p2p.Protocol{}
|
return []p2p.Protocol{}
|
||||||
|
@ -184,11 +193,15 @@ func (s *Service) Stop() error {
|
||||||
type tracker struct {
|
type tracker struct {
|
||||||
w *whisper.Whisper
|
w *whisper.Whisper
|
||||||
handler EnvelopeEventsHandler
|
handler EnvelopeEventsHandler
|
||||||
|
mailServerConfirmation bool
|
||||||
|
|
||||||
mu sync.Mutex
|
mu sync.Mutex
|
||||||
cache map[common.Hash]EnvelopeState
|
cache map[common.Hash]EnvelopeState
|
||||||
batches map[common.Hash]map[common.Hash]struct{}
|
batches map[common.Hash]map[common.Hash]struct{}
|
||||||
|
|
||||||
|
mailMu sync.Mutex
|
||||||
|
mailservers map[enode.ID]struct{}
|
||||||
|
|
||||||
wg sync.WaitGroup
|
wg sync.WaitGroup
|
||||||
quit chan struct{}
|
quit chan struct{}
|
||||||
}
|
}
|
||||||
|
@ -216,6 +229,15 @@ func (t *tracker) Add(hash common.Hash) {
|
||||||
t.cache[hash] = EnvelopePosted
|
t.cache[hash] = EnvelopePosted
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (t *tracker) UpdateMailservers(nodes []*enode.Node) {
|
||||||
|
t.mailMu.Lock()
|
||||||
|
defer t.mailMu.Unlock()
|
||||||
|
t.mailservers = map[enode.ID]struct{}{}
|
||||||
|
for _, n := range nodes {
|
||||||
|
t.mailservers[n.ID()] = struct{}{}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Add request hash to a tracker.
|
// Add request hash to a tracker.
|
||||||
func (t *tracker) AddRequest(hash common.Hash, timerC <-chan time.Time) {
|
func (t *tracker) AddRequest(hash common.Hash, timerC <-chan time.Time) {
|
||||||
t.mu.Lock()
|
t.mu.Lock()
|
||||||
|
@ -268,6 +290,12 @@ func (t *tracker) handleEvent(event whisper.EnvelopeEvent) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *tracker) handleEventEnvelopeSent(event whisper.EnvelopeEvent) {
|
func (t *tracker) handleEventEnvelopeSent(event whisper.EnvelopeEvent) {
|
||||||
|
if t.mailServerConfirmation {
|
||||||
|
if !t.isMailserver(event.Peer) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
t.mu.Lock()
|
t.mu.Lock()
|
||||||
defer t.mu.Unlock()
|
defer t.mu.Unlock()
|
||||||
|
|
||||||
|
@ -292,7 +320,28 @@ func (t *tracker) handleEventEnvelopeSent(event whisper.EnvelopeEvent) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (t *tracker) isMailserver(peer enode.ID) bool {
|
||||||
|
t.mailMu.Lock()
|
||||||
|
defer t.mailMu.Unlock()
|
||||||
|
if len(t.mailservers) == 0 {
|
||||||
|
log.Error("mail servers are empty")
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
_, exist := t.mailservers[peer]
|
||||||
|
if !exist {
|
||||||
|
log.Debug("confirmation received not from a mail server is skipped", "peer", peer)
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
func (t *tracker) handleAcknowledgedBatch(event whisper.EnvelopeEvent) {
|
func (t *tracker) handleAcknowledgedBatch(event whisper.EnvelopeEvent) {
|
||||||
|
if t.mailServerConfirmation {
|
||||||
|
if !t.isMailserver(event.Peer) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
t.mu.Lock()
|
t.mu.Lock()
|
||||||
defer t.mu.Unlock()
|
defer t.mu.Unlock()
|
||||||
|
|
||||||
|
|
|
@ -12,6 +12,7 @@ import (
|
||||||
"github.com/ethereum/go-ethereum/common"
|
"github.com/ethereum/go-ethereum/common"
|
||||||
"github.com/ethereum/go-ethereum/node"
|
"github.com/ethereum/go-ethereum/node"
|
||||||
"github.com/ethereum/go-ethereum/p2p"
|
"github.com/ethereum/go-ethereum/p2p"
|
||||||
|
"github.com/ethereum/go-ethereum/p2p/enode"
|
||||||
"github.com/status-im/status-go/t/helpers"
|
"github.com/status-im/status-go/t/helpers"
|
||||||
whisper "github.com/status-im/whisper/whisperv6"
|
whisper "github.com/status-im/whisper/whisperv6"
|
||||||
"github.com/stretchr/testify/suite"
|
"github.com/stretchr/testify/suite"
|
||||||
|
@ -92,6 +93,7 @@ func (s *ShhExtSuite) SetupTest() {
|
||||||
DataDir: os.TempDir(),
|
DataDir: os.TempDir(),
|
||||||
Debug: true,
|
Debug: true,
|
||||||
PFSEnabled: false,
|
PFSEnabled: false,
|
||||||
|
MailServerConfirmations: true,
|
||||||
}
|
}
|
||||||
s.services[i] = New(s.whisper[i], nil, nil, config)
|
s.services[i] = New(s.whisper[i], nil, nil, config)
|
||||||
s.NoError(stack.Register(func(n *node.ServiceContext) (node.Service, error) {
|
s.NoError(stack.Register(func(n *node.ServiceContext) (node.Service, error) {
|
||||||
|
@ -106,6 +108,7 @@ func (s *ShhExtSuite) SetupTest() {
|
||||||
func (s *ShhExtSuite) TestPostMessageWithConfirmation() {
|
func (s *ShhExtSuite) TestPostMessageWithConfirmation() {
|
||||||
mock := newHandlerMock(1)
|
mock := newHandlerMock(1)
|
||||||
s.services[0].tracker.handler = mock
|
s.services[0].tracker.handler = mock
|
||||||
|
s.services[0].UpdateMailservers([]*enode.Node{s.nodes[1].Server().Self()})
|
||||||
s.nodes[0].Server().AddPeer(s.nodes[1].Server().Self())
|
s.nodes[0].Server().AddPeer(s.nodes[1].Server().Self())
|
||||||
symID, err := s.whisper[0].GenerateSymKey()
|
symID, err := s.whisper[0].GenerateSymKey()
|
||||||
s.NoError(err)
|
s.NoError(err)
|
||||||
|
@ -413,16 +416,21 @@ func (s *TrackerSuite) SetupTest() {
|
||||||
s.tracker = &tracker{
|
s.tracker = &tracker{
|
||||||
cache: map[common.Hash]EnvelopeState{},
|
cache: map[common.Hash]EnvelopeState{},
|
||||||
batches: map[common.Hash]map[common.Hash]struct{}{},
|
batches: map[common.Hash]map[common.Hash]struct{}{},
|
||||||
|
mailservers: map[enode.ID]struct{}{},
|
||||||
|
mailServerConfirmation: true,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *TrackerSuite) TestConfirmed() {
|
func (s *TrackerSuite) TestConfirmed() {
|
||||||
|
testPeer := enode.ID{1}
|
||||||
|
s.tracker.mailservers[testPeer] = struct{}{}
|
||||||
s.tracker.Add(testHash)
|
s.tracker.Add(testHash)
|
||||||
s.Contains(s.tracker.cache, testHash)
|
s.Contains(s.tracker.cache, testHash)
|
||||||
s.Equal(EnvelopePosted, s.tracker.cache[testHash])
|
s.Equal(EnvelopePosted, s.tracker.cache[testHash])
|
||||||
s.tracker.handleEvent(whisper.EnvelopeEvent{
|
s.tracker.handleEvent(whisper.EnvelopeEvent{
|
||||||
Event: whisper.EventEnvelopeSent,
|
Event: whisper.EventEnvelopeSent,
|
||||||
Hash: testHash,
|
Hash: testHash,
|
||||||
|
Peer: testPeer,
|
||||||
})
|
})
|
||||||
s.Contains(s.tracker.cache, testHash)
|
s.Contains(s.tracker.cache, testHash)
|
||||||
s.Equal(EnvelopeSent, s.tracker.cache[testHash])
|
s.Equal(EnvelopeSent, s.tracker.cache[testHash])
|
||||||
|
@ -430,18 +438,22 @@ func (s *TrackerSuite) TestConfirmed() {
|
||||||
|
|
||||||
func (s *TrackerSuite) TestConfirmedWithAcknowledge() {
|
func (s *TrackerSuite) TestConfirmedWithAcknowledge() {
|
||||||
testBatch := common.Hash{1}
|
testBatch := common.Hash{1}
|
||||||
|
testPeer := enode.ID{1}
|
||||||
s.tracker.Add(testHash)
|
s.tracker.Add(testHash)
|
||||||
|
s.tracker.mailservers[testPeer] = struct{}{}
|
||||||
s.Contains(s.tracker.cache, testHash)
|
s.Contains(s.tracker.cache, testHash)
|
||||||
s.Equal(EnvelopePosted, s.tracker.cache[testHash])
|
s.Equal(EnvelopePosted, s.tracker.cache[testHash])
|
||||||
s.tracker.handleEvent(whisper.EnvelopeEvent{
|
s.tracker.handleEvent(whisper.EnvelopeEvent{
|
||||||
Event: whisper.EventEnvelopeSent,
|
Event: whisper.EventEnvelopeSent,
|
||||||
Hash: testHash,
|
Hash: testHash,
|
||||||
Batch: testBatch,
|
Batch: testBatch,
|
||||||
|
Peer: testPeer,
|
||||||
})
|
})
|
||||||
s.Equal(EnvelopePosted, s.tracker.cache[testHash])
|
s.Equal(EnvelopePosted, s.tracker.cache[testHash])
|
||||||
s.tracker.handleEvent(whisper.EnvelopeEvent{
|
s.tracker.handleEvent(whisper.EnvelopeEvent{
|
||||||
Event: whisper.EventBatchAcknowledged,
|
Event: whisper.EventBatchAcknowledged,
|
||||||
Batch: testBatch,
|
Batch: testBatch,
|
||||||
|
Peer: testPeer,
|
||||||
})
|
})
|
||||||
s.Contains(s.tracker.cache, testHash)
|
s.Contains(s.tracker.cache, testHash)
|
||||||
s.Equal(EnvelopeSent, s.tracker.cache[testHash])
|
s.Equal(EnvelopeSent, s.tracker.cache[testHash])
|
||||||
|
|
Loading…
Reference in New Issue