intro: 4-ipfs-code-execution (#15)

This commit is contained in:
folex
2021-07-21 11:37:25 +03:00
committed by GitHub
parent 5ecce263a6
commit e5e0d59835
35 changed files with 31554 additions and 0 deletions

View File

@ -0,0 +1,23 @@
# See https://help.github.com/articles/ignoring-files/ for more about ignoring files.
# dependencies
/node_modules
/.pnp
.pnp.js
# testing
/coverage
# production
/build
# misc
.DS_Store
.env.local
.env.development.local
.env.test.local
.env.production.local
npm-debug.log*
yarn-debug.log*
yarn-error.log*

View File

@ -0,0 +1,2 @@
# Getting Started with Fluence

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,53 @@
{
"name": "ipfs-aqua-browser",
"version": "0.1.0",
"private": true,
"dependencies": {
"@fluencelabs/aqua-ipfs": "0.1.8",
"@fluencelabs/ipfs-execution": "file:../aqua",
"@fluencelabs/fluence": "0.9.53",
"@fluencelabs/fluence-network-environment": "1.0.10",
"ipfs-http-client": "^50.1.2",
"@testing-library/jest-dom": "^5.14.1",
"@testing-library/react": "^11.2.7",
"@testing-library/user-event": "^12.8.3",
"@types/jest": "^26.0.24",
"@types/node": "^12.20.16",
"@types/react": "^17.0.14",
"@types/react-dom": "^17.0.9",
"react": "^17.0.2",
"react-dom": "^17.0.2",
"react-scripts": "4.0.3",
"typescript": "^4.3.5",
"web-vitals": "^1.1.2"
},
"scripts": {
"prestart": "(cd ../aqua; npm run build)",
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject"
},
"eslintConfig": {
"extends": [
"react-app",
"react-app/jest"
]
},
"browserslist": {
"production": [
">0.2%",
"not dead",
"not op_mini all"
],
"development": [
"last 1 chrome version",
"last 1 firefox version",
"last 1 safari version"
]
},
"devDependencies": {
"chokidar-cli": "^2.1.0",
"node-sass": "^6.0.1"
}
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 15 KiB

View File

@ -0,0 +1,39 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<link rel="icon" href="%PUBLIC_URL%/favicon.ico" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<meta name="theme-color" content="#000000" />
<link rel="apple-touch-icon" href="%PUBLIC_URL%/logo192.png" />
<!--
manifest.json provides metadata used when your web app is installed on a
user's mobile device or desktop. See https://developers.google.com/web/fundamentals/web-app-manifest/
-->
<link rel="manifest" href="%PUBLIC_URL%/manifest.json" />
<!--
Notice the use of %PUBLIC_URL% in the tags above.
It will be replaced with the URL of the `public` folder during the build.
Only files inside the `public` folder can be referenced from the HTML.
Unlike "/favicon.ico" or "favicon.ico", "%PUBLIC_URL%/favicon.ico" will
work correctly both with client-side routing and a non-root public URL.
Learn how to configure a non-root public URL by running `npm run build`.
-->
<title>Fluence getting started</title>
</head>
<body>
<noscript>You need to enable JavaScript to run this app.</noscript>
<div id="root"></div>
<!--
This HTML file is a template.
If you open it directly in the browser, you will see an empty page.
You can add webfonts, meta tags, or analytics to this file.
The build step will place the bundled scripts into the <body> tag.
To begin the development, run `npm start` or `yarn start`.
To create a production bundle, use `npm run build` or `yarn build`.
-->
</body>
</html>

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.9 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 14 KiB

View File

@ -0,0 +1,25 @@
{
"short_name": "React App",
"name": "Create React App Sample",
"icons": [
{
"src": "favicon.ico",
"sizes": "64x64 32x32 24x24 16x16",
"type": "image/x-icon"
},
{
"src": "logo192.png",
"type": "image/png",
"sizes": "192x192"
},
{
"src": "logo512.png",
"type": "image/png",
"sizes": "512x512"
}
],
"start_url": ".",
"display": "standalone",
"theme_color": "#000000",
"background_color": "#ffffff"
}

View File

@ -0,0 +1,3 @@
# https://www.robotstxt.org/robotstxt.html
User-agent: *
Disallow:

View File

@ -0,0 +1,202 @@
$color1: black;
$color2: rgb(214, 214, 214);
$accent-color: rgb(225, 30, 90);
.logo {
height: 15vmin;
pointer-events: none;
}
.mono {
font-family: monospace, monospace;
}
.bold {
font-weight: bold;
}
header {
margin-top: 10vmin;
}
header,
h1,
h2,
h3 {
text-align: center;
}
.content {
width: 800px;
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
align-content: center;
margin-left: auto;
margin-right: auto;
}
ul,
li {
list-style-type: none;
margin: 0;
padding: 0;
}
.p {
width: 550px;
}
.btn-clipboard {
border: none;
background-color: transparent;
cursor: pointer;
&:hover,
&:focus {
color: $accent-color;
}
&:focus {
outline: none;
}
}
.btn {
height: 26px;
border: 1px solid;
border-color: $color2;
background-color: transparent;
margin: 5px;
font-size: 16px;
color: $color1;
&::placeholder {
color: $color2;
}
&:hover,
&:focus {
outline: 1px solid white;
border-color: $accent-color;
color: $accent-color;
}
}
.btn-hello {
width: 200px;
display: inline;
float: right;
}
table {
text-align: right;
}
.label {
width: 70px;
display: inline-block;
}
.input {
width: 500px;
height: 26px;
box-sizing: border-box;
margin: 5px;
border: 1px solid;
border-color: $color2;
color: $color1;
&::placeholder {
color: $color2;
}
&:hover,
&:focus {
outline: 1px solid white;
border-color: $accent-color;
}
}
.gg-clipboard {
box-sizing: border-box;
position: relative;
display: block;
transform: scale(var(--ggs, 1));
width: 18px;
height: 18px;
border: 2px solid;
border-radius: 2px;
}
.gg-clipboard::after,
.gg-clipboard::before {
content: "";
display: block;
box-sizing: border-box;
position: absolute;
border-radius: 2px;
width: 10px;
left: 2px;
}
.gg-clipboard::before {
border: 2px solid;
border-bottom-left-radius: 3px;
border-bottom-right-radius: 3px;
top: -2px;
height: 6px;
}
.gg-clipboard::after {
height: 2px;
background: currentColor;
box-shadow: 0 -4px 0 0;
bottom: 2px;
}
.gg-trash {
box-sizing: border-box;
position: relative;
display: block;
transform: scale(var(--ggs,1));
width: 10px;
height: 12px;
border: 2px solid transparent;
box-shadow:
0 0 0 2px,
inset -2px 0 0,
inset 2px 0 0;
border-bottom-left-radius: 1px;
border-bottom-right-radius: 1px;
margin-top: 4px
}
.gg-trash::after,
.gg-trash::before {
content: "";
display: block;
box-sizing: border-box;
position: absolute
}
.gg-trash::after {
background: currentColor;
border-radius: 3px;
width: 16px;
height: 2px;
top: -4px;
left: -5px
}
.gg-trash::before {
width: 10px;
height: 4px;
border: 2px solid;
border-bottom: transparent;
border-top-left-radius: 2px;
border-top-right-radius: 2px;
top: -7px;
left: -2px
}

View File

@ -0,0 +1,304 @@
import React, { useState } from "react";
import logo from "./logo.svg";
import "./App.scss";
import { createClient, FluenceClient } from "@fluencelabs/fluence";
import { get_external_api_multiaddr } from "@fluencelabs/aqua-ipfs";
import { stage } from "@fluencelabs/fluence-network-environment";
import { deploy_service, get_file_size, remove_service, provideFile } from "@fluencelabs/ipfs-execution";
const { create, globSource, urlSource, CID } = require('ipfs-http-client');
const relayNodes = [stage[0], stage[1], stage[2]];
const copyToClipboard = (text: string) => {
navigator.clipboard.writeText(text);
};
function App() {
const [client, setClient] = useState<FluenceClient | null>(null);
const [serviceId, setServiceId] = useState<string | null>(null);
const [peerIdInput, setPeerIdInput] = useState<string>("");
const [relayPeerIdInput, setRelayPeerIdInput] = useState<string>("");
const [wasm, setWasm] = useState<string | null>("QmVg9EnanAbwTuEqjjuc1R2uf3AdtEkrNagSifQMkHfyNU");
const [rpcAddr, setRpcAddr] = useState<string | null>("");
const [fileCID, setFileCID] = useState<string>("");
const [fileSize, setFileSize] = useState<string | null>(null);
const isConnected = client !== null;
const gotRpcAddr = rpcAddr !== null;
const deployed = serviceId !== null;
const connect = async (relayPeerId: string) => {
try {
let client = await createClient(relayPeerId);
setClient(client);
await getRpcAddr(client);
} catch (err) {
console.log("Client initialization failed", err);
}
};
const getRpcAddr = async (client: FluenceClient) => {
if (client === null) {
console.log("getRpcAddr client is null");
return;
}
let result = await get_external_api_multiaddr(client, client.relayPeerId!);
console.log("getRpcAddr result", result);
let rpcAddr = result.multiaddr;
setRpcAddr(rpcAddr);
}
const deployService = async () => {
console.log("wasm %s rpcAddr %s", wasm, rpcAddr);
if (client === null || wasm === null || rpcAddr === null) {
return;
}
let service_id = await deploy_service(
client,
client.relayPeerId!, wasm, rpcAddr,
(msg, value) => console.log(msg, value),
{ ttl: 10000 }
);
setServiceId(service_id);
};
const getFileSize = async () => {
if (client === null || serviceId === null || rpcAddr === null) {
return;
}
let size = await get_file_size(client, client.relayPeerId!, fileCID, rpcAddr, serviceId, { ttl: 10000 });
if (size.success) {
setFileSize(size.size.toString());
} else {
setFileSize("Error: " + size.error);
}
};
const removeService = async () => {
if (client === null || serviceId === null) {
return;
}
await remove_service(client, client.relayPeerId!, serviceId, { ttl: 10000 });
setServiceId(null);
};
console.log("isConnected gotRpcAddr deployed\n", isConnected, gotRpcAddr, deployed);
if (!isConnected) {
return (<div className="App">
<header>
<img src={logo} className="logo" alt="logo" />
</header>
<div className="content">
<>
<h1>Pick a relay</h1>
<ul>
{relayNodes.map((x) => (
<li key={x.peerId}>
<span className="mono">{x.peerId}</span>
<button className="btn" onClick={async () => await connect(x.multiaddr)}>
Connect
</button>
</li>
))}
</ul>
</>
</div>
</div>
);
} else if (isConnected && gotRpcAddr && !deployed) {
return (
<div className="App">
<header>
<img src={logo} className="logo" alt="logo" />
</header>
<div className="content">
<>
<h1>Connected</h1>
<table>
<tr>
<td className="bold">Peer id:</td>
<td className="mono">{client!.selfPeerId}</td>
<td>
<button
className="btn-clipboard"
onClick={() => copyToClipboard(client!.selfPeerId)}
>
<i className="gg-clipboard"></i>
</button>
</td>
</tr>
<tr>
<td className="bold">Relay peer id:</td>
<td className="mono">{client!.relayPeerId}</td>
<td>
<button
className="btn-clipboard"
onClick={() => copyToClipboard(client!.relayPeerId!)}
>
<i className="gg-clipboard"></i>
</button>
</td>
</tr>
<tr>
<td className="bold">IPFS RPC:</td>
<td className="mono">{rpcAddr?.substring(0, 49) + "..."}</td>
<td>
<button
className="btn-clipboard"
onClick={() => copyToClipboard(rpcAddr!)}
>
<i className="gg-clipboard"></i>
</button>
</td>
</tr>
</table>
<div>
<div className="row">
<h2>Set process_files.wasm module CID</h2>
<p className="p">
To deploy a service, specify CID of WebAssembly module.
</p>
<input
className="input"
type="text"
onChange={(e) => setWasm(e.target.value)}
value={wasm!}
/>
</div>
</div>
<div>
<h2>Deploy ProcessFiles service</h2>
<p className="p">
process_files.wasm will be downloaded to the Fluence node,
and then a service will be dynamically created from it!
After that, you will be able to use that service to get sizes of IPFS files!
</p>
<div className="row">
<button className="btn btn-hello" onClick={deployService}>
deploy service
</button>
</div>
</div>
</>
</div>
</div>
)
} else if (deployed) {
return (
<div className="App">
<header>
<img src={logo} className="logo" alt="logo" />
</header>
<div className="content">
<>
<h1>Deployed</h1>
<table>
<tr>
<td className="bold">Peer id:</td>
<td className="mono">{client!.selfPeerId}</td>
<td>
<button
className="btn-clipboard"
onClick={() => copyToClipboard(client!.selfPeerId)}
>
<i className="gg-clipboard"></i>
</button>
</td>
</tr>
<tr>
<td className="bold">Relay peer id:</td>
<td className="mono">{client!.relayPeerId}</td>
<td>
<button
className="btn-clipboard"
onClick={() => copyToClipboard(client!.relayPeerId!)}
>
<i className="gg-clipboard"></i>
</button>
</td>
</tr>
<tr>
<td className="bold">process_files.wasm CID:</td>
<td className="mono">{wasm}</td>
<td>
<button
className="btn-clipboard"
onClick={() => copyToClipboard(wasm!)}
>
<i className="gg-clipboard"></i>
</button>
</td>
</tr>
<tr>
<td className="bold">ProcessFiles service ID:</td>
<td className="mono">{serviceId}</td>
<button
className="btn-clipboard"
onClick={() => removeService()}
>
<i className="gg-trash"></i>
</button>
</tr>
<tr>
<td className="bold">File Size:</td>
<td className="mono">{fileSize}</td>
</tr>
</table>
<div>
<h2>Get file size</h2>
<p className="p">
Upload any file to IPFS node
<p><b>{ rpcAddr }</b></p>
paste CID here and get the size of that file via ProcessFiles service you have just deployed
</p>
<div className="row">
<label className="label bold">IPFS CID</label>
<input
className="input"
type="text"
onChange={(e) => setFileCID(e.target.value)}
value={fileCID}
/>
</div>
<div className="row">
<button className="btn btn-hello" onClick={getFileSize} >
get size
</button>
</div>
<div className="row">
<label className="label bold">File Size:</label>
<label className="mono"> {fileSize}</label>
</div>
</div>
</>
</div>
</div>
)
} else {
return (
<div className="App">
<header>
<img src={logo} className="logo" alt="logo" />
</header>
<div className="content">
<>
<h2>Unimplemented! isConnected {isConnected} deployed {deployed} wasm {wasm} </h2>
</>
</div>
</div>
)
}
}
export default App;

View File

@ -0,0 +1,13 @@
body {
margin: 0;
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'Roboto', 'Oxygen',
'Ubuntu', 'Cantarell', 'Fira Sans', 'Droid Sans', 'Helvetica Neue',
sans-serif;
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
}
code {
font-family: source-code-pro, Menlo, Monaco, Consolas, 'Courier New',
monospace;
}

View File

@ -0,0 +1,12 @@
import React from 'react';
import ReactDOM from 'react-dom';
import './index.css';
import App from './App';
ReactDOM.render(
<React.StrictMode>
<App />
</React.StrictMode>,
document.getElementById('root')
);

File diff suppressed because one or more lines are too long

After

Width:  |  Height:  |  Size: 11 KiB

View File

@ -0,0 +1 @@
/// <reference types="react-scripts" />

View File

@ -0,0 +1,26 @@
{
"compilerOptions": {
"target": "es5",
"lib": [
"dom",
"dom.iterable",
"esnext"
],
"allowJs": true,
"skipLibCheck": true,
"esModuleInterop": true,
"allowSyntheticDefaultImports": true,
"strict": true,
"forceConsistentCasingInFileNames": true,
"noFallthroughCasesInSwitch": true,
"module": "esnext",
"moduleResolution": "node",
"resolveJsonModule": true,
"isolatedModules": true,
"noEmit": true,
"jsx": "react-jsx"
},
"include": [
"src"
]
}