Skip to content

Commit

Permalink
Merge pull request #72 from firstbatchxyz/fix-micro
Browse files Browse the repository at this point in the history
Fix micro test & code
  • Loading branch information
anilaltuner authored Jan 11, 2024
2 parents 611b1c4 + ae1ea50 commit 00d0a0d
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 16 deletions.
6 changes: 3 additions & 3 deletions examples/micro/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ This backend implementation uses [Micro](https://github.com/vercel/micro) which

## Usage

First, you need an Arweave wallet. Provide the path to the wallet with the `WALLET_PATH` environment variable.
First, you need an Arweave wallet. Provide the path to the wallet with the `WALLET` environment variable.

> [!NOTE]
>
Expand All @@ -23,14 +23,14 @@ yarn install
Finally, start the server with:

```sh
CONTRACT_TXID="your-contract" yarn start
CONTRACT="your-contract" yarn start
```

### Configurations

There are several environment variables to configure the server. You can provide them within the command line, or via `.env` file. An example is given [here](./.env.example).

- `WALLET_PATH=path/to/wallet.json` <br> HollowDB requires an Arweave wallet, specified by this variable. If none is given, it defaults to `./config/wallet.json`.
- `WALLET=path/to/wallet.json` <br> HollowDB requires an Arweave wallet, specified by this variable. If none is given, it defaults to `./config/wallet.json`.

- `REDIS_URL=<redis-url>` <br> You need a Redis server running before you start the server, the URL to the server can be provided with a `REDIS_URL` environment variable. The connection URL defaults to `redis://default:redispw@localhost:6379`.

Expand Down
10 changes: 5 additions & 5 deletions examples/micro/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@
},
"main": "build/src/index.js",
"scripts": {
"precompile": "yarn clean",
"compile": "npx tsc",
"prestart": "yarn compile",
"start": "npx micro",
"prebuild": "yarn clean",
"build": "npx tsc",
"prestart": "yarn build",
"start": "node ./build/src/index.js",
"test": "npx mocha --bail",
"clean": "rm -rf ./build"
},
Expand All @@ -29,7 +29,7 @@
"@types/mocha": "^10.0.1",
"@types/node": "^18.16.3",
"@types/test-listen": "^1.1.0",
"arlocal": "^1.1.60",
"arlocal": "1.1.62",
"chai": "^4.3.7",
"hollowdb-prover": "^0.1.4",
"mocha": "^10.2.0",
Expand Down
2 changes: 1 addition & 1 deletion examples/micro/src/config/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ export default {
/** Redis URL to connect to. Defaults to `redis://default:redispw@localhost:6379`. */
REDIS_URL: process.env.REDIS_URL || "redis://default:redispw@localhost:6379",
/** Path to Arweave wallet. */
WALLET_PATH: process.env.WALLET_PATH || "./config/wallet.json",
WALLET_PATH: process.env.WALLET || "./config/wallet.json",
/** Log level for underlying Warp. */
WARP_LOG_LEVEL: (process.env.WARP_LOG_LEVEL || "info") as Parameters<typeof LoggerFactory.INST.logLevel>[0],
/** Arweave port for `arlocal`. */
Expand Down
18 changes: 14 additions & 4 deletions examples/micro/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,37 @@
// load env variables (put this at top)
import "dotenv/config";

import http from "http";
import { Redis } from "ioredis";
import { readFileSync } from "fs";
import { existsSync, readFileSync } from "fs";
import { SDK } from "hollowdb";
import { serve } from "micro";
import type { JWKInterface } from "warp-contracts";

import makeServer from "./server";
import config from "./config";
import { makeWarp } from "./util";
import { createCaches } from "./cache";

const contractTxId = process.env.CONTRACT_TXID;
const contractTxId = process.env.CONTRACT;
if (!contractTxId) {
throw new Error("Please provide CONTRACT_TXID environment variable.");
}
if (Buffer.from(contractTxId, "base64").toString("hex").length !== 64) {
throw new Error("Invalid CONTRACT_TXID.");
}
const redisClient = new Redis(config.REDIS_URL, {
lazyConnect: false, // explicitly connect
});
const caches = createCaches(contractTxId, redisClient);
if (!existsSync(config.WALLET_PATH)) {
throw new Error("No wallet found at: " + config.WALLET_PATH);
}
const wallet = JSON.parse(readFileSync(config.WALLET_PATH, "utf-8")) as JWKInterface;
const warp = makeWarp(caches);

const hollowdb = new SDK(wallet, contractTxId, warp);

// module.exports needed by Micro
module.exports = makeServer(hollowdb, contractTxId);
const server = new http.Server(serve(makeServer(hollowdb, contractTxId)));

server.listen(3000);
4 changes: 2 additions & 2 deletions examples/micro/test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ describe("crud operations", () => {
const { contractTxId } = await deploy(owner, warp);

// start the server & connect to the contract
const circuitsDir = __dirname + "../../../config/circuits/hollow-authz-plonk";
prover = new Prover(circuitsDir + "/circuit/hollow-authz.wasm", circuitsDir + "/circuit/prover_key.zkey", "plonk");
const circuitsDir = __dirname + "/../../../config/circuits/hollow-authz-plonk";
prover = new Prover(circuitsDir + "/hollow-authz.wasm", circuitsDir + "/prover_key.zkey", "plonk");
caches = createCaches(contractTxId, redisClient);
warp = makeLocalWarp(config.ARWEAVE_PORT, caches);
const hollowdb = new SDK(owner, contractTxId, warp);
Expand Down
31 changes: 30 additions & 1 deletion examples/micro/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1933,7 +1933,36 @@ argparse@^2.0.1:
resolved "https://registry.yarnpkg.com/argparse/-/argparse-2.0.1.tgz#246f50f3ca78a3240f6c997e8a9bd1eac49e4b38"
integrity sha512-8+9WqebbFzpX9OR+Wa6O29asIogeRMzcGtAINdpMHHyAg10f05aSFVBbcEqGf/PXw1EjAZ+q2/bEBg3DvurK3Q==

arlocal@^1.1.59, arlocal@^1.1.60:
[email protected]:
version "1.1.62"
resolved "https://registry.yarnpkg.com/arlocal/-/arlocal-1.1.62.tgz#aecb7e8bc2e915a7e4be8c82b74a0a694c5a080a"
integrity sha512-BnpolFe3obbQBGKvvkvrAZ4oleLHox7Xcly2l896cHzysPqvo8C9lYofUiQKYcprHIhHH1F7AFLrxAvZq5yQLQ==
dependencies:
"@koa/cors" "^3.1.0"
apollo-server-koa "^3.6.2"
arbundles "^0.6.19"
arweave-bundles "^1.0.3"
asn1.js "5.4.1"
bignumber.js "^9.0.2"
bytes "^3.1.2"
graphql "^16.2.0"
knex "^0.95.6"
koa "^2.13.4"
koa-body "^4.2.0"
koa-bodyparser "^4.3.0"
koa-json "^2.0.2"
koa-logger "^3.2.1"
koa-router "^10.1.1"
lodash "^4.17.21"
mime "^3.0.0"
minimist "^1.2.5"
moment "^2.29.1"
rfc4648 "^1.5.1"
smartweave "^0.4.46"
sqlite3 "^5.0.3"
tsc-watch "^4.6.0"

arlocal@^1.1.59:
version "1.1.64"
resolved "https://registry.yarnpkg.com/arlocal/-/arlocal-1.1.64.tgz#153f6bdb8fdc8840813f4d147ac60d4c50d1b81a"
integrity sha512-623LT99n+TVYYO7USXaETN+uTOcZOWxMbMtdnlQ6uGWiroGJzu13jSsB0w3Q0JHsaiOwIHG5tZFNVUpNCplrNQ==
Expand Down

0 comments on commit 00d0a0d

Please sign in to comment.