From 7d04a902537fa2639a74bc06fc83453ed61c70bf Mon Sep 17 00:00:00 2001 From: dc-andysign <58742260+dc-andysign@users.noreply.github.com> Date: Wed, 8 May 2024 15:37:58 +0100 Subject: [PATCH] HyP-183 Offline compatible (#140) * Added onerror fallback functions * Removed react and react-dom from webpack config and from index html. * Change to digicatapult/sqnc-hyproof-api:v0.13.25 , knex@3.1.0 , sqnc-process-management@2.2.30 * Changed to digicatapult/sqnc-identity-service:v1.12.0 * Change to digicatapult/sqnc-ipfs:v2.10.20 * Changed to digicatapult/sqnc-node:v11.3.0 and added open ports to view the blockchain w/ polkadot js * Changed to digicatapult/veritable-cloudagent:v0.5.1 * Added fall back block of code for off line mode * Init script small fixes * Update readme with non-production notice * Bumped up version * Removed some left over comments * Bumped up version * Removed comments * Added the new versions in order to allow docker to pull the right versions for all the images it needs. * Updated process manager * Fixed incorrect url * Changed the code and added the then catch pattern to seed up the init script process to under 1 min. * Moved get hardcoded factor out of then catch * Better hardcoded factor function * Added options as second arg to fetch issuance call * Added issueAndWaitForComplete function * Removed comments * UpdatedApi * Changed process manager ver * UpdatedIdentitiImage * UpdatedIpfsImg --------- Co-authored-by: rmlearney-digicatapult --- README.md | 20 ++---------- docker/hyproof-apis.yml | 14 ++++---- docker/identity-services.yml | 6 ++-- docker/ipfs.yml | 6 ++-- docker/nodes.yml | 18 +++++++++-- docker/veritable.yml | 6 ++-- package-lock.json | 4 +-- package.json | 4 +-- scripts/initialise_demo.mjs | 59 +++++++++++++++++++++++++--------- src/index.html | 2 -- src/pages/CertificateViewer.js | 19 ++++++++++- webpack.config.js | 5 +-- 12 files changed, 100 insertions(+), 63 deletions(-) diff --git a/README.md b/README.md index de7bbd4b..60f6e2ed 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,7 @@ # SQNC HyProof Client +# This repository is not production software. It should be used for investigation and demonstration purposes only + This repository contains the code for the HyProof front-end (HyProof Client). Instructions for running the **Demonstrator** can be found in `/demo/hyproof-demo.md` @@ -10,14 +12,11 @@ This repository contains the front-end for the **HyProof** proof of concept proj **HyProof** is a DLT-based _MVP_ delivered as part of the wider **HII Seed** initiative which aims to push the boundaries when it comes to building the socio-technical acceptability and business implications of a fully digitised UK certification infrastructure for verifiable hydrogen provenance ( e.g: green hydrogen, etc ). ---- ## Configuration At the moment, this project is configured to use environment variables in a _dot env_ ( `.env` ) file at root level ( although no env variables are need at the moment ). ---- - ## Installation ```sh @@ -26,7 +25,6 @@ npm i Installs dependencies from **`package.json`**. ---- ## Usage @@ -39,8 +37,6 @@ Runs the application using the default port. Open **[http://localhost:3000](http://localhost:3000)** to view it in the Browser. ---- - ## Dev Usage ```sh @@ -51,8 +47,6 @@ Runs the application using the default port in dev mode. Open **[http://localhost:3000](http://localhost:3000)** to view it in the Browser. ---- - ## Build ```sh @@ -61,22 +55,14 @@ npm run build Builds the app for production to the build folder. ---- - ## Assets -In terms of assets, this project contains the following: +This project contains the following assets: * fonts are stored in **[src/assets/fonts](./src/assets/fonts/)** and imported globally in **[src/index.js](./src/index.js)** * images are stored in **[src/assets/images](./src/assets/images/)** and loaders configured in **[webpack.config](./webpack.config)** ---- - ## License This project is licensed under the **Apache 2.0** license. - ---- - ---- diff --git a/docker/hyproof-apis.yml b/docker/hyproof-apis.yml index 74a6b1fe..7d6d731f 100644 --- a/docker/hyproof-apis.yml +++ b/docker/hyproof-apis.yml @@ -19,11 +19,11 @@ services: hydrogen-producer-hyproof-api: container_name: hydrogen-producer-hyproof-api - image: digicatapult/sqnc-hyproof-api:latest + image: digicatapult/sqnc-hyproof-api:v0.13.36 command: /bin/sh -c " sleep 10 && - npx knex migrate:latest --knexfile build/lib/db/knexfile && - npx @digicatapult/sqnc-process-management@latest create -h hydrogen-producer-node -p 9944 -u //Alice -f ./processFlows.json && + npx knex@3.1.0 migrate:latest --knexfile build/lib/db/knexfile && + npx @digicatapult/sqnc-process-management@2.2.37 create -h hydrogen-producer-node -p 9944 -u //Alice -f ./processFlows.json && npm start" environment: - PORT=8000 @@ -77,10 +77,10 @@ services: energy-owner-hyproof-api: container_name: energy-owner-hyproof-api - image: digicatapult/sqnc-hyproof-api:latest + image: digicatapult/sqnc-hyproof-api:v0.13.36 command: /bin/sh -c " sleep 10 && - npx knex migrate:latest --knexfile build/lib/db/knexfile && + npx knex@3.1.0 migrate:latest --knexfile build/lib/db/knexfile && npm start" environment: - PORT=8010 @@ -134,10 +134,10 @@ services: regulator-hyproof-api: container_name: regulator-hyproof-api - image: digicatapult/sqnc-hyproof-api:latest + image: digicatapult/sqnc-hyproof-api:v0.13.36 command: /bin/sh -c " sleep 10 && - npx knex migrate:latest --knexfile build/lib/db/knexfile && + npx knex@3.1.0 migrate:latest --knexfile build/lib/db/knexfile && npm start" environment: - PORT=8020 diff --git a/docker/identity-services.yml b/docker/identity-services.yml index 167a9605..c804730d 100644 --- a/docker/identity-services.yml +++ b/docker/identity-services.yml @@ -18,7 +18,7 @@ services: networks: ['hydrogen-producer'] hydrogen-producer-identity: - image: digicatapult/sqnc-identity-service:latest + image: digicatapult/sqnc-identity-service:v1.12.5 container_name: hydrogen-producer-identity command: /bin/sh -c " sleep 10 && @@ -66,7 +66,7 @@ services: networks: ['energy-owner'] energy-owner-identity: - image: digicatapult/sqnc-identity-service:latest + image: digicatapult/sqnc-identity-service:v1.12.5 container_name: energy-owner-identity command: /bin/sh -c " sleep 10 && @@ -114,7 +114,7 @@ services: networks: ['regulator'] regulator-identity: - image: digicatapult/sqnc-identity-service:latest + image: digicatapult/sqnc-identity-service:v1.12.5 container_name: regulator-identity command: /bin/sh -c " sleep 10 && diff --git a/docker/ipfs.yml b/docker/ipfs.yml index 1edb8c2c..9f3c6ad8 100644 --- a/docker/ipfs.yml +++ b/docker/ipfs.yml @@ -9,7 +9,7 @@ services: ################################################################################## hydrogen-producer-ipfs: - image: digicatapult/sqnc-ipfs:latest + image: digicatapult/sqnc-ipfs:v2.10.25 container_name: hydrogen-producer-ipfs depends_on: hydrogen-producer-node: @@ -39,7 +39,7 @@ services: ################################################################################## energy-owner-ipfs: - image: digicatapult/sqnc-ipfs:latest + image: digicatapult/sqnc-ipfs:v2.10.25 container_name: energy-owner-ipfs depends_on: energy-owner-node: @@ -69,7 +69,7 @@ services: ################################################################################## regulator-ipfs: - image: digicatapult/sqnc-ipfs:latest + image: digicatapult/sqnc-ipfs:v2.10.25 container_name: regulator-ipfs depends_on: regulator-node: diff --git a/docker/nodes.yml b/docker/nodes.yml index 134e5063..8dd39256 100644 --- a/docker/nodes.yml +++ b/docker/nodes.yml @@ -9,13 +9,17 @@ services: ################################################################################## hydrogen-producer-node: - image: digicatapult/sqnc-node:latest + image: digicatapult/sqnc-node:v11.3.0 container_name: hydrogen-producer-node command: --base-path /data --alice --unsafe-rpc-external --rpc-cors all --node-key 0000000000000000000000000000000000000000000000000000000000000001 + ports: + - 30333:30333 + - 9944:9944 + - 9933:9933 volumes: - hydrogen-producer-node:/data restart: on-failure @@ -26,7 +30,7 @@ services: ################################################################################## energy-owner-node: - image: digicatapult/sqnc-node:latest + image: digicatapult/sqnc-node:v11.3.0 container_name: energy-owner-node command: --base-path /data/ --bob @@ -34,6 +38,10 @@ services: --rpc-cors all --node-key 0000000000000000000000000000000000000000000000000000000000000002 --bootnodes /dns4/alice/tcp/30333/p2p/12D3KooWEyoppNCUx8Yx66oV9fJnriXwCcXwDDUA2kj6vnc6iDEp + ports: + - 31333:30333 + - 10044:9944 + - 10033:9933 volumes: - energy-owner-node:/data restart: on-failure @@ -44,7 +52,7 @@ services: ################################################################################## regulator-node: - image: digicatapult/sqnc-node:latest + image: digicatapult/sqnc-node:v11.3.0 container_name: regulator-node command: --base-path /data/ --charlie @@ -52,6 +60,10 @@ services: --rpc-cors all --node-key 0000000000000000000000000000000000000000000000000000000000000003 --bootnodes /dns4/alice/tcp/30333/p2p/12D3KooWEyoppNCUx8Yx66oV9fJnriXwCcXwDDUA2kj6vnc6iDEp + ports: + - 32333:30333 + - 10144:9944 + - 10133:9933 volumes: - regulator-node:/data restart: on-failure diff --git a/docker/veritable.yml b/docker/veritable.yml index 6eef6445..cae82b6f 100644 --- a/docker/veritable.yml +++ b/docker/veritable.yml @@ -9,7 +9,7 @@ services: ################################################################################## hydrogen-producer-veritable: - image: digicatapult/veritable-cloudagent:latest + image: digicatapult/veritable-cloudagent:v0.6.0 container_name: hydrogen-producer-veritable depends_on: hydrogen-producer-ipfs: @@ -38,7 +38,7 @@ services: ################################################################################## energy-owner-veritable: - image: digicatapult/veritable-cloudagent:latest + image: digicatapult/veritable-cloudagent:v0.6.0 container_name: energy-owner-veritable depends_on: energy-owner-ipfs: @@ -67,7 +67,7 @@ services: ################################################################################## regulator-veritable: - image: digicatapult/veritable-cloudagent:latest + image: digicatapult/veritable-cloudagent:v0.6.0 container_name: regulator-veritable depends_on: regulator-ipfs: diff --git a/package-lock.json b/package-lock.json index 1591ec78..576283ed 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "@digicatapult/sqnc-hyproof-client", - "version": "0.5.38", + "version": "0.5.39", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "@digicatapult/sqnc-hyproof-client", - "version": "0.5.38", + "version": "0.5.39", "license": "Apache-2.0", "dependencies": { "@digicatapult/ui-component-library": "^0.19.30", diff --git a/package.json b/package.json index 3deb601f..300b01ae 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@digicatapult/sqnc-hyproof-client", - "version": "0.5.38", + "version": "0.5.39", "description": "User interface for HyProof", "homepage": "https://github.com/digicatapult/sqnc-hyproof-client", "main": "src/index.js", @@ -11,7 +11,7 @@ "init": "./scripts/initialise_demo.mjs", "dev": "webpack-dev-server --mode=development", "start": "serve -s ./build", - "build": "./node_modules/.bin/webpack --mode=production", + "build": "$(npm root)/.bin/webpack --mode=production", "depcheck": "depcheck", "test": "echo \"Error: no test specified\" && exit 1" }, diff --git a/scripts/initialise_demo.mjs b/scripts/initialise_demo.mjs index a9f70028..cf185cd1 100755 --- a/scripts/initialise_demo.mjs +++ b/scripts/initialise_demo.mjs @@ -119,7 +119,7 @@ async function waitForCertificateState(certificateId, state, port) { if (body.state === state) { return body } - await await delay(100) + await delay(100) } const message = `Timeout waiting for state ${state} on certificate ${certificateId} for port ${port}` console.error(message) @@ -209,29 +209,56 @@ async function issueCertificate( } const issueEndpoint = `http://localhost:${energyProviderPort}/v1/certificate/${id}/issuance` - const issueResult = await fetch(issueEndpoint, { + + const carbonIntensityApiUrl = `https://api.carbonintensity.org.uk/intensity/${new Date(production_end_time).toISOString()}/${new Date(production_start_time).toISOString()}` + + const hardcodedFactor = (gap) => Math.random() * (gap[1] - gap[0]) + gap[0] + const hardcodedEco2 = (e) => Math.floor(hardcodedFactor([0.03, 0.11]) * e) + + const defaultCertOptions = { method: 'POST', headers: { 'content-type': 'application/json', }, body: JSON.stringify({}), - }) - if (!issueResult.ok) { - const message = `Error issuing certificate ${id} on port ${energyProviderPort}. Error was ${issueResult.statusText}` - console.error(message) - throw new Error(message) } - const waitForComplete = async () => { - await waitForCertificateState(id, 'issued', energyProviderPort) - const finalCert = await waitForCertificateState( - id, - 'issued', - hydrogenProducerPort - ) - return finalCert + const hardcodedCertOptions = { + method: 'POST', + headers: { + 'content-type': 'application/json', + }, + body: JSON.stringify({ + embodied_co2: hardcodedEco2(energy_consumed_wh), + }), + } + + const issueAndWaitForComplete = async (options) => { + const issueResult = await fetch(issueEndpoint, options) + if (!issueResult.ok) { + const message = `Error issuing certificate ${id} on port ${energyProviderPort}. Error was ${issueResult.statusText}` + console.error(message) + throw new Error(message) + } + const waitForComplete = async () => { + await waitForCertificateState(id, 'issued', energyProviderPort) + const finalCert = await waitForCertificateState( + id, + 'issued', + hydrogenProducerPort + ) + return finalCert + } + return waitForComplete + } + + try { + await fetch(carbonIntensityApiUrl) + return issueAndWaitForComplete(defaultCertOptions) + } catch (e) { + console.log('Detected off-line mode when using fetch. Using random vals.') + return issueAndWaitForComplete(hardcodedCertOptions) } - return waitForComplete } const healthChecks = await Promise.all([ diff --git a/src/index.html b/src/index.html index ff9bf3be..a57f659a 100644 --- a/src/index.html +++ b/src/index.html @@ -1,8 +1,6 @@ - - diff --git a/src/pages/CertificateViewer.js b/src/pages/CertificateViewer.js index 52726c67..56995a30 100644 --- a/src/pages/CertificateViewer.js +++ b/src/pages/CertificateViewer.js @@ -137,7 +137,24 @@ export default function CertificateViewer() { if (resLocal?.state !== 'initiated') return url = `${origin}/v1/certificate/${id}/issuance` - body = {} + const carbonIntensityApiUrl = `https://api.carbonintensity.org.uk/intensity/${new Date(start).toISOString()}/${new Date(end).toISOString()}` + const checkCarbonIntensityAPI = async (u) => { + try { + const result = await fetch(u) + return result.ok + } catch (e) { + return false + } + } + const getHardcodedFactor = () => { + const factorLimits = [0.03, 0.11] + const rnd = Math.random() + return rnd * (factorLimits[1] - factorLimits[0]) + factorLimits[0] + } + const getHardcodedEco2 = (e) => Math.floor(getHardcodedFactor() * e) + body = (await checkCarbonIntensityAPI(carbonIntensityApiUrl)) + ? {} + : { embodied_co2: getHardcodedEco2(energy) } const resChain = await fetchCert({ url, body }) if (resChain?.state !== 'submitted') return diff --git a/webpack.config.js b/webpack.config.js index 16b58069..7d319ee1 100644 --- a/webpack.config.js +++ b/webpack.config.js @@ -74,10 +74,7 @@ module.exports = (vars) => { analyzerMode: 'disabled', // disable analyser. Replace with "server" or "static" to render analyser }), ], - externals: { - react: 'React', - 'react-dom': 'ReactDOM', - }, + externals: {}, } }