-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
## tl;dr - Implements storage of IdentityUpdate messages from the blockchain, without any validation
- Loading branch information
Showing
5 changed files
with
244 additions
and
15 deletions.
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
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,63 @@ | ||
package storer | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
"github.com/ethereum/go-ethereum/core/types" | ||
"github.com/xmtp/xmtpd/pkg/abis" | ||
"github.com/xmtp/xmtpd/pkg/db/queries" | ||
"go.uber.org/zap" | ||
) | ||
|
||
type IdentityUpdateStorer struct { | ||
contract *abis.IdentityUpdates | ||
queries *queries.Queries | ||
logger *zap.Logger | ||
} | ||
|
||
func NewIdentityUpdateStorer( | ||
queries *queries.Queries, | ||
logger *zap.Logger, | ||
contract *abis.IdentityUpdates, | ||
) *IdentityUpdateStorer { | ||
return &IdentityUpdateStorer{ | ||
queries: queries, | ||
logger: logger.Named("IdentityUpdateStorer"), | ||
contract: contract, | ||
} | ||
} | ||
|
||
// Validate and store an identity update log event | ||
func (s *IdentityUpdateStorer) StoreLog(ctx context.Context, event types.Log) LogStorageError { | ||
msgSent, err := s.contract.ParseIdentityUpdateCreated(event) | ||
if err != nil { | ||
return NewLogStorageError(err, false) | ||
} | ||
|
||
// TODO:nm figure out topic structure | ||
topic := BuildInboxTopic(msgSent.InboxId) | ||
|
||
s.logger.Debug("Inserting identity update from contract", zap.String("topic", topic)) | ||
|
||
/** | ||
TODO:nm validate the identity update | ||
**/ | ||
|
||
if _, err = s.queries.InsertGatewayEnvelope(ctx, queries.InsertGatewayEnvelopeParams{ | ||
// We may not want to hardcode this to 0 and have an originator ID for each smart contract? | ||
OriginatorNodeID: 0, | ||
OriginatorSequenceID: int64(msgSent.SequenceId), | ||
Topic: []byte(topic), | ||
OriginatorEnvelope: msgSent.Update, // TODO:nm parse originator envelope and do some validation | ||
}); err != nil { | ||
s.logger.Error("Error inserting envelope from smart contract", zap.Error(err)) | ||
return NewLogStorageError(err, true) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func BuildInboxTopic(inboxId [32]byte) string { | ||
return fmt.Sprintf("1/i/%x", inboxId) | ||
} |
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,68 @@ | ||
package storer | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
|
||
"github.com/ethereum/go-ethereum/common" | ||
"github.com/stretchr/testify/require" | ||
"github.com/xmtp/xmtpd/pkg/abis" | ||
"github.com/xmtp/xmtpd/pkg/blockchain" | ||
"github.com/xmtp/xmtpd/pkg/db" | ||
"github.com/xmtp/xmtpd/pkg/db/queries" | ||
"github.com/xmtp/xmtpd/pkg/testutils" | ||
) | ||
|
||
func buildIdentityUpdateStorer(t *testing.T) (*IdentityUpdateStorer, func()) { | ||
ctx, cancel := context.WithCancel(context.Background()) | ||
db, _, cleanup := testutils.NewDB(t, ctx) | ||
queryImpl := queries.New(db) | ||
config := testutils.GetContractsOptions(t) | ||
contractAddress := config.IdentityUpdatesContractAddress | ||
|
||
client, err := blockchain.NewClient(ctx, config.RpcUrl) | ||
require.NoError(t, err) | ||
contract, err := abis.NewIdentityUpdates( | ||
common.HexToAddress(contractAddress), | ||
client, | ||
) | ||
|
||
require.NoError(t, err) | ||
storer := NewIdentityUpdateStorer(queryImpl, testutils.NewLog(t), contract) | ||
|
||
return storer, func() { | ||
cancel() | ||
cleanup() | ||
} | ||
} | ||
|
||
func TestStoreIdentityUpdate(t *testing.T) { | ||
ctx := context.Background() | ||
storer, cleanup := buildIdentityUpdateStorer(t) | ||
defer cleanup() | ||
|
||
// Using the RandomInboxId function, since they are both 32 bytes and we treat inbox IDs as | ||
// strings outside the blockchain | ||
inboxId := testutils.RandomGroupID() | ||
message := testutils.RandomBytes(30) | ||
sequenceID := uint64(1) | ||
|
||
logMessage := testutils.BuildIdentityUpdateLog(t, inboxId, message, sequenceID) | ||
|
||
err := storer.StoreLog( | ||
ctx, | ||
logMessage, | ||
) | ||
require.NoError(t, err) | ||
|
||
envelopes, queryErr := storer.queries.SelectGatewayEnvelopes( | ||
ctx, | ||
queries.SelectGatewayEnvelopesParams{OriginatorNodeID: db.NullInt32(0)}, | ||
) | ||
require.NoError(t, queryErr) | ||
|
||
require.Equal(t, len(envelopes), 1) | ||
|
||
firstEnvelope := envelopes[0] | ||
require.Equal(t, firstEnvelope.OriginatorEnvelope, message) | ||
} |
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