The original link

In this part, Alice and Bob start playing a guessing game! First, let’s think about how to make a fist gesture. An easy way to do this is to use the numbers 0, 1, and 2 for “rock,” “paper,” and “scissors.” However, Reach does not support two-digit unsigned integers, so they are best represented as integer equivalence classes of modulo 3, that is, 0 and 3 are equal and both represent stones. We use the same method to represent the three outcomes of guessing: B wins, A draws and A wins. The first step is to modify the Reach program so that Alice and Bob’s front ends interact to get the gestures they want to make, and then notify them of the results of the game.

tut-3/index.rsh

 1    'reach 0.1';
 2    
 3    const Player =
 4          { getHand: Fun([], UInt),
 5            seeOutcome: Fun([UInt], Null) };
 6    
 7    export const main =
 8      Reach.App(
 9        {},
10        [Participant('Alice', Player), Participant('Bob', Player)],
11        (A, B) => {
..          // ...
26          exit(); });
Copy the code
  • Lines 3 through 5 define the actor’s interface, which is invoked by both actors. In this example, there are two methods: getHand, which returns a number; SeeOutcome, it receives a number.
  • In line 10, both participants invoke the interface. With this line of code, two actor instances are bound to the front-end code.

Before we continue writing the Reach program, let’s go back to JavaScript and implement these methods in the front end.

tut-3/index.mjs

.// ...
14    const HAND = ['Rock'.'Paper'.'Scissors'];0
15    const OUTCOME = ['Bob wins'.'Draw'.'Alice wins'];
16    const Player = (Who) = > ({
17      getHand: () = > {
18        const hand = Math.floor(Math.random() * 3);
19        console.log(`${Who} played ${HAND[hand]}`);
20        return hand;
21      },
22      seeOutcome: (outcome) = > {
23        console.log(`${Who} saw outcome ${OUTCOME[outcome]}`);
24      },
25    });
26  
27    await Promise.all([
28      backend.Alice(
29        ctcAlice,
30        Player('Alice'),
31      ),
32      backend.Bob(
33        ctcBob,
34        Player('Bob'),
35      ),
36]); .// ...
Copy the code
  • Lines 14 and 15 define arrays that represent the contents of the punch gesture and the guessing result.
  • Line 16 defines the constructor for the Player implementation.
  • Lines 17 to 21 define the getHand method.
  • Lines 22 through 24 define the seeOutcome method.
  • Finally, lines 30 and 34 instantiate objects for Alice and Bob, respectively. These are the instantiated objects bound in the Reach program.

This part of the code is fairly simple, nothing special; This is the beauty of Reach: we only need to write the business logic and don’t have to worry about the details of consensus networks and decentralized applications.

Let’s go back to the Reach program and see what Alice and Bob do. In the real-life rock-paper-Scissors scenario, Alice and Bob decide which hand gesture they want to make and punch at the same time. “Simultaneity” is a complex concept that is difficult to practice. For example, you might find yourself doing a fist guessing game with a child and find that they “slow down “- trying to win by seeing your hand signals before throwing a punch. However, in a decentralized application, it is impossible to achieve at the same time. Instead, one participant must “go first.” In this example, we will let Alice go first.

Does Alice play first, or do we call the player who plays first Alice? This distinction may seem unnecessary, but Reach makes clever use of it. On the front end, we make a clear distinction between backend.Alice and backend.Bob. So we submit a particular JavaScript thread as Alice or Bob. In our game, whoever runs Alice’s back end first is the one who punches first. Later in the tutorial, when the user can choose the role to play, the concept will become clearer.

The game is played in three steps. First, Alice’s back end interacts with the front end, takes her gesture, and then publishes it.

.// ...
12    A.only(() = > {
13      const handA = declassify(interact.getHand()); });
14    A.publish(handA);
15commit(); .// ...
Copy the code
  • Line 12 indicates that this code block is executed only by A (that is, Alice).
  • This means that the variable handA bound on line 13 is visible only to Alice.
  • Line 13 assigns Alice’s gesture to the variable handA.
  • Line 13 also decrypts the value, because in Reach, all information from the front end is encrypted.
  • In line 14, Alice posts the value to the consensus network so that the outcome of the game can be determined. Once at this point, the code is in a “consensus step” that all participants participate in.
  • Line 15 commits the status of the consensus network and returns to the “local step,” where each participant can run separately.

In a similar move, Bob posts his gesture. The difference is that instead of submitting the status immediately, we calculate the result of the toss first.

.// ...
17    B.only(() = > {
18      const handB = declassify(interact.getHand()); });
19    B.publish(handB);
20  
21    const outcome = (handA + (4 - handB)) % 3;
22commit(); .// ...
Copy the code
  • Lines 17 to 19 are similar to Alice’s steps to publish the application via consensus migration.
  • Line 21 computes the results of the game before submission. ((handA + (4-handb)) % 3 is a clever operation used to calculate the winner of a guessing game using a formulaic algorithm. Consider that when handA is 0 (rock) and handB is 2 (scissors), the equation is ((handA +(4-Handb)) % 3)=((0 +(4-2)) % 3)=((0 + 2) % 3)=(2% 3)= 2, indicating that A wins, as we expect.)

Finally, we call the each method to send the results of each participant’s run to their front end.

.// ...
24    each([A, B], () = > {
25interact.seeOutcome(outcome); }); .// ...
Copy the code
  • Line 24 indicates that participants A and B are traversed.

At this point, we can run the program to see the output $./reach Run the result of the player’s run is random, so the result will be different each time. For example, we run the program three times and get the following results:

$ ./reach run
Alice played Scissors
Bob played Paper
Alice saw outcome Alice wins
Bob saw outcome Alice wins
Copy the code
$ ./reach run
Alice played Scissors
Bob played Paper
Alice saw outcome Alice wins
Bob saw outcome Alice wins
Copy the code
$ ./reach run
Alice played Paper
Bob played Rock
Alice saw outcome Alice wins
Bob saw outcome Alice wins
Copy the code

(Alice is good at guessing!!)

As we can see, consensus networks (and Reach in particular) guarantee that all participants agree on the results of their calculations. This is where the name consensus networks comes from, as they enable these dispersed and untrusted parties to reach consensus agreements on the intermediate state of computing. If they agree on the intermediate state, then they agree on the output. That’s why Alice and Bob see the same result every time they run./reach run!

If your code doesn’t work correctly, check out the full versions of tut-3/index.rsh and tut-3/index.mjs, and make sure you copy everything correctly!

In the next step, we will add a betting mechanism that allows Alice to cash in on her punches!

Do you know? :

Reach applications allow you to interact with the user interface in which of the following ways

  1. Write a custom back end connected to the user interface of the generated smart contract
  2. Allows the front end to provide values directly to Reach applications,
  3. Allows the Reach program to call back to the front end using interactive objects.

Answer: 2 and 3. Reach implements bidirectional interaction between the front end and back end through the participant interface.

Do you know? :

How do participants in a Reach application share information with each other and know what others share?

  1. Reach generates smart contracts, but you need to implement a process to scan the blockchain for events that correspond to sharing;
  2. The original statement publish of Reach allows participants to share information with all other participants, which happens automatically without anyone doing anything special;
  3. The original statement publish of Reach allows participants to share information with all other participants, but they need to explicitly run the receive original statement to receive published information.

Answer: 2. The original publish statement does it for you!

2.2 Creating scaffolding and setting it up

2.4 Bets and Wagers