Skip to content

Commit d38b155

Browse files
authored
chore_: move Mailserver to waku/types (#6456)
1 parent d76db2c commit d38b155

14 files changed

+173
-155
lines changed

protocol/messenger_config_test.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"github.com/status-im/status-go/protocol/communities"
1010
"github.com/status-im/status-go/services/mailservers"
1111
"github.com/status-im/status-go/t/helpers"
12+
"github.com/status-im/status-go/waku/types"
1213
)
1314

1415
func WithTestStoreNode(s *suite.Suite, id string, address multiaddr.Multiaddr, fleet string, collectiblesServiceMock *CollectiblesServiceMock) Option {
@@ -17,7 +18,7 @@ func WithTestStoreNode(s *suite.Suite, id string, address multiaddr.Multiaddr, f
1718
s.Require().NoError(err)
1819

1920
db := mailservers.NewDB(sqldb)
20-
err = db.Add(mailservers.Mailserver{
21+
err = db.Add(types.Mailserver{
2122
ID: id,
2223
Name: id,
2324
Addr: &address,

protocol/messenger_mailserver_cycle.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,10 @@ import (
88
"github.com/status-im/status-go/params"
99
"github.com/status-im/status-go/services/mailservers"
1010
"github.com/status-im/status-go/signal"
11+
"github.com/status-im/status-go/waku/types"
1112
)
1213

13-
func (m *Messenger) AllMailservers() ([]mailservers.Mailserver, error) {
14+
func (m *Messenger) AllMailservers() ([]types.Mailserver, error) {
1415
// Get configured fleet
1516
fleet, err := m.getFleet()
1617
if err != nil {
@@ -149,7 +150,7 @@ func (m *Messenger) checkForStorenodeCycleSignals() {
149150
return
150151
}
151152

152-
mailserverMap := make(map[peer.ID]mailservers.Mailserver)
153+
mailserverMap := make(map[peer.ID]types.Mailserver)
153154
for _, ms := range allMailservers {
154155
peerID, err := ms.PeerID()
155156
if err != nil {

protocol/messenger_response.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"golang.org/x/exp/maps"
77

88
ensservice "github.com/status-im/status-go/services/ens"
9+
"github.com/status-im/status-go/waku/types"
910

1011
"github.com/status-im/status-go/services/browsers"
1112
"github.com/status-im/status-go/services/wallet"
@@ -23,7 +24,6 @@ import (
2324
"github.com/status-im/status-go/protocol/storenodes"
2425
"github.com/status-im/status-go/protocol/verification"
2526
localnotifications "github.com/status-im/status-go/services/local-notifications"
26-
"github.com/status-im/status-go/services/mailservers"
2727
)
2828

2929
type RemovedMessage struct {
@@ -49,7 +49,7 @@ type MessengerResponse struct {
4949
Invitations []*GroupChatInvitation
5050
CommunityChanges []*communities.CommunityChanges
5151
AnonymousMetrics []*appmetrics.AppMetric
52-
Mailservers []mailservers.Mailserver
52+
Mailservers []types.Mailserver
5353
CommunityStorenodes []storenodes.Storenode
5454
Bookmarks []*browsers.Bookmark
5555
Settings []*settings.SyncSettingField
@@ -109,7 +109,7 @@ func (r *MessengerResponse) MarshalJSON() ([]byte, error) {
109109
Invitations []*GroupChatInvitation `json:"invitations,omitempty"`
110110
CommunityChanges []*communities.CommunityChanges `json:"communityChanges,omitempty"`
111111
RequestsToJoinCommunity []*communities.RequestToJoin `json:"requestsToJoinCommunity,omitempty"`
112-
Mailservers []mailservers.Mailserver `json:"mailservers,omitempty"`
112+
Mailservers []types.Mailserver `json:"mailservers,omitempty"`
113113
CommunityStorenodes []storenodes.Storenode `json:"communityStorenodes,omitempty"`
114114
Bookmarks []*browsers.Bookmark `json:"bookmarks,omitempty"`
115115
ClearedHistories []*ClearedHistory `json:"clearedHistories,omitempty"`

protocol/messenger_storenode_comunity_test.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import (
1919
"go.uber.org/zap"
2020

2121
"github.com/ethereum/go-ethereum/crypto"
22+
2223
"github.com/status-im/status-go/appdatabase"
2324
"github.com/status-im/status-go/protocol/protobuf"
2425
"github.com/status-im/status-go/protocol/requests"
@@ -123,7 +124,7 @@ func (s *MessengerStoreNodeCommunitySuite) newMessenger(name string, storenodeAd
123124
s.Require().NoError(err)
124125

125126
mailserversDatabase := mailserversDB.NewDB(mailserversSQLDb)
126-
err = mailserversDatabase.Add(mailserversDB.Mailserver{
127+
err = mailserversDatabase.Add(wakutypes.Mailserver{
127128
ID: localMailserverID,
128129
Name: localMailserverID,
129130
Addr: storenodeAddress,

protocol/messenger_storenode_request_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -1070,7 +1070,7 @@ func (s *MessengerStoreNodeRequestSuite) TestFetchRealCommunity() {
10701070
// We run a separate request for each node in the fleet.
10711071
for i, mailserver := range nodesList {
10721072
wg.Add(1)
1073-
go func(i int, mailserver mailserversDB.Mailserver) {
1073+
go func(i int, mailserver wakutypes.Mailserver) {
10741074
defer wg.Done()
10751075

10761076
fmt.Printf("--- starting request [%d] from %s\n", i, mailserver.ID)

protocol/storenodes/models.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import (
55

66
"github.com/status-im/status-go/eth-node/types"
77
"github.com/status-im/status-go/protocol/protobuf"
8-
"github.com/status-im/status-go/services/mailservers"
8+
wakutypes "github.com/status-im/status-go/waku/types"
99
)
1010

1111
// Storenode is a struct that represents a storenode, it is very closely related to `mailservers.Mailserver`
@@ -63,8 +63,8 @@ func FromProtobuf(storenodes []*protobuf.Storenode, clock uint64) Storenodes {
6363
return result
6464
}
6565

66-
func toMailserver(m Storenode) mailservers.Mailserver {
67-
return mailservers.Mailserver{
66+
func toMailserver(m Storenode) wakutypes.Mailserver {
67+
return wakutypes.Mailserver{
6868
ID: m.StorenodeID,
6969
Name: m.Name,
7070
Custom: true,

protocol/storenodes/storenodes.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import (
1111
"github.com/waku-org/go-waku/waku/v2/utils"
1212

1313
"github.com/status-im/status-go/eth-node/types"
14-
"github.com/status-im/status-go/services/mailservers"
14+
wakutypes "github.com/status-im/status-go/waku/types"
1515
)
1616

1717
var (
@@ -44,13 +44,13 @@ type storenodesData struct {
4444
}
4545

4646
// GetStorenodeByCommunityID returns the active storenode for a community
47-
func (m *CommunityStorenodes) GetStorenodeByCommunityID(communityID string) (mailservers.Mailserver, error) {
47+
func (m *CommunityStorenodes) GetStorenodeByCommunityID(communityID string) (wakutypes.Mailserver, error) {
4848
m.storenodesByCommunityIDMutex.RLock()
4949
defer m.storenodesByCommunityIDMutex.RUnlock()
5050

5151
msData, ok := m.storenodesByCommunityID[communityID]
5252
if !ok || len(msData.storenodes) == 0 {
53-
return mailservers.Mailserver{}, ErrNotFound
53+
return wakutypes.Mailserver{}, ErrNotFound
5454
}
5555
return toMailserver(msData.storenodes[0]), nil
5656
}

protocol/storenodes/storenodes_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import (
66
"github.com/multiformats/go-multiaddr"
77
"github.com/stretchr/testify/require"
88

9-
"github.com/status-im/status-go/services/mailservers"
9+
"github.com/status-im/status-go/waku/types"
1010
)
1111

1212
func TestSerialization(t *testing.T) {
@@ -74,7 +74,7 @@ func TestUpdateStorenodesInDB(t *testing.T) {
7474
matchStoreNode(t, snodes2[0], ms2)
7575
}
7676

77-
func matchStoreNode(t *testing.T, sn Storenode, ms mailservers.Mailserver) {
77+
func matchStoreNode(t *testing.T, sn Storenode, ms types.Mailserver) {
7878
require.Equal(t, sn.StorenodeID, ms.ID)
7979
require.Equal(t, sn.Name, ms.Name)
8080
require.Equal(t, sn.Address.String(), (*ms.Addr).String())

services/mailservers/api.go

+7-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
package mailservers
22

3-
import "context"
3+
import (
4+
"context"
5+
6+
"github.com/status-im/status-go/waku/types"
7+
)
48

59
func NewAPI(db *Database) *API {
610
return &API{db}
@@ -11,11 +15,11 @@ type API struct {
1115
db *Database
1216
}
1317

14-
func (a *API) AddMailserver(ctx context.Context, m Mailserver) error {
18+
func (a *API) AddMailserver(ctx context.Context, m types.Mailserver) error {
1519
return a.db.Add(m)
1620
}
1721

18-
func (a *API) GetMailservers(ctx context.Context) ([]Mailserver, error) {
22+
func (a *API) GetMailservers(ctx context.Context) ([]types.Mailserver, error) {
1923
return a.db.Mailservers()
2024
}
2125

services/mailservers/api_test.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,10 @@ func TestAddGetDeleteMailserver(t *testing.T) {
2626
db, close := setupTestDB(t)
2727
defer close()
2828
api := &API{db: db}
29-
testMailserver := Mailserver{
29+
testMailserver := wakutypes.Mailserver{
3030
ID: "mailserver001",
3131
Name: "My Mailserver",
32-
Addr: MustDecodeMultiaddress("/dns4/node-01.do-ams3.waku.test.status.im/tcp/30303/p2p/16Uiu2HAkykgaECHswi3YKJ5dMLbq2kPVCo89fcyTd38UcQD6ej5W"),
32+
Addr: wakutypes.MustDecodeMultiaddress("/dns4/node-01.do-ams3.waku.test.status.im/tcp/30303/p2p/16Uiu2HAkykgaECHswi3YKJ5dMLbq2kPVCo89fcyTd38UcQD6ej5W"),
3333
Custom: true,
3434
Fleet: "prod",
3535
}
@@ -44,14 +44,14 @@ func TestAddGetDeleteMailserver(t *testing.T) {
4444

4545
mailservers, err := api.GetMailservers(context.Background())
4646
require.NoError(t, err)
47-
require.EqualValues(t, []Mailserver{testMailserver, testMailserverWithPassword}, mailservers)
47+
require.EqualValues(t, []wakutypes.Mailserver{testMailserver, testMailserverWithPassword}, mailservers)
4848

4949
err = api.DeleteMailserver(context.Background(), testMailserver.ID)
5050
require.NoError(t, err)
5151
// Verify they was deleted.
5252
mailservers, err = api.GetMailservers(context.Background())
5353
require.NoError(t, err)
54-
require.EqualValues(t, []Mailserver{testMailserverWithPassword}, mailservers)
54+
require.EqualValues(t, []wakutypes.Mailserver{testMailserverWithPassword}, mailservers)
5555
// Delete non-existing mailserver.
5656
err = api.DeleteMailserver(context.Background(), "other-id")
5757
require.NoError(t, err)

services/mailservers/database.go

+7-85
Original file line numberDiff line numberDiff line change
@@ -9,90 +9,12 @@ import (
99
"strings"
1010
"time"
1111

12-
"github.com/libp2p/go-libp2p/core/peer"
1312
"github.com/multiformats/go-multiaddr"
1413

15-
"github.com/waku-org/go-waku/waku/v2/protocol/enr"
16-
"github.com/waku-org/go-waku/waku/v2/utils"
17-
18-
"github.com/ethereum/go-ethereum/p2p/enode"
19-
2014
"github.com/status-im/status-go/protocol/transport"
15+
"github.com/status-im/status-go/waku/types"
2116
)
2217

23-
func MustDecodeENR(enrStr string) *enode.Node {
24-
node, err := enode.Parse(enode.ValidSchemes, enrStr)
25-
if err != nil || node == nil {
26-
panic("could not decode enr: " + enrStr)
27-
}
28-
return node
29-
}
30-
31-
func MustDecodeMultiaddress(multiaddrsStr string) *multiaddr.Multiaddr {
32-
maddr, err := multiaddr.NewMultiaddr(multiaddrsStr)
33-
if err != nil || maddr == nil {
34-
panic("could not decode multiaddr: " + multiaddrsStr)
35-
}
36-
return &maddr
37-
}
38-
39-
type Mailserver struct {
40-
ID string `json:"id"`
41-
Name string `json:"name"`
42-
Custom bool `json:"custom"`
43-
ENR *enode.Node `json:"enr"`
44-
Addr *multiaddr.Multiaddr `json:"addr"`
45-
46-
// Deprecated: only used with WakuV1
47-
Password string `json:"password,omitempty"`
48-
Fleet string `json:"fleet"`
49-
FailedRequests uint `json:"-"`
50-
}
51-
52-
func (m Mailserver) PeerInfo() (peer.AddrInfo, error) {
53-
var maddrs []multiaddr.Multiaddr
54-
55-
if m.ENR != nil {
56-
addrInfo, err := enr.EnodeToPeerInfo(m.ENR)
57-
if err != nil {
58-
return peer.AddrInfo{}, err
59-
}
60-
addrInfo.Addrs = utils.EncapsulatePeerID(addrInfo.ID, addrInfo.Addrs...)
61-
maddrs = append(maddrs, addrInfo.Addrs...)
62-
}
63-
64-
if m.Addr != nil {
65-
maddrs = append(maddrs, *m.Addr)
66-
}
67-
68-
p, err := peer.AddrInfosFromP2pAddrs(maddrs...)
69-
if err != nil {
70-
return peer.AddrInfo{}, err
71-
}
72-
73-
if len(p) != 1 {
74-
return peer.AddrInfo{}, errors.New("invalid mailserver setup")
75-
}
76-
77-
return p[0], nil
78-
}
79-
80-
func (m Mailserver) PeerID() (peer.ID, error) {
81-
p, err := m.PeerInfo()
82-
if err != nil {
83-
return "", err
84-
}
85-
return p.ID, nil
86-
}
87-
88-
func (m Mailserver) nullablePassword() (val sql.NullString) {
89-
if m.Password != "" {
90-
val.String = m.Password
91-
val.Valid = true
92-
}
93-
return
94-
}
95-
9618
type MailserverRequestGap struct {
9719
ID string `json:"id"`
9820
ChatID string `json:"chatId"`
@@ -145,7 +67,7 @@ func NewDB(db *sql.DB) *Database {
14567
return &Database{db: db}
14668
}
14769

148-
func (d *Database) Add(mailserver Mailserver) error {
70+
func (d *Database) Add(mailserver types.Mailserver) error {
14971
// TODO: we are only storing the multiaddress.
15072
// In a future PR we must allow storing multiple multiaddresses and ENR
15173
_, err := d.db.Exec(`INSERT OR REPLACE INTO mailservers(
@@ -158,13 +80,13 @@ func (d *Database) Add(mailserver Mailserver) error {
15880
mailserver.ID,
15981
mailserver.Name,
16082
(*mailserver.Addr).String(),
161-
mailserver.nullablePassword(),
83+
mailserver.NullablePassword(),
16284
mailserver.Fleet,
16385
)
16486
return err
16587
}
16688

167-
func (d *Database) Mailservers() ([]Mailserver, error) {
89+
func (d *Database) Mailservers() ([]types.Mailserver, error) {
16890
rows, err := d.db.Query(`SELECT id, name, address, password, fleet FROM mailservers`)
16991
if err != nil {
17092
return nil, err
@@ -173,12 +95,12 @@ func (d *Database) Mailservers() ([]Mailserver, error) {
17395
return toMailservers(rows)
17496
}
17597

176-
func toMailservers(rows *sql.Rows) ([]Mailserver, error) {
177-
var result []Mailserver
98+
func toMailservers(rows *sql.Rows) ([]types.Mailserver, error) {
99+
var result []types.Mailserver
178100

179101
for rows.Next() {
180102
var (
181-
m Mailserver
103+
m types.Mailserver
182104
addrStr string
183105
password sql.NullString
184106
)

0 commit comments

Comments
 (0)