Skip to content

Commit

Permalink
Make consensus check optional
Browse files Browse the repository at this point in the history
  • Loading branch information
prathamesh0 committed Sep 13, 2023
1 parent 7923c86 commit 90059e8
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 17 deletions.
12 changes: 6 additions & 6 deletions src/libp2p-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export function createMessageToL2Handler (
gasLimit?: number
},
paymentsManager: PaymentsManager,
consensus: Consensus
consensus?: Consensus
) {
return async (peerId: string, data: any): Promise<void> => {
log(`[${getCurrentTime()}] Received a message on mobymask P2P network from peer:`, peerId);
Expand All @@ -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);
};
}

Expand Down Expand Up @@ -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<void> {
// 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;
}
Expand Down
21 changes: 10 additions & 11 deletions src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,32 +32,31 @@ export const main = async (): Promise<any> => {
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;
Expand All @@ -66,10 +65,10 @@ export const main = async (): Promise<any> => {
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);
});
}
Expand Down

0 comments on commit 90059e8

Please sign in to comment.