Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: add regen-sandbox docker setup #1550

Merged
merged 26 commits into from
Nov 15, 2022
Merged
Show file tree
Hide file tree
Changes from 18 commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
6a86381
test: add regen-sandbox docker setup
clevinson Oct 15, 2022
a46f1fd
update data module script
clevinson Oct 15, 2022
4395a96
fix port forwarding
clevinson Oct 21, 2022
1eaf41c
Merge remote-tracking branch 'origin/main' into clev/add-docker-sandbox
clevinson Oct 27, 2022
4606169
Update images/regen-sandbox/README.md
clevinson Nov 7, 2022
d528a62
Update images/regen-sandbox/README.md
clevinson Nov 7, 2022
e3a4665
Update images/regen-sandbox/README.md
clevinson Nov 7, 2022
1a98f22
Update images/regen-sandbox/bootstrap-solo-network.sh
clevinson Nov 7, 2022
358c552
run apk update in Dockerfile
clevinson Nov 10, 2022
d9c581f
fix bridge setup script, add polygon as allowed bridge in genesis.json
clevinson Nov 10, 2022
c83449f
Merge remote-tracking branch 'origin/clev/add-docker-sandbox' into cl…
clevinson Nov 10, 2022
ca32a57
add support for auto generating mnemonic if not provided
clevinson Nov 10, 2022
a59705a
update readme
clevinson Nov 10, 2022
d5d19b5
Update images/regen-sandbox/Dockerfile
clevinson Nov 10, 2022
21566c0
Merge branch 'main' into clev/add-docker-sandbox
clevinson Nov 10, 2022
3bf94d0
add overwrite flag
clevinson Nov 10, 2022
b74320d
update readme
clevinson Nov 10, 2022
f742b45
Merge branch 'main' into clev/add-docker-sandbox
clevinson Nov 10, 2022
2243140
rename bootstrap script
clevinson Nov 15, 2022
68c312f
fix setup scripts to run with correct credit classes
clevinson Nov 15, 2022
568f93a
Update images/regen-sandbox/README.md
clevinson Nov 15, 2022
b020d82
remove vim from regen-sandbox Dockerfile
clevinson Nov 15, 2022
f3a5674
update readme
clevinson Nov 15, 2022
d4b7331
Merge branch 'main' into clev/add-docker-sandbox
clevinson Nov 15, 2022
ed721c3
Merge branch 'main' into clev/add-docker-sandbox
clevinson Nov 15, 2022
d2d75e3
Update images/regen-sandbox/README.md
clevinson Nov 15, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions images/regen-sandbox/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.regen
33 changes: 33 additions & 0 deletions images/regen-sandbox/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
FROM golang:1.18-alpine AS build
clevinson marked this conversation as resolved.
Show resolved Hide resolved

RUN apk -U upgrade
RUN apk add build-base git linux-headers
WORKDIR /work

COPY go.mod go.sum /work/
COPY api/go.mod api/go.sum /work/api/
COPY errors/go.mod errors/go.sum /work/errors/
COPY types/go.mod types/go.sum /work/types/
COPY x/ecocredit/go.mod x/ecocredit/go.sum /work/x/ecocredit/
COPY x/data/go.mod x/data/go.sum /work/x/data/
COPY x/intertx/go.mod x/intertx/go.sum /work/x/intertx/

RUN go mod download
COPY ./ /work

RUN LEDGER_ENABLED=false make clean build

FROM alpine:3.14 AS run
RUN apk add bash curl jq vim
clevinson marked this conversation as resolved.
Show resolved Hide resolved
COPY ./images/regen-sandbox/bootstrap-solo-network.sh /usr/bin/bootstrap-solo-network.sh
clevinson marked this conversation as resolved.
Show resolved Hide resolved

VOLUME /regen
COPY --from=build /work/build/regen /usr/bin/
WORKDIR /regen
EXPOSE 26656 26657 9090 1317

