From b5dfeffc8490d01d08b65ca663e373bcacb57a0b Mon Sep 17 00:00:00 2001 From: Martin Kysel Date: Mon, 24 Feb 2025 16:36:11 -0500 Subject: [PATCH] Allow running without payer key (#547) 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. --- cmd/replication/main.go | 20 -------------------- pkg/server/server.go | 28 +++++++++++++++++++++++++--- pkg/server/server_test.go | 8 +------- 3 files changed, 26 insertions(+), 30 deletions(-) diff --git a/cmd/replication/main.go b/cmd/replication/main.go index 5ab54207..c440c735 100644 --- a/cmd/replication/main.go +++ b/cmd/replication/main.go @@ -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, ) diff --git a/pkg/server/server.go b/pkg/server/server.go index fba43689..3022b540 100644 --- a/pkg/server/server.go +++ b/pkg/server/server.go @@ -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) { @@ -135,7 +134,6 @@ func NewReplicationServer( options, s, writerDB, - blockchainPublisher, listenAddress, serverVersion, ) @@ -170,7 +168,6 @@ func startAPIServer( options config.ServerOptions, s *ReplicationServer, writerDB *sql.DB, - blockchainPublisher blockchain.IBlockchainPublisher, listenAddress string, serverVersion *semver.Version, ) error { @@ -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, diff --git a/pkg/server/server_test.go b/pkg/server/server_test.go index 26a1b778..41aab841 100644 --- a/pkg/server/server_test.go +++ b/pkg/server/server_test.go @@ -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" @@ -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{ @@ -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