Skip to content

Commit

Permalink
Improve CLI for owners in bash tests (#3388)
Browse files Browse the repository at this point in the history
## Motivation

Allow removing the hard-coded OWNER variables from README tests

## Proposal

Make sure to return the owner on stdout every time the CLI generates a
key pair.

## Test Plan

I modified the main README file.

## Links

Continues #3385
  • Loading branch information
ma2bd authored Feb 23, 2025
1 parent 3328219 commit f7e48ce
Show file tree
Hide file tree
Showing 18 changed files with 402 additions and 185 deletions.
21 changes: 19 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,15 @@ export LINERA_STORAGE="rocksdb:$LINERA_TMP_DIR/client.db"
linera wallet init --faucet $FAUCET_URL

# Request chains.
CHAIN1=$(linera wallet request-chain --faucet $FAUCET_URL | head -n 1)
CHAIN2=$(linera wallet request-chain --faucet $FAUCET_URL | head -n 1)
INFO1=($(linera wallet request-chain --faucet $FAUCET_URL))
INFO2=($(linera wallet request-chain --faucet $FAUCET_URL))
CHAIN1="${INFO1[0]}"
ACCOUNT1="User:${INFO1[3]}"
CHAIN2="${INFO2[0]}"
ACCOUNT2="User:${INFO2[3]}"

# Show the different chains tracked by the wallet.
linera wallet show

# Query the chain balance of some of the chains.
linera query-balance "$CHAIN1"
Expand All @@ -107,6 +114,16 @@ linera transfer 5 --from "$CHAIN2" --to "$CHAIN1"
# Query balances again.
linera query-balance "$CHAIN1"
linera query-balance "$CHAIN2"

# Now let's fund the user balances.
linera transfer 5 --from "$CHAIN1" --to "$CHAIN1:$ACCOUNT1"
linera transfer 2 --from "$CHAIN1:$ACCOUNT1" --to "$CHAIN2:$ACCOUNT2"

# Query user balances again.
linera query-balance "$CHAIN1:$ACCOUNT1"
linera query-balance "$CHAIN2:$ACCOUNT2"

# TODO(#1713): The syntax `User:$OWNER` for user accounts will change in the future.
```

More complex examples may be found in our [developer manual](https://linera.dev) as well
Expand Down
35 changes: 24 additions & 11 deletions examples/amm/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ It supports the following operations. All operations need to be executed remotel

Before getting started, make sure that the binary tools `linera*` corresponding to
your version of `linera-sdk` are in your PATH. For scripting purposes, we also assume
that the BASH function `linera_spawn_and_read_wallet_variables` is defined.
that the BASH function `linera_spawn` is defined.

From the root of Linera repository, this can be achieved as follows:

Expand All @@ -38,22 +38,35 @@ export PATH="$PWD/target/debug:$PATH"
source /dev/stdin <<<"$(linera net helper 2>/dev/null)"
```

To start the local Linera network:
Next, start the local Linera network and run a faucet:

```bash
linera_spawn_and_read_wallet_variables linera net up --testing-prng-seed 37
FAUCET_PORT=8079
FAUCET_URL=http://localhost:$FAUCET_PORT
linera_spawn linera net up --with-faucet --faucet-port $FAUCET_PORT

# If you're using a testnet, run this instead:
# LINERA_TMP_DIR=$(mktemp -d)
# FAUCET_URL=https://faucet.testnet-XXX.linera.net # for some value XXX
```

We use the test-only CLI option `--testing-prng-seed` to make keys deterministic and simplify our
explanation.
Create the user wallet and add chains to it:

```bash
OWNER_1=789dcf5afb0afb9c5c473568bab33c68b83005bf7b964dc2a93e5d4368a9da1f
OWNER_2=38e7f6b6d1887b0ea1511e04a08382577aaf551bb440ee6011966d73d19b8150
CHAIN_1=678e9f66507069d38955b593e93ddf192a23a4087225fd307eadad44e5544ae3
CHAIN_2=a3edc33d8e951a1139333be8a4b56646b5598a8f51216e86592d881808972b07
CHAIN_AMM=aee928d4bf3880353b4a3cd9b6f88e6cc6e5ed050860abae439e7782e9b2dfe8
OWNER_AMM=de166237331a2966d8cf6778e81a8c007b4084be80dc1e0409d51f216c1deaa1
export LINERA_WALLET="$LINERA_TMP_DIR/wallet.json"
export LINERA_STORAGE="rocksdb:$LINERA_TMP_DIR/client.db"

linera wallet init --faucet $FAUCET_URL

INFO_1=($(linera wallet request-chain --faucet $FAUCET_URL))
INFO_2=($(linera wallet request-chain --faucet $FAUCET_URL))
INFO_AMM=($(linera wallet request-chain --faucet $FAUCET_URL))
CHAIN_1="${INFO_1[0]}"
CHAIN_2="${INFO_2[0]}"
CHAIN_AMM="${INFO_AMM[0]}"
OWNER_1="${INFO_1[3]}"
OWNER_2="${INFO_2[3]}"
OWNER_AMM="${INFO_AMM[3]}"
```

Now we have to publish and create the fungible applications. The flag `--wait-for-outgoing-messages` waits until a quorum of validators has confirmed that all sent cross-chain messages have been delivered.
Expand Down
25 changes: 18 additions & 7 deletions examples/counter/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ application would give us 4 (1+3 = 4).

Before getting started, make sure that the binary tools `linera*` corresponding to
your version of `linera-sdk` are in your PATH. For scripting purposes, we also assume
that the BASH function `linera_spawn_and_read_wallet_variables` is defined.
that the BASH function `linera_spawn` is defined.

From the root of Linera repository, this can be achieved as follows:

Expand All @@ -27,18 +27,29 @@ export PATH="$PWD/target/debug:$PATH"
source /dev/stdin <<<"$(linera net helper 2>/dev/null)"
```

To start the local Linera network:
Next, start the local Linera network and run a faucet:

```bash
linera_spawn_and_read_wallet_variables linera net up --testing-prng-seed 37
FAUCET_PORT=8079
FAUCET_URL=http://localhost:$FAUCET_PORT
linera_spawn linera net up --with-faucet --faucet-port $FAUCET_PORT

# If you're using a testnet, run this instead:
# LINERA_TMP_DIR=$(mktemp -d)
# FAUCET_URL=https://faucet.testnet-XXX.linera.net # for some value XXX
```

We use the test-only CLI option `--testing-prng-seed` to make keys deterministic and simplify our
explanation.
Create the user wallet and add chains to it:

```bash
CHAIN_1=aee928d4bf3880353b4a3cd9b6f88e6cc6e5ed050860abae439e7782e9b2dfe8
OWNER_1=de166237331a2966d8cf6778e81a8c007b4084be80dc1e0409d51f216c1deaa1
export LINERA_WALLET="$LINERA_TMP_DIR/wallet.json"
export LINERA_STORAGE="rocksdb:$LINERA_TMP_DIR/client.db"

linera wallet init --faucet $FAUCET_URL

INFO_1=($(linera wallet request-chain --faucet $FAUCET_URL))
CHAIN_1="${INFO_1[0]}"
OWNER_1="${INFO_1[3]}"
```

Now, compile the `counter` application WebAssembly binaries, publish and create an application instance.
Expand Down
52 changes: 31 additions & 21 deletions examples/crowd-funding/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,40 +42,50 @@ The WebAssembly binaries for the bytecode can be built and published using [step
book](https://linera.dev/developers/getting_started.html),
summarized below.

Set up the path and the helper function.
Set up the path and the helper function. From the root of Linera repository, this can be
achieved as follows:

```bash
export PATH=$PWD/target/debug:$PATH
export PATH="$PWD/target/debug:$PATH"
source /dev/stdin <<<"$(linera net helper 2>/dev/null)"
```

Using the helper function defined by `linera net helper`, set up a local network with two
wallets, and define variables holding their wallet paths (`$LINERA_WALLET_0`,
`$LINERA_WALLET_1`) and storage paths (`$LINERA_STORAGE_0`, `$LINERA_STORAGE_1`). These
variables are named according to a convention that we can access using `--with-wallet $n`
to use the variable `LINERA_WALLET_$n` and `LINERA_STORAGE_$n`; e.g.
`linera --with-wallet 0` is equivalent to
`linera --wallet "$LINERA_WALLET_0" --storage "$LINERA_STORAGE_0"`.
Start the local Linera network and run a faucet:

```bash
linera_spawn_and_read_wallet_variables \
linera net up \
--extra-wallets 1 \
--testing-prng-seed 37
FAUCET_PORT=8079
FAUCET_URL=http://localhost:$FAUCET_PORT
linera_spawn linera net up --with-faucet --faucet-port $FAUCET_PORT

# If you're using a testnet, run this instead:
# LINERA_TMP_DIR=$(mktemp -d)
# FAUCET_URL=https://faucet.testnet-XXX.linera.net # for some value XXX
```

We use the `--testing-prng-seed` argument to ensure that the chain and owner IDs are
predictable.
Create the user wallets and add chains to them:

```bash
CHAIN_0=aee928d4bf3880353b4a3cd9b6f88e6cc6e5ed050860abae439e7782e9b2dfe8
OWNER_0=de166237331a2966d8cf6778e81a8c007b4084be80dc1e0409d51f216c1deaa1
CHAIN_1=582843bc9322ed1928239ce3f6a855f6cd9ea94c8690907f113d6d7a8296a119
OWNER_1=849baa540589d95e167d2622018fa341553bf2aff9f328d760443282c6654deb
export LINERA_WALLET_0="$LINERA_TMP_DIR/wallet_0.json"
export LINERA_STORAGE_0="rocksdb:$LINERA_TMP_DIR/client_0.db"
export LINERA_WALLET_1="$LINERA_TMP_DIR/wallet_1.json"
export LINERA_STORAGE_1="rocksdb:$LINERA_TMP_DIR/client_1.db"

linera --with-wallet 0 wallet init --faucet $FAUCET_URL
linera --with-wallet 1 wallet init --faucet $FAUCET_URL

INFO_0=($(linera --with-wallet 0 wallet request-chain --faucet $FAUCET_URL))
INFO_1=($(linera --with-wallet 1 wallet request-chain --faucet $FAUCET_URL))
CHAIN_0="${INFO_0[0]}"
CHAIN_1="${INFO_1[0]}"
OWNER_0="${INFO_0[3]}"
OWNER_1="${INFO_1[3]}"
```

Alternatively, the command below can be used to list the chains created for the test as
known by each wallet:
Note that `linera --with-wallet 0` is equivalent to `linera --wallet "$LINERA_WALLET_0"
--storage "$LINERA_STORAGE_0"`.

The command below can be used to list the chains created for the test as known by each
wallet:

```bash
linera --with-wallet 0 wallet show
Expand Down
45 changes: 24 additions & 21 deletions examples/fungible/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ summarized below.

Before getting started, make sure that the binary tools `linera*` corresponding to
your version of `linera-sdk` are in your PATH. For scripting purposes, we also assume
that the BASH function `linera_spawn_and_read_wallet_variables` is defined.
that the BASH function `linera_spawn` is defined.

From the root of Linera repository, this can be achieved as follows:

Expand All @@ -42,20 +42,33 @@ export PATH="$PWD/target/debug:$PATH"
source /dev/stdin <<<"$(linera net helper 2>/dev/null)"
```

You may also use `cargo install linera-service` and append the output of
`linera net helper` to your `~/.bash_profile`.

Now, we are ready to set up a local network with an initial wallet owning several initial
chains. In a new BASH shell, enter:
Next, start the local Linera network and run a faucet:

```bash
linera_spawn_and_read_wallet_variables linera net up --testing-prng-seed 37
FAUCET_PORT=8079
FAUCET_URL=http://localhost:$FAUCET_PORT
linera_spawn linera net up --with-faucet --faucet-port $FAUCET_PORT

# If you're using a testnet, run this instead:
# LINERA_TMP_DIR=$(mktemp -d)
# FAUCET_URL=https://faucet.testnet-XXX.linera.net # for some value XXX
```

A new test network is now running and the environment variables `LINERA_WALLET` and
`LINERA_STORAGE` are now defined for the duration of the shell session. We used the
test-only CLI option `--testing-prng-seed` to make keys deterministic and simplify our
presentation.
Create the user wallet and add chains to it:

```bash
export LINERA_WALLET="$LINERA_TMP_DIR/wallet.json"
export LINERA_STORAGE="rocksdb:$LINERA_TMP_DIR/client.db"

linera wallet init --faucet $FAUCET_URL

INFO_1=($(linera wallet request-chain --faucet $FAUCET_URL))
CHAIN_1="${INFO_1[0]}"
OWNER_1="${INFO_1[3]}"
INFO_2=($(linera wallet request-chain --faucet $FAUCET_URL))
CHAIN_2="${INFO_2[0]}"
OWNER_2="${INFO_2[3]}"
```

Now, compile the `fungible` application WebAssembly binaries, and publish them as an application
bytecode:
Expand Down Expand Up @@ -87,16 +100,6 @@ A table will be shown with the chains registered in the wallet and their meta-da
chain should be highlighted in green. Each chain has an `Owner` field, and that is what is used
for the account.

Let's define some variables corresponding to these values. (Note that owner addresses
would not be predictable without `--testing-prng-seed` above.)

```bash
CHAIN_1=aee928d4bf3880353b4a3cd9b6f88e6cc6e5ed050860abae439e7782e9b2dfe8 # default chain for the wallet
OWNER_1=de166237331a2966d8cf6778e81a8c007b4084be80dc1e0409d51f216c1deaa1 # owner of chain 1
CHAIN_2=63620ea465af9e9e0e8e4dd8d21593cc3a719feac5f096df8440f90738f4dbd8 # another chain in the wallet
OWNER_2=598d18f67709fe76ed6a36b75a7c9889012d30b896800dfd027ee10e1afd49a3 # owner of chain 2
```

The example below creates a token application on the default chain CHAIN_1 and gives the owner 100 tokens:

```bash
Expand Down
45 changes: 33 additions & 12 deletions examples/gen-nft/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,46 @@ For more details on the application's contract and how it manages multiple diffe

### Setting Up

Most of this can be referred to the [fungible app README](https://github.com/linera-io/linera-protocol/blob/main/examples/fungible/README.md#setting-up), except for at the end when compiling and publishing the bytecode, what you'll need to do will be slightly different.
Before getting started, make sure that the binary tools `linera*` corresponding to
your version of `linera-sdk` are in your PATH. For scripting purposes, we also assume
that the BASH function `linera_spawn` is defined.

From the root of Linera repository, this can be achieved as follows:

```bash
export PATH="$PWD/target/debug:$PATH"
source /dev/stdin <<<"$(linera net helper 2>/dev/null)"
```

Next, start the local Linera network and run a faucet:

```bash
FAUCET_PORT=8079
FAUCET_URL=http://localhost:$FAUCET_PORT
linera_spawn linera net up --with-faucet --faucet-port $FAUCET_PORT

linera_spawn_and_read_wallet_variables linera net up --testing-prng-seed 37
# If you're using a testnet, run this instead:
# LINERA_TMP_DIR=$(mktemp -d)
# FAUCET_URL=https://faucet.testnet-XXX.linera.net # for some value XXX
```

Compile the `non-fungible` application WebAssembly binaries, and publish them as an application bytecode:
Create the user wallet and add chains to it:

```bash
export LINERA_WALLET="$LINERA_TMP_DIR/wallet.json"
export LINERA_STORAGE="rocksdb:$LINERA_TMP_DIR/client.db"

linera wallet init --faucet $FAUCET_URL

INFO_1=($(linera wallet request-chain --faucet $FAUCET_URL))
INFO_2=($(linera wallet request-chain --faucet $FAUCET_URL))
CHAIN_1="${INFO_1[0]}"
CHAIN_2="${INFO_2[0]}"
OWNER_1="${INFO_1[3]}"
OWNER_2="${INFO_2[3]}"
```

Next, compile the `non-fungible` application WebAssembly binaries, and publish them as an application bytecode:

```bash
(cd examples/gen-nft && cargo build --release --target wasm32-unknown-unknown)
Expand All @@ -52,15 +82,6 @@ Unlike fungible tokens, each NFT is unique and identified by a unique token ID.

Refer to the [fungible app README](https://github.com/linera-io/linera-protocol/blob/main/examples/fungible/README.md#creating-a-token) to figure out how to list the chains created for the test in the default wallet, as well as defining some variables corresponding to these values.

```bash
linera wallet show

CHAIN_1=aee928d4bf3880353b4a3cd9b6f88e6cc6e5ed050860abae439e7782e9b2dfe8 # default chain for the wallet
OWNER_1=de166237331a2966d8cf6778e81a8c007b4084be80dc1e0409d51f216c1deaa1 # owner of chain 1
CHAIN_2=63620ea465af9e9e0e8e4dd8d21593cc3a719feac5f096df8440f90738f4dbd8 # another chain in the wallet
OWNER_2=598d18f67709fe76ed6a36b75a7c9889012d30b896800dfd027ee10e1afd49a3 # owner of chain 2
```

To create the NFT application, run the command below:

```bash
Expand Down
36 changes: 28 additions & 8 deletions examples/hex-game/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,28 +25,48 @@ does not have to wait for any other chain owner to accept any message.
Make sure you have the `linera` binary in your `PATH`, and that it is compatible with your
`linera-sdk` version.

For scripting purposes, we also assume that the BASH function
`linera_spawn_and_read_wallet_variables` is defined. From the root of Linera repository, this can
be achieved as follows:
For scripting purposes, we also assume that the BASH function `linera_spawn` is defined.
From the root of Linera repository, this can be achieved as follows:

```bash
export PATH="$PWD/target/debug:$PATH"
source /dev/stdin <<<"$(linera net helper 2>/dev/null)"
```

To start the local Linera network and create two wallets:
Start the local Linera network and run a faucet:

```bash
linera_spawn_and_read_wallet_variables linera net up --testing-prng-seed 37 --extra-wallets 1
FAUCET_PORT=8079
FAUCET_URL=http://localhost:$FAUCET_PORT
linera_spawn linera net up --with-faucet --faucet-port $FAUCET_PORT

# If you're using a testnet, run this instead:
# LINERA_TMP_DIR=$(mktemp -d)
# FAUCET_URL=https://faucet.testnet-XXX.linera.net # for some value XXX
```

We use the test-only CLI option `--testing-prng-seed` to make keys deterministic and simplify our
explanation.
Create the user wallets and add chains to them:

```bash
CHAIN_1=aee928d4bf3880353b4a3cd9b6f88e6cc6e5ed050860abae439e7782e9b2dfe8
export LINERA_WALLET_0="$LINERA_TMP_DIR/wallet_0.json"
export LINERA_STORAGE_0="rocksdb:$LINERA_TMP_DIR/client_0.db"
export LINERA_WALLET_1="$LINERA_TMP_DIR/wallet_1.json"
export LINERA_STORAGE_1="rocksdb:$LINERA_TMP_DIR/client_1.db"

linera --with-wallet 0 wallet init --faucet $FAUCET_URL
linera --with-wallet 1 wallet init --faucet $FAUCET_URL

INFO_1=($(linera --with-wallet 0 wallet request-chain --faucet $FAUCET_URL))
INFO_2=($(linera --with-wallet 1 wallet request-chain --faucet $FAUCET_URL))
CHAIN_1="${INFO_1[0]}"
CHAIN_2="${INFO_2[0]}"
OWNER_1="${INFO_1[3]}"
OWNER_2="${INFO_2[3]}"
```

Note that `linera -with-wallet 0` or `linera -w0` is equivalent to `linera --wallet
"$LINERA_WALLET_0" --storage "$LINERA_STORAGE_0"`.

### Creating the Game Chain

We open a new chain owned by both `$OWNER_1` and `$OWNER_2`, create the application on it, and
Expand Down
Loading

0 comments on commit f7e48ce

Please sign in to comment.