index.ts 2.2 KB
Newer Older
John Doe's avatar
John Doe committed
1 2 3 4
import { Publisher } from "./modules/momiji";
import * as types from "../momiji-helpers/utils/types";
import { ethers } from "ethers";
import 'dotenv/config';
John Doe's avatar
🟣  
John Doe committed
5
import { WebSocketServer } from "ws";
John Doe's avatar
John Doe committed
6 7 8 9 10 11 12 13 14 15 16 17 18 19

const main = async () => {
  let _globalConfig: types.GlobalConfig = {
    anonRPC: process.env.ANON_RPC,
    signer: new ethers.Wallet(process.env.PRIVATE_KEY as string, new ethers.JsonRpcProvider(process.env.RPC_URL)),
    provider: new ethers.JsonRpcProvider(process.env.RPC_URL),
    nargo: process.env.NARGO_PATH,
    threads: parseInt(process.env.THREADS as string),
    gossip: {
      seeds: process.env.SEEDS ? process.env.SEEDS.split(",") : [],
      relays: process.env.RELAYS ? process.env.RELAYS.split(",") : [],
      maxRelays: process.env.MAX_RELAYS ? parseInt(process.env.MAX_RELAYS) : 8,
    },
    verbose: (process.env.VERBOSE) ? (process.env.VERBOSE.toLowerCase() === "true") : false,
Greybeard's avatar
Greybeard committed
20 21
    profit: (process.env.PROFIT) ? parseInt(process.env.PROFIT) : 0,
    ip: (process.env.PUBLIC_IP)
John Doe's avatar
John Doe committed
22
  };
John Doe's avatar
🟣  
John Doe committed
23

John Doe's avatar
John Doe committed
24 25 26
  let publisher = new Publisher(_globalConfig);

  await publisher.initializePublisher();
John Doe's avatar
🟣  
John Doe committed
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
  const wss = new WebSocketServer({
    host: '0.0.0.0',
    port: 5150
  });
  wss.on('connection', function connection(ws) {
    ws.on('error', console.error);
    ws.on('message', async function message(data) {
      if (publisher.queue.length >= 14){
        ws.send('{status:"error", msg:"Prover queue is full"}')
        return
      }
      const valid = await publisher.newTransactionReceivedMultiple(JSON.parse(data.toString()));
      if (valid){
        ws.send('{status:"ok", msg:"Proof added to queue"}');  
      } else {
        ws.send('{status:"error", msg:"Invalid proof"}');
      }
    });
  });
  let queuing = false
  const wenQueue = async() => {
    if (!queuing && publisher.queue.length > 0) {
      queuing = true;
      await publisher.proveMultiple()
      queuing = false;
    }
    
  }

  let publishing = false
  const wenPublish = async () => {
    if (!publishing && !queuing && publisher.batch.length > 0) {
      publishing = true
      await publisher.publishSingle()
      publishing = false
    }
  }

  setInterval(wenQueue, 1e4)
  setInterval(wenPublish, 1e5)
John Doe's avatar
John Doe committed
67 68 69
}

console.log(`💫 Starting publisher...`)
Greybeard's avatar
Greybeard committed
70
main();