Skip to content

Commit

Permalink
Merge pull request #25 from lambdaclass/recover-pubdata
Browse files Browse the repository at this point in the history
Recovering Pubdata from Turing Avail DA
  • Loading branch information
jorbush authored Jun 11, 2024
2 parents 083f23f + b3a34c2 commit 697776a
Show file tree
Hide file tree
Showing 7 changed files with 90 additions and 4 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ account.json
.vscode/
venv/
logs/
pubdata/
12 changes: 12 additions & 0 deletions validium/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,18 @@ const isVerified = await contractInstance.verifyBlobLeaf([
deno task test
```

## Recover Pubdata from Turing Avail DA

To get the pubdata from the Turing Avail DA, you can use the following command
passing the transaction hash and block hash as arguments:

```sh
deno task get-pubdata <tx_hash> <block_hash>
```

This will return the pubdata of the transaction and write it into the `pubdata`
folder.

## Faucet

Use the [Polkadot extension](https://polkadot.js.org/extension/) to manage your
Expand Down
3 changes: 2 additions & 1 deletion validium/deno.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"tasks": {
"validium": "deno run --allow-read --allow-net --allow-env main.ts",
"test": "deno test --allow-net --allow-read --allow-env tests.ts"
"test": "deno test --allow-net --allow-read --allow-env tests.ts",
"get-pubdata": "deno run --allow-write --allow-read --allow-net --allow-env get_pubdata.ts"
}
}
3 changes: 3 additions & 0 deletions validium/get_pubdata.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { getPubdata } from "./validium.ts";

await getPubdata(Deno.args[0], Deno.args[1]);
9 changes: 9 additions & 0 deletions validium/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,12 @@ export function initializeAvailApi(availRpc: string): Promise<ApiPromise> {
export function createAccount(suri: string): KeyringPair {
return new Keyring({ type: "sr25519" }).addFromUri(suri);
}

export function writeJson(path: string, data: string): string {
try {
Deno.writeTextFileSync(path, JSON.stringify(data));
return "Written to " + path;
} catch (e) {
return e.message;
}
}
32 changes: 30 additions & 2 deletions validium/tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,13 @@ import {
assertEquals,
} from "https://deno.land/[email protected]/assert/mod.ts";
import { load } from "https://deno.land/[email protected]/dotenv/mod.ts";
import { ProofData, submitDataAndVerify, verifyProof } from "./validium.ts";
import {
getPubdata,
ProofData,
submitDataAndVerify,
SubmitDataHash,
verifyProof,
} from "./validium.ts";
import { pubdataTest } from "./pubdata_test.ts";

const env = await load();
Expand Down Expand Up @@ -63,11 +69,33 @@ Deno.test("verifyBlobLeaf function should return expected result", async () => {
assertEquals(isVerified, expectedValue);
});

const resultHashes: SubmitDataHash[] = [];
let isSubmitDataAndVerifyDone = false;

Deno.test({
name: "submitDataAndVerify",
async fn() {
for (const batch of pubdataTest) {
await submitDataAndVerify(batch);
resultHashes.push(await submitDataAndVerify(batch));
}
console.log(resultHashes);
isSubmitDataAndVerifyDone = true;
},
sanitizeOps: false,
sanitizeResources: false,
});

Deno.test({
name: "getPubdata function should return expected pubdata",
async fn() {
while (!isSubmitDataAndVerifyDone) {
await new Promise((resolve) => setTimeout(resolve, 100));
}
let index = 0;
for (const hash of resultHashes) {
const result = await getPubdata(hash.txHash, hash.blockHash);
assertEquals(result, pubdataTest[index]);
index++;
}
},
sanitizeOps: false,
Expand Down
34 changes: 33 additions & 1 deletion validium/validium.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ import { ethers } from "npm:[email protected]";
import { load } from "https://deno.land/[email protected]/dotenv/mod.ts";
import ABI from "./abi/availbridge.json" with { type: "json" };
import { KeyringPair } from "https://deno.land/x/[email protected]/keyring/types.ts";
import { createAccount, initializeAvailApi } from "./helpers.ts";
import { createAccount, initializeAvailApi, writeJson } from "./helpers.ts";
import { H256 } from "https://deno.land/x/[email protected]/types/interfaces/types.ts";

const env = await load();

Expand Down Expand Up @@ -41,6 +42,11 @@ interface SubmitDataResult extends ISubmittableResult {
blockNumber: string;
}

export interface SubmitDataHash {
txHash: string;
blockHash: string;
}

/**
* Submitting data to Avail as a transaction.
*
Expand Down Expand Up @@ -167,4 +173,30 @@ export async function submitDataAndVerify(data: string) {
}
await proofAndVerify(result);
await availApi.disconnect();
const submitDataHash: SubmitDataHash = {
txHash: result.txHash.toHex(),
blockHash: (result.status.asFinalized as H256).toHex(),
};
return submitDataHash;
}

export async function getPubdata(
tx_hash: string,
block_hash: string,
): Promise<string> {
const availApi = await initializeAvailApi(AVAIL_RPC);
const block = await availApi.rpc.chain.getBlock(block_hash);
const tx = block.block.extrinsics.find((tx) => tx.hash.toHex() == tx_hash);
if (tx == undefined) {
console.log("Failed to find the Submit Data transaction");
Deno.exit(1);
}
console.log(tx.toHuman());
const dataHex = tx.method.args.map((a) => a.toString()).join(", ");
// Data retrieved from the extrinsic data
console.log(`submitted data: ${dataHex}`);
const fileName = `./pubdata/${tx_hash}.json`;
console.log(writeJson(fileName, dataHex));
await availApi.disconnect();
return dataHex;
}

0 comments on commit 697776a

Please sign in to comment.