example: improved (#46)

This commit is contained in:
Aleksey Proshutisnkiy 2022-07-08 17:40:22 +04:00 committed by GitHub
parent edc7dc404f
commit 2a5e324dd5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 71 additions and 32 deletions

View File

@ -1,10 +1,44 @@
## Description ## Description
This example shows how to use Trust Graph to label peers. There are some `trusted_computation` which can only be executed This example shows how to use Trust Graph for code execution only on trusted peers. There are some `trusted_computation` which can only be performed on a trusted peer. The label is determined by the presence of the certificate from `INIT_PEER_ID` to this peer. We use peer id from [`example_secret_key.ed25519`](../example_secret_key.ed25519) as `INIT_PEER_ID` since every node bundled with the certificate issued to this key, it should be used only for test purposes.
on labeled peer. The label is determined by the presence of certificate from `%init_peer_id` to this peer.
## Run example on network
1. Run `npm i`
2. Run `npm run start`
## Run example locally ## Run example locally
1. Go to `local-network` 1. Go to `local-network`
2. Run `docker compose up -d` to start Fluence node 2. Run `docker compose up -d` to start Fluence node
3. Go back to `../example` 3. It takes some time depending on your machine for node to start and builtin services deployed. Wait for this log line: `[2022-07-06T11:33:50.782054Z INFO particle_node] Fluence has been successfully started.`
4. Run `npm i` 4. Go back to `../example`
5. Run `npm run start` 5. Run `npm i`
6. Run `npm run start local`
## Expected output
After successful execution you will get this result:
```
In this example we try to execute some trusted computations based on trusts
📘 Will connect to testNet
📗 created a fluence peer 12D3KooWD2vAZva1u3TQgoxebBUBsaGMNawKjVkp57M6UcwNwXNv with relay 12D3KooWEXNUbCXooUwHrHBbrmjsrpHXoEphPwbjQXEGyzbqKnE9
📕 Trusted computation on node 12D3KooWEXNUbCXooUwHrHBbrmjsrpHXoEphPwbjQXEGyzbqKnE9 failed, error: there is no certs for this peer
📕 Trusted computation on node 12D3KooWMhVpgfQxBLkQkJed8VFNvgN4iE6MD7xCybb1ZYWW2Gtz failed, error: there is no certs for this peer
📕 Trusted computation on node 12D3KooWHk9BjDQBUqnavciRPhAYFvqKBe4ZiPPvde7vDaqgn5er failed, error: there is no certs for this peer
🌀 Issue trust to nodeB 12D3KooWMhVpgfQxBLkQkJed8VFNvgN4iE6MD7xCybb1ZYWW2Gtz and nodeC: 12D3KooWHk9BjDQBUqnavciRPhAYFvqKBe4ZiPPvde7vDaqgn5er
Trust issued for 12D3KooWMhVpgfQxBLkQkJed8VFNvgN4iE6MD7xCybb1ZYWW2Gtz successfully added
Trust issued for 12D3KooWHk9BjDQBUqnavciRPhAYFvqKBe4ZiPPvde7vDaqgn5er successfully added
📕 Trusted computation on node 12D3KooWEXNUbCXooUwHrHBbrmjsrpHXoEphPwbjQXEGyzbqKnE9 failed, error: there is no certs for this peer
📗 Trusted computation on node 12D3KooWMhVpgfQxBLkQkJed8VFNvgN4iE6MD7xCybb1ZYWW2Gtz successful, result is 5
📗 Trusted computation on node 12D3KooWHk9BjDQBUqnavciRPhAYFvqKBe4ZiPPvde7vDaqgn5er successful, result is 5
🚫 Revoke trust to nodeB
Trust issued for 12D3KooWMhVpgfQxBLkQkJed8VFNvgN4iE6MD7xCybb1ZYWW2Gtz revoked
📕 Trusted computation on node 12D3KooWEXNUbCXooUwHrHBbrmjsrpHXoEphPwbjQXEGyzbqKnE9 failed, error: there is no certs for this peer
📕 Trusted computation on node 12D3KooWMhVpgfQxBLkQkJed8VFNvgN4iE6MD7xCybb1ZYWW2Gtz failed, error: there is no certs for this peer
📗 Trusted computation on node 12D3KooWHk9BjDQBUqnavciRPhAYFvqKBe4ZiPPvde7vDaqgn5er successful, result is 5
```

View File

@ -39,22 +39,14 @@ let local: Node[] = [
}, },
]; ];
async function revoke_all(relay: string, revoked_by: string) { async function revoke_all(relay: string, revoked_by: string, nodes: Node[]) {
for (var node of local) { for (var node of nodes) {
let error = await tg.revoke(relay, revoked_by, node.peerId); let error = await tg.revoke(relay, revoked_by, node.peerId);
if (error !== null) { if (error !== null) {
console.log(error) console.log(error)
} }
} }
} }
async function add_root(relay: string, peer_id: string) {
let current_time = await tg.timestamp_sec();
let far_future = current_time + 9999999;
let error = await tg.add_root_trust(relay, peer_id, 2, far_future);
if (error !== null) {
console.log(error)
}
}
async function add_new_trust_checked(relay: string, issuer: string, issued_for_peer_id: string, expires_at_sec: number) { async function add_new_trust_checked(relay: string, issuer: string, issued_for_peer_id: string, expires_at_sec: number) {
let error = await tg.add_trust(relay, issuer, issued_for_peer_id, expires_at_sec); let error = await tg.add_trust(relay, issuer, issued_for_peer_id, expires_at_sec);
@ -84,14 +76,13 @@ async function exec_trusted_computation(node: string) {
} }
} }
async function main() { async function main(nodes: Node[]) {
console.log("In this example we try to execute some trusted computations based on trusts"); // example_secret_key.ed25519
console.log("📘 Will connect to local nodes"); let sk = bs58.decode("E5ay3731i4HN8XjJozouV92RDMGAn3qSnb9dKSnujiWv");
// key from local-network/builtins_secret_key.ed25519 to connect as builtins owner
let sk = bs58.decode("5FwE32bDcphFzuMca7Y2qW1gdR64fTBYoRNvD4MLE1hecDGhCMQGKn8aseMr5wRo4Xo2CRFdrEAawUNLYkgQD78K").slice(0, 32); // first 32 bytes - secret key, second - public key
let builtins_keypair = await KeyPair.fromEd25519SK(sk); let builtins_keypair = await KeyPair.fromEd25519SK(sk);
let relay = local[0]; let relay = nodes[0];
await Fluence.start({ connectTo: relay, KeyPair: builtins_keypair }); await Fluence.start({ connectTo: relay, KeyPair: builtins_keypair });
console.log( console.log(
"📗 created a fluence peer %s with relay %s", "📗 created a fluence peer %s with relay %s",
@ -105,41 +96,55 @@ async function main() {
let far_future = current_time + 9999999; let far_future = current_time + 9999999;
// clear all trusts from our peer id on relay // clear all trusts from our peer id on relay
await revoke_all(relay.peerId, local_peer_id); await revoke_all(relay.peerId, local_peer_id, nodes.slice(0, 3));
// wait to be sure that last revocation will be older than future trusts at least on 1 second (because timestamp in secs) // wait to be sure that last revocation will be older than future trusts at least on 1 second (because timestamp in secs)
await new Promise(f => setTimeout(f, 1000)); await new Promise(f => setTimeout(f, 1000));
// set our peer id as root to our relay let nodeA = nodes[0].peerId
await add_root(relay.peerId, local_peer_id); let nodeB = nodes[1].peerId
let nodeC = nodes[2].peerId
let nodeA = local[0].peerId
let nodeB = local[1].peerId
let nodeC = local[2].peerId
console.log();
// try to exec computation on every node, will fail // try to exec computation on every node, will fail
await exec_trusted_computation(nodeA); // fail await exec_trusted_computation(nodeA); // fail
await exec_trusted_computation(nodeB); // fail await exec_trusted_computation(nodeB); // fail
await exec_trusted_computation(nodeC); // fail await exec_trusted_computation(nodeC); // fail
console.log("🌀 Issue trust to nodeB: %s", nodeB); console.log();
console.log("🌀 Issue trust to nodeB %s and nodeC: %s", nodeB, nodeC);
await add_new_trust_checked(relay.peerId, local_peer_id, nodeB, far_future); await add_new_trust_checked(relay.peerId, local_peer_id, nodeB, far_future);
await add_new_trust_checked(relay.peerId, local_peer_id, nodeC, far_future);
console.log();
await exec_trusted_computation(nodeA); // fail await exec_trusted_computation(nodeA); // fail
await exec_trusted_computation(nodeB); // success await exec_trusted_computation(nodeB); // success
await exec_trusted_computation(nodeC); // fail await exec_trusted_computation(nodeC); // success
console.log();
await new Promise(f => setTimeout(f, 1000)); await new Promise(f => setTimeout(f, 1000));
console.log("🚫 Revoke trust to nodeB"); console.log("🚫 Revoke trust to nodeB");
await revoke_checked(relay.peerId, local_peer_id, nodeB); await revoke_checked(relay.peerId, local_peer_id, nodeB);
console.log();
await exec_trusted_computation(nodeA); // fail await exec_trusted_computation(nodeA); // fail
await exec_trusted_computation(nodeB); // fail await exec_trusted_computation(nodeB); // fail
await exec_trusted_computation(nodeC); // fail await exec_trusted_computation(nodeC); // success
return; return;
} }
console.log("In this example we try to execute some trusted computations based on trusts");
let args = process.argv.slice(2);
var environment: Node[];
if (args.length >= 1 && args[0] == "local") {
environment = local;
console.log("📘 Will connect to local nodes");
} else {
environment = testNet;
console.log("📘 Will connect to testNet");
}
main() main(environment)
.then(() => process.exit(0)) .then(() => process.exit(0))
.catch((error) => { .catch((error) => {
console.error(error); console.error(error);