From e457b96daaf4a2e3f07cf73a66e98055d6e57a3d Mon Sep 17 00:00:00 2001 From: KimlikDAO-bot Date: Mon, 27 May 2024 11:56:07 -0700 Subject: [PATCH] =?UTF-8?q?=F0=9F=9B=95=20MinaState=20refactor=201/2:=20Mo?= =?UTF-8?q?ve=20mina=20listeners=20into=20DO=20from=20worker?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib | 2 +- mina/MinaState.js | 17 ++++++++ mina/mina.js | 106 +++------------------------------------------- 3 files changed, 23 insertions(+), 102 deletions(-) diff --git a/lib b/lib index 284422c..296cbb4 160000 --- a/lib +++ b/lib @@ -1 +1 @@ -Subproject commit 284422c9d0f52b6260eda165ee3182de78a4b731 +Subproject commit 296cbb4b67df7027cc2c91db92f75ec9a33c6aa4 diff --git a/mina/MinaState.js b/mina/MinaState.js index 45c936c..8c3c456 100644 --- a/mina/MinaState.js +++ b/mina/MinaState.js @@ -1,5 +1,13 @@ import { DurableObject } from "cloudflare:workers"; +// sdk/mina/HumanIDv1.InitEventUID +const InitEventUID = + 0x363b52a04cf908f3357575efb35b0bf635ebb4fc2e921c140e99426fb1ef89dcn; + +// sdk/mina/HumanIDv1.AddEventUID +const AddEventUID = + 0x13c6e18cd3ba5dab50481970932ded0f7513e22ada9b77949a83dd54fc7c4e6dn; + /** * @implements {cloudflare.DurableObject} */ @@ -34,6 +42,15 @@ class MinaState extends DurableObject { this.height = height; return this.storage.put("height", height); } + + async start() { + if (await this.storage.getAlarm() != null) return; + this.alert(); + } + + async alert() { + this.storage.setAlarm(Date.now() + 1000); + } } export { MinaState }; diff --git a/mina/mina.js b/mina/mina.js index 5b9d152..71cd0d8 100644 --- a/mina/mina.js +++ b/mina/mina.js @@ -1,29 +1,9 @@ import { MerkleTree } from "./MerkleTree"; import { MinaState } from "./MinaState"; -const LEARN2EARN = "B62qnnFm3SEtrMgStoj4SRVxKSTERh8Ho3Y9jCCa8TvgBF1mqa97Sij"; - /** @define {string} */ const KIMLIKDAO_NODE_URL = "https://mina.kimlikdao.org"; -/** @const {string} */ -const MINA_NODE_URL = "https://api.minascan.io/archive/devnet/v1/graphql"; - -/** @const {string} */ -const EventsQuery = `{ - events(input: { address: "B62qnnFm3SEtrMgStoj4SRVxKSTERh8Ho3Y9jCCa8TvgBF1mqa97Sij" }) { - blockInfo { - height - } - eventData { - transactionInfo { - status - } - data - } - } -}`; - /** * @implements {cloudflare.ModuleWorker} */ @@ -57,90 +37,14 @@ const MinaWorker = { } }) ); + } else if (pathname == "/start") { + const id = env.MinaState.idFromName(""); + console.log(id); + env.MinaState.get(id).start(); + return new Response(); } return Promise.reject(); }, - - /** - * TODO(KimlikDAO-bot): Implement a robust Mina synchronizer. - * - * A very simple and error prone Mina sychronization method. - * Will not survive forks. - * - * @param {!MinaEnv} env - */ - async scheduled(event, env) { - /** @const {!MerkleTree} */ - const merkleTree = /** @type {!MerkleTree} */(env.MerkleTree.get(env.MerkleTree.idFromName(LEARN2EARN))); - /** @const {!MinaState} */ - const minaState = /** @type {!MinaState} */(env.MinaState.get(env.MinaState.idFromName(""))); - /** @const {number} */ - const lastHeight = await minaState.getHeight(); - console.log("Last height", lastHeight); - - /** - * @typedef {{ - * height: number - * }} - */ - const BlockInfo = {}; - - /** - * @typedef {{ - * status: string - * }} - */ - const TransactionInfo = {}; - - /** - * @typedef {{ - * transactionInfo: TransactionInfo, - * data: !Array - * }} - */ - const EventData = {}; - - /** - * @typedef {{ - * blockInfo: BlockInfo, - * eventData: !Array - * }} - */ - const EventInfo = {}; - - /** @type {!Array} */ - let events = /** @type {!Array} */(await fetch(MINA_NODE_URL, { - method: "POST", - headers: { "content-type": "application/json" }, - body: JSON.stringify({ query: EventsQuery }), - }) - .then((res) => res.json()) - .then((data) => /** !Array */(data["data"]["events"]).filter((/** EventInfo */ e) => - e.eventData[0].transactionInfo.status == "applied" && - e.blockInfo.height > lastHeight - ))); - events.sort((x, y) => x.blockInfo.height - y.blockInfo.height); - - for (const e of events) { - const [type, val] = e.eventData[0].data; - switch (type) { - case "1": - await merkleTree.setHeight(+val); - console.log(`height set ${+val}`); - break; - case "0": - await merkleTree.setLeaf(BigInt(val).toString(16), 1n); - console.log(`leaf set ${val}`); - break; - } - } - - if (events.length) { - const height = +(events.pop().blockInfo.height); - console.log(`mina state height set ${height}`); - await minaState.setHeight(height); - } - }, }; export default MinaWorker;