Skip to content

Commit

Permalink
Allow running without payer key (#547)
Browse files Browse the repository at this point in the history
If you were to try to run the XMTP API/SYNC without providing the payer
key, the node will crash:
```
$ ./dev/run
2025-02-24T14:01:43.485-0500    INFO    replication     Version: v0.2.0-9-g1f8192f
2025-02-24T14:01:43.495-0500    INFO    replication     Successfully connected to DB    {"namespace": "xmtpd_c04b2306614f"}
2025-02-24T14:01:43.512-0500    FATAL   replication     initializing signer     {"error": "unable to parse private key: invalid length, need 256 bits"}
exit status 1
```

The blockchain publisher is only required for the payer. If we ever need
it for the node, it will have to use the node key.

I will break apart the XMTPD image and the payer image in the next few
days. But I need this to get the infra code moving.
  • Loading branch information
mkysel authored Feb 24, 2025
1 parent 1d52fae commit b5dfeff
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 30 deletions.
20 changes: 0 additions & 20 deletions cmd/replication/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,32 +110,12 @@ func main() {
logger.Fatal("starting smart contract registry", zap.Error(err))
}

signer, err := blockchain.NewPrivateKeySigner(
options.Payer.PrivateKey,
options.Contracts.ChainID,
)
if err != nil {
logger.Fatal("initializing signer", zap.Error(err))
}

blockchainPublisher, err := blockchain.NewBlockchainPublisher(
ctx,
logger,
ethclient,
signer,
options.Contracts,
)
if err != nil {
logger.Fatal("initializing message publisher", zap.Error(err))
}

s, err := server.NewReplicationServer(
ctx,
logger,
options,
chainRegistry,
dbInstance,
blockchainPublisher,
fmt.Sprintf("0.0.0.0:%d", options.API.Port),
version,
)
Expand Down
28 changes: 25 additions & 3 deletions pkg/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@ func NewReplicationServer(
options config.ServerOptions,
nodeRegistry registry.NodeRegistry,
writerDB *sql.DB,
blockchainPublisher blockchain.IBlockchainPublisher,
listenAddress string,
serverVersion *semver.Version,
) (*ReplicationServer, error) {
Expand Down Expand Up @@ -135,7 +134,6 @@ func NewReplicationServer(
options,
s,
writerDB,
blockchainPublisher,
listenAddress,
serverVersion,
)
Expand Down Expand Up @@ -170,7 +168,6 @@ func startAPIServer(
options config.ServerOptions,
s *ReplicationServer,
writerDB *sql.DB,
blockchainPublisher blockchain.IBlockchainPublisher,
listenAddress string,
serverVersion *semver.Version,
) error {
Expand Down Expand Up @@ -223,6 +220,31 @@ func startAPIServer(
if err != nil {
return err
}

signer, err := blockchain.NewPrivateKeySigner(
options.Payer.PrivateKey,
options.Contracts.ChainID,
)
if err != nil {
log.Fatal("initializing signer", zap.Error(err))
}

ethclient, err := blockchain.NewClient(ctx, options.Contracts.RpcUrl)
if err != nil {
log.Fatal("initializing blockchain client", zap.Error(err))
}

blockchainPublisher, err := blockchain.NewBlockchainPublisher(
ctx,
log,
ethclient,
signer,
options.Contracts,
)
if err != nil {
log.Fatal("initializing message publisher", zap.Error(err))
}

payerService, err := payer.NewPayerApiService(
ctx,
log,
Expand Down
8 changes: 1 addition & 7 deletions pkg/server/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import (
"github.com/ethereum/go-ethereum/crypto"
"github.com/stretchr/testify/require"
"github.com/xmtp/xmtpd/pkg/config"
"github.com/xmtp/xmtpd/pkg/mocks/blockchain"
mocks "github.com/xmtp/xmtpd/pkg/mocks/registry"
"github.com/xmtp/xmtpd/pkg/proto/xmtpv4/envelopes"
"github.com/xmtp/xmtpd/pkg/proto/xmtpv4/message_api"
Expand Down Expand Up @@ -49,7 +48,6 @@ func NewTestServer(
privateKey *ecdsa.PrivateKey,
) *s.ReplicationServer {
log := testutils.NewLog(t)
messagePublisher := blockchain.NewMockIBlockchainPublisher(t)

server, err := s.NewReplicationServer(context.Background(), log, config.ServerOptions{
Contracts: config.ContractsOptions{
Expand All @@ -71,11 +69,7 @@ func NewTestServer(
Replication: config.ReplicationOptions{
Enable: true,
},
Payer: config.PayerOptions{
Enable: true,
PrivateKey: hex.EncodeToString(crypto.FromECDSA(privateKey)),
},
}, registry, db, messagePublisher, fmt.Sprintf("localhost:%d", port), testutils.GetLatestVersion(t))
}, registry, db, fmt.Sprintf("localhost:%d", port), testutils.GetLatestVersion(t))
require.NoError(t, err)

return server
Expand Down

0 comments on commit b5dfeff

Please sign in to comment.