-
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.
Add E2E test for indexing from the chain
- Loading branch information
Showing
17 changed files
with
247 additions
and
18 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
File renamed without changes.
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,60 @@ | ||
package blockchain | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/ethereum/go-ethereum/accounts/abi/bind" | ||
"github.com/ethereum/go-ethereum/common" | ||
"github.com/ethereum/go-ethereum/ethclient" | ||
"github.com/xmtp/xmtpd/pkg/abis" | ||
"github.com/xmtp/xmtpd/pkg/config" | ||
"go.uber.org/zap" | ||
) | ||
|
||
/* | ||
* | ||
Can publish to the blockchain, signing messages directly with the provided private key | ||
* | ||
*/ | ||
type GroupMessagePublisher struct { | ||
signer TransactionSigner | ||
contract *abis.GroupMessages | ||
logger *zap.Logger | ||
} | ||
|
||
func NewGroupMessagePublisher( | ||
logger *zap.Logger, | ||
client *ethclient.Client, | ||
signer TransactionSigner, | ||
contractOptions config.ContractsOptions, | ||
) (*GroupMessagePublisher, error) { | ||
contract, err := abis.NewGroupMessages( | ||
common.HexToAddress(contractOptions.MessagesContractAddress), | ||
client, | ||
) | ||
|
||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return &GroupMessagePublisher{ | ||
signer: signer, | ||
logger: logger.Named("GroupMessagePublisher"). | ||
With(zap.String("contractAddress", contractOptions.MessagesContractAddress)), | ||
contract: contract, | ||
}, nil | ||
} | ||
|
||
func (g *GroupMessagePublisher) Publish( | ||
ctx context.Context, | ||
groupID [32]byte, | ||
message []byte, | ||
) error { | ||
_, err := g.contract.AddMessage(&bind.TransactOpts{ | ||
Context: ctx, | ||
From: g.signer.FromAddress(), | ||
Signer: g.signer.SignerFunc(), | ||
}, groupID, message) | ||
|
||
return err | ||
} |
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
File renamed without changes.
File renamed without changes.
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,52 @@ | ||
package blockchain | ||
|
||
import ( | ||
"crypto/ecdsa" | ||
"fmt" | ||
"math/big" | ||
"strings" | ||
|
||
"github.com/ethereum/go-ethereum/accounts/abi/bind" | ||
"github.com/ethereum/go-ethereum/common" | ||
"github.com/ethereum/go-ethereum/crypto" | ||
) | ||
|
||
type PrivateKeySigner struct { | ||
accountAddress common.Address | ||
signFunction bind.SignerFn | ||
} | ||
|
||
func NewPrivateKeySigner(privateKeyString string, chainID int) (*PrivateKeySigner, error) { | ||
privateKey, err := crypto.HexToECDSA(strings.TrimPrefix(privateKeyString, "0x")) | ||
if err != nil { | ||
return nil, fmt.Errorf("unable to parse private key: %v", err) | ||
} | ||
|
||
publicKey := privateKey.Public() | ||
publicKeyECDSA, ok := publicKey.(*ecdsa.PublicKey) | ||
if !ok { | ||
return nil, fmt.Errorf("Failed to cast to ECDSA public key %v", err) | ||
} | ||
|
||
fromAddress := crypto.PubkeyToAddress(*publicKeyECDSA) | ||
auth, err := bind.NewKeyedTransactorWithChainID( | ||
privateKey, | ||
big.NewInt(int64(chainID)), | ||
) | ||
if err != nil { | ||
return nil, fmt.Errorf("Failed to create transactor: %v", err) | ||
} | ||
|
||
return &PrivateKeySigner{ | ||
accountAddress: fromAddress, | ||
signFunction: auth.Signer, | ||
}, nil | ||
} | ||
|
||
func (s *PrivateKeySigner) FromAddress() common.Address { | ||
return s.accountAddress | ||
} | ||
|
||
func (s *PrivateKeySigner) SignerFunc() bind.SignerFn { | ||
return s.signFunction | ||
} |
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,84 @@ | ||
package indexer | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
"time" | ||
|
||
"github.com/stretchr/testify/require" | ||
"github.com/xmtp/xmtpd/pkg/blockchain" | ||
"github.com/xmtp/xmtpd/pkg/db/queries" | ||
"github.com/xmtp/xmtpd/pkg/indexer/storer" | ||
"github.com/xmtp/xmtpd/pkg/testutils" | ||
) | ||
|
||
func startIndexing(t *testing.T) (*queries.Queries, context.Context, func()) { | ||
ctx, cancel := context.WithCancel(context.Background()) | ||
logger := testutils.NewLog(t) | ||
db, _, cleanup := testutils.NewDB(t, ctx) | ||
cfg := testutils.GetContractsOptions(t) | ||
querier := queries.New(db) | ||
|
||
err := StartIndexer(ctx, logger, querier, cfg) | ||
require.NoError(t, err) | ||
|
||
return querier, ctx, func() { | ||
cleanup() | ||
cancel() | ||
} | ||
} | ||
|
||
func messagePublisher(t *testing.T, ctx context.Context) *blockchain.GroupMessagePublisher { | ||
payerCfg := testutils.GetPayerOptions(t) | ||
contractsCfg := testutils.GetContractsOptions(t) | ||
var signer blockchain.TransactionSigner | ||
signer, err := blockchain.NewPrivateKeySigner(payerCfg.PrivateKey, contractsCfg.ChainID) | ||
require.NoError(t, err) | ||
|
||
client, err := blockchain.NewClient(ctx, contractsCfg.RpcUrl) | ||
require.NoError(t, err) | ||
|
||
publisher, err := blockchain.NewGroupMessagePublisher( | ||
testutils.NewLog(t), | ||
client, | ||
signer, | ||
contractsCfg, | ||
) | ||
require.NoError(t, err) | ||
|
||
return publisher | ||
} | ||
|
||
func TestStoreMessages(t *testing.T) { | ||
querier, ctx, cleanup := startIndexing(t) | ||
publisher := messagePublisher(t, ctx) | ||
defer cleanup() | ||
|
||
message := testutils.RandomBytes(32) | ||
groupID := testutils.RandomGroupID() | ||
topic := []byte(storer.BuildGroupMessageTopic(groupID)) | ||
|
||
// Publish the message onto the blockchain | ||
require.NoError(t, publisher.Publish(ctx, groupID, message)) | ||
|
||
// Poll the DB until the stored message shows up | ||
require.Eventually(t, func() bool { | ||
envelopes, err := querier.SelectGatewayEnvelopes( | ||
context.Background(), | ||
queries.SelectGatewayEnvelopesParams{ | ||
Topic: topic, | ||
}, | ||
) | ||
require.NoError(t, err) | ||
|
||
if len(envelopes) == 0 { | ||
return false | ||
} | ||
|
||
firstEnvelope := envelopes[0] | ||
require.Equal(t, firstEnvelope.OriginatorEnvelope, message) | ||
require.Equal(t, firstEnvelope.Topic, topic) | ||
|
||
return true | ||
}, 5*time.Second, 100*time.Millisecond, "Failed to find indexed envelope") | ||
} |
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
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
Oops, something went wrong.