Skip to content

Commit

Permalink
chore_: move waku types out of eth-node
Browse files Browse the repository at this point in the history
  • Loading branch information
osmaczko committed Jan 17, 2025
1 parent 4a5a338 commit 67134d9
Show file tree
Hide file tree
Showing 126 changed files with 866 additions and 711 deletions.
3 changes: 2 additions & 1 deletion api/geth_backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ import (
"github.com/status-im/status-go/signal"
"github.com/status-im/status-go/sqlite"
"github.com/status-im/status-go/transactions"
wakutypes "github.com/status-im/status-go/waku/types"
"github.com/status-im/status-go/walletdatabase"
)

Expand Down Expand Up @@ -2669,7 +2670,7 @@ func (b *GethStatusBackend) LocalPairingStarted() error {
return accountDB.MnemonicWasShown()
}

func (b *GethStatusBackend) injectAccountsIntoWakuService(w types.WakuKeyManager, st *ext.Service) error {
func (b *GethStatusBackend) injectAccountsIntoWakuService(w wakutypes.WakuKeyManager, st *ext.Service) error {
chatAccount, err := b.accountManager.SelectedChatAccount()
if err != nil {
return err
Expand Down
7 changes: 4 additions & 3 deletions db/history.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/syndtr/goleveldb/leveldb/util"

"github.com/status-im/status-go/eth-node/types"
wakutypes "github.com/status-im/status-go/waku/types"
)

var (
Expand All @@ -26,7 +27,7 @@ type DB interface {
}

// TopicHistoryKey defines bytes that are used as unique key for TopicHistory.
// first 4 bytes are types.TopicType bytes
// first 4 bytes are wakutypes.TopicType bytes
// next 8 bytes are time.Duration encoded in big endian notation.
type TopicHistoryKey [12]byte

Expand All @@ -36,7 +37,7 @@ func LoadTopicHistoryFromKey(db DB, key TopicHistoryKey) (th TopicHistory, err e
if (key == TopicHistoryKey{}) {
return th, ErrEmptyKey
}
topic := types.TopicType{}
topic := wakutypes.TopicType{}
copy(topic[:], key[:4])
duration := binary.BigEndian.Uint64(key[4:])
th = TopicHistory{db: db, Topic: topic, Duration: time.Duration(duration)}
Expand All @@ -47,7 +48,7 @@ func LoadTopicHistoryFromKey(db DB, key TopicHistoryKey) (th TopicHistory, err e
type TopicHistory struct {
db DB
// whisper topic
Topic types.TopicType
Topic wakutypes.TopicType

Duration time.Duration
// Timestamp that was used for the first request with this topic.
Expand Down
7 changes: 4 additions & 3 deletions db/history_store.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"github.com/syndtr/goleveldb/leveldb/errors"

"github.com/status-im/status-go/eth-node/types"
wakutypes "github.com/status-im/status-go/waku/types"
)

// NewHistoryStore returns HistoryStore instance.
Expand All @@ -24,7 +25,7 @@ type HistoryStore struct {

// GetHistory creates history instance and loads history from database.
// Returns instance populated with topic and duration if history is not found in database.
func (h HistoryStore) GetHistory(topic types.TopicType, duration time.Duration) (TopicHistory, error) {
func (h HistoryStore) GetHistory(topic wakutypes.TopicType, duration time.Duration) (TopicHistory, error) {
thist := h.NewHistory(topic, duration)
err := thist.Load()
if err != nil && err != errors.ErrNotFound {
Expand All @@ -39,7 +40,7 @@ func (h HistoryStore) NewRequest() HistoryRequest {
}

// NewHistory creates TopicHistory object with required values.
func (h HistoryStore) NewHistory(topic types.TopicType, duration time.Duration) TopicHistory {
func (h HistoryStore) NewHistory(topic wakutypes.TopicType, duration time.Duration) TopicHistory {
return TopicHistory{db: h.topicDB, Duration: duration, Topic: topic}
}

Expand Down Expand Up @@ -74,7 +75,7 @@ func (h HistoryStore) GetAllRequests() ([]HistoryRequest, error) {
// GetHistoriesByTopic returns all histories with a given topic.
// This is needed when we will have multiple range per single topic.
// TODO explain
func (h HistoryStore) GetHistoriesByTopic(topic types.TopicType) ([]TopicHistory, error) {
func (h HistoryStore) GetHistoriesByTopic(topic wakutypes.TopicType) ([]TopicHistory, error) {
rst := []TopicHistory{}
iter := h.topicDB.NewIterator(h.topicDB.Range(topic[:], nil))
for iter.Next() {
Expand Down
7 changes: 4 additions & 3 deletions db/history_store_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"github.com/stretchr/testify/require"

"github.com/status-im/status-go/eth-node/types"
wakutypes "github.com/status-im/status-go/waku/types"
)

func createInMemStore(t *testing.T) HistoryStore {
Expand All @@ -16,7 +17,7 @@ func createInMemStore(t *testing.T) HistoryStore {
}

func TestGetNewHistory(t *testing.T) {
topic := types.TopicType{1}
topic := wakutypes.TopicType{1}
duration := time.Hour
store := createInMemStore(t)
th, err := store.GetHistory(topic, duration)
Expand All @@ -26,7 +27,7 @@ func TestGetNewHistory(t *testing.T) {
}

func TestGetExistingHistory(t *testing.T) {
topic := types.TopicType{1}
topic := wakutypes.TopicType{1}
duration := time.Hour
store := createInMemStore(t)
th, err := store.GetHistory(topic, duration)
Expand All @@ -49,7 +50,7 @@ func TestNewHistoryRequest(t *testing.T) {
req := store.NewRequest()
req.ID = id

th, err := store.GetHistory(types.TopicType{1}, time.Hour)
th, err := store.GetHistory(wakutypes.TopicType{1}, time.Hour)
require.NoError(t, err)
req.AddHistory(th)
require.NoError(t, req.Save())
Expand Down
9 changes: 5 additions & 4 deletions db/history_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,15 @@ import (
"github.com/stretchr/testify/require"

"github.com/status-im/status-go/eth-node/types"
wakutypes "github.com/status-im/status-go/waku/types"
)

func TestTopicHistoryStoreLoadFromKey(t *testing.T) {
db, err := NewMemoryDBNamespace(TopicHistoryBucket)
require.NoError(t, err)
th := TopicHistory{
db: db,
Topic: types.TopicType{1, 1, 1},
Topic: wakutypes.TopicType{1, 1, 1},
Duration: 10 * time.Hour,
}
require.NoError(t, th.Save())
Expand Down Expand Up @@ -71,7 +72,7 @@ func TestTopicHistorySameRange(t *testing.T) {
}

func TestAddHistory(t *testing.T) {
topic := types.TopicType{1, 1, 1}
topic := wakutypes.TopicType{1, 1, 1}
now := time.Now()

topicdb, err := NewMemoryDBNamespace(TopicHistoryBucket)
Expand All @@ -94,8 +95,8 @@ func TestAddHistory(t *testing.T) {
}

func TestRequestIncludesMethod(t *testing.T) {
topicOne := types.TopicType{1}
topicTwo := types.TopicType{2}
topicOne := wakutypes.TopicType{1}
topicTwo := wakutypes.TopicType{2}
testCases := []struct {
description string
result bool
Expand Down
15 changes: 9 additions & 6 deletions eth-node/bridge/geth/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,11 @@ import (
"github.com/ethereum/go-ethereum/p2p/enode"

gethens "github.com/status-im/status-go/eth-node/bridge/geth/ens"
"github.com/status-im/status-go/eth-node/types"
gethnode "github.com/status-im/status-go/eth-node/node"
enstypes "github.com/status-im/status-go/eth-node/types/ens"

wakubridge "github.com/status-im/status-go/waku/bridge"
wakutypes "github.com/status-im/status-go/waku/types"
)

type gethNodeWrapper struct {
Expand All @@ -22,7 +25,7 @@ type gethNodeWrapper struct {
waku2 *wakuv2.Waku
}

func NewNodeBridge(stack *node.Node, waku1 *wakuv1.Waku, waku2 *wakuv2.Waku) types.Node {
func NewNodeBridge(stack *node.Node, waku1 *wakuv1.Waku, waku2 *wakuv2.Waku) gethnode.Node {
return &gethNodeWrapper{stack: stack, waku1: waku1, waku2: waku2}
}

Expand All @@ -42,20 +45,20 @@ func (w *gethNodeWrapper) SetWaku2(waku *wakuv2.Waku) {
w.waku2 = waku
}

func (w *gethNodeWrapper) GetWaku(ctx interface{}) (types.Waku, error) {
func (w *gethNodeWrapper) GetWaku(ctx interface{}) (wakutypes.Waku, error) {
if w.waku1 == nil {
return nil, errors.New("waku service is not available")
}

return NewGethWakuWrapper(w.waku1), nil
return wakubridge.NewGethWakuWrapper(w.waku1), nil
}

func (w *gethNodeWrapper) GetWakuV2(ctx interface{}) (types.Waku, error) {
func (w *gethNodeWrapper) GetWakuV2(ctx interface{}) (wakutypes.Waku, error) {
if w.waku2 == nil {
return nil, errors.New("waku service is not available")
}

return NewGethWakuV2Wrapper(w.waku2), nil
return wakubridge.NewGethWakuV2Wrapper(w.waku2), nil
}

func (w *gethNodeWrapper) AddPeer(url string) error {
Expand Down
17 changes: 17 additions & 0 deletions eth-node/node/node.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package gethnode

import (
"go.uber.org/zap"

enstypes "github.com/status-im/status-go/eth-node/types/ens"
wakutypes "github.com/status-im/status-go/waku/types"
)

type Node interface {
NewENSVerifier(logger *zap.Logger) enstypes.ENSVerifier
GetWaku(ctx interface{}) (wakutypes.Waku, error)
GetWakuV2(ctx interface{}) (wakutypes.Waku, error)
AddPeer(url string) error
RemovePeer(url string) error
PeersCount() int
}
13 changes: 0 additions & 13 deletions eth-node/types/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,6 @@ package types

import (
"fmt"

"go.uber.org/zap"

enstypes "github.com/status-im/status-go/eth-node/types/ens"
)

// EnodeID is a unique identifier for each node.
Expand All @@ -15,12 +11,3 @@ type EnodeID [32]byte
func (n EnodeID) String() string {
return fmt.Sprintf("%x", n[:])
}

type Node interface {
NewENSVerifier(logger *zap.Logger) enstypes.ENSVerifier
GetWaku(ctx interface{}) (Waku, error)
GetWakuV2(ctx interface{}) (Waku, error)
AddPeer(url string) error
RemovePeer(url string) error
PeersCount() int
}
3 changes: 2 additions & 1 deletion mailserver/cleaner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/ethereum/go-ethereum/rlp"

"github.com/status-im/status-go/eth-node/types"
wakutypes "github.com/status-im/status-go/waku/types"
wakuv1common "github.com/status-im/status-go/wakuv1/common"
)

Expand Down Expand Up @@ -126,7 +127,7 @@ func countMessages(t *testing.T, db DB) int {
var (
count int
zero types.Hash
emptyTopic types.TopicType
emptyTopic wakutypes.TopicType
)

now := time.Now()
Expand Down
9 changes: 5 additions & 4 deletions mailserver/db_key.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@ import (
"errors"

"github.com/status-im/status-go/eth-node/types"
wakutypes "github.com/status-im/status-go/waku/types"
)

const (
// DBKeyLength is a size of the envelope key.
DBKeyLength = types.HashLength + timestampLength + types.TopicLength
DBKeyLength = types.HashLength + timestampLength + wakutypes.TopicLength
CursorLength = types.HashLength + timestampLength
)

Expand All @@ -29,8 +30,8 @@ func (k *DBKey) Bytes() []byte {
return k.raw
}

func (k *DBKey) Topic() types.TopicType {
return types.BytesToTopic(k.raw[timestampLength+types.HashLength:])
func (k *DBKey) Topic() wakutypes.TopicType {
return wakutypes.BytesToTopic(k.raw[timestampLength+types.HashLength:])
}

func (k *DBKey) EnvelopeHash() types.Hash {
Expand All @@ -43,7 +44,7 @@ func (k *DBKey) Cursor() []byte {
}

// NewDBKey creates a new DBKey with the given values.
func NewDBKey(timestamp uint32, topic types.TopicType, h types.Hash) *DBKey {
func NewDBKey(timestamp uint32, topic wakutypes.TopicType, h types.Hash) *DBKey {
var k DBKey
k.raw = make([]byte, DBKeyLength)
binary.BigEndian.PutUint32(k.raw, timestamp)
Expand Down
3 changes: 2 additions & 1 deletion mailserver/db_key_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@ import (
"github.com/stretchr/testify/require"

"github.com/status-im/status-go/eth-node/types"
wakutypes "github.com/status-im/status-go/waku/types"
)

func TestNewDBKey(t *testing.T) {
topic := types.BytesToTopic([]byte{0x01, 0x02, 0x03, 0x04})
topic := wakutypes.BytesToTopic([]byte{0x01, 0x02, 0x03, 0x04})

hash := types.BytesToHash([]byte{0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x30, 0x31, 0x32})
dbKey := NewDBKey(0xabcdef12, topic, hash)
Expand Down
19 changes: 10 additions & 9 deletions mailserver/mailserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,12 @@ import (
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/rlp"
gocommon "github.com/status-im/status-go/common"
gethbridge "github.com/status-im/status-go/eth-node/bridge/geth"
"github.com/status-im/status-go/eth-node/crypto"
"github.com/status-im/status-go/eth-node/types"
"github.com/status-im/status-go/logutils"
"github.com/status-im/status-go/params"
"github.com/status-im/status-go/waku/bridge"
wakutypes "github.com/status-im/status-go/waku/types"
"github.com/status-im/status-go/wakuv1"
wakuv1common "github.com/status-im/status-go/wakuv1/common"
)
Expand Down Expand Up @@ -125,7 +126,7 @@ func (s *WakuMailServer) Close() {
}

func (s *WakuMailServer) Archive(env *wakuv1common.Envelope) {
s.ms.Archive(gethbridge.NewWakuEnvelope(env))
s.ms.Archive(bridge.NewWakuEnvelope(env))
}

func (s *WakuMailServer) Deliver(peerID []byte, req wakuv1common.MessagesRequest) {
Expand Down Expand Up @@ -310,7 +311,7 @@ func (s *WakuMailServer) decodeRequest(peerID []byte, request *wakuv1common.Enve
type adapter interface {
CreateRequestFailedPayload(reqID types.Hash, err error) []byte
CreateRequestCompletedPayload(reqID, lastEnvelopeHash types.Hash, cursor []byte) []byte
CreateSyncResponse(envelopes []types.Envelope, cursor []byte, final bool, err string) interface{}
CreateSyncResponse(envelopes []wakutypes.Envelope, cursor []byte, final bool, err string) interface{}
CreateRawSyncResponse(envelopes []rlp.RawValue, cursor []byte, final bool, err string) interface{}
}

Expand All @@ -330,7 +331,7 @@ func (wakuAdapter) CreateRequestCompletedPayload(reqID, lastEnvelopeHash types.H
return wakuv1.CreateMailServerRequestCompletedPayload(common.Hash(reqID), common.Hash(lastEnvelopeHash), cursor)
}

func (wakuAdapter) CreateSyncResponse(_ []types.Envelope, _ []byte, _ bool, _ string) interface{} {
func (wakuAdapter) CreateSyncResponse(_ []wakutypes.Envelope, _ []byte, _ bool, _ string) interface{} {
return nil
}

Expand Down Expand Up @@ -437,7 +438,7 @@ func (s *mailServer) setupCleaner(retention time.Duration) {
s.cleaner.Start()
}

func (s *mailServer) Archive(env types.Envelope) {
func (s *mailServer) Archive(env wakutypes.Envelope) {
err := s.db.SaveEnvelope(env)
if err != nil {
logutils.ZapLogger().Error("Could not save envelope", zap.Stringer("hash", env.Hash()))
Expand Down Expand Up @@ -706,7 +707,7 @@ func (s *mailServer) exceedsPeerRequests(peerID types.Hash) bool {
func (s *mailServer) createIterator(req MessagesRequestPayload) (Iterator, error) {
var (
emptyHash types.Hash
emptyTopic types.TopicType
emptyTopic wakutypes.TopicType
ku, kl *DBKey
)

Expand Down Expand Up @@ -753,12 +754,12 @@ func (s *mailServer) processRequestInBundles(
zap.Int("limit", limit),
)

var topicsMap map[types.TopicType]bool
var topicsMap map[wakutypes.TopicType]bool

if len(topics) != 0 {
topicsMap = make(map[types.TopicType]bool)
topicsMap = make(map[wakutypes.TopicType]bool)
for _, t := range topics {
topicsMap[types.BytesToTopic(t)] = true
topicsMap[wakutypes.BytesToTopic(t)] = true
}
}

Expand Down
Loading

0 comments on commit 67134d9

Please sign in to comment.