Skip to content

Commit

Permalink
πŸ›• MinaState refactor 1/2: Move mina listeners into DO from worker
Browse files Browse the repository at this point in the history
  • Loading branch information
KimlikDAO-bot committed May 27, 2024
1 parent 415bf3e commit e457b96
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 102 deletions.
2 changes: 1 addition & 1 deletion lib
17 changes: 17 additions & 0 deletions mina/MinaState.js
Original file line number Diff line number Diff line change
@@ -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}
*/
Expand Down Expand Up @@ -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 };
106 changes: 5 additions & 101 deletions mina/mina.js
Original file line number Diff line number Diff line change
@@ -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}
*/
Expand Down Expand Up @@ -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<string>
* }}
*/
const EventData = {};

/**
* @typedef {{
* blockInfo: BlockInfo,
* eventData: !Array<EventData>
* }}
*/
const EventInfo = {};

/** @type {!Array<EventInfo>} */
let events = /** @type {!Array<EventInfo>} */(await fetch(MINA_NODE_URL, {
method: "POST",
headers: { "content-type": "application/json" },
body: JSON.stringify({ query: EventsQuery }),
})
.then((res) => res.json())
.then((data) => /** !Array<EventInfo> */(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;
Expand Down

0 comments on commit e457b96

Please sign in to comment.