-
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.
- Loading branch information
Showing
17 changed files
with
369 additions
and
88 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
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,11 @@ | ||
package blockchain | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/ethereum/go-ethereum/ethclient" | ||
) | ||
|
||
func NewClient(ctx context.Context, rpcUrl string) (*ethclient.Client, error) { | ||
return ethclient.DialContext(ctx, rpcUrl) | ||
} |
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
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,123 @@ | ||
package storer | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
|
||
"github.com/ethereum/go-ethereum/common" | ||
"github.com/ethereum/go-ethereum/core/types" | ||
"github.com/stretchr/testify/require" | ||
"github.com/xmtp/xmtpd/pkg/abis" | ||
"github.com/xmtp/xmtpd/pkg/db" | ||
"github.com/xmtp/xmtpd/pkg/db/queries" | ||
"github.com/xmtp/xmtpd/pkg/indexer/blockchain" | ||
testutils "github.com/xmtp/xmtpd/pkg/testing" | ||
"github.com/xmtp/xmtpd/pkg/utils" | ||
) | ||
|
||
func buildGroupMessageStorer(t *testing.T) (*GroupMessageStorer, func()) { | ||
ctx, cancel := context.WithCancel(context.Background()) | ||
db, _, cleanup := testutils.NewDB(t, ctx) | ||
queryImpl := queries.New(db) | ||
config := testutils.GetContractsOptions(t) | ||
contractAddress := config.MessagesContractAddress | ||
|
||
client, err := blockchain.NewClient(ctx, config.RpcUrl) | ||
require.NoError(t, err) | ||
contract, err := abis.NewGroupMessages( | ||
common.HexToAddress(contractAddress), | ||
client, | ||
) | ||
|
||
require.NoError(t, err) | ||
storer := NewGroupMessageStorer(queryImpl, testutils.NewLog(t), contract) | ||
|
||
return storer, func() { | ||
cancel() | ||
cleanup() | ||
} | ||
} | ||
|
||
func TestStoreGroupMessages(t *testing.T) { | ||
ctx := context.Background() | ||
storer, cleanup := buildGroupMessageStorer(t) | ||
defer cleanup() | ||
|
||
var groupID [32]byte | ||
copy(groupID[:], testutils.RandomBytes(32)) | ||
message := testutils.RandomBytes(30) | ||
sequenceID := uint64(1) | ||
|
||
logMessage := testutils.BuildMessageSentLog(t, groupID, 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) | ||
} | ||
|
||
func TestStoreGroupMessageDuplicate(t *testing.T) { | ||
ctx := context.Background() | ||
storer, cleanup := buildGroupMessageStorer(t) | ||
defer cleanup() | ||
|
||
var groupID [32]byte | ||
copy(groupID[:], testutils.RandomBytes(32)) | ||
message := testutils.RandomBytes(30) | ||
sequenceID := uint64(1) | ||
|
||
logMessage := testutils.BuildMessageSentLog(t, groupID, message, sequenceID) | ||
|
||
err := storer.StoreLog( | ||
ctx, | ||
logMessage, | ||
) | ||
require.NoError(t, err) | ||
// Store the log a second time | ||
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) | ||
} | ||
|
||
func TestStoreGroupMessageMalformed(t *testing.T) { | ||
ctx := context.Background() | ||
storer, cleanup := buildGroupMessageStorer(t) | ||
defer cleanup() | ||
|
||
abi, err := abis.GroupMessagesMetaData.GetAbi() | ||
require.NoError(t, err) | ||
|
||
topic, err := utils.GetEventTopic(abi, "MessageSent") | ||
require.NoError(t, err) | ||
|
||
logMessage := types.Log{ | ||
Topics: []common.Hash{topic}, | ||
Data: []byte("foo"), | ||
} | ||
|
||
storageErr := storer.StoreLog(ctx, logMessage) | ||
require.Error(t, storageErr) | ||
require.False(t, storageErr.ShouldRetry()) | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.