ENV BINARY /usr/bin/regen
ENV REGENHOME /regen/.regen

ENTRYPOINT ["/usr/bin/bootstrap-solo-network.sh"]
STOPSIGNAL SIGTERM
31 changes: 31 additions & 0 deletions images/regen-sandbox/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# Regen Ledger Sandbox

This container (and corresponding setup scripts) is intended to aid with end-to-end testing of client libraries, applications, manual local network tests, and other scenarios where you want to be able to quickly bootstrap a single node regen network (targeting any version or commit hash of regen-ledger), and populate the network with some basic data for your end-to-end testing or exploratory purposes.


Build locally:
```sh
docker build . -f images/regen-sandbox/Dockerfile -t regen-sandbox
```

Run the container:
```sh
# Optionally pass your own testing memonic.
# If no $REGEN_MNEMONIC is provided, a mnemonic
# will be auto-generated and printed to STDOUT
export REGEN_MNEMONIC="YOUR TESTING MNEMONIC"
ryanchristo marked this conversation as resolved.
Show resolved Hide resolved

(cd images/regen-sandbox && docker run -v $(pwd):/regen --env REGEN_MNEMONIC regen-sandbox:latest)
```
clevinson marked this conversation as resolved.
Show resolved Hide resolved

The above command will start up a new chain from genesis, with 5 accounts [`addr1`, ...`addr5`]. All accounts are generated from the same mnemonic with incremental `--account` indices (using HD derivation), and seeded with 10000 REGEN tokens. `addr1` is set as the single validator in this network and has an additional 40000 REGEN tokens, all of which are self-delegated.

If a `./.regen` home directory is detected, it will not initiatize a new chain, but simply run `regen start` with the existing home directory. If you want to always create a fresh home directory, you can override this behavior by adding a `-o` or `--overwrite-home-dir` flag to the end of your `docker run` command.

You can additionally provide a comma separated list of setup scripts to run as an argument at the end of the `docker run` command, like so:

```
docker run -v $(pwd):/regen --env REGEN_MNEMONIC regen-sandbox:latest ecocredit,data
clevinson marked this conversation as resolved.
Show resolved Hide resolved
```

This will look in `./setup` for two files, `./setup/ecocredit.sh` and `./setup/data.sh` and run them in order. Stopping the docker process and running with these arguments a second time will attempt to run the scripts again and potentially popupate a second round of data.
clevinson marked this conversation as resolved.
Show resolved Hide resolved
129 changes: 129 additions & 0 deletions images/regen-sandbox/bootstrap-solo-network.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
#!/usr/bin/env bash

set -eo pipefail

REGEN_CHAIN_ID="regen-sandbox"

regen() {
$BINARY --home $REGENHOME "$@"
}

# parse flags
POSITIONAL_ARGS=()
while [[ $# -gt 0 ]]; do
case $1 in
-o|--overwrite-home-dir)
OVERWRITE_HOMEDIR=true
shift # past argument
;;
-*|--*)
echo "Unknown option $1"
exit 1
;;
*)
POSITIONAL_ARGS+=("$1") # save positional arg
shift # past argument
;;
esac
done

set -- "${POSITIONAL_ARGS[@]}" # restore positional parameters


if [ -d "$REGENHOME" ] && [ "$OVERWRITE_HOMEDIR" != true ]; then
echo "Regen home ($REGENHOME) already exists, skipping bootstrap..."
else
rm -rf $REGENHOME
regen config keyring-backend test
regen config node http://localhost:26657
regen config chain-id $REGEN_CHAIN_ID
regen config broadcast-mode block
regen config output json

# initialize .regen home directory and genesis.json
regen init test_moniker --chain-id $REGEN_CHAIN_ID 2>&1 | jq -Rr '. as $raw | try (fromjson | "Created genesis. chain-id: \(.chain_id), moniker: \(.moniker)") catch $raw'

