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

8e8b2c/emissions example #113

Merged
merged 8 commits into from
Sep 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
23 changes: 17 additions & 6 deletions .github/workflows/e2e.yml
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ jobs:
- run: npm run -w @holoom/types build
- run: npm run -w @holoom/client build
- run: npm run -w @holoom/authority build
- run: npm run -w @holoom/sandbox build

- uses: actions/upload-artifact@v4
with:
Expand All @@ -78,6 +79,11 @@ jobs:
name: authority-dist
path: packages/authority/dist
retention-days: 1
- uses: actions/upload-artifact@v4
with:
name: sandbox-dist
path: packages/sandbox/dist
retention-days: 1

tryorama-tests:
name: Tryorama Tests
Expand Down Expand Up @@ -148,21 +154,22 @@ jobs:
- name: Move DNA
run: mv happ_workdir/holoom.dna ./holoom.dna

- name: Download types dist
uses: actions/download-artifact@v4
- uses: actions/download-artifact@v4
with:
name: types-dist
path: packages/types/dist
- name: Download client dist
uses: actions/download-artifact@v4
- uses: actions/download-artifact@v4
with:
name: client-dist
path: packages/client/dist
- name: Download external-id-attestor dist
uses: actions/download-artifact@v4
- uses: actions/download-artifact@v4
with:
name: authority-dist
path: packages/authority/dist
- uses: actions/download-artifact@v4
with:
name: sandbox-dist
path: packages/sandbox/dist

- name: Release binaries
uses: softprops/action-gh-release@v2
Expand All @@ -185,3 +192,7 @@ jobs:
run: npm publish --access public -w @holoom/authority
env:
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
- name: Publish sandbox to npm
run: npm publish --access public -w @holoom/sandbox
env:
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
36 changes: 36 additions & 0 deletions examples/emissions/co2-sensor/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# We use ubuntu as it's glibc version is compatible with the prebuilt binaries
FROM ubuntu

RUN apt-get update && apt-get install -y wget

# Install node v20.12.2
ENV NVM_DIR /usr/local/nvm
ENV NODE_VERSION v20.12.2
RUN mkdir -p $NVM_DIR && wget -qO- https://raw.githubusercontent.com/nvm-sh/nvm/v0.40.1/install.sh | bash
RUN /bin/bash -c "source $NVM_DIR/nvm.sh && nvm install $NODE_VERSION && nvm use --delete-prefix $NODE_VERSION"
ENV NODE_PATH $NVM_DIR/versions/node/$NODE_VERSION/bin
ENV PATH $NODE_PATH:$PATH

# Install prebuilt holochain binaries
RUN wget -nv https://github.com/holochain/holochain/releases/download/holochain-0.4.0-dev.20/hc-x86_64-linux \
-O /usr/local/bin/hc
RUN wget -nv https://github.com/holochain/holochain/releases/download/holochain-0.4.0-dev.20/holochain-x86_64-linux \
-O /usr/local/bin/holochain
RUN wget -nv https://github.com/holochain/holochain/releases/download/holochain-0.4.0-dev.20/lair-keystore-x86_64-linux \
-O /usr/local/bin/lair-keystore
RUN chmod 755 /usr/local/bin/hc /usr/local/bin/holochain /usr/local/bin/lair-keystore

# Install tsx
WORKDIR /home/node
RUN /bin/bash -c "source $NVM_DIR/nvm.sh && npm i tsx"

# Copy the actual server script
COPY ./co2-sensor.ts ./co2-sensor.ts

# So container runs with nvm loaded
SHELL ["/bin/bash", "--login", "-c"]

# The ./packages directory is mounted from the locally built npm workspace. We
# npm install at launch to avoid the need for the package.json to know the
# latest version or features of the workspace.
CMD npm install ./packages/types ./packages/sandbox; npx tsx ./co2-sensor.ts
47 changes: 47 additions & 0 deletions examples/emissions/co2-sensor/co2-sensor.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/**
* A mock CO₂ sensor, that emits a random measure in grams roughly every 2
* seconds. The "measurements" are published by a sandboxed holochain agent
* that serves no other role in the network.
*/

import { ensureAndConnectToHapp } from "@holoom/sandbox";
import { UsernameRegistryCoordinator } from "@holoom/types";

async function main() {
// Create a conductor sandbox (with holoom installed) at the specified
// directory if it doesn't already exist, and connect to it.
const { appWs } = await ensureAndConnectToHapp(
"/sandbox",
"/workdir/holoom.happ",
"emissions-local-test-2024-09-04T12:59",
{
bootstrapServerUrl: new URL("https://bootstrap-0.infra.holochain.org"),
signalingServerUrl: new URL("wss://sbd-0.main.infra.holo.host"),
password: "password",
}
);
const usernameRegistryCoordinator = new UsernameRegistryCoordinator(appWs);

setInterval(() => publishMeasurement(usernameRegistryCoordinator), 1_000);
}

