How does this cell code work  

  RSS

Joshy Orndorff
(@joshyorndorff)
New Member
Joined:3 months  ago
Posts: 2
01/08/2018 6:34 am  

The cell code below comes from https://github.com/rchain/rchain/blob/dev/rholang/examples/old/sugar/Cell1.rho

/*
 * A Cell contract that uses a parallel process
 * expression to handle get and set.
 */

contract Cell( get, set, state ) = {
  for( rtn <- get; v <- state ) {
      rtn!( v ) | state!( v ) | Cell( get, set, state )
  } |
  for( newValue <- set; v <- state ) {
      state!( newValue ) | Cell( get, set, state )
  }
}

I understand what it does and the idea of the state channel. But I have two questions.

1. When I call this contract I need to send three values, get, set, and state, right? So how can I use those joins inside the contract?

2. What does is mean to write the contract signature again inside its body like

state!( newValue ) | Cell( get, set, state )

?


ReplyQuote
MichaelBirch
(@michaelbirch)
Member Moderator
Joined:6 months  ago
Posts: 38
01/08/2018 2:56 pm  

1. You are sending three *channels* to the contact, not values. That is why you can listen on them in the input guards and then send on them in the continuations.

2. It recursively calls itself. The pseudo-code for this contract might look something like

  • (1) Input channels get, set, state
  • (2) Listen for messages on (get, state) and (set, state)
  • (3) If (get, state) then send value received on state over channel¬†received on get and over state again; goto (2)
  • (4) If (set, state) then send value received on set over state; goto (2)

Notice that the cell would be single use without the recursive call.

 


ReplyQuote
  
Working

Please Login or Register