# modify genesis file and config files (platform dependent usage of sed)
if [[ $(uname -s) == 'Darwin' ]]; then
# change stake denom to uregen
sed -i "" "s/stake/uregen/g" $REGENHOME/config/genesis.json
# set min gas price
sed -i "" "s/minimum-gas-prices = \"\"/minimum-gas-prices = \"0.025uregen\"/g" $REGENHOME/config/app.toml
# decrease block-time so tests run faster
sed -i "" "s/timeout_commit = \"5s\"/timeout_commit = \"500ms\"/g" $REGENHOME/config/config.toml
# bind on all interfaces, enabling ports to be exposed outside docker
sed -i "" "s/127\.0\.0\.1/0.0.0.0/g" $REGENHOME/config/config.toml
else
# change stake denom to uregen
sed -i "s/stake/uregen/g" $REGENHOME/config/genesis.json
# set min gas price
sed -i "s/minimum-gas-prices = \"\"/minimum-gas-prices = \"0.025uregen\"/g" $REGENHOME/config/app.toml
# decrease block-time so tests run faster
sed -i "s/timeout_commit = \"5s\"/timeout_commit = \"500ms\"/g" $REGENHOME/config/config.toml
# bind on all interfaces, enabling ports to be exposed outside docker
sed -i "s/127\.0\.0\.1/0.0.0.0/g" $REGENHOME/config/config.toml
fi

# app specific genesis file modifications
cat .regen/config/genesis.json | jq '.app_state.ecocredit."regen.ecocredit.v1.AllowedBridgeChain"[0] = {"chain_name": "polygon"}' > genesis.json.tmp && mv genesis.json.tmp .regen/config/genesis.json

# setup initial wallets, and validator address with gentx
if ! [ -z "$REGEN_MNEMONIC" ]; then
echo "Adding key to keyring for account name: addr1"
echo $REGEN_MNEMONIC | regen keys add addr1 --account 0 --recover > /dev/null
else
REGEN_MNEMONIC=$(regen keys add addr1 | jq -r '.mnemonic')
echo ""
echo "No \$REGEN_MNEMONIC provided, using generated mnemonic:"
echo " $REGEN_MNEMONIC"
echo ""
echo "Adding key to keyring for account name: addr1"
fi

echo "Adding key to keyring for account name: addr2"
echo $REGEN_MNEMONIC | regen keys add addr2 --account 1 --recover > /dev/null
regen add-genesis-account addr2 10000000000uregen --keyring-backend test

echo "Adding key to keyring for account name: addr3"
echo $REGEN_MNEMONIC | regen keys add addr3 --account 2 --recover > /dev/null
regen add-genesis-account addr3 10000000000uregen --keyring-backend test

echo "Adding key to keyring for account name: addr4"
echo $REGEN_MNEMONIC | regen keys add addr4 --account 3 --recover > /dev/null
regen add-genesis-account addr4 10000000000uregen --keyring-backend test

echo "Adding key to keyring for account name: addr5"
echo $REGEN_MNEMONIC | regen keys add addr5 --account 4 --recover > /dev/null
regen add-genesis-account addr5 10000000000uregen --keyring-backend test

echo "Setting up validator (from addr1)..."
regen add-genesis-account addr1 50000000000uregen --keyring-backend test
regen gentx addr1 40000000000uregen --ip 127.0.0.1

regen collect-gentxs 2>&1 | jq -r '"Collecting gentxs from \(.gentxs_dir) into genesis.json"'
fi

# Make sure we kill the regen process if our script exits while node is running
# in the background
trap "trap - SIGTERM && kill -- -$$" SIGINT SIGTERM EXIT INT

# Start regen chain and immediately move it to the background
echo "Starting up regen node..."
regen start --log_level warn &
REGEN_PID=$!

# Sleep for regen node to full boot up
sleep 3

