Skip to content

Commit

Permalink
feat(sawtooth-ledger): add single sawtooth test ledger image
Browse files Browse the repository at this point in the history
- Enclose starting sawtooth ledger into single container that will fetch and run the entire setup.
- Remove reduntant files, update the readme.
- Add SawtoothTestLedger class to simplify setting the test ledger in jest tests.
- Refactor electricity-trade to generate usage without sawtooth shell patching.
- Refactor electricity-trade to use new sawtooth ledger container and wait until it's healthy.

Closes: hyperledger-cacti#2108

Depends on: hyperledger-cacti#2030

Signed-off-by: Michal Bajer <michal.bajer@fujitsu.com>
  • Loading branch information
outSH authored and petermetz committed Aug 25, 2022
1 parent a776a60 commit d072a23
Show file tree
Hide file tree
Showing 32 changed files with 573 additions and 2,024 deletions.
56 changes: 56 additions & 0 deletions .github/workflows/sawtooth-all-in-one-publish.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
name: sawtooth-all-in-one-publish

on:
push:
# Publish `main` as Docker `latest` image.
branches:
- main

# Publish `v1.2.3` tags as releases.
tags:
- v*

env:
IMAGE_NAME: cactus-sawtooth-all-in-one

jobs:
# Push image to GitHub Packages.
# See also https://docs.docker.com/docker-hub/builds/
build-tag-push-container:
runs-on: ubuntu-20.04
env:
DOCKER_BUILDKIT: 1
DOCKERFILE_PATH: ./tools/docker/sawtooth-all-in-one/Dockerfile
DOCKER_BUILD_DIR: ./tools/docker/sawtooth-all-in-one/
permissions:
packages: write
contents: read

steps:
- uses: actions/checkout@v2.4.0

- name: Build image
run: docker build $DOCKER_BUILD_DIR --file $DOCKERFILE_PATH --tag $IMAGE_NAME --label "runnumber=${GITHUB_RUN_ID}"

- name: Log in to registry
# This is where you will update the PAT to GITHUB_TOKEN
run: echo "${{ secrets.GITHUB_TOKEN }}" | docker login ghcr.io -u ${{ github.actor }} --password-stdin

- name: Push image
run: |
SHORTHASH=$(git rev-parse --short "$GITHUB_SHA")
TODAYS_DATE="$(date +%F)"
DOCKER_TAG="$TODAYS_DATE-$SHORTHASH"
IMAGE_ID=ghcr.io/${{ github.repository_owner }}/$IMAGE_NAME
# Change all uppercase to lowercase
IMAGE_ID=$(echo $IMAGE_ID | tr '[A-Z]' '[a-z]')
# Strip git ref prefix from version
VERSION=$(echo "${{ github.ref }}" | sed -e 's,.*/\(.*\),\1,')
# Strip "v" prefix from tag name
[[ "${{ github.ref }}" == "refs/tags/"* ]] && VERSION=$(echo $VERSION | sed -e 's/^v//')
# Do not use the `latest` tag at all, tag with date + git short hash if there is no git tag
[ "$VERSION" == "main" ] && VERSION=$DOCKER_TAG
echo IMAGE_ID=$IMAGE_ID
echo VERSION=$VERSION
docker tag $IMAGE_NAME $IMAGE_ID:$VERSION
docker push $IMAGE_ID:$VERSION
19 changes: 8 additions & 11 deletions examples/cactus-example-electricity-trade/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,7 @@ In this example, we use the Sawtooth intkey transaction processor as an applicat
- This script will start all ledger docker containers, networks, and will setup configuration needed to operate the sample app.
- (NOTICE: Before executing the above, your account needs to be added to the docker group (`usermod -a -G docker YourAccount` from root user))
- On success, this should start the following containers:
- `sawtooth-shell-default`
- `sawtooth-settings-tp-default`
- `sawtooth-intkey-tp-python-default`
- `sawtooth-xo-tp-python-default`
- `sawtooth-rest-api-default`
- `sawtooth-devmode-engine-rust-default`
- `sawtooth-validator-default`
- `sawtooth_all_in_one_ledger_1x`
- `geth1`
1. Launch electricity-trade and validators from local `docker-compose.yml` (use separate console for that, docker-compose will block your prompt):
Expand Down Expand Up @@ -131,10 +125,13 @@ In this example, we use the Sawtooth intkey transaction processor as an applicat
```
# Create intkey batch representing electricity usage
Writing to batches.intkey...
# Sumbit electricity usage
batches: 2, batch/sec: 159.92313264955962
{
"link": "http://rest-api:8008/batch_statuses?id=4e85337e170917c138e4f7de44c85c9dea9c5e17916fded672b90adb85a07ca009002580f8629660e26e1117e9ac15f4c1164d9dc05fc77ac8e212672dc5e97a"
}
# Increase usage
{
"link": "http://rest-api:8008/batch_statuses?id=88b4dfa2128c7ad4b646b4fe6be878948f7c17651baf9c6384080a9eaae5036e219c432b46f74331a2d56b80bf2dcc94496ff261d1a941f23210d637badacf14"
}
```
1. (Optional) Check the balance on Ethereum accounts using the following script
Expand Down
4 changes: 2 additions & 2 deletions examples/cactus-example-electricity-trade/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ services:
ports:
- "5140:5140"
networks:
- sawtooth_net
- sawtooth_aio_testnet_1x
- cactus-example-electricity-trade-net
volumes:
- type: bind
Expand Down Expand Up @@ -61,7 +61,7 @@ services:
target: /etc/cactus

