Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion appdatabase/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func (a DbInitializer) Initialize(path, password string, kdfIterationsNumber int
}

func doMigration(db *sql.DB) error {
lastMigration, migrationTableExists, err := sqlite.GetLastMigrationVersion(db)
lastMigration, migrationTableExists, err := sqlite.GetLastMigrationVersion(db, sqlite.MigrationTableName)
if err != nil {
return err
}
Expand Down
26 changes: 20 additions & 6 deletions messaging/adapters/persistence.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package adapters
import (
"crypto/ecdsa"

"github.com/status-im/status-go/messaging/internal"
"github.com/status-im/status-go/messaging/layers/encryption"
"github.com/status-im/status-go/messaging/layers/transport"
"github.com/status-im/status-go/messaging/types"
wakupersistence "github.com/status-im/status-go/messaging/waku/persistence"
Expand All @@ -15,11 +17,11 @@ type KeysPersistence struct {
var _ transport.KeysPersistence = (*KeysPersistence)(nil)

func (kp *KeysPersistence) All() (map[string][]byte, error) {
return kp.P.WakuKeys()
return kp.P.WakuStorage().Keys()
}

func (kp *KeysPersistence) Add(chatID string, key []byte) error {
return kp.P.AddWakuKey(chatID, key)
return kp.P.WakuStorage().AddKey(chatID, key)
}

type ProcessedMessageIDsCache struct {
Expand Down Expand Up @@ -48,19 +50,19 @@ type WakuProtectedTopics struct {
var _ wakupersistence.ProtectedTopics = (*WakuProtectedTopics)(nil)

func (wpt *WakuProtectedTopics) Insert(pubsubTopic string, privKey *ecdsa.PrivateKey, publicKey *ecdsa.PublicKey) error {
return wpt.P.WakuInsertProtectedTopic(pubsubTopic, privKey, publicKey)
return wpt.P.WakuStorage().InsertProtectedTopic(pubsubTopic, privKey, publicKey)
}

func (wpt *WakuProtectedTopics) Delete(pubsubTopic string) error {
return wpt.P.WakuDeleteProtectedTopic(pubsubTopic)
return wpt.P.WakuStorage().DeleteProtectedTopic(pubsubTopic)
}

func (wpt *WakuProtectedTopics) FetchPrivateKey(topic string) (*ecdsa.PrivateKey, error) {
return wpt.P.WakuFetchPrivateKeyForProtectedTopic(topic)
return wpt.P.WakuStorage().FetchPrivateKeyForProtectedTopic(topic)
}

func (wpt *WakuProtectedTopics) ProtectedTopics() ([]wakupersistence.ProtectedTopic, error) {
pt, err := wpt.P.WakuProtectedTopics()
pt, err := wpt.P.WakuStorage().ProtectedTopics()
if err != nil {
return nil, err
}
Expand All @@ -73,3 +75,15 @@ func (wpt *WakuProtectedTopics) ProtectedTopics() ([]wakupersistence.ProtectedTo
}
return result, nil
}

func EncryptionPersistence(p types.Persistence) encryption.Persistence {
encryptionStorage := p.EncryptionStorage()
if encryptionStorage == nil {
return nil
}
internal, ok := encryptionStorage.(*internal.SQLiteEncryptionPersistence)
if !ok {
panic("custom EncryptionPersistence implementations are not supported yet")
}
return internal.SQLitePersistence
}
Comment on lines +79 to +89
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This part confuses me.

Sorry I had to write it down to consume, you probably know all of this 😄

Part 1. Interfaces

  1. In newCore we get a persistence pointer from the user as part of params:

    func newCore(waku wakutypes.Waku, params CoreParams, config *config) (*Core, error) {
    Persistence types.Persistence

  2. types.Persistence is expected to provide EncryptionPersistence:

    EncryptionStorage() EncryptionPersistence

  3. Yet the encryption package expects it's own internal Persistence interface:

    type Persistence interface {
    KeysStorage() dr.KeysStorage
    SessionStorage() dr.SessionStorage
    SharedSecretStorage() sharedsecret.Persistence
    MultideviceStorage() multidevice.Persistence

  4. That's why we use this EncryptionPersistence adapter here

    encryptor := encryption.New(
    adapters.EncryptionPersistence(params.Persistence),

Part 2. Adapter.

  1. The adapter works, because status-go uses the default sqlite encryption persistence:

    func (m *messagingPersistence) EncryptionStorage() messagingtypes.EncryptionPersistence {
    return messaging.NewEncryptionSQLitePersistence(m.db)
    }

    ... which, by the way is marked as "internal implementation" 🤔

    // Returns internal implementation of EncryptionPersistence.
    // Clients should not use this interface directly.
    func NewEncryptionSQLitePersistence(db *sql.DB) types.EncryptionPersistence {
    return &internal.SQLiteEncryptionPersistence{
    SQLitePersistence: encryption.NewSQLitePersistence(db),
    }
    }

  2. The implementation of SQLiteEncryptionPersistence is simply empty:

    // Implements the EncryptionPersistence interface using SQLite.
    type SQLiteEncryptionPersistence struct {
    *encryption.SQLitePersistence
    }
    var _ types.EncryptionPersistence = (*SQLiteEncryptionPersistence)(nil)
    func (e *SQLiteEncryptionPersistence) X3DHBundlesStorage() types.X3DHBundlesPersistence {
    return nil
    }
    func (e *SQLiteEncryptionPersistence) DRKeysStorage() types.DRKeysPersistence {
    return nil
    }
    func (e *SQLiteEncryptionPersistence) DRSessionStorage() types.DRSessionPersistence {
    return nil
    }
    func (e *SQLiteEncryptionPersistence) SharedSecretStorage() types.SharedSecretPersistence {
    return nil
    }
    func (e *SQLiteEncryptionPersistence) MultideviceStorage() types.MultidevicePersistence {
    return nil
    }
    func (e *SQLiteEncryptionPersistence) HashRatchetStorage() types.HashRatchetPersistence {
    return nil
    }

    ... yet it composes encryption.SQLitePersistence.
    And this is what the adapter relies on.

Part 3. Question

messagingtypes.EncryptionPersistence seem to be completely unused 🤔

Do we really need it? And the other persistence adapters?

Maybe we could just re-export the internal interfaces from messaging module?

12 changes: 6 additions & 6 deletions messaging/common/message_segmentation.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,20 +165,20 @@ func (s *MessageSender) handleSegmentationLayer(message *types.Message) error {
zap.Uint32("ParitySegmentIndex", segmentMessage.ParitySegmentIndex),
zap.Uint32("ParitySegmentsCount", segmentMessage.ParitySegmentsCount))

alreadyCompleted, err := s.persistence.IsMessageAlreadyCompleted(segmentMessage.EntireMessageHash)
alreadyCompleted, err := s.persistence.SegmentationStorage().IsMessageAlreadyCompleted(segmentMessage.EntireMessageHash)
if err != nil {
return err
}
if alreadyCompleted {
return ErrMessageSegmentsAlreadyCompleted
}

err = s.persistence.SaveMessageSegment(segmentMessage, message.TransportLayer.SigPubKey, time.Now().Unix())
err = s.persistence.SegmentationStorage().SaveMessageSegment(segmentMessage, message.TransportLayer.SigPubKey, time.Now().Unix())
if err != nil {
return err
}

segments, err := s.persistence.GetMessageSegments(segmentMessage.EntireMessageHash, message.TransportLayer.SigPubKey)
segments, err := s.persistence.SegmentationStorage().GetMessageSegments(segmentMessage.EntireMessageHash, message.TransportLayer.SigPubKey)
if err != nil {
return err
}
Expand Down Expand Up @@ -258,7 +258,7 @@ func (s *MessageSender) handleSegmentationLayer(message *types.Message) error {
return ErrMessageSegmentsHashMismatch
}

err = s.persistence.CompleteMessageSegments(segmentMessage.EntireMessageHash, message.TransportLayer.SigPubKey, time.Now().Unix())
err = s.persistence.SegmentationStorage().CompleteMessageSegments(segmentMessage.EntireMessageHash, message.TransportLayer.SigPubKey, time.Now().Unix())
if err != nil {
return err
}
Expand All @@ -271,12 +271,12 @@ func (s *MessageSender) handleSegmentationLayer(message *types.Message) error {
func (s *MessageSender) CleanupSegments() error {
monthAgo := time.Now().AddDate(0, -1, 0).Unix()

err := s.persistence.RemoveMessageSegmentsOlderThan(monthAgo)
err := s.persistence.SegmentationStorage().RemoveMessageSegmentsOlderThan(monthAgo)
if err != nil {
return err
}

err = s.persistence.RemoveMessageSegmentsCompletedOlderThan(monthAgo)
err = s.persistence.SegmentationStorage().RemoveMessageSegmentsCompletedOlderThan(monthAgo)
if err != nil {
return err
}
Expand Down
12 changes: 2 additions & 10 deletions messaging/common/message_segmentation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,10 @@ import (
"github.com/stretchr/testify/suite"
"go.uber.org/zap"

"github.com/status-im/status-go/appdatabase"
"github.com/status-im/status-go/crypto"
"github.com/status-im/status-go/messaging/types"
wakutypes "github.com/status-im/status-go/messaging/waku/types"
"github.com/status-im/status-go/protocol/protobuf"
"github.com/status-im/status-go/protocol/sqlite"
"github.com/status-im/status-go/t/helpers"
)

func TestMessageSegmentationSuite(t *testing.T) {
Expand All @@ -41,18 +38,13 @@ func (s *MessageSegmentationSuite) SetupTest() {
identity, err := crypto.GenerateKey()
s.Require().NoError(err)

database, err := helpers.SetupTestMemorySQLDB(appdatabase.DbInitializer{})
s.Require().NoError(err)
err = sqlite.Migrate(database)
s.Require().NoError(err)

s.logger, err = zap.NewDevelopment()
s.Require().NoError(err)

s.sender, err = NewMessageSender(
identity,
database,
NewStubPersistence(),
nil,
NewPersistenceInMemory(),
nil,
nil,
s.logger,
Expand Down
44 changes: 26 additions & 18 deletions messaging/common/message_sender_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,21 @@ import (

"github.com/golang/protobuf/proto"
"github.com/libp2p/go-libp2p/core/peer"
bindata "github.com/status-im/migrate/v4/source/go_bindata"
datasyncproto "github.com/status-im/mvds/protobuf"
"github.com/stretchr/testify/suite"
"go.uber.org/zap"

datasyncproto "github.com/status-im/mvds/protobuf"

"github.com/status-im/status-go/appdatabase"
"github.com/status-im/status-go/crypto"
"github.com/status-im/status-go/messaging/adapters"
"github.com/status-im/status-go/messaging/datasync"
"github.com/status-im/status-go/messaging/layers/encryption"
"github.com/status-im/status-go/messaging/layers/encryption/migrations"
"github.com/status-im/status-go/messaging/layers/transport"
messagingtypes "github.com/status-im/status-go/messaging/types"
wakuv2 "github.com/status-im/status-go/messaging/waku"
wakutypes "github.com/status-im/status-go/messaging/waku/types"
"github.com/status-im/status-go/protocol/protobuf"
"github.com/status-im/status-go/protocol/sqlite"
v1protocol "github.com/status-im/status-go/protocol/v1"
"github.com/status-im/status-go/t/helpers"
)
Expand Down Expand Up @@ -56,13 +55,16 @@ func (s *MessageSenderSuite) SetupTest() {
identity, err := crypto.GenerateKey()
s.Require().NoError(err)

database, err := helpers.SetupTestMemorySQLDB(appdatabase.DbInitializer{})
s.Require().NoError(err)
err = sqlite.Migrate(database)
db, err := helpers.SetupTestMemorySQLDB(helpers.NewTestDBInitializer(bindata.Resource(
migrations.AssetNames(),
func(name string) ([]byte, error) {
return migrations.Asset(name)
},
Comment on lines +59 to +62
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
migrations.AssetNames(),
func(name string) ([]byte, error) {
return migrations.Asset(name)
},
migrations.AssetNames(),
migrations.Asset,

)))
s.Require().NoError(err)

encryptionProtocol := encryption.New(
database,
encryption.NewSQLitePersistence(db),
"installation-1",
s.logger,
)
Expand All @@ -80,7 +82,7 @@ func (s *MessageSenderSuite) SetupTest() {
s.Require().NoError(err)
s.Require().NoError(shh.Start())

stubPersistence := NewStubPersistence()
stubPersistence := NewPersistenceInMemory()

transport, err := transport.NewTransport(
shh,
Expand All @@ -94,7 +96,7 @@ func (s *MessageSenderSuite) SetupTest() {

s.sender, err = NewMessageSender(
identity,
database,
db,
stubPersistence,
transport,
encryptionProtocol,
Expand Down Expand Up @@ -196,13 +198,16 @@ func (s *MessageSenderSuite) TestHandleDecodedMessagesDatasyncEncrypted() {
s.Require().NoError(err)

// Create sender encryption protocol.
senderDatabase, err := helpers.SetupTestMemorySQLDB(appdatabase.DbInitializer{})
s.Require().NoError(err)
err = sqlite.Migrate(senderDatabase)
senderDatabase, err := helpers.SetupTestMemorySQLDB(helpers.NewTestDBInitializer(bindata.Resource(
migrations.AssetNames(),
func(name string) ([]byte, error) {
return migrations.Asset(name)
},
)))
s.Require().NoError(err)

senderEncryptionProtocol := encryption.New(
senderDatabase,
encryption.NewSQLitePersistence(senderDatabase),
"installation-2",
s.logger,
)
Expand Down Expand Up @@ -246,13 +251,16 @@ func (s *MessageSenderSuite) TestHandleOutOfOrderHashRatchet() {
s.Require().NoError(err)

// Create sender encryption protocol.
senderDatabase, err := helpers.SetupTestMemorySQLDB(appdatabase.DbInitializer{})
s.Require().NoError(err)
err = sqlite.Migrate(senderDatabase)
senderDatabase, err := helpers.SetupTestMemorySQLDB(helpers.NewTestDBInitializer(bindata.Resource(
migrations.AssetNames(),
func(name string) ([]byte, error) {
return migrations.Asset(name)
},
)))
s.Require().NoError(err)

senderEncryptionProtocol := encryption.New(
senderDatabase,
encryption.NewSQLitePersistence(senderDatabase),
"installation-2",
s.logger,
)
Expand Down
Loading
Loading