Fix price-oracle/client-peer (#31)

This commit is contained in:
folex 2021-10-20 12:11:53 +03:00 committed by GitHub
parent ff4f4075b9
commit dfefd0025e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 151 additions and 83 deletions

View File

@ -36,7 +36,7 @@ As outlined in Figure 1, we use one or more services distributed across the Flue
Let's get right to it:
```text
```bash
% cd web
% npm install
% npm start
@ -52,7 +52,7 @@ Please note that the coin name must the full name, e.g., ethereum or bitcoin ins
If you like things a little closer to metal, see the [client-peer](./client-peer) directory for a peer-client based on the Fluence JS-SDK. To run the headless client:
```text
```bash
% cd client-peer
% npm instal
% npm start run
@ -71,9 +71,9 @@ As evident from our results, we are executing two different workflows to get our
```typescript
// client-peer/index.ts
import { createClient, setLogLevel, FluenceClient } from "@fluencelabs/fluence";
import { Fluence } from "@fluencelabs/fluence";
import { krasnodar, Node } from "@fluencelabs/fluence-network-environment";
import { get_price, get_price_par } from "./get_crypto_prices";
import { get_price, get_price_par } from "./_aqua/get_crypto_prices";
interface NodeServicePair {
node: string;
@ -82,37 +82,47 @@ interface NodeServicePair {
// (node, service) tuples, json-style, for price getter services
let getter_topo: Array<NodeServicePair>;
// and a mean service
let mean_topo: NodeServicePair;
getter_topo = Array({ "node": "12D3KooWCMr9mU894i8JXAFqpgoFtx6qnV1LFPSfVc3Y34N4h4LS", "service_id": "c315073d-4311-4db3-be57-8f154f032d28" }, { "node": "12D3KooWFEwNWcHqi9rtsmDhsYcDbRUCDXH84RC4FW6UfsFWaoHi", "service_id": "25f9123a-f386-4cb2-9c1e-bb7c247c9c09" });
mean_topo = { "node": "12D3KooWCMr9mU894i8JXAFqpgoFtx6qnV1LFPSfVc3Y34N4h4LS", "service_id": "dd47389f-25d9-4870-a2a9-909359e73580" };
// description of the services' locations, copypaste from data/deployed_services.json
getter_topo = [
{
node: "12D3KooWCMr9mU894i8JXAFqpgoFtx6qnV1LFPSfVc3Y34N4h4LS",
service_id: "b67586f7-e96f-49ee-914e-9eabe1a0b83d",
},
{
node: "12D3KooWFEwNWcHqi9rtsmDhsYcDbRUCDXH84RC4FW6UfsFWaoHi",
service_id: "f5b456fa-ee18-4df1-b18b-84fe7ebc7ad0",
}
];
mean_topo = [
{
node: "12D3KooWCMr9mU894i8JXAFqpgoFtx6qnV1LFPSfVc3Y34N4h4LS",
service_id: "79b8ddb9-e2e6-4924-9293-c5d55c94af6b",
},
{
node: "12D3KooWFEwNWcHqi9rtsmDhsYcDbRUCDXH84RC4FW6UfsFWaoHi",
service_id: "debecd02-ba7d-40a2-92ab-08a9321da2cf"
}
];
async function main() {
// create the Fluence client for the Krasnodar testnet
await Fluence.start({ connectTo: krasnodar[5] });
// create the Fluence client for the Krasnodar testnet
const fluence = await createClient(krasnodar[2]);
console.log("created a fluence client %s with relay %s", fluence.selfPeerId, fluence.relayPeerId);
// call the get_price function -- sequential processing
const network_result = await get_price(
"ethereum", "usd",
getter_topo[1].node, getter_topo[1].service_id, mean_topo[1].service_id
);
console.log("seq result: ", network_result);
// call the get_price function -- sequential processing
const network_result = await get_price(fluence, "ethereum", "usd", "12D3KooWFEwNWcHqi9rtsmDhsYcDbRUCDXH84RC4FW6UfsFWaoHi", "25f9123a-f386-4cb2-9c1e-bb7c247c9c09", "b2790307-055e-41ca-9640-3c41856d464b");
console.log("seq result: ", network_result);
// call the get_price_par function -- parallel processing
const network_result_par = await get_price_par("ethereum", "usd", getter_topo, mean_topo[0]);
console.log("par result: ", network_result_par);
// call the get_price_par function -- parallel processing
const network_result_par = await get_price_par(fluence, "ethereum", "usd", getter_topo, mean_topo);
console.log("par result: ", network_result_par);
return;
await Fluence.stop();
}
main()
.then(() => process.exit(0))
.catch(error => {
console.error(error);
process.exit(1);
});
```
where the Aqua script can be found in the `aqua-scripts` dirctory and the compiled Aqua code is found in the `get_crypto_prices.ts` file. For more on the Aqua script, see below.
@ -134,7 +144,7 @@ As seen in Figure 3, we link the price_getter module and curl adapter module int
We now have our code in place and area ready to compile and our compilation instructions are contain in the `scripts/build.sh` script, which basically instructs the the code is compiled with `marine` and that the resulting Wasm modules are copied to the `artifacts` directory. In the project directory:
```text
```bash
./scripts/build.sh
```
@ -142,7 +152,7 @@ which gives you the updated Wasm modules in the `artifacts` directory.
The next step is to deploy the two services to one or more peers and we use the `fldist` tool to get this done. First, we need to now what peers are available and we can get an enumeration from:
```text
```bash
fldist env
```
@ -150,24 +160,22 @@ fldist env
Pick any of the peer ids from the listed peers to deploy your services. Let's say we use peer id `12D3KooWFEwNWcHqi9rtsmDhsYcDbRUCDXH84RC4FW6UfsFWaoHi`:
```text
fldist --node-id 12D3KooWFEwNWcHqi9rtsmDhsYcDbRUCDXH84RC4FW6UfsFWaoHi new_service --ms artifacts/curl_adapter.wasm:configs/curl_adapter_cfg.json artifacts/price_getter_service.wasm:configs/price_getter_service_cfg.json --name price-getter-service-0
```bash
$ fldist --node-id 12D3KooWFEwNWcHqi9rtsmDhsYcDbRUCDXH84RC4FW6UfsFWaoHi new_service --ms artifacts/curl_adapter.wasm:configs/curl_adapter_cfg.json artifacts/price_getter_service.wasm:configs/price_getter_service_cfg.json --name price-getter-service-0
service id: f5b456fa-ee18-4df1-b18b-84fe7ebc7ad0 # <--- REMEMBER service id !!
service created successfully
```
to deploy a price-getter service and
```text
fldist --node-id 12D3KooWFEwNWcHqi9rtsmDhsYcDbRUCDXH84RC4FW6UfsFWaoHi new_service --ms artifacts/mean_service.wasm:configs/mean_service_cfg.json --name mean-service-0
```
to deploy a mean service. Please take note of the service-id you get back for each fo the deployments, which are needed to locate the service in the future. For example
```text
fldist --node-id 12D3KooWFEwNWcHqi9rtsmDhsYcDbRUCDXH84RC4FW6UfsFWaoHi new_service --ms artifacts/mean_service.wasm:configs/mean_service_cfg.json --name mean-service-0
service id: b2790307-055e-41ca-9640-3c41856d464b <-- REMEMBER ME !!
```bash
$ fldist --node-id 12D3KooWFEwNWcHqi9rtsmDhsYcDbRUCDXH84RC4FW6UfsFWaoHi new_service --ms artifacts/mean_service.wasm:configs/mean_service_cfg.json --name mean-service-0
service id: debecd02-ba7d-40a2-92ab-08a9321da2cf # <--- REMEMBER service id !!
service created successfully
```
to deploy a mean service. Please take note of the service-id you get back for each fo the deployments, which are needed to locate the service in the future.
That's it for service development and deployment!
## Application Composition with Aqua

View File

@ -50,16 +50,14 @@ func get_price(coin: string, currency: string, node: string, pg_sid: string, mea
func get_price_par(coin: string, currency: string, getter_topo: []NodeServicePair, mean_topo: NodeServicePair) -> Result:
prices: *f64
for topo <- getter_topo par:
on topo.node:
k <- Op.string_to_b58(topo.node)
on topo.node:
PriceGetterService topo.service_id
ts_ms <- Peer.timestamp_ms()
res <- PriceGetterService.price_getter(coin, currency, ts_ms)
prices <- F64Op.identity(res.result)
F64Op.identity(prices!2)
on mean_topo.node:
F64Op.identity(prices!1)
MeanService mean_topo.service_id
result <- MeanService.mean(prices)
<- result

View File

@ -15,7 +15,7 @@
"it-all": "^1.0.5"
},
"devDependencies": {
"@fluencelabs/aqua": "^0.3.0-226",
"@fluencelabs/aqua": "0.3.2-233",
"typescript": "^3.9.5"
}
},
@ -51,14 +51,16 @@
}
},
"node_modules/@fluencelabs/aqua": {
"version": "0.3.0-226",
"resolved": "https://registry.npmjs.org/@fluencelabs/aqua/-/aqua-0.3.0-226.tgz",
"integrity": "sha512-9o0TdgsVNcBvifqo7VqIkN62P9EReE0LUxgWG1rGHi9yxJiGElvEBvrVUzqShffF66Ene7VGEe85lhFIGCfDgg==",
"version": "0.3.2-233",
"resolved": "https://registry.npmjs.org/@fluencelabs/aqua/-/aqua-0.3.2-233.tgz",
"integrity": "sha512-LxwNt/O2ijHA2bG7+qdmnFT//kpIVj7mACBp28LxRpb2kJYMcVDNb9VfyWDiDdoX10PAT96OpkiYvVJPs0mpEw==",
"dev": true,
"dependencies": {
"@fluencelabs/fluence": "0.12.1"
},
"bin": {
"aqua": "index.js",
"aqua-cli": "error.js",
"aqua-j": "index-java.js"
"aqua-cli": "error.js"
}
},
"node_modules/@fluencelabs/aqua-lib": {
@ -66,6 +68,30 @@
"resolved": "https://registry.npmjs.org/@fluencelabs/aqua-lib/-/aqua-lib-0.1.14.tgz",
"integrity": "sha512-H2Q4gIvociUxc4J2mwmH0D+mrU2N2Z+enKCHgBCanMVEE2wZDsZ80GTbDKsQjEq+gpqbnJIk8lJBYW6lyvLJTg=="
},
"node_modules/@fluencelabs/aqua/node_modules/@fluencelabs/fluence": {
"version": "0.12.1",
"resolved": "https://registry.npmjs.org/@fluencelabs/fluence/-/fluence-0.12.1.tgz",
"integrity": "sha512-JrMKMHjYILAHQsLLd5H0fLt/UMZv+/PQYxJYe6h9HFyJlZrN1bUV+EcZnUw1u3DZE5k/RXBx0udfmkahggwrqA==",
"dev": true,
"dependencies": {
"@chainsafe/libp2p-noise": "4.0.0",
"@fluencelabs/avm": "0.14.4",
"async": "3.2.0",
"base64-js": "1.5.1",
"bs58": "4.0.1",
"cids": "0.8.1",
"it-length-prefixed": "3.0.1",
"it-pipe": "1.1.0",
"libp2p": "0.32.3",
"libp2p-crypto": "0.19.7",
"libp2p-mplex": "0.10.4",
"libp2p-websockets": "0.16.1",
"loglevel": "1.7.0",
"multiaddr": "10.0.0",
"peer-id": "0.15.3",
"uuid": "8.3.0"
}
},
"node_modules/@fluencelabs/avm": {
"version": "0.14.4",
"resolved": "https://registry.npmjs.org/@fluencelabs/avm/-/avm-0.14.4.tgz",
@ -2628,10 +2654,39 @@
}
},
"@fluencelabs/aqua": {
"version": "0.3.0-226",
"resolved": "https://registry.npmjs.org/@fluencelabs/aqua/-/aqua-0.3.0-226.tgz",
"integrity": "sha512-9o0TdgsVNcBvifqo7VqIkN62P9EReE0LUxgWG1rGHi9yxJiGElvEBvrVUzqShffF66Ene7VGEe85lhFIGCfDgg==",
"dev": true
"version": "0.3.2-233",
"resolved": "https://registry.npmjs.org/@fluencelabs/aqua/-/aqua-0.3.2-233.tgz",
"integrity": "sha512-LxwNt/O2ijHA2bG7+qdmnFT//kpIVj7mACBp28LxRpb2kJYMcVDNb9VfyWDiDdoX10PAT96OpkiYvVJPs0mpEw==",
"dev": true,
"requires": {
"@fluencelabs/fluence": "0.12.1"
},
"dependencies": {
"@fluencelabs/fluence": {
"version": "0.12.1",
"resolved": "https://registry.npmjs.org/@fluencelabs/fluence/-/fluence-0.12.1.tgz",
"integrity": "sha512-JrMKMHjYILAHQsLLd5H0fLt/UMZv+/PQYxJYe6h9HFyJlZrN1bUV+EcZnUw1u3DZE5k/RXBx0udfmkahggwrqA==",
"dev": true,
"requires": {
"@chainsafe/libp2p-noise": "4.0.0",
"@fluencelabs/avm": "0.14.4",
"async": "3.2.0",
"base64-js": "1.5.1",
"bs58": "4.0.1",
"cids": "0.8.1",
"it-length-prefixed": "3.0.1",
"it-pipe": "1.1.0",
"libp2p": "0.32.3",
"libp2p-crypto": "0.19.7",
"libp2p-mplex": "0.10.4",
"libp2p-websockets": "0.16.1",
"loglevel": "1.7.0",
"multiaddr": "10.0.0",
"peer-id": "0.15.3",
"uuid": "8.3.0"
}
}
}
},
"@fluencelabs/aqua-lib": {
"version": "0.1.14",

View File

@ -13,9 +13,9 @@
"package-lock.json"
],
"dependencies": {
"@fluencelabs/aqua-lib": "^0.1.9",
"@fluencelabs/fluence": "^0.13.0",
"@fluencelabs/fluence-network-environment": "^1.0.10",
"@fluencelabs/aqua-lib": "0.1.14",
"@fluencelabs/fluence": "0.13.0",
"@fluencelabs/fluence-network-environment": "1.0.10",
"it-all": "^1.0.5"
},
"scripts": {
@ -43,7 +43,7 @@
},
"homepage": "git+https://github.com/fluencelabs/examples/aqua-examples/price-oracle#readme",
"devDependencies": {
"@fluencelabs/aqua": "^0.3.0-226",
"@fluencelabs/aqua": "0.3.2-233",
"typescript": "^3.9.5"
}
}