networks:
sawtooth_net:
sawtooth_aio_testnet_1x:
external: true
geth1net:
external: true
Expand Down
14 changes: 4 additions & 10 deletions examples/cactus-example-electricity-trade/script-cleanup.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,17 @@ echo ">> Remove the config files on your machine"
rm -rf ./etc/cactus/

echo ">> Stop and remove the docker containers"
docker rm -f sawtooth-shell-default \
sawtooth-settings-tp-default \
sawtooth-intkey-tp-python-default \
sawtooth-xo-tp-python-default \
sawtooth-rest-api-default \
sawtooth-devmode-engine-rust-default \
sawtooth-validator-default \
docker rm -f sawtooth_all_in_one_ledger_1x \
geth1 \
cactus-example-electricity-trade-blp \
cactus-example-electricity-trade-ethereum-validator \
cactus-example-electricity-trade-sawtooth-validator \
cmd-socketio-base-dummy

echo ">> Remove docker networks"
docker network rm sawtooth_net \
electricity-trade_default \
electricity-trade_electricity-trade-net \
docker network rm sawtooth_aio_testnet_1x \
cactus-example-electricity-trade_default \
cactus-example-electricity-trade_cactus-example-electricity-trade-net \
geth1net \
geth-testnet_default

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
# Copyright 2020-2022 Hyperledger Cactus Contributors
# SPDX-License-Identifier: Apache-2.0

echo "# Create intkey batch representing electricity usage"
docker exec -it sawtooth-shell-default intkey create_batch --key-name MI000001 --value-set 50 --value-inc 24
echo "# Create intkey representing electricity usage"
docker exec -t sawtooth_all_in_one_ledger_1x shell intkey set MI000001 50 --url http://rest-api:8008

echo -e "\n# Sumbit electricity usage"
docker exec -it sawtooth-shell-default sawtooth batch submit -f batches.intkey --url http://rest-api:8008
echo "# Increase usage"
docker exec -t sawtooth_all_in_one_ledger_1x shell intkey inc MI000001 24 --url http://rest-api:8008
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@ echo -e "\n\n# Destination Eth balance:"
curl localhost:5034/api/v1/bl/balance/9d624f7995e8bd70251f8265f2f9f2b49f169c55

echo -e "\n\n# Electricity usage"
docker exec -it sawtooth-shell-default intkey list --url http://rest-api:8008
docker exec -t sawtooth_all_in_one_ledger_1x shell intkey list --url http://rest-api:8008
16 changes: 13 additions & 3 deletions examples/cactus-example-electricity-trade/script-start-ledgers.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ set -e

ROOT_DIR="../.." # Path to cactus root dir
CONFIG_VOLUME_PATH="./etc/cactus" # Docker volume with shared configuration
WAIT_TIME=10 # How often to check container status

export CACTUS_SAWTOOTH_LEDGER_CONTAINER_NAME="sawtooth_all_in_one_ledger_1x"

# Cert options
CERT_CURVE_NAME="prime256v1"
Expand Down Expand Up @@ -66,12 +69,19 @@ function copy_ethereum_validator_config() {
}

function start_sawtooth_testnet() {
pushd "${ROOT_DIR}/tools/docker/sawtooth-testnet"
pushd "${ROOT_DIR}/tools/docker/sawtooth-all-in-one"
./script-start-docker.sh
popd

# Patch create_batch with our logic (will generate electricity usage)
docker cp ./tools/create_batch/create_batch3.py sawtooth-shell-default:/usr/lib/python3/dist-packages/sawtooth_intkey/client_cli/create_batch.py
# Wait for fabric cotnainer to become healthy
health_status="$(docker inspect -f '{{.State.Health.Status}}' ${CACTUS_SAWTOOTH_LEDGER_CONTAINER_NAME})"
while ! [ "${health_status}" == "healthy" ]
do
echo "Waiting for sawtooth container... current status => ${health_status}"
sleep $WAIT_TIME
health_status="$(docker inspect -f '{{.State.Health.Status}}' ${CACTUS_SAWTOOTH_LEDGER_CONTAINER_NAME})"
done
echo ">> Sawtooth ${CACTUS_FABRIC_ALL_IN_ONE_VERSION} started."
}

function copy_sawtooth_validator_config() {
Expand Down
Loading

0 comments on commit d072a23

Please sign in to comment.