# run initialization of suites
SUITE_NAMES=$1
for suite in ${SUITE_NAMES//,/ }
do
echo "INFO: Initializing state from './setup/$suite.sh'"
./setup/$suite.sh
done

# wait again on the regen node process so it can be terminated with ctrl+C
echo "Node started & state inialized!"
wait $REGEN_PID

104 changes: 104 additions & 0 deletions images/regen-sandbox/setup/bridge.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
source $(dirname $0)/utils.sh

set -eo pipefail

TX_FLAGS="--from $ADDR1 --yes --fees 5000uregen"

echo "INFO: Creating Credit Class - C01"
clevinson marked this conversation as resolved.
Show resolved Hide resolved
regen tx ecocredit create-class $ADDR1 C "Bridging Credit Class" --class-fee 20000000uregen $TX_FLAGS | log_response

clevinson marked this conversation as resolved.
Show resolved Hide resolved
TEMPDIR=$(mktemp -d)
trap "rm -rf $TEMPDIR" 0 2 3 15

echo "INFO: Bridging credits from polygon and creating new credit batch C01-001-20200101-20210101-001"
cat > $TEMPDIR/msg_bridge_rcv.json <<EOL
{
"body": {
"messages": [
{
"@type": "/regen.ecocredit.v1.MsgBridgeReceive",
"issuer": "$ADDR1",
"class_id": "C01",
"project": {
"reference_id": "VCS-001",
"jurisdiction": "CA",
"metadata": "regen:foobar.rdf"
},
"batch": {
"metadata": "regen:batch1.rdf",
"start_date": "2020-01-01T00:00:00Z",
"end_date": "2021-01-01T00:00:00Z",
"recipient": "$ADDR2",
"amount": "1000"
},
"origin_tx": {
"id": "0x7a70692a348e8688f54ab2bdfe87d925d8cc88932520492a11eaa02dc1283434",
"source": "polygon",
"contract": "0x0000000000000000000000000000000000000001",
"note": "first bridge"
}
}
],
"memo": "",
"timeout_height": "0",
"extension_options": [],
"non_critical_extension_options": []
},
"auth_info": {
"signer_infos": [],
"fee": {
"amount": [{
"denom": "uregen",
"amount": "5000"
}],
"gas_limit": "200000",
"payer": "",
"granter": ""
}
},
"signatures": []
}
EOL

regen tx sign $TEMPDIR/msg_bridge_rcv.json --from $ADDR1 > $TEMPDIR/msg_bridge_rcv_signed.json
regen tx broadcast $TEMPDIR/msg_bridge_rcv_signed.json | log_response

echo "INFO: Bridging credits back to polygon"
cat > $TEMPDIR/msg_bridge.json <<EOL
{
"body": {
"messages": [
{
"@type": "/regen.ecocredit.v1.MsgBridge",
"owner": "$ADDR2",
"target": "polygon",
"recipient": "0x0000000000000000000000000000000000000002",
"credits": [{
"batch_denom": "C01-001-20200101-20210101-001",
"amount": "0.000001"
}]
}
],
"memo": "",
"timeout_height": "0",
"extension_options": [],
"non_critical_extension_options": []
},
"auth_info": {
"signer_infos": [],
"fee": {
"amount": [{
"denom": "uregen",
"amount": "5000"
}],
"gas_limit": "200000",
"payer": "",
"granter": ""
}
},
"signatures": []
}
EOL

regen tx sign $TEMPDIR/msg_bridge.json --from $ADDR2 > $TEMPDIR/msg_bridge_signed.json
regen tx broadcast $TEMPDIR/msg_bridge_signed.json | log_response
33 changes: 33 additions & 0 deletions images/regen-sandbox/setup/data.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
source $(dirname $0)/utils.sh

TX_FLAGS="--from $ADDR1 --yes --fees 5000uregen"

echo "INFO: Anchoring dataset: regen:13toVgf5UjYBz6J29x28pLQyjKz5FpcW3f4bT5uRKGxGREWGKjEdXYG.rdf"
regen tx data anchor regen:13toVgf5UjYBz6J29x28pLQyjKz5FpcW3f4bT5uRKGxGREWGKjEdXYG.rdf $TX_FLAGS | log_response

echo "INFO: Attesting dataset: regen:13toVgf5UjYBz6J29x28pLQyjKz5FpcW3f4bT5uRKGxGREWGKjEdXYG.rdf"
regen tx data attest regen:13toVgf5UjYBz6J29x28pLQyjKz5FpcW3f4bT5uRKGxGREWGKjEdXYG.rdf $TX_FLAGS | log_response

echo "INFO: Defining resolver http://resolver.mydataservice.com"
regen tx data define-resolver "http://resolver.mydataservice.com" $TX_FLAGS | log_response

echo "INFO: Registering dataset to resolver http://resolver.mydataservice.com"
TEMPDIR=$(mktemp -d)
trap "rm -rf $TEMPDIR" 0 2 3 15

cat > $TEMPDIR/content.json <<EOL
{
"content_hashes": [
{
"graph": {
"hash": "YWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWE=",
"digest_algorithm": "DIGEST_ALGORITHM_BLAKE2B_256",
"canonicalization_algorithm": "GRAPH_CANONICALIZATION_ALGORITHM_URDNA2015",
"merkle_tree": "GRAPH_MERKLE_TREE_NONE_UNSPECIFIED"
}
}
]
}
EOL

regen tx data register-resolver 1 $TEMPDIR/content.json $TX_FLAGS | log_response
45 changes: 45 additions & 0 deletions images/regen-sandbox/setup/ecocredit.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
source $(dirname $0)/utils.sh

set -e

TX_FLAGS="--from $ADDR1 --yes --fees 5000uregen"
echo "INFO: Creating Credit Class - C01"
regen tx ecocredit create-class $ADDR1 C "Test Credit Class" --class-fee 20000000uregen $TX_FLAGS | log_response

echo "INFO: Creating project C01-001"
regen tx ecocredit create-project C01 US "Horsetail Ranch" $TX_FLAGS | log_response

echo "INFO: Creating credit batch C01-001-20200101-20210101-001"
TEMPDIR=$(mktemp -d)
trap "rm -rf $TEMPDIR" 0 2 3 15

cat > $TEMPDIR/batch.json <<EOL
{
"project_id": "C01-001",
"issuer": "$ADDR1",
"issuance": [
{
"recipient": "$ADDR1",
"tradable_amount": "1000",
"retired_amount": "500",
"retirement_jurisdiction": "US-WA"
},
{
"recipient": "$ADDR2",
"tradable_amount": "1000",
"retired_amount": "500",
"retirement_jurisdiction": "US-OR"
}
],
"metadata": "regen:13toVgf5UjYBz6J29x28pLQyjKz5FpcW3f4bT5uRKGxGREWGKjEdXYG.rdf",
"start_date": "2020-01-01T00:00:00Z",
"end_date": "2021-01-01T00:00:00Z",
"open": false
}
EOL

regen tx ecocredit create-batch $TEMPDIR/batch.json $TX_FLAGS | log_response


echo "INFO: Creating NCT basket (with C01 as allowed credit class)"
regen tx ecocredit create-basket NCT --credit-type-abbrev C --allowed-classes C01 --basket-fee 20000000uregen --description "Testing NCT Basket" $TX_FLAGS | log_response
13 changes: 13 additions & 0 deletions images/regen-sandbox/setup/utils.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
regen() {
$BINARY --home $REGENHOME "$@"
}

log_response() {
jq -r "if .code == 0 then \"INFO: TxHash: \(.txhash)\" else \"ERROR: (code \(.code)) \(.raw_log)\" end"
}

ADDR1=$(regen keys show -a addr1)
ADDR2=$(regen keys show -a addr2)
ADDR3=$(regen keys show -a addr3)
ADDR4=$(regen keys show -a addr4)
ADDR5=$(regen keys show -a addr5)