From ba1727892710780591aecfa9f7735790e3962eb1 Mon Sep 17 00:00:00 2001 From: Prathamesh Musale Date: Wed, 13 Sep 2023 11:05:54 +0530 Subject: [PATCH] Make consensus check optional --- src/libp2p-utils.ts | 12 ++++++------ src/server.ts | 21 ++++++++++----------- 2 files changed, 16 insertions(+), 17 deletions(-) diff --git a/src/libp2p-utils.ts b/src/libp2p-utils.ts index e33759d..e4f849f 100644 --- a/src/libp2p-utils.ts +++ b/src/libp2p-utils.ts @@ -30,7 +30,7 @@ export function createMessageToL2Handler ( gasLimit?: number }, paymentsManager: PaymentsManager, - consensus: Consensus + consensus?: Consensus ) { return async (peerId: string, data: any): Promise => { log(`[${getCurrentTime()}] Received a message on mobymask P2P network from peer:`, peerId); @@ -41,7 +41,7 @@ export function createMessageToL2Handler ( // TODO: Check payment status before sending tx to l2 await handlePayment(paymentsManager, payment, payload.kind); - sendMessageToL2(consensus, signer, { contractAddress, gasLimit }, payload); + sendMessageToL2(signer, { contractAddress, gasLimit }, payload, consensus); }; } @@ -81,16 +81,16 @@ export async function handlePayment ( } export async function sendMessageToL2 ( - consensus: Consensus, signer: Signer, { contractAddress, gasLimit = DEFAULT_GAS_LIMIT }: { contractAddress: string, gasLimit?: number }, - data: any + data: any, + consensus?: Consensus ): Promise { - // Send tx to L2 only if we are the leader - if (!consensus.isLeader()) { + // If consensus is setup, send tx to L2 only if we are the leader + if (consensus && !consensus.isLeader()) { log('Not a leader, skipped sending L2 tx'); return; } diff --git a/src/server.ts b/src/server.ts index 0b5a9a4..2949a10 100644 --- a/src/server.ts +++ b/src/server.ts @@ -32,32 +32,31 @@ export const main = async (): Promise => { await serverCmd.initIndexer(Indexer); // Initialize / start the p2p nodes - await serverCmd.initP2P(); - - // Initialize / start the consensus engine - await serverCmd.initConsensus(); + const [, peer] = await serverCmd.initP2P(); // Initialize / start the Nitro node - await serverCmd.initNitro({ + const nitro = await serverCmd.initNitro({ nitroAdjudicatorAddress, consensusAppAddress, virtualPaymentAppAddress }); + // Initialize / start the consensus engine + const consensus = await serverCmd.initConsensus(); + let nitroPaymentsManager: PaymentsManager | undefined; const { enablePeer, peer: { enableL2Txs, l2TxsConfig, pubSubTopic }, nitro: { payments } } = serverCmd.config.server.p2p; if (enablePeer) { - assert(serverCmd.peer); - assert(serverCmd.nitro); - assert(serverCmd.consensus); + assert(peer); + assert(nitro); // Setup the payments manager if peer is enabled const ratesConfig: RatesConfig = await getConfig(payments.ratesFile); nitroPaymentsManager = new PaymentsManager(payments, ratesConfig); // Start subscription for payment vouchers received by the client - nitroPaymentsManager.subscribeToVouchers(serverCmd.nitro); + nitroPaymentsManager.subscribeToVouchers(nitro); // Register the pubsub topic handler let p2pMessageHandler = parseLibp2pMessage; @@ -66,10 +65,10 @@ export const main = async (): Promise => { if (enableL2Txs) { assert(l2TxsConfig); const wallet = new ethers.Wallet(l2TxsConfig.privateKey, serverCmd.ethProvider); - p2pMessageHandler = createMessageToL2Handler(wallet, l2TxsConfig, nitroPaymentsManager, serverCmd.consensus); + p2pMessageHandler = createMessageToL2Handler(wallet, l2TxsConfig, nitroPaymentsManager, consensus); } - serverCmd.peer.subscribeTopic(pubSubTopic, (peerId, data) => { + peer.subscribeTopic(pubSubTopic, (peerId, data) => { p2pMessageHandler(peerId.toString(), data); }); }