Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Tutorial 6 #568

Merged
merged 20 commits into from
Sep 4, 2023
Merged
Show file tree
Hide file tree
Changes from 10 commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
efa5f3d
feat(tutorial6): add offchain library to examples
MartinMinkov Aug 24, 2023
0a619fb
chore(package.json): update snarkyjs peer dependency version from 0.8…
MartinMinkov Aug 25, 2023
1fc95e3
refactor(zkapps): update import paths and improve code readability
MartinMinkov Aug 25, 2023
0b90b63
refactor(main.ts): remove useLocal condition to always compile smart …
MartinMinkov Aug 25, 2023
5a617ac
feat(tutorial6): refactor UI to work on localhost and localblockchain
MartinMinkov Aug 25, 2023
cef9d47
feat(.gitignore): update ignore patterns to include nested node_modul…
MartinMinkov Aug 25, 2023
0c50d17
feat(tutorial6): add project
MartinMinkov Aug 25, 2023
5a74238
feat(package.json): add repository, homepage, and bugs fields to prov…
MartinMinkov Aug 25, 2023
9d6c33c
feat(index.page.tsx): add proveUpdateTransaction method call to ensur…
MartinMinkov Aug 25, 2023
58905bc
docs(06-offchain-storage.mdx): update SnarkyJS version and library li…
MartinMinkov Aug 25, 2023
b7e5444
docs(README.md): fix typo in useLocalBlockchain variable formatting f…
MartinMinkov Aug 25, 2023
52bb963
feat(tutorial6): fix ui to work with local env
MartinMinkov Aug 28, 2023
87b57a3
feat(storageServer.ts): switch useLocalBlockchain from false to true …
MartinMinkov Aug 28, 2023
f11f295
feat(tutorial6): update readme
MartinMinkov Aug 28, 2023
1fc7a5d
feat(tutorial6): update readme
MartinMinkov Aug 28, 2023
5ea68c0
feat(package.json): bump version from 0.1.2 to 0.1.3 to reflect new c…
MartinMinkov Aug 28, 2023
88459bb
docs(main.ts, mina-setup.ts): update usage instructions to reflect ne…
MartinMinkov Aug 29, 2023
6853521
feat(.gitignore): add out directory to gitignore to prevent tracking …
MartinMinkov Sep 4, 2023
ab682c6
fix(tutorial4): remove nextjs output from git
MartinMinkov Sep 4, 2023
36dc619
Merge branch 'main' into fix/tutorial-6-offchain-storage
MartinMinkov Sep 4, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
# Dependencies
/node_modules
node_modules
/**/*/node_modules

# Production
/build
/**/*/build
/**/*/dist
build
dist

# Generated files
.docusaurus
.cache-loader


# Misc
.env
.DS_Store
Expand Down
18 changes: 9 additions & 9 deletions docs/zkapps/tutorials/06-offchain-storage.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ zkApp programmability is not yet available on the Mina Mainnet. You can get star

:::note

