-
Notifications
You must be signed in to change notification settings - Fork 3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Implement PublishIdentityUpdate and GetInboxLogs endpoints #377
Merged
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
18bcab7
GetInboxLogs
richardhuaaa dfba4b3
Merge remote-tracking branch 'origin/main' into rich/implement-reads
richardhuaaa 0d4c8ab
Implement PublishIdentityUpdate
richardhuaaa cf50434
Add tests
richardhuaaa 5194d6c
Automatic proto updates
richardhuaaa 514d12a
Fix unsupported isolation level, sequences unsupported by default
richardhuaaa eafaab2
Add inbox size limit test
richardhuaaa 7ede4b5
Remaining tests
richardhuaaa 5b575aa
Rename files for clarity
richardhuaaa 5b731b5
Fix lint
richardhuaaa File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,218 @@ | ||
package api | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
"time" | ||
|
||
"github.com/nats-io/nats-server/v2/server" | ||
"github.com/stretchr/testify/require" | ||
"github.com/uptrace/bun" | ||
mlsstore "github.com/xmtp/xmtp-node-go/pkg/mls/store" | ||
identity "github.com/xmtp/xmtp-node-go/pkg/proto/identity/api/v1" | ||
associations "github.com/xmtp/xmtp-node-go/pkg/proto/identity/associations" | ||
test "github.com/xmtp/xmtp-node-go/pkg/testing" | ||
) | ||
|
||
func newTestService(t *testing.T, ctx context.Context) (*Service, *bun.DB, func()) { | ||
log := test.NewLog(t) | ||
db, _, mlsDbCleanup := test.NewMLSDB(t) | ||
store, err := mlsstore.New(ctx, mlsstore.Config{ | ||
Log: log, | ||
DB: db, | ||
}) | ||
require.NoError(t, err) | ||
natsServer, err := server.NewServer(&server.Options{ | ||
Port: server.RANDOM_PORT, | ||
}) | ||
require.NoError(t, err) | ||
go natsServer.Start() | ||
if !natsServer.ReadyForConnections(4 * time.Second) { | ||
t.Fail() | ||
} | ||
|
||
svc, err := NewService(log, store) | ||
require.NoError(t, err) | ||
|
||
return svc, db, func() { | ||
svc.Close() | ||
mlsDbCleanup() | ||
} | ||
} | ||
|
||
func makeCreateInbox(address string) *associations.IdentityAction { | ||
return &associations.IdentityAction{ | ||
Kind: &associations.IdentityAction_CreateInbox{ | ||
CreateInbox: &associations.CreateInbox{ | ||
InitialAddress: address, | ||
Nonce: 0, | ||
InitialAddressSignature: &associations.Signature{}, | ||
}, | ||
}, | ||
} | ||
} | ||
func makeAddAssociation() *associations.IdentityAction { | ||
return &associations.IdentityAction{ | ||
Kind: &associations.IdentityAction_Add{ | ||
Add: &associations.AddAssociation{ | ||
NewMemberIdentifier: &associations.MemberIdentifier{}, | ||
ExistingMemberSignature: &associations.Signature{}, | ||
NewMemberSignature: &associations.Signature{}, | ||
}, | ||
}, | ||
} | ||
} | ||
func makeRevokeAssociation() *associations.IdentityAction { | ||
return &associations.IdentityAction{ | ||
Kind: &associations.IdentityAction_Revoke{ | ||
Revoke: &associations.RevokeAssociation{ | ||
MemberToRevoke: &associations.MemberIdentifier{}, | ||
RecoveryAddressSignature: &associations.Signature{}, | ||
}, | ||
}, | ||
} | ||
} | ||
func makeChangeRecoveryAddress() *associations.IdentityAction { | ||
return &associations.IdentityAction{ | ||
Kind: &associations.IdentityAction_ChangeRecoveryAddress{ | ||
ChangeRecoveryAddress: &associations.ChangeRecoveryAddress{ | ||
NewRecoveryAddress: "", | ||
ExistingRecoveryAddressSignature: &associations.Signature{}, | ||
}, | ||
}, | ||
} | ||
} | ||
func makeIdentityUpdate(inbox_id string, actions ...*associations.IdentityAction) *associations.IdentityUpdate { | ||
return &associations.IdentityUpdate{ | ||
InboxId: inbox_id, | ||
ClientTimestampNs: 0, | ||
Actions: actions, | ||
} | ||
} | ||
|
||
func publishIdentityUpdateRequest(inbox_id string, actions ...*associations.IdentityAction) *identity.PublishIdentityUpdateRequest { | ||
return &identity.PublishIdentityUpdateRequest{ | ||
IdentityUpdate: makeIdentityUpdate(inbox_id, actions...), | ||
} | ||
} | ||
|
||
func makeUpdateRequest(inbox_id string, sequence_id uint64) *identity.GetIdentityUpdatesRequest_Request { | ||
return &identity.GetIdentityUpdatesRequest_Request{ | ||
InboxId: inbox_id, | ||
SequenceId: sequence_id, | ||
} | ||
} | ||
|
||
func getIdentityUpdatesRequest(requests ...*identity.GetIdentityUpdatesRequest_Request) *identity.GetIdentityUpdatesRequest { | ||
return &identity.GetIdentityUpdatesRequest{ | ||
Requests: requests, | ||
} | ||
} | ||
|
||
func TestPublishedUpdatesCanBeRead(t *testing.T) { | ||
ctx := context.Background() | ||
svc, _, cleanup := newTestService(t, ctx) | ||
defer cleanup() | ||
|
||
inbox_id := "test_inbox" | ||
address := "test_address" | ||
|
||
_, err := svc.PublishIdentityUpdate(ctx, publishIdentityUpdateRequest(inbox_id, makeCreateInbox(address))) | ||
require.NoError(t, err) | ||
|
||
res, err := svc.GetIdentityUpdates(ctx, getIdentityUpdatesRequest(makeUpdateRequest(inbox_id, 0))) | ||
require.NoError(t, err) | ||
|
||
require.Len(t, res.Responses, 1) | ||
require.Equal(t, res.Responses[0].InboxId, inbox_id) | ||
require.Len(t, res.Responses[0].Updates, 1) | ||
require.Len(t, res.Responses[0].Updates[0].Update.Actions, 1) | ||
require.Equal(t, res.Responses[0].Updates[0].Update.Actions[0].GetCreateInbox().InitialAddress, address) | ||
} | ||
|
||
func TestPublishedUpdatesAreInOrder(t *testing.T) { | ||
ctx := context.Background() | ||
svc, _, cleanup := newTestService(t, ctx) | ||
defer cleanup() | ||
|
||
inbox_id := "test_inbox" | ||
address := "test_address" | ||
|
||
_, err := svc.PublishIdentityUpdate(ctx, publishIdentityUpdateRequest(inbox_id, makeCreateInbox(address))) | ||
require.NoError(t, err) | ||
_, err = svc.PublishIdentityUpdate(ctx, publishIdentityUpdateRequest(inbox_id, makeAddAssociation(), makeChangeRecoveryAddress())) | ||
require.NoError(t, err) | ||
_, err = svc.PublishIdentityUpdate(ctx, publishIdentityUpdateRequest(inbox_id, makeRevokeAssociation())) | ||
require.NoError(t, err) | ||
|
||
res, err := svc.GetIdentityUpdates(ctx, getIdentityUpdatesRequest(makeUpdateRequest(inbox_id, 0))) | ||
require.NoError(t, err) | ||
|
||
require.Len(t, res.Responses, 1) | ||
require.Equal(t, res.Responses[0].InboxId, inbox_id) | ||
require.Len(t, res.Responses[0].Updates, 3) | ||
require.NotNil(t, res.Responses[0].Updates[0].Update.Actions[0].GetCreateInbox()) | ||
require.NotNil(t, res.Responses[0].Updates[1].Update.Actions[0].GetAdd()) | ||
require.NotNil(t, res.Responses[0].Updates[1].Update.Actions[1].GetChangeRecoveryAddress()) | ||
require.NotNil(t, res.Responses[0].Updates[2].Update.Actions[0].GetRevoke()) | ||
|
||
res, err = svc.GetIdentityUpdates(ctx, getIdentityUpdatesRequest(makeUpdateRequest(inbox_id, 1))) | ||
require.NoError(t, err) | ||
|
||
require.Len(t, res.Responses, 1) | ||
require.Equal(t, res.Responses[0].InboxId, inbox_id) | ||
require.Len(t, res.Responses[0].Updates, 2) | ||
require.NotNil(t, res.Responses[0].Updates[0].Update.Actions[0].GetAdd()) | ||
require.NotNil(t, res.Responses[0].Updates[0].Update.Actions[1].GetChangeRecoveryAddress()) | ||
require.NotNil(t, res.Responses[0].Updates[1].Update.Actions[0].GetRevoke()) | ||
} | ||
|
||
func TestQueryMultipleInboxes(t *testing.T) { | ||
ctx := context.Background() | ||
svc, _, cleanup := newTestService(t, ctx) | ||
defer cleanup() | ||
|
||
first_inbox_id := "test_inbox" | ||
second_inbox_id := "second_inbox" | ||
first_address := "test_address" | ||
second_address := "test_address" | ||
|
||
_, err := svc.PublishIdentityUpdate(ctx, publishIdentityUpdateRequest(first_inbox_id, makeCreateInbox(first_address))) | ||
require.NoError(t, err) | ||
_, err = svc.PublishIdentityUpdate(ctx, publishIdentityUpdateRequest(second_inbox_id, makeCreateInbox(second_address))) | ||
require.NoError(t, err) | ||
|
||
res, err := svc.GetIdentityUpdates(ctx, getIdentityUpdatesRequest(makeUpdateRequest(first_inbox_id, 0), makeUpdateRequest(second_inbox_id, 0))) | ||
require.NoError(t, err) | ||
|
||
require.Len(t, res.Responses, 2) | ||
require.Equal(t, res.Responses[0].Updates[0].Update.Actions[0].GetCreateInbox().InitialAddress, first_address) | ||
require.Equal(t, res.Responses[1].Updates[0].Update.Actions[0].GetCreateInbox().InitialAddress, second_address) | ||
} | ||
|
||
func TestInboxSizeLimit(t *testing.T) { | ||
ctx := context.Background() | ||
svc, _, cleanup := newTestService(t, ctx) | ||
defer cleanup() | ||
|
||
inbox_id := "test_inbox" | ||
address := "test_address" | ||
|
||
_, err := svc.PublishIdentityUpdate(ctx, publishIdentityUpdateRequest(inbox_id, makeCreateInbox(address))) | ||
require.NoError(t, err) | ||
|
||
for i := 0; i < 255; i++ { | ||
_, err = svc.PublishIdentityUpdate(ctx, publishIdentityUpdateRequest(inbox_id, makeAddAssociation())) | ||
require.NoError(t, err) | ||
} | ||
|
||
_, err = svc.PublishIdentityUpdate(ctx, publishIdentityUpdateRequest(inbox_id, makeAddAssociation())) | ||
require.Error(t, err) | ||
|
||
res, err := svc.GetIdentityUpdates(ctx, getIdentityUpdatesRequest(makeUpdateRequest(inbox_id, 0))) | ||
require.NoError(t, err) | ||
|
||
require.Len(t, res.Responses, 1) | ||
require.Equal(t, res.Responses[0].InboxId, inbox_id) | ||
require.Len(t, res.Responses[0].Updates, 256) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,6 +3,7 @@ package store | |
import ( | ||
"context" | ||
"crypto/sha256" | ||
"database/sql" | ||
"errors" | ||
"sort" | ||
"strings" | ||
|
@@ -11,8 +12,11 @@ import ( | |
"github.com/uptrace/bun" | ||
"github.com/uptrace/bun/migrate" | ||
migrations "github.com/xmtp/xmtp-node-go/pkg/migrations/mls" | ||
identity "github.com/xmtp/xmtp-node-go/pkg/proto/identity/api/v1" | ||
"github.com/xmtp/xmtp-node-go/pkg/proto/identity/associations" | ||
mlsv1 "github.com/xmtp/xmtp-node-go/pkg/proto/mls/api/v1" | ||
"go.uber.org/zap" | ||
"google.golang.org/protobuf/proto" | ||
) | ||
|
||
const maxPageSize = 100 | ||
|
@@ -23,7 +27,14 @@ type Store struct { | |
db *bun.DB | ||
} | ||
|
||
type IdentityStore interface { | ||
PublishIdentityUpdate(ctx context.Context, req *identity.PublishIdentityUpdateRequest) (*identity.PublishIdentityUpdateResponse, error) | ||
GetInboxLogs(ctx context.Context, req *identity.GetIdentityUpdatesRequest) (*identity.GetIdentityUpdatesResponse, error) | ||
} | ||
|
||
type MlsStore interface { | ||
IdentityStore | ||
|
||
CreateInstallation(ctx context.Context, installationId []byte, walletAddress string, credentialIdentity, keyPackage []byte, expiration uint64) error | ||
UpdateKeyPackage(ctx context.Context, installationId, keyPackage []byte, expiration uint64) error | ||
FetchKeyPackages(ctx context.Context, installationIds [][]byte) ([]*Installation, error) | ||
|
@@ -53,6 +64,111 @@ func New(ctx context.Context, config Config) (*Store, error) { | |
return s, nil | ||
} | ||
|
||
func (s *Store) PublishIdentityUpdate(ctx context.Context, req *identity.PublishIdentityUpdateRequest) (*identity.PublishIdentityUpdateResponse, error) { | ||
new_update := req.GetIdentityUpdate() | ||
if new_update == nil { | ||
return nil, errors.New("IdentityUpdate is required") | ||
} | ||
|
||
// TODO: Implement serializable isolation level once supported | ||
if err := s.db.RunInTx(ctx, &sql.TxOptions{ /*Isolation: sql.LevelSerializable*/ }, func(ctx context.Context, tx bun.Tx) error { | ||
inbox_log_entries := make([]*InboxLogEntry, 0) | ||
|
||
if err := s.db.NewSelect(). | ||
Model(&inbox_log_entries). | ||
Where("inbox_id = ?", new_update.GetInboxId()). | ||
Order("sequence_id ASC"). | ||
For("UPDATE"). | ||
Scan(ctx); err != nil { | ||
return err | ||
} | ||
|
||
if len(inbox_log_entries) >= 256 { | ||
return errors.New("inbox log is full") | ||
} | ||
|
||
updates := make([]*associations.IdentityUpdate, 0, len(inbox_log_entries)+1) | ||
for _, log := range inbox_log_entries { | ||
identity_update := &associations.IdentityUpdate{} | ||
if err := proto.Unmarshal(log.IdentityUpdateProto, identity_update); err != nil { | ||
return err | ||
} | ||
updates = append(updates, identity_update) | ||
} | ||
_ = append(updates, new_update) | ||
|
||
// TODO: Validate the updates, and abort transaction if failed | ||
|
||
proto_bytes, err := proto.Marshal(new_update) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
new_entry := InboxLogEntry{ | ||
InboxId: new_update.GetInboxId(), | ||
ServerTimestampNs: nowNs(), | ||
IdentityUpdateProto: proto_bytes, | ||
} | ||
|
||
_, err = s.db.NewInsert(). | ||
Model(&new_entry). | ||
Returning("sequence_id"). | ||
Exec(ctx) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
// TODO: Insert or update the address_log table using sequence_id | ||
|
||
return nil | ||
}); err != nil { | ||
return nil, err | ||
} | ||
|
||
return &identity.PublishIdentityUpdateResponse{}, nil | ||
} | ||
|
||
func (s *Store) GetInboxLogs(ctx context.Context, batched_req *identity.GetIdentityUpdatesRequest) (*identity.GetIdentityUpdatesResponse, error) { | ||
reqs := batched_req.GetRequests() | ||
resps := make([]*identity.GetIdentityUpdatesResponse_Response, len(reqs)) | ||
|
||
for i, req := range reqs { | ||
inbox_log_entries := make([]*InboxLogEntry, 0) | ||
|
||
err := s.db.NewSelect(). | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Would be nice to get all of these in a single SQL query, but we can optimize that later. |
||
Model(&inbox_log_entries). | ||
Where("sequence_id > ?", req.GetSequenceId()). | ||
Where("inbox_id = ?", req.GetInboxId()). | ||
Order("sequence_id ASC"). | ||
Scan(ctx) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
updates := make([]*identity.GetIdentityUpdatesResponse_IdentityUpdateLog, len(inbox_log_entries)) | ||
for j, entry := range inbox_log_entries { | ||
identity_update := &associations.IdentityUpdate{} | ||
if err := proto.Unmarshal(entry.IdentityUpdateProto, identity_update); err != nil { | ||
return nil, err | ||
} | ||
updates[j] = &identity.GetIdentityUpdatesResponse_IdentityUpdateLog{ | ||
SequenceId: entry.SequenceId, | ||
ServerTimestampNs: uint64(entry.ServerTimestampNs), | ||
Update: identity_update, | ||
} | ||
} | ||
|
||
resps[i] = &identity.GetIdentityUpdatesResponse_Response{ | ||
InboxId: req.GetInboxId(), | ||
Updates: updates, | ||
} | ||
} | ||
|
||
return &identity.GetIdentityUpdatesResponse{ | ||
Responses: resps, | ||
}, nil | ||
} | ||
|
||
// Creates the installation and last resort key package | ||
func (s *Store) CreateInstallation(ctx context.Context, installationId []byte, walletAddress string, credentialIdentity, keyPackage []byte, expiration uint64) error { | ||
createdAt := nowNs() | ||
|
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why is every tool for working with SQL in Go so terrible?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I hate Bun. I hate Gorm. Sqlx is fine but very bare-bones.
I've heard good things about
sqlc
but never tried itThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Awesome, this will be a good list to try when we come back to this. Always hate working with ORM's