View File

@ -1,5 +1,5 @@
/*
* Copyright 2021 Fluence Labs Limited
* Copyright 2021 Fluence Labs Limited
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -24,53 +24,60 @@ interface NodeServicePair {
}
let getter_topo: Array<NodeServicePair>;
let mean_topo: NodeServicePair;
let mean_topo: Array<NodeServicePair>;
getter_topo = Array(
// description of the services' locations, copypaste from data/deployed_services.json
getter_topo = [
{
node: "12D3KooWCMr9mU894i8JXAFqpgoFtx6qnV1LFPSfVc3Y34N4h4LS",
service_id: "c315073d-4311-4db3-be57-8f154f032d28",
service_id: "b67586f7-e96f-49ee-914e-9eabe1a0b83d",
},
{
node: "12D3KooWFEwNWcHqi9rtsmDhsYcDbRUCDXH84RC4FW6UfsFWaoHi",
service_id: "25f9123a-f386-4cb2-9c1e-bb7c247c9c09",
service_id: "f5b456fa-ee18-4df1-b18b-84fe7ebc7ad0",
}
);
mean_topo = {
node: "12D3KooWCMr9mU894i8JXAFqpgoFtx6qnV1LFPSfVc3Y34N4h4LS",
service_id: "dd47389f-25d9-4870-a2a9-909359e73580",
};
];
mean_topo = [
{
node: "12D3KooWCMr9mU894i8JXAFqpgoFtx6qnV1LFPSfVc3Y34N4h4LS",
service_id: "79b8ddb9-e2e6-4924-9293-c5d55c94af6b",
},
{
node: "12D3KooWFEwNWcHqi9rtsmDhsYcDbRUCDXH84RC4FW6UfsFWaoHi",
service_id: "debecd02-ba7d-40a2-92ab-08a9321da2cf"
}
];
async function main() {
console.log("hello crypto investors");
// Uncomment to enable debug logs:
// setLogLevel('DEBUG');
await Fluence.start({ connectTo: krasnodar[2] });
// create the Fluence client for the Krasnodar testnet
await Fluence.start({ connectTo: krasnodar[5] });
console.log(
"created a fluence client %s with relay %s",
Fluence.getStatus().peerId,
Fluence.getStatus().relayPeerId
);
// call the get_price function -- sequential processing
const network_result = await get_price(
"ethereum",
"usd",
"12D3KooWFEwNWcHqi9rtsmDhsYcDbRUCDXH84RC4FW6UfsFWaoHi",
"25f9123a-f386-4cb2-9c1e-bb7c247c9c09",
"b2790307-055e-41ca-9640-3c41856d464b"
getter_topo[1].node,
getter_topo[1].service_id,
mean_topo[1].service_id
);
console.log("seq result: ", network_result);
const network_result_par = await get_price_par(
"ethereum",
"usd",
getter_topo,
mean_topo
// call the get_price_par function -- parallel processing
const network_result_par = await get_price_par("ethereum", "usd", getter_topo, mean_topo[0]
);
console.log("par result: ", network_result_par);
await Fluence.stop();
return;
}
main()

View File

@ -2,21 +2,21 @@
"price_getter": [
{
"node": "12D3KooWCMr9mU894i8JXAFqpgoFtx6qnV1LFPSfVc3Y34N4h4LS",
"service_id": "c315073d-4311-4db3-be57-8f154f032d28"
"service_id": "b67586f7-e96f-49ee-914e-9eabe1a0b83d"
},
{
"node": "12D3KooWFEwNWcHqi9rtsmDhsYcDbRUCDXH84RC4FW6UfsFWaoHi",
"service_id": "25f9123a-f386-4cb2-9c1e-bb7c247c9c09"
"service_id": "f5b456fa-ee18-4df1-b18b-84fe7ebc7ad0"
}
],
"mean": [
{
"node": "12D3KooWCMr9mU894i8JXAFqpgoFtx6qnV1LFPSfVc3Y34N4h4LS",
"service_id": "dd47389f-25d9-4870-a2a9-909359e73580"
"service_id": "79b8ddb9-e2e6-4924-9293-c5d55c94af6b"
},
{
"node": "12D3KooWFEwNWcHqi9rtsmDhsYcDbRUCDXH84RC4FW6UfsFWaoHi",
"service_id": "b2790307-055e-41ca-9640-3c41856d464b"
"service_id": "debecd02-ba7d-40a2-92ab-08a9321da2cf"
}
]
}