This tutorial was last tested with [SnarkyJS](https://www.npmjs.com/package/snarkyjs) 0.8.0.
This tutorial was last tested with [SnarkyJS](https://www.npmjs.com/package/snarkyjs) 0.12.1.

:::

Expand Down Expand Up @@ -76,7 +76,7 @@ This implementation requires a trust assumption for zkApps using it, that both d

This makes it useful for both prototyping applications that need off-chain storage, and putting applications into production where these trust assumptions are reasonable, but not for zkApps where a trustless solution is needed.

See the library [here](https://github.com/es92/zkApp-offchain-storage) if you'd like to learn more about this implementation and see how it works.
See the library [here](https://github.com/o1-labs/docs2/tree/main/examples/zkapps/06-offchain-storage/experimental-zkapp-offchain-storage) if you'd like to learn more about this implementation and see how it works.

### Further Development of Single-Server Off-Chain Storage

Expand All @@ -92,9 +92,9 @@ Some suggested improvements:

## Implementing a Project Using Off-Chain Storage

As usual, there is sample code for this project, which you can find [here](https://github.com/o1-labs/docs2/tree/main/examples/zkapps/06-offchain-storage/contracts).
As usual, there is sample code for this project, which you can find [here](https://github.com/o1-labs/docs2/tree/main/examples/zkapps/06-offchain-storage/offchain-storage-zkapp/contracts).

We will be writing and discussing the [src/main.ts](https://github.com/o1-labs/docs2/tree/main/examples/zkapps/06-offchain-storage/contracts/src/main.ts) and [src/NumberTreeContract.ts](https://github.com/o1-labs/docs2/tree/main/examples/zkapps/06-offchain-storage/contracts/src/NumberTreeContract.ts) files in this project.
We will be writing and discussing the [src/main.ts](https://github.com/o1-labs/docs2/tree/main/examples/zkapps/06-offchain-storage/offchain-storage-zkapp/contracts/src/main.ts) and [src/NumberTreeContract.ts](https://github.com/o1-labs/docs2/tree/main/examples/zkapps/06-offchain-storage/offchain-storage-zkapp/contracts/src/NumberTreeContract.ts) files in this project.

This project implements a tree, where each leaf is either empty or stores a number, which will be our data. Updates to the tree can update a leaf, if the new number in the leaf is greater than the old number. The root of the tree is stored on chain, while the tree itself is stored on an off-chain storage server.

Expand Down Expand Up @@ -128,7 +128,7 @@ export { NumberTreeContract };
Now, add the library for the off-chain storage server we'll be using:

```sh
$ npm install experimental-zkapp-offchain-storage --save
$ npm install experimental-offchain-zkapp-storage --save
```

Also install `xmlhttprequest-ts`, which we will use to make network requests when running from nodejs, where the browser's XMLHttpRequest is not available by default:
Expand All @@ -152,7 +152,7 @@ This will fail when first run, but once we start adding code to `main.ts` and `N
Also for this project, we will need to run our storage server. To do this, in a new terminal window, change into the root of your project and run:

```sh
$ node node_modules/experimental-zkapp-offchain-storage/build/src/storageServer.js
$ node node_modules/experimental-offchain-zkapp-storage/build/src/storageServer.js
```

This will start a storage server and create a `database.json` file in the current directory to store our data for this tutorial.
Expand Down Expand Up @@ -182,7 +182,7 @@ Start by adding our imports:
16 OffChainStorage,
17 Update,
18 MerkleWitness8,
19 } from 'experimental-zkapp-offchain-storage';
19 } from 'experimental-offchain-zkapp-storage';
...
```

Expand Down Expand Up @@ -294,7 +294,7 @@ That completes the smart contract!

## Implementing `main.ts`

You can find a full copy of this file [here](https://github.com/o1-labs/docs2/blob/main/examples/zkapps/06-offchain-storage/contracts/src/main.ts) for reference.
You can find a full copy of this file [here](https://github.com/o1-labs/docs2/blob/main/examples/zkapps/06-offchain-storage/offchain-storage-zkapp/contracts/src/main.ts) for reference.

We will not be implementing this full file, instead just discussing a few parts of it, since much is repeated from earlier tutorials. So download it from the above link, place it in your src folder, and then open it in your editor.

Expand Down Expand Up @@ -413,7 +413,7 @@ We call our smart contract as follows:
...
```

That completes our review of the code to interact with the off-chain storage server! As mentioned previously, you can find the full copy of this file [here](https://github.com/o1-labs/docs2/tree/main/examples/zkapps/06-offchain-storage/contracts/src/main.ts) for review.
That completes our review of the code to interact with the off-chain storage server! As mentioned previously, you can find the full copy of this file [here](https://github.com/o1-labs/docs2/tree/main/examples/zkapps/06-offchain-storage/offchain-storage-zkapp/contracts/src/main.ts) for review.

## Conclusion

Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# NodeJS
node_modules
build
coverage

# Editor
.vscode

# System
.DS_Store

# Never commit keys to Git!
keys

*.swp

database.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/bin/sh
. "$(dirname "$0")/_/husky.sh"

npx lint-staged
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
# Mina zkApp: Zkapp Offchain Storage

An experimental library for storing zkApp data offchain. This project is currently under development and should not be used in production.

## Usage

### Dependencies:

Ensure all required modules are installed. To do so, run the following command:

```bash
npm install
```

### Running the Example

#### Blockchain Mode

Toggle the `useLocalBlockchain`` variable to switch between local testing and real-world usage.
MartinMinkov marked this conversation as resolved.
Show resolved Hide resolved
MartinMinkov marked this conversation as resolved.
Show resolved Hide resolved

#### Account Configuration:

For local testing, predefined accounts will be used.
For `Berkeley` mode, you must provide a `deployerPrivateKey` and `zkAppPrivateKey` as command line arguments.

To generate these keys, you can use the following command:

```bash
npm run generate-keys
MartinMinkov marked this conversation as resolved.
Show resolved Hide resolved

```

### Running the Application:

Ensure that the offchain server is running. To do so, run the following command:

```bash
npm run server
MartinMinkov marked this conversation as resolved.
Show resolved Hide resolved
```

This will start the server on port `3001`.

Finally, to run the example, use the following command:

```bash
npm run build
node build/src/examples/main.js [deployerPrivateKey] [zkAppPrivateKey]
```

**Note:**
Always remember to safeguard private keys and never expose them publicly. Use environment variables or secure vaults to manage sensitive information in production environments.

## zkApp Storage Server

This server is designed to interact with the zkApp and help store and manage zkApp related data. It is currently set up for development purposes and should not be relied upon for production-level storage.

### Features

1. CORS enabled for cross-origin requests.
1. Storing data for a given zkApp address with a height and items.
1. Fetching stored data for a zkApp address based on its root.
1. Provides the public key of the server.
1. Background job running cleanup operations to manage outdated or irrelevant data.
1. Both local and Berkeley blockchains are supported for the Mina network.

### Endpoints

1. POST `/data`

- Save data for a zkApp address.
- Body parameters: height, items, zkAppAddress.

2. GET `/data`

- Fetch stored data for a zkApp address based on its root.
- Query parameters: zkAppAddress, root.

3. GET `/publicKey`

- Provides the public key of the server.

### Usage

MartinMinkov marked this conversation as resolved.
Show resolved Hide resolved
1. Installation: Install all the necessary packages.

```bash
npm install
```

2. Running the server: Use the following command to start the server.

```bash
npm run server
```

The server will start listening on port 3001.

3. Generate keys: Use the following command to generate keys for the server.

```bash
npm run generate-keys
```

4. Run the example: Use the following command to run the example.

```bash
npm run build
node build/src/examples/main.js [deployerPrivateKey] [zkAppPrivateKey]
```

## License

[Apache-2.0](LICENSE)
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"version": 1,
"networks": {}
}
Loading