Skip to content

Commit

Permalink
Merge pull request #159 from Staketab/status
Browse files Browse the repository at this point in the history
status
  • Loading branch information
dfstio authored Jul 11, 2024
2 parents 0434da4 + 89bd168 commit 8be652f
Show file tree
Hide file tree
Showing 3 changed files with 924 additions and 36 deletions.
3 changes: 2 additions & 1 deletion contracts/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "nameservice",
"version": "0.5.12",
"version": "0.5.24",
"author": "@staketab",
"license": "Apache-2.0",
"scripts": {
Expand All @@ -10,6 +10,7 @@
"dependencies": {
"axios": "^1.6.8",
"minanft": "1.11.3",
"nats": "^2.28.0",
"o1js": "1.2.0",
"tslib": "^2.6.2",
"unique-names-generator": "^4.7.1",
Expand Down
93 changes: 88 additions & 5 deletions contracts/src/worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,22 +76,24 @@ import { nameContract } from "./config";
import { RollupNFTData, createRollupNFT } from "./rollup/rollup-nft";
import { Metadata } from "minanft";
import { isMainThread } from "worker_threads";
import { PINATA_JWT } from "../env.json";
import { PINATA_JWT, NATS_SERVER } from "../env.json";
import { connect } from "nats";
import { hash } from "crypto";
const pinataJWT = PINATA_JWT;

const fullValidation = true;
const proofsOff = false as boolean;
const proofsOff = true as boolean;

export class DomainNameServiceWorker extends zkCloudWorker {
static mapUpdateVerificationKey: VerificationKey | undefined = undefined;
static contractVerificationKey: VerificationKey | undefined = undefined;
static blockContractVerificationKey: VerificationKey | undefined = undefined;
static validatorsVerificationKey: VerificationKey | undefined = undefined;
readonly cache: Cache;
readonly MIN_TIME_BETWEEN_BLOCKS = 1000 * 60 * 2; // 2 minutes
readonly MIN_TIME_BETWEEN_BLOCKS = 1000 * 60 * 4; // 4 minutes
readonly MAX_TIME_BETWEEN_BLOCKS = 1000 * 60 * 60; // 60 minutes
readonly MIN_TRANSACTIONS = 1;
readonly MAX_TRANSACTIONS = 4;
readonly MAX_TRANSACTIONS = 10;

constructor(cloud: Cloud) {
super(cloud);
Expand Down Expand Up @@ -387,6 +389,38 @@ export class DomainNameServiceWorker extends zkCloudWorker {
}
}

private async publishTransactionsStatus(params: {
txs: CloudTransaction[];
status: string;
hash: string;
}): Promise<void> {
try {
const { txs, status, hash } = params;
console.time("published txs status");
if (NATS_SERVER === undefined) {
console.error("NATS_SERVER is undefined");
console.timeEnd("published txs status");
return;
}
const nc = await connect({
servers: NATS_SERVER,
timeout: 5000,
});
const js = nc.jetstream({ timeout: 5000 });
const kv = await js.views.kv("profiles", { timeout: 5000 });
for (const tx of txs) {
const data = { ...tx, status, hash };
console.log("Publishing tx status", tx.txId, data);
await kv.put(`zkcloudworker.rolluptx.${tx.txId}`, JSON.stringify(data));
}
await nc.drain();
console.timeEnd("published txs status");
} catch (error) {
console.error("Error in publishTransactionsStatus", error);
console.timeEnd("published txs status");
}
}

public async task(): Promise<string | undefined> {
if (this.cloud.task === undefined) throw new Error("task is undefined");
console.log(
Expand Down Expand Up @@ -865,6 +899,8 @@ export class DomainNameServiceWorker extends zkCloudWorker {
}
const block = new BlockContract(blockAddress, tokenId);
const flags = BlockParams.unpack(block.blockParams.get());
const blockStorage = block.storage.get();
const hash = blockStorage.toIpfsHash();
if (flags.isValidated.toBoolean() === false) {
console.log(`Block ${blockNumber} is not yet validated`);
console.timeEnd("proveBlock");
Expand Down Expand Up @@ -986,6 +1022,23 @@ export class DomainNameServiceWorker extends zkCloudWorker {
//console.log("Deleting proveBlock task", this.cloud.taskId);
console.log(`Block ${blockNumber} is proved`);
await this.cloud.deleteTask(this.cloud.taskId);
const json = await loadFromIPFS(hash);
const processedTransactions: CloudTransaction[] = [];
for (const element of json.transactions) {
if (element.tx)
processedTransactions.push({
txId: element.tx.txId,
status: element.tx.status,
transaction: element.tx.transaction,
timeReceived: element.tx.timeReceived,
});
}
console.log("prove processedTransactions", processedTransactions);
await this.publishTransactionsStatus({
txs: processedTransactions,
status: `included into proved block ${blockNumber}`,
hash: txSent.hash,
});
console.timeEnd("proveBlock");
if (this.cloud.isLocalCloud === true) {
if (this.cloud.chain !== "zeko") {
Expand Down Expand Up @@ -1032,6 +1085,7 @@ export class DomainNameServiceWorker extends zkCloudWorker {
let previousBlockAddress: PublicKey | undefined = undefined;

let blockNumber = 0;
const processedTransactions: CloudTransaction[] = [];
try {
await this.fetchMinaAccount({
publicKey: blockAddress,
Expand Down Expand Up @@ -1077,14 +1131,16 @@ export class DomainNameServiceWorker extends zkCloudWorker {
if (job == undefined) onlyRestartProving = true;
else if (job.jobStatus === "failed") onlyRestartProving = true;
else {
if (job?.jobStatus)
if (job?.jobStatus) {
const hash = block.storage.get().toIpfsHash();
await this.cloud.addTask({
args: JSON.stringify(
{
contractAddress: args.contractAddress,
blockAddress: args.blockAddress,
blockNumber: blockNumber,
jobId,
hash,
},
null,
2
Expand All @@ -1094,6 +1150,7 @@ export class DomainNameServiceWorker extends zkCloudWorker {
userId: this.cloud.userId,
maxAttempts: 50,
});
}
await this.cloud.deleteTask(this.cloud.taskId);
console.timeEnd(`block ${args.blockNumber} validated`);

Expand Down Expand Up @@ -1308,6 +1365,7 @@ export class DomainNameServiceWorker extends zkCloudWorker {
blockAddress: args.blockAddress,
blockNumber: blockNumber,
jobId,
hash,
},
null,
2
Expand All @@ -1323,6 +1381,16 @@ export class DomainNameServiceWorker extends zkCloudWorker {
return `Block ${blockNumber} is already validated`;
}

for (const element of elements) {
if (element.serializedTx)
processedTransactions.push({
txId: element.serializedTx.txId,
status: element.serializedTx.status,
transaction: element.serializedTx.transaction,
timeReceived: element.serializedTx.timeReceived,
});
}

await this.compile();
if (
DomainNameServiceWorker.mapUpdateVerificationKey === undefined ||
Expand Down Expand Up @@ -1480,6 +1548,7 @@ export class DomainNameServiceWorker extends zkCloudWorker {
blockNumber: args.blockNumber,
txHash: txSent.hash,
jobId,
hash,
},
null,
2
Expand All @@ -1489,6 +1558,12 @@ export class DomainNameServiceWorker extends zkCloudWorker {
userId: this.cloud.userId,
maxAttempts: 20,
});

await this.publishTransactionsStatus({
txs: processedTransactions,
status: `included into validated block ${blockNumber}`,
hash: txSent.hash,
});
}
console.timeEnd(`block ${args.blockNumber} validated`);
if (this.cloud.isLocalCloud === true) {
Expand Down Expand Up @@ -1920,11 +1995,13 @@ export class DomainNameServiceWorker extends zkCloudWorker {
const elements: DomainCloudTransactionData[] = [];
let count = 0;
let approvedNames: string[] = [];
const processedTxs: CloudTransaction[] = [];
let rejectedNames: string[] = [];
for (let i = 0; i < txs.length; i++) {
if (count >= this.MAX_TRANSACTIONS) break;
try {
const element = await this.convertTransaction(txs[i]);
processedTxs.push({ ...txs[i], status: element.serializedTx.status });
if (element.domainData !== undefined) {
count++;
approvedNames.push(
Expand All @@ -1944,6 +2021,7 @@ export class DomainNameServiceWorker extends zkCloudWorker {
console.error("Error in convertTransaction: catch:", error);
}
}
await this.cloud.sendTransactions(processedTxs);

console.log("Approved names:", approvedNames);
console.log("Rejected names:", rejectedNames);
Expand Down Expand Up @@ -2283,6 +2361,11 @@ export class DomainNameServiceWorker extends zkCloudWorker {
userId: this.cloud.userId,
maxAttempts: 50,
});
await this.publishTransactionsStatus({
txs: processedTxs,
status: `included into block ${blockNumber}`,
hash: txSent.hash,
});

console.timeEnd(`block created`);

Expand Down
Loading

0 comments on commit 8be652f

Please sign in to comment.