Skip to content
This repository has been archived by the owner on Nov 4, 2021. It is now read-only.

Commit

Permalink
Writing the wisdom for sale tutorial.
Browse files Browse the repository at this point in the history
  • Loading branch information
hagenhaus committed Oct 13, 2021
1 parent 7ba4945 commit f9db830
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 48 deletions.
136 changes: 88 additions & 48 deletions en/books/essentials/tutorials/wisdom-for-sale/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,9 @@ Developers use the Reach programming language to write (in *index.rsh*) the pre-
load: https://raw.githubusercontent.com/hagenhaus/wisdom-for-sale/master/starter/index.rsh
```

The Reach compiler transforms *reach.rsh* into *index.main.mjs*, the compiled backend code composed of two parts:
The Reach compiler transforms *reach.rsh* into *index.main.mjs* which is composed of two parts:

1. The Gateway facilitates communication between your frontend and the consensus network. It is compiler-generated JavaScript:
1. The Compiled Backend facilitates communication between your frontend and the consensus network. It is compiler-generated JavaScript:

``` js
export const _version = '0.1.5';
Expand Down Expand Up @@ -100,6 +100,8 @@ Open [index.mjs](https://github.com/hagenhaus/wisdom-for-sale/blob/master/starte
load: https://raw.githubusercontent.com/hagenhaus/wisdom-for-sale/master/starter/index.mjs
```

Below is a line-by-line description:

* Line 1: Import the Reach JS Standard Library loader.
* Line 2: Import the JS backend compiled from index.rsh.
* Line 3: Import a Reach Node.js package to help with command-line i/o.
Expand All @@ -122,6 +124,8 @@ Open [index.rsh](https://github.com/hagenhaus/wisdom-for-sale/blob/master/starte
load: https://raw.githubusercontent.com/hagenhaus/wisdom-for-sale/master/starter/index.rsh
```

Below is a line-by-line description:

* Line 1: Instruction to the compiler.
* Line 3: Reach standard application initialization.
* Line 4: Define a constant to represent the seller. The `{}` indicates an empty interact object.
Expand All @@ -131,11 +135,11 @@ load: https://raw.githubusercontent.com/hagenhaus/wisdom-for-sale/master/starter

# Run the app

The following diagram illustrates your environment before you run your starter app. Your application consists of your frontend (index.mjs), your backend (index.rsh), and the Reach Docker environment where everything will run: the Reach compiler, your application, the consensus network devnets, and your smart contract.
The following diagram illustrates your environment before you run the starter app. Your application consists of a frontend (index.mjs), a backend (index.rsh), and the Reach Docker environment where everything will run including the Reach compiler, your application, the consensus network devnets, and the smart contract.

<p><img src="reach-run-1.png" class="img-fluid" width=420 loading="lazy"></p>

Now, run your starter app in the vscode terminal three times, once for each supported consensus network. Use the commands below in succession, and note the output:
Now, run your starter app in the vscode terminal three times, once for each supported consensus network. Use the commands below in succession:

``` nonum
$ REACH_CONNECTOR_MODE=ALGO-devnet reach run
Expand All @@ -145,83 +149,119 @@ $ REACH_CONNECTOR_MODE=CFX-devnet reach run

<p><img src="vscode-run.png" class="img-fluid" width=700 loading="lazy"></p>

Here's what happens when you run `reach run`:
Here is sample output:

```
current % REACH_CONNECTOR_MODE=ALGO-devnet reach run
Verifying knowledge assertions
Verifying for generic connector
Verifying when ALL participants are honest
Verifying when NO participants are honest
Verifying when ONLY "Buyer" is honest
Verifying when ONLY "Seller" is honest
Checked 4 theorems; No failures!
[+] Building 0.2s (7/7) FINISHED
=> [internal] load build definition from Dockerfile 0.0s
=> => transferring dockerfile: 234B 0.0s
=> [internal] load .dockerignore 0.0s
=> => transferring context: 75B 0.0s
=> [internal] load metadata for docker.io/reachsh/runner:0.1.5 0.0s
=> [internal] load build context 0.0s
=> => transferring context: 4.60kB 0.0s
=> CACHED [1/2] FROM docker.io/reachsh/runner:0.1.5 0.0s
=> [2/2] COPY . /app 0.0s
=> exporting to image 0.0s
=> => exporting layers 0.0s
=> => writing image sha256:13e69eb72504bbc85074476e5d1183b6ad2734a5eabc9e56c2138023d64a507a 0.0s
=> => naming to docker.io/reachsh/reach-app-current:latest 0.0s
Use 'docker scan' to run Snyk tests against images to find vulnerabilities and learn how to fix them
Creating reach2021-10-13t14-26-55z-vcv1_reach-app-current_run ... done
> @reach-sh/current@ index /app
> node --experimental-modules --unhandled-rejections=strict index.mjs
The consensus network is ALGO.
Your role is seller.
```

1. sss
Below is a line-by-line description:

* Line 1: The Reach Compiler inputs *reach.rsh*.

<p><img src="reach-run-2.png" class="img-fluid" width=420 loading="lazy"></p>

1. sss
And, it outputs *index.main.mjs* consisting of the compiled backend (blue) and the smart contract (dark orange):

<p><img src="reach-run-3.png" class="img-fluid" width=420 loading="lazy"></p>

1. sss
* Line 2: The Reach Verification Engine validates the smart contract:

<p><img src="reach-run-4.png" class="img-fluid" width=420 loading="lazy"></p>

1. sss
* Lines 10-22: The process builds a Docker image for your application.

<p><img src="reach-run-5.png" class="img-fluid" width=420 loading="lazy"></p>

1. sss
<p><img src="reach-run-6.png" class="img-fluid" width=420 loading="lazy"></p>
Note the mention of *Dockerfile* and *.dockerignore*. The `reach run` command creates the following set of files, deleting all but *index.main.mjs* on completion:

1. sss
``` nonum
build/index.main.mjs
.dockerignore
.gitignore
Dockerfile
package.json
```

<p><img src="reach-run-7.png" class="img-fluid" width=420 loading="lazy"></p>
For some of your Reach projects, you may find it useful to retain and edit these files, especially *package.json*. To create and retain these files, run `reach scaffold`.

## Review build files
* Line 24: The process checks the new image for vulnerabilities.

You see the following directory and files appear. Some files disappear.
<p><img src="reach-run-6.png" class="img-fluid" width=420 loading="lazy"></p>

* build/index.main.mjs
* .dockerignore
* .gitignore
* Dockerfile
* package.json
* Line 27: The process runs your app in a container built from the image.

Understand significance.
<p><img src="reach-run-7.png" class="img-fluid" width=420 loading="lazy"></p>

Explain `reach scaffold`.
* Line 30-31: Output from your application frontend.

# Pass arguments

This section represents the first change that the developer makes to the project.
In this section, you modify the starter app to accept a command-line argument specifying whether to run as the seller or buyer. Although this change does not involve Reach directly, it does emphasize that your application represents two different participants negotiating via the same contract.

Replace `const role = 'seller';` with the following:
1. In *index.mjs*, replace `const role = 'seller';` with the following:

``` js
if (process.argv.length < 3 || ['seller', 'buyer'].includes(process.argv[2]) == false) {
console.log('Usage: reach run index [seller|buyer]');
process.exit(0);
}
const role = process.argv[2];
```
``` js
if (process.argv.length < 3 || ['seller', 'buyer'].includes(process.argv[2]) == false) {
console.log('Usage: reach run index [seller|buyer]');
process.exit(0);
}
const role = process.argv[2];
```

## Run in two terminals
1. Open two terminals (i.e. shells), and change directory in both to `~/reach/wisdom-for-sale/current`:

Open two terminals (i.e. shells) positioned side by side.
<p><img src="terminals-empty.png" class="img-fluid" width=700 loading="lazy"></p>

Change directory in both to `~/reach/wisdom-for-sale/current`.
The left terminal is the *Seller Terminal*, and the right is the *Buyer Terminal*.

The left terminal is the *Seller Terminal*. In this terminal, run one of the following:
1. In the Seller Terminal, run one of the following:

``` nonum
$ REACH_CONNECTOR_MODE=ALGO-devnet reach run index seller
$ REACH_CONNECTOR_MODE=ETH-devnet reach run index seller
$ REACH_CONNECTOR_MODE=CFX-devnet reach run index seller
```
``` nonum
$ REACH_CONNECTOR_MODE=ALGO-devnet reach run index seller
$ REACH_CONNECTOR_MODE=ETH-devnet reach run index seller
$ REACH_CONNECTOR_MODE=CFX-devnet reach run index seller
```

The right terminal is the *Buyer Terminal*. In this terminal, run the corresponding command:
1. In the *Buyer Terminal*, run the corresponding command:

``` nonum
$ REACH_CONNECTOR_MODE=ALGO-devnet reach run index buyer
$ REACH_CONNECTOR_MODE=ETH-devnet reach run index buyer
$ REACH_CONNECTOR_MODE=CFX-devnet reach run index buyer
```
``` nonum
$ REACH_CONNECTOR_MODE=ALGO-devnet reach run index buyer
$ REACH_CONNECTOR_MODE=ETH-devnet reach run index buyer
$ REACH_CONNECTOR_MODE=CFX-devnet reach run index buyer
```

Try all of them if you like. Use these two terminal for the rest of the tutorial.
1. Repeat for the other consensus networks.

## About adding Node packages

Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit f9db830

Please sign in to comment.