mirror of
https://github.com/cerc-io/watcher-ts
synced 2025-01-07 20:08:06 +00:00
Integrate peer in watcher server and send txs to L2 chain (#344)
* Integrate peer in watcher and send txs to L2 * Remove passing debug log in functions * Remove unnecessary relay advertise config option --------- Co-authored-by: prathamesh0 <prathamesh.musale0@gmail.com>
This commit is contained in:
parent
ee63e33cc5
commit
ac5e030b13
@ -27,7 +27,7 @@ interface Arguments {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export class PeerCmd {
|
export class PeerCmd {
|
||||||
async exec (pubSubTopic?: string, parseLibp2pMessage?: (log: debug.Debugger, peerId: string, data: any) => void): Promise<any> {
|
async exec (pubSubTopic?: string, parseLibp2pMessage?: (peerId: string, data: any) => void): Promise<any> {
|
||||||
const argv: Arguments = _getArgv();
|
const argv: Arguments = _getArgv();
|
||||||
|
|
||||||
const { Peer } = await import('@cerc-io/peer');
|
const { Peer } = await import('@cerc-io/peer');
|
||||||
@ -52,7 +52,7 @@ export class PeerCmd {
|
|||||||
if (pubSubTopic) {
|
if (pubSubTopic) {
|
||||||
peer.subscribeTopic(pubSubTopic, (peerId, data) => {
|
peer.subscribeTopic(pubSubTopic, (peerId, data) => {
|
||||||
if (parseLibp2pMessage) {
|
if (parseLibp2pMessage) {
|
||||||
parseLibp2pMessage(log, peerId.toString(), data);
|
parseLibp2pMessage(peerId.toString(), data);
|
||||||
} else {
|
} else {
|
||||||
log(`> ${peerId.toString()} > ${data}`);
|
log(`> ${peerId.toString()} > ${data}`);
|
||||||
}
|
}
|
||||||
|
@ -105,7 +105,7 @@ export class ServerCmd {
|
|||||||
async exec (
|
async exec (
|
||||||
createResolvers: (indexer: IndexerInterface, eventWatcher: EventWatcher) => Promise<any>,
|
createResolvers: (indexer: IndexerInterface, eventWatcher: EventWatcher) => Promise<any>,
|
||||||
typeDefs: TypeSource,
|
typeDefs: TypeSource,
|
||||||
parseLibp2pMessage?: (log: debug.Debugger, peerId: string, data: any) => void
|
parseLibp2pMessage?: (peerId: string, data: any) => void
|
||||||
): Promise<{
|
): Promise<{
|
||||||
app: Application,
|
app: Application,
|
||||||
server: ApolloServer
|
server: ApolloServer
|
||||||
@ -146,7 +146,7 @@ export class ServerCmd {
|
|||||||
|
|
||||||
async _startP2PNodes (
|
async _startP2PNodes (
|
||||||
p2pConfig: P2PConfig,
|
p2pConfig: P2PConfig,
|
||||||
parseLibp2pMessage?: (log: debug.Debugger, peerId: string, data: any) => void
|
parseLibp2pMessage?: (peerId: string, data: any) => void
|
||||||
): Promise<void> {
|
): Promise<void> {
|
||||||
const { createRelayNode, Peer } = await import('@cerc-io/peer');
|
const { createRelayNode, Peer } = await import('@cerc-io/peer');
|
||||||
const {
|
const {
|
||||||
@ -208,7 +208,7 @@ export class ServerCmd {
|
|||||||
|
|
||||||
peer.subscribeTopic(peerConfig.pubSubTopic, (peerId, data) => {
|
peer.subscribeTopic(peerConfig.pubSubTopic, (peerId, data) => {
|
||||||
if (parseLibp2pMessage) {
|
if (parseLibp2pMessage) {
|
||||||
parseLibp2pMessage(log, peerId.toString(), data);
|
parseLibp2pMessage(peerId.toString(), data);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
[server]
|
[server]
|
||||||
host = "127.0.0.1"
|
host = "127.0.0.1"
|
||||||
port = 3010
|
port = 3010
|
||||||
kind = "active"
|
kind = "lazy"
|
||||||
|
|
||||||
# Checkpointing state.
|
# Checkpointing state.
|
||||||
checkpointing = true
|
checkpointing = true
|
||||||
@ -32,6 +32,11 @@
|
|||||||
[server.p2p.peer]
|
[server.p2p.peer]
|
||||||
relayMultiaddr = ''
|
relayMultiaddr = ''
|
||||||
pubSubTopic = 'mobymask'
|
pubSubTopic = 'mobymask'
|
||||||
|
peerIdFile = ''
|
||||||
|
|
||||||
|
[server.p2p.peer.l2TxConfig]
|
||||||
|
privateKey = ''
|
||||||
|
contractAddress = ''
|
||||||
|
|
||||||
[metrics]
|
[metrics]
|
||||||
host = "127.0.0.1"
|
host = "127.0.0.1"
|
||||||
|
@ -8,7 +8,7 @@ import { TransactionReceipt, TransactionResponse } from '@ethersproject/provider
|
|||||||
|
|
||||||
import { abi as PhisherRegistryABI } from './artifacts/PhisherRegistry.json';
|
import { abi as PhisherRegistryABI } from './artifacts/PhisherRegistry.json';
|
||||||
|
|
||||||
const log = debug('laconic:libp2p-utils');
|
const log = debug('vulcanize:libp2p-utils');
|
||||||
|
|
||||||
const contractInterface = new ethers.utils.Interface(PhisherRegistryABI);
|
const contractInterface = new ethers.utils.Interface(PhisherRegistryABI);
|
||||||
|
|
||||||
@ -17,11 +17,26 @@ const MESSAGE_KINDS = {
|
|||||||
REVOKE: 'revoke'
|
REVOKE: 'revoke'
|
||||||
};
|
};
|
||||||
|
|
||||||
export async function sendMessageToL2 (
|
const DEFAULT_GAS_LIMIT = 500000;
|
||||||
|
|
||||||
|
export function createMessageToL2Handler (
|
||||||
signer: Signer,
|
signer: Signer,
|
||||||
{ contractAddress, gasLimit }: {
|
{ contractAddress, gasLimit }: {
|
||||||
contractAddress: string,
|
contractAddress: string,
|
||||||
gasLimit: number
|
gasLimit?: number
|
||||||
|
}
|
||||||
|
) {
|
||||||
|
return (peerId: string, data: any): void => {
|
||||||
|
log(`[${getCurrentTime()}] Received a message on mobymask P2P network from peer:`, peerId);
|
||||||
|
sendMessageToL2(signer, { contractAddress, gasLimit }, data);
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function sendMessageToL2 (
|
||||||
|
signer: Signer,
|
||||||
|
{ contractAddress, gasLimit = DEFAULT_GAS_LIMIT }: {
|
||||||
|
contractAddress: string,
|
||||||
|
gasLimit?: number
|
||||||
},
|
},
|
||||||
data: any
|
data: any
|
||||||
): Promise<void> {
|
): Promise<void> {
|
||||||
@ -82,18 +97,18 @@ export async function sendMessageToL2 (
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export function parseLibp2pMessage (log: debug.Debugger, peerId: string, data: any): void {
|
export function parseLibp2pMessage (peerId: string, data: any): void {
|
||||||
log(`[${getCurrentTime()}] Received a message on mobymask P2P network from peer:`, peerId);
|
log(`[${getCurrentTime()}] Received a message on mobymask P2P network from peer:`, peerId);
|
||||||
const { kind, message } = data;
|
const { kind, message } = data;
|
||||||
|
|
||||||
switch (kind) {
|
switch (kind) {
|
||||||
case MESSAGE_KINDS.INVOKE: {
|
case MESSAGE_KINDS.INVOKE: {
|
||||||
_parseInvocation(log, message);
|
_parseInvocation(message);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
case MESSAGE_KINDS.REVOKE: {
|
case MESSAGE_KINDS.REVOKE: {
|
||||||
_parseRevocation(log, message);
|
_parseRevocation(message);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -112,7 +127,7 @@ export const getCurrentTime = (): string => {
|
|||||||
return `${now.getHours()}:${now.getMinutes()}:${now.getSeconds()}`;
|
return `${now.getHours()}:${now.getMinutes()}:${now.getSeconds()}`;
|
||||||
};
|
};
|
||||||
|
|
||||||
function _parseInvocation (log: debug.Debugger, msg: any): void {
|
function _parseInvocation (msg: any): void {
|
||||||
log('Signed invocations:');
|
log('Signed invocations:');
|
||||||
log(JSON.stringify(msg, null, 2));
|
log(JSON.stringify(msg, null, 2));
|
||||||
|
|
||||||
@ -125,7 +140,7 @@ function _parseInvocation (log: debug.Debugger, msg: any): void {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
function _parseRevocation (log: debug.Debugger, msg: any): void {
|
function _parseRevocation (msg: any): void {
|
||||||
const { signedDelegation, signedIntendedRevocation } = msg;
|
const { signedDelegation, signedIntendedRevocation } = msg;
|
||||||
log('Signed delegation:');
|
log('Signed delegation:');
|
||||||
log(JSON.stringify(signedDelegation, null, 2));
|
log(JSON.stringify(signedDelegation, null, 2));
|
||||||
|
@ -6,13 +6,14 @@ import fs from 'fs';
|
|||||||
import path from 'path';
|
import path from 'path';
|
||||||
import 'reflect-metadata';
|
import 'reflect-metadata';
|
||||||
import debug from 'debug';
|
import debug from 'debug';
|
||||||
|
import { ethers } from 'ethers';
|
||||||
|
|
||||||
import { ServerCmd } from '@cerc-io/cli';
|
import { ServerCmd } from '@cerc-io/cli';
|
||||||
|
|
||||||
import { createResolvers } from './resolvers';
|
import { createResolvers } from './resolvers';
|
||||||
import { Indexer } from './indexer';
|
import { Indexer } from './indexer';
|
||||||
import { Database } from './database';
|
import { Database } from './database';
|
||||||
import { parseLibp2pMessage } from './libp2p-utils';
|
import { createMessageToL2Handler, parseLibp2pMessage } from './libp2p-utils';
|
||||||
|
|
||||||
const log = debug('vulcanize:server');
|
const log = debug('vulcanize:server');
|
||||||
|
|
||||||
@ -21,8 +22,16 @@ export const main = async (): Promise<any> => {
|
|||||||
await serverCmd.init(Database);
|
await serverCmd.init(Database);
|
||||||
await serverCmd.initIndexer(Indexer);
|
await serverCmd.initIndexer(Indexer);
|
||||||
|
|
||||||
|
let p2pMessageHandler = parseLibp2pMessage;
|
||||||
|
const { l2TxConfig } = serverCmd.config.server.p2p.peer;
|
||||||
|
|
||||||
|
if (l2TxConfig) {
|
||||||
|
const wallet = new ethers.Wallet(l2TxConfig.privateKey, serverCmd.ethProvider);
|
||||||
|
p2pMessageHandler = createMessageToL2Handler(wallet, l2TxConfig);
|
||||||
|
}
|
||||||
|
|
||||||
const typeDefs = fs.readFileSync(path.join(__dirname, 'schema.gql')).toString();
|
const typeDefs = fs.readFileSync(path.join(__dirname, 'schema.gql')).toString();
|
||||||
return serverCmd.exec(createResolvers, typeDefs, parseLibp2pMessage);
|
return serverCmd.exec(createResolvers, typeDefs, p2pMessageHandler);
|
||||||
};
|
};
|
||||||
|
|
||||||
main().then(() => {
|
main().then(() => {
|
||||||
|
@ -85,9 +85,6 @@ export async function createRelayNode (init: RelayNodeInitConfig): Promise<Libp2
|
|||||||
hop: {
|
hop: {
|
||||||
enabled: true,
|
enabled: true,
|
||||||
timeout: HOP_TIMEOUT
|
timeout: HOP_TIMEOUT
|
||||||
},
|
|
||||||
advertise: {
|
|
||||||
enabled: true
|
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
webRTCSignal: {
|
webRTCSignal: {
|
||||||
|
@ -69,6 +69,18 @@ export interface RelayConfig {
|
|||||||
enableDebugInfo?: boolean;
|
enableDebugInfo?: boolean;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// L2 tx config
|
||||||
|
interface L2TxConfig {
|
||||||
|
// Address of contract for which txs are sent
|
||||||
|
contractAddress: string;
|
||||||
|
|
||||||
|
// Private key of tx signer (needs to have some balance)
|
||||||
|
privateKey: string;
|
||||||
|
|
||||||
|
// Gas limit for tx
|
||||||
|
gasLimit?: number
|
||||||
|
}
|
||||||
|
|
||||||
// Peer config
|
// Peer config
|
||||||
export interface PeerConfig {
|
export interface PeerConfig {
|
||||||
// Multiaddr of the primary relay node for this peer
|
// Multiaddr of the primary relay node for this peer
|
||||||
@ -100,6 +112,9 @@ export interface PeerConfig {
|
|||||||
|
|
||||||
// Participate in exchange of debug info over floodsub
|
// Participate in exchange of debug info over floodsub
|
||||||
enableDebugInfo?: boolean;
|
enableDebugInfo?: boolean;
|
||||||
|
|
||||||
|
// Config for sending txs to L2
|
||||||
|
l2TxConfig?: L2TxConfig;
|
||||||
}
|
}
|
||||||
|
|
||||||
// P2P config
|
// P2P config
|
||||||
|
Loading…
Reference in New Issue
Block a user