Skip to content
Draft
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 .github/workflows/build-node-go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ jobs:
with:
context: .
file: ./dev/docker/${{ steps.set_dockerfile.outputs.dockerfile }}
push: ${{ github.event_name != 'pull_request' }}
push: true
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
build-args: |
Expand Down
126 changes: 87 additions & 39 deletions pkg/mls/store/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ import (
"context"
"crypto/sha256"
"database/sql"
"encoding/binary"
"errors"
"os"
"strings"
"time"

Expand Down Expand Up @@ -314,23 +316,45 @@ func (s *Store) InsertWelcomeMessage(
wrapperAlgorithm types.WrapperAlgorithm,
welcomeMetadata []byte,
) (*queries.WelcomeMessage, error) {
dataHash := sha256.Sum256(append(installationId, data...))
message, err := s.queries.InsertWelcomeMessage(ctx, queries.InsertWelcomeMessageParams{
InstallationKey: installationId,
Data: data,
InstallationKeyDataHash: dataHash[:],
HpkePublicKey: hpkePublicKey,
WrapperAlgorithm: int16(wrapperAlgorithm),
WelcomeMetadata: welcomeMetadata,
})
if err != nil {
if strings.Contains(err.Error(), "duplicate key value violates unique constraint") {
return nil, NewAlreadyExistsError(err)
enableDuplicates := os.Getenv("XMTP_ENABLE_DUPLICATE_WELCOMES") == "true"
numInserts := 1
if enableDuplicates {
numInserts = 3
}

var firstMessage *queries.WelcomeMessage
Copy link

Choose a reason for hiding this comment

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

The loop aborts on the first duplicate-key error, so later attempts never run when XMTP_ENABLE_DUPLICATE_WELCOMES is true. Consider continuing on duplicate-key errors and only return an error after all attempts fail; return the first successful insert.

🚀 Reply to ask Macroscope to explain or update this suggestion.

👍 Helpful? React to give us feedback.

for i := 0; i < numInserts; i++ {
// Include the index in the hash to ensure duplicates can be inserted
hashInput := append(installationId, data...)
if enableDuplicates {
indexBytes := make([]byte, 4)
binary.BigEndian.PutUint32(indexBytes, uint32(i))
hashInput = append(hashInput, indexBytes...)
}
dataHash := sha256.Sum256(hashInput)

message, err := s.queries.InsertWelcomeMessage(ctx, queries.InsertWelcomeMessageParams{
InstallationKey: installationId,
Data: data,
InstallationKeyDataHash: dataHash[:],
HpkePublicKey: hpkePublicKey,
WrapperAlgorithm: int16(wrapperAlgorithm),
WelcomeMetadata: welcomeMetadata,
})
if err != nil {
if strings.Contains(err.Error(), "duplicate key value violates unique constraint") {
return nil, NewAlreadyExistsError(err)
}
return nil, err
}

// Store the first message to return
if i == 0 {
firstMessage = &message
}
return nil, err
}

return &message, nil
return firstMessage, nil
}

func (s *Store) InsertWelcomePointerMessage(
Expand All @@ -344,35 +368,59 @@ func (s *Store) InsertWelcomePointerMessage(
return nil, errors.New("hpke public key is required")
}

h := sha256.New()
_, err := h.Write(installationKey)
if err != nil {
return nil, err
enableDuplicates := os.Getenv("XMTP_ENABLE_DUPLICATE_WELCOMES") == "true"
numInserts := 1
if enableDuplicates {
numInserts = 3
}
_, err = h.Write(welcomePointerData)
if err != nil {
return nil, err
}
var dataHash [32]byte
copy(dataHash[:], h.Sum(nil))
message, err := s.queries.InsertWelcomePointerMessage(
ctx,
queries.InsertWelcomePointerMessageParams{
InstallationKey: installationKey,
WelcomePointerData: welcomePointerData,
InstallationKeyDataHash: dataHash[:],
HpkePublicKey: hpkePublicKey,
WrapperAlgorithm: int16(wrapperAlgorithm),
},
)
if err != nil {
if strings.Contains(err.Error(), "duplicate key value violates unique constraint") {
return nil, NewAlreadyExistsError(err)

var firstMessage *queries.WelcomeMessage
Copy link

Choose a reason for hiding this comment

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

firstMessage = &message takes the address of the loop variable, so after later iterations it points to the last value, not the first. Consider storing the first result in a separate variable outside the loop and take its address instead.

-    var firstMessage *queries.WelcomeMessage
+    var firstMessage *queries.WelcomeMessage
+    var first queries.WelcomeMessage
@@
-        if i == 0 {
-            firstMessage = &message
-        }
+        if i == 0 {
+            first = message
+            firstMessage = &first
+        }

🚀 Reply to ask Macroscope to explain or update this suggestion.

👍 Helpful? React to give us feedback.

for i := 0; i < numInserts; i++ {
h := sha256.New()
_, err := h.Write(installationKey)
if err != nil {
return nil, err
}
_, err = h.Write(welcomePointerData)
if err != nil {
return nil, err
}
// Include the index in the hash to ensure duplicates can be inserted
if enableDuplicates {
indexBytes := make([]byte, 4)
binary.BigEndian.PutUint32(indexBytes, uint32(i))
_, err = h.Write(indexBytes)
if err != nil {
return nil, err
}
}
var dataHash [32]byte
copy(dataHash[:], h.Sum(nil))

message, err := s.queries.InsertWelcomePointerMessage(
ctx,
queries.InsertWelcomePointerMessageParams{
InstallationKey: installationKey,
WelcomePointerData: welcomePointerData,
InstallationKeyDataHash: dataHash[:],
HpkePublicKey: hpkePublicKey,
WrapperAlgorithm: int16(wrapperAlgorithm),
},
)
if err != nil {
if strings.Contains(err.Error(), "duplicate key value violates unique constraint") {
return nil, NewAlreadyExistsError(err)
}
return nil, err
}

// Store the first message to return
if i == 0 {
firstMessage = &message
}
return nil, err
}

return &message, nil
return firstMessage, nil
}

func (s *Store) GetInboxIds(
Expand Down
Loading