async function publishMeasurement(
usernameRegistryCoordinator: UsernameRegistryCoordinator
) {
const time = Math.floor(Date.now() / 1000);
const name = `co2-sensor/time/${time}`;

// A pretend measure of detected CO₂
const gramsCo2 = Math.floor(Math.random() * 1000);
try {
await usernameRegistryCoordinator.createOracleDocument({
name,
json_data: JSON.stringify({ time, gramsCo2 }),
});
console.log("Published", time, gramsCo2);
} catch (err) {
console.error("Failed to publish reading with error:", err);
}
}

main();
7 changes: 7 additions & 0 deletions examples/emissions/docker-compose.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
services:

co2-sensor:
build: ./co2-sensor
volumes:
- ../../packages:/home/node/packages
- ../../workdir:/workdir
9 changes: 5 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 4 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,10 @@
},
"scripts": {
"build:dna": "scripts/build_dna.sh",
"test:tryorama": "npm run build:dna && npm run prepare-build:types && npm run -w @holoom/client -w @holoom/authority build && npm t -w @holoom/tryorama",
"test:dna": "npm run build:dna && cargo nextest run -j 1",
"prepare-build:types": "rimraf crates/holoom_types/bindings && cargo test --package holoom_types && npm run -w @holoom/types prepare:bindings && npm run -w @holoom/types build",
"build:client": "npm run build -w @holoom/client",
"build:authority": "npm run build -w @holoom/authority",
"test:tryorama": "npm run build:dna && npm run prepare:types && npm run build:packages && npm t -w @holoom/tryorama",
"prepare:types": "rimraf crates/holoom_types/bindings && cargo test --package holoom_types && npm run -w @holoom/types prepare:bindings",
"build:packages": "npm run -w @holoom/types -w @holoom/client -w @holoom/authority -w @holoom/sandbox build",
"example:emissions": "npm run build:packages && cd examples/emissions && docker-compose build && docker-compose up",
"typedoc": "typedoc --options packages/typedoc.json"
},
"devDependencies": {
Expand Down
2 changes: 1 addition & 1 deletion packages/authority/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{
"extends": "../../tsconfig.json",
"include": ["src", "rollup.node.config.ts", "rollup.browser.config.ts"]
}
}
19 changes: 19 additions & 0 deletions packages/sandbox/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
MIT License

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
3 changes: 3 additions & 0 deletions packages/sandbox/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# @holoom/authority

Helpers for creating and connecting to a holochain sandbox with holoom installed.
8 changes: 8 additions & 0 deletions packages/sandbox/build.tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"extends": "../../tsconfig.json",
"compilerOptions": {
"baseUrl": ".",
"declarationDir": "dist/types"
},
"include": ["src"]
}
48 changes: 48 additions & 0 deletions packages/sandbox/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
{
"name": "@holoom/sandbox",
"version": "0.1.0-dev.13",
"description": "Helpers for starting a holochain sandbox with holoom installed",
"type": "module",
"license": "MIT",
"types": "dist/types/index.d.ts",
"exports": {
".": {
"types": "./dist/types/index.d.ts",
"node": {
"require": "./dist/index.node.cjs",
"default": "./dist/index.node.js"
}
}
},
"files": [
"dist/",
"src/",
"LICENSE",
"README.md"
],
"repository": "https://github.com/holochain-open-dev/holoom",
"homepage": "https://github.com/holochain-open-dev/holoom/tree/main/packages/sandbox",
"bugs": {
"url": "https://github.com/holochain-open-dev/holoom.git/issues"
},
"scripts": {
"build": "rimraf dist && npm run build:node",
"build:node": "rollup -c rollup.node.config.ts --configPlugin typescript"
},
"publishConfig": {
"access": "public"
},
"devDependencies": {
"@rollup/plugin-alias": "^5.1.0",
"@rollup/plugin-json": "^6.1.0",
"@rollup/plugin-typescript": "^11.1.6",
"@types/ws": "^8.5.10",
"rimraf": "^5.0.5",
"rollup": "^4.12.0",
"rollup-plugin-cleanup": "^3.2.1"
},
"dependencies": {
"@holochain/client": "^0.18.0-dev",
"yaml": "^2.5.1"
}
}
48 changes: 48 additions & 0 deletions packages/sandbox/rollup.node.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import json from "@rollup/plugin-json";
import typescript from "@rollup/plugin-typescript";
import * as fs from "fs";
import cleanup from "rollup-plugin-cleanup";
import { RollupOptions } from "rollup";

const pkg = JSON.parse(fs.readFileSync("./package.json", "utf-8"));
const banner = `/**
* @module ${pkg.name}
* @version ${pkg.version}
* @file ${pkg.description}
* @license ${pkg.license}
* @see [Github]{@link ${pkg.homepage}}
*/`;

const config: RollupOptions = {
input: "src/index.ts",
output: [
{
file: pkg.exports["."].node.require,
format: "cjs",
banner,
exports: "auto",
},
{
file: pkg.exports["."].node.default,
format: "es",
banner,
},
],
external: [
...Object.keys(pkg.dependencies),
"node:fs/promises",
"node:child_process",
],
plugins: [
typescript({
tsconfig: "./build.tsconfig.json",
}),
cleanup({ comments: "jsdoc" }),
json(),
],
onwarn: (warning) => {
throw new Error(warning.message);
},
};

export default config;
Loading