Skip to content

Commit

Permalink
Add GetAddressLog SQL
Browse files Browse the repository at this point in the history
  • Loading branch information
neekolas committed Apr 25, 2024
1 parent 3f72749 commit 3e8f57a
Show file tree
Hide file tree
Showing 5 changed files with 177 additions and 0 deletions.
9 changes: 9 additions & 0 deletions pkg/mls/store/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,15 @@ import (
"github.com/uptrace/bun"
)

type AddressLogEntry struct {
bun.BaseModel `bun:"table:address_log"`

Address string `bun:",notnull"`
InboxId string `bun:",notnull"`
AssociationSequenceId *uint64 `bun:","`
RevocationSequenceId *uint64 `bun:","`
}

type InboxLogEntry struct {
bun.BaseModel `bun:"table:inbox_log"`

Expand Down
21 changes: 21 additions & 0 deletions pkg/mls/store/queries.sql
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,27 @@ JOIN (
) as b on b.inbox_id = a.inbox_id AND a.sequence_id > b.sequence_id
ORDER BY a.sequence_id ASC;

-- name: GetAddressLogs :many
SELECT
a.address,
a.inbox_id,
a.association_sequence_id
FROM
address_log a
INNER JOIN (
SELECT
address,
MAX(association_sequence_id) AS max_association_sequence_id
FROM
address_log
WHERE
address = ANY (@addresses::text[])
AND
revocation_sequence_id IS NULL
GROUP BY
address
) b ON a.address = b.address AND a.association_sequence_id = b.max_association_sequence_id;

-- name: InsertInboxLog :one
INSERT INTO inbox_log (inbox_id, server_timestamp_ns, identity_update_proto)
VALUES ($1, $2, $3)
Expand Down
51 changes: 51 additions & 0 deletions pkg/mls/store/queries/queries.sql.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

32 changes: 32 additions & 0 deletions pkg/mls/store/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ type Store struct {
type IdentityStore interface {
PublishIdentityUpdate(ctx context.Context, req *identity.PublishIdentityUpdateRequest) (*identity.PublishIdentityUpdateResponse, error)
GetInboxLogs(ctx context.Context, req *identity.GetIdentityUpdatesRequest) (*identity.GetIdentityUpdatesResponse, error)
GetInboxIds(ctx context.Context, req *identity.GetInboxIdsRequest) (*identity.GetInboxIdsResponse, error)
}

type MlsStore interface {
Expand Down Expand Up @@ -67,6 +68,37 @@ func New(ctx context.Context, config Config) (*Store, error) {
return s, nil
}

func (s *Store) GetInboxIds(ctx context.Context, req *identity.GetInboxIdsRequest) (*identity.GetInboxIdsResponse, error) {

addresses := []string{}
for _, request := range req.Requests {
addresses = append(addresses, request.GetAddress())
}

addressLogEntries, err := s.queries.GetAddressLogs(ctx, addresses)
if err != nil {
return nil, err
}

out := make([]*identity.GetInboxIdsResponse_Response, len(addresses))

for index, address := range addresses {
resp := identity.GetInboxIdsResponse_Response{}
resp.Address = address

for _, log_entry := range addressLogEntries {
if log_entry.Address == address {
resp.InboxId = &log_entry.InboxID
}
}
out[index] = &resp
}

return &identity.GetInboxIdsResponse{
Responses: out,
}, nil
}

func (s *Store) PublishIdentityUpdate(ctx context.Context, req *identity.PublishIdentityUpdateRequest) (*identity.PublishIdentityUpdateResponse, error) {
new_update := req.GetIdentityUpdate()
if new_update == nil {
Expand Down
64 changes: 64 additions & 0 deletions pkg/mls/store/store_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"time"

"github.com/stretchr/testify/require"
identity "github.com/xmtp/xmtp-node-go/pkg/proto/identity/api/v1"
mlsv1 "github.com/xmtp/xmtp-node-go/pkg/proto/mls/api/v1"
test "github.com/xmtp/xmtp-node-go/pkg/testing"
)
Expand All @@ -26,6 +27,69 @@ func NewTestStore(t *testing.T) (*Store, func()) {
return store, dbCleanup
}

func InsertAddressLog(store *Store, address string, inboxId string, associationSequenceId *uint64, revocationSequenceId *uint64) error {

entry := AddressLogEntry{
Address: address,
InboxId: inboxId,
AssociationSequenceId: associationSequenceId,
RevocationSequenceId: nil,
}
ctx := context.Background()

_, err := store.db.NewInsert().
Model(&entry).
Exec(ctx)

return err
}

func TestInboxIds(t *testing.T) {
store, cleanup := NewTestStore(t)
defer cleanup()

seq, rev := uint64(1), uint64(5)
err := InsertAddressLog(store, "address", "inbox1", &seq, &rev)
require.NoError(t, err)
seq, rev = uint64(2), uint64(8)
err = InsertAddressLog(store, "address", "inbox1", &seq, &rev)
require.NoError(t, err)
seq, rev = uint64(3), uint64(9)
err = InsertAddressLog(store, "address", "inbox1", &seq, &rev)
require.NoError(t, err)
seq, rev = uint64(4), uint64(1)
err = InsertAddressLog(store, "address", "correct", &seq, &rev)
require.NoError(t, err)

reqs := make([]*identity.GetInboxIdsRequest_Request, 0)
reqs = append(reqs, &identity.GetInboxIdsRequest_Request{
Address: "address",
})
req := &identity.GetInboxIdsRequest{
Requests: reqs,
}
resp, _ := store.GetInboxIds(context.Background(), req)
t.Log(resp)

require.Equal(t, "correct", *resp.Responses[0].InboxId)

seq = uint64(5)
err = InsertAddressLog(store, "address", "correct_inbox2", &seq, nil)
require.NoError(t, err)
resp, _ = store.GetInboxIds(context.Background(), req)
require.Equal(t, "correct_inbox2", *resp.Responses[0].InboxId)

reqs = append(reqs, &identity.GetInboxIdsRequest_Request{Address: "address2"})
req = &identity.GetInboxIdsRequest{
Requests: reqs,
}
seq, rev = uint64(8), uint64(2)
err = InsertAddressLog(store, "address2", "inbox2", &seq, &rev)
require.NoError(t, err)
resp, _ = store.GetInboxIds(context.Background(), req)
require.Equal(t, "inbox2", *resp.Responses[1].InboxId)
}

func TestCreateInstallation(t *testing.T) {
store, cleanup := NewTestStore(t)
defer cleanup()
Expand Down

0 comments on commit 3e8f57a

Please sign in to comment.