Skip to content

Commit

Permalink
Fix/pep pub key (#9)
Browse files Browse the repository at this point in the history
* Remove pub key from create aggregated key msg & Update decryption to rely on active pub key

* Fix incorrect args num in cli

* Add setup relayer script & update relayer config & pep test script

* Add hermes relayer to test requirement

* Fix pub key ibc ack packet is empty

* Update setup testing env readme

* Remove ./ for the encrypter command

* Fix key file deleted before adding to hermes

* add incorrect key submission test

---------

Co-authored-by: p0p3yee <[email protected]>
Co-authored-by: p0p3ye <[email protected]>
  • Loading branch information
3 people authored May 31, 2023
1 parent 4659717 commit 9e92281
Show file tree
Hide file tree
Showing 17 changed files with 239 additions and 211 deletions.
6 changes: 6 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,7 @@ sync-docs:
###############################################################################

integration-test-all: init-test-framework \
init-relayer \
test-keyshare-module \
test-pep-module
-@rm -rf ./data
Expand All @@ -184,6 +185,11 @@ test-pep-module:
@echo "Testing Pep module..."
./scripts/tests/pep.sh

init-relayer:
@echo "Initializing hermes relayer..."
./scripts/tests/relayer.sh
@sleep 2

init-test-framework: clean-testing-data install
@echo "Initializing fairyring..."
./scripts/tests/start.sh
Expand Down
16 changes: 8 additions & 8 deletions hermes_config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,10 @@ host = '127.0.0.1'
port = 3001

[[chains]]
id = 'auction'
id = 'fairyring_test_1'
type = 'CosmosSdk'
rpc_addr = 'http://0.0.0.0:26657'
websocket_addr = 'ws://0.0.0.0:26657/websocket'
rpc_addr = 'http://0.0.0.0:16657'
websocket_addr = 'ws://0.0.0.0:16657/websocket'
grpc_addr = 'http://0.0.0.0:9090'
rpc_timeout = '10s'
account_prefix = 'cosmos'
Expand All @@ -53,17 +53,17 @@ denominator = '3'

[chains.gas_price]
price = 0.1
denom = 'stake'
denom = 'frt'

[chains.address_type]
derivation = 'cosmos'


[[chains]]
id = 'destination'
id = 'fairyring_test_2'
type = 'CosmosSdk'
rpc_addr = 'http://0.0.0.0:26659'
websocket_addr = 'ws://0.0.0.0:26659/websocket'
rpc_addr = 'http://0.0.0.0:26657'
websocket_addr = 'ws://0.0.0.0:26657/websocket'
grpc_addr = 'http://0.0.0.0:9092'
rpc_timeout = '10s'
account_prefix = 'cosmos'
Expand All @@ -85,7 +85,7 @@ denominator = '3'

[chains.gas_price]
price = 0.1
denom = 'stake'
denom = 'frt'

[chains.address_type]
derivation = 'cosmos'
3 changes: 1 addition & 2 deletions proto/fairyring/pep/aggregated_key_share.proto
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ option go_package = "fairyring/x/pep/types";
message AggregatedKeyShare {
uint64 height = 1;
string data = 2;
string publicKey = 3;
string creator = 4;
string creator = 3;
}

1 change: 0 additions & 1 deletion proto/fairyring/pep/tx.proto
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ message MsgCreateAggregatedKeyShare {
string creator = 1;
uint64 height = 2;
string data = 3;
string publicKey = 4;
}

message MsgCreateAggregatedKeyShareResponse {}
5 changes: 4 additions & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,10 @@ make install
3. Building the executable of [ShareGenerator](https://github.com/FairBlock/ShareGenerator) and [Encrypter](https://github.com/FairBlock/encrypter) and put them in this directory


4. Running the tests
4. Install [Hermes Relayer](https://hermes.informal.systems/) by following this [guide](https://hermes.informal.systems/quick-start/installation.html)


5. Running the tests

```bash
make integration-test-all
Expand Down
25 changes: 23 additions & 2 deletions scripts/tests/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,33 @@ This folder contains the test scripts for keyshare & pep modules

## Requirement

1. Make sure you have the executable of [ShareGenerator](https://github.com/FairBlock/ShareGenerator) and [Encrypter](https://github.com/FairBlock/encrypter) ready in the `fairyring` directory. (Same directory as where the `Makefile` is located at)
1. Make sure you have the executable of [ShareGenerator](https://github.com/FairBlock/ShareGenerator) and [Encrypter](https://github.com/FairBlock/encrypter) installed, that you were able to call them in terminal

2. Rename them to `ShareGenerator` and `encrypter` or you can update the name in scripts.
To make sure you have them in the $PATH and is able to run in terminal

```bash
> where ShareGenerator
/Your_HOME/go/bin/ShareGenerator

where encrypter
/Your_HOME/go/bin/encrypter
```

2. Make sure you have [Hermes Relayer](https://hermes.informal.systems/) installed, here is the [installation guide](https://hermes.informal.systems/quick-start/installation.html) of Hermes Relayer

3. Make sure you have `fairyringd` executable in your PATH, type `where fairyringd` in terminal to check

```bash
> where fairyringd
/Your_HOME/go/bin/fairyringd
```

If it show `faiyringd not found`, make sure you already run the following command in fairyring directory:
```bash
make build
make install
```

## Start testing

```
Expand Down
74 changes: 47 additions & 27 deletions scripts/tests/pep.sh
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,19 @@ ENCRYPTER=encrypter
BINARY=fairyringd
CHAIN_DIR=$(pwd)/data
CHAINID_1=fairyring_test_1
CHAIN1_NODE=tcp://localhost:16657
CHAINID_2=fairyring_test_2
CHAIN2_NODE=tcp://localhost:26657


WALLET_1=$($BINARY keys show wallet1 -a --keyring-backend test --home $CHAIN_DIR/$CHAINID_1)
VALIDATOR_1=$($BINARY keys show val1 -a --keyring-backend test --home $CHAIN_DIR/$CHAINID_1)
WALLET_2=$($BINARY keys show wallet2 -a --keyring-backend test --home $CHAIN_DIR/$CHAINID_2)
VALIDATOR_2=$($BINARY keys show val2 -a --keyring-backend test --home $CHAIN_DIR/$CHAINID_2)


echo "Query new account pep nonce from pep module on chain fairyring_test_1"
RESULT=$($BINARY query pep show-pep-nonce $VALIDATOR_1 --home $CHAIN_DIR/$CHAINID_1 --chain-id $CHAINID_1 --node tcp://localhost:16657 -o json)
echo "Query new account pep nonce from pep module on chain fairyring_test_2"
RESULT=$($BINARY query pep show-pep-nonce $VALIDATOR_2 --home $CHAIN_DIR/$CHAINID_2 --chain-id $CHAINID_2 --node $CHAIN2_NODE -o json)
VALIDATOR_PEP_NONCE=$(echo "$RESULT" | jq -r '.pepNonce.nonce')
if [ "$VALIDATOR_PEP_NONCE" != "0" ]; then
echo "ERROR: Pep module query Pep Nonce error. Expected Pep Nonce to be 0, got '$VALIDATOR_PEP_NONCE'"
Expand All @@ -34,8 +38,8 @@ fi


echo "Query aggregated key share from key share module for submitting to pep module on chain fairyring_test_1"
CURRENT_BLOCK=$($BINARY query block --home $CHAIN_DIR/$CHAINID_1 --node tcp://localhost:16657 | jq -r '.block.header.height')
RESULT=$($BINARY query keyshare list-aggregated-key-share --node tcp://localhost:16657 -o json)
CURRENT_BLOCK=$($BINARY query block --home $CHAIN_DIR/$CHAINID_1 --node $CHAIN1_NODE | jq -r '.block.header.height')
RESULT=$($BINARY query keyshare list-aggregated-key-share --node $CHAIN1_NODE -o json)
AGG_KEY_HEIGHT=$(echo "$RESULT" | jq -r '.aggregatedKeyShare[0].height')
AGG_KEY=$(echo "$RESULT" | jq -r '.aggregatedKeyShare[0].data')
if [ "$CURRENT_BLOCK" -ge "$AGG_KEY_HEIGHT" ]; then
Expand All @@ -45,16 +49,16 @@ fi


echo "Query master public key from key share module for submitting to pep module on chain fairyring_test_1"
PUB_KEY=$($BINARY query keyshare show-active-pub-key --node tcp://localhost:16657 -o json | jq -r '.activePubKey.publicKey')
PUB_KEY=$($BINARY query keyshare show-active-pub-key --node $CHAIN1_NODE -o json | jq -r '.activePubKey.publicKey')
if [ "$PUB_KEY" == "" ]; then
echo "ERROR: Query master public key from key share module error, expecting an active public key, got '$PUB_KEY'"
exit 1
fi


echo "Submit encrypted tx with invalid block height to pep module on chain fairyring_test_1"
echo "Submit encrypted tx with invalid block height to pep module on chain fairyring_test_2"
CURRENT_BLOCK=$($BINARY query block --home $CHAIN_DIR/$CHAINID_1 --node tcp://localhost:16657 | jq -r '.block.header.height')
RESULT=$($BINARY tx pep submit-encrypted-tx 0000 $((CURRENT_BLOCK - 1)) --from $VALIDATOR_1 --home $CHAIN_DIR/$CHAINID_1 --chain-id $CHAINID_1 --node tcp://localhost:16657 --broadcast-mode block --keyring-backend test -o json -y)
RESULT=$($BINARY tx pep submit-encrypted-tx 0000 $((CURRENT_BLOCK - 1)) --from $VALIDATOR_2 --home $CHAIN_DIR/$CHAINID_2 --chain-id $CHAINID_2 --node $CHAIN2_NODE --broadcast-mode block --keyring-backend test -o json -y)
ERROR_MSG=$(echo "$RESULT" | jq -r '.raw_log')
if [[ "$ERROR_MSG" != *"Invalid target block height"* ]]; then
echo "ERROR: Pep module submit encrypted tx with invalid block height error. Expected tx to failed with error invalid target block height, got '$ERROR_MSG'"
Expand All @@ -63,8 +67,8 @@ if [[ "$ERROR_MSG" != *"Invalid target block height"* ]]; then
fi


echo "Query account pep nonce before submitting encrypted tx from pep module on chain fairyring_test_1"
RESULT=$($BINARY query pep show-pep-nonce $VALIDATOR_1 --home $CHAIN_DIR/$CHAINID_1 --chain-id $CHAINID_1 --node tcp://localhost:16657 -o json)
echo "Query account pep nonce before submitting encrypted tx from pep module on chain fairyring_test_2"
RESULT=$($BINARY query pep show-pep-nonce $VALIDATOR_2 --home $CHAIN_DIR/$CHAINID_2 --chain-id $CHAINID_2 --node $CHAIN2_NODE -o json)
VALIDATOR_PEP_NONCE_BEFORE=$(echo "$RESULT" | jq -r '.pepNonce.nonce')
if [ "$VALIDATOR_PEP_NONCE_BEFORE" != "0" ]; then
echo "ERROR: Pep module query Pep Nonce error. Expected Pep Nonce to be 0, got '$VALIDATOR_PEP_NONCE'"
Expand All @@ -73,28 +77,28 @@ if [ "$VALIDATOR_PEP_NONCE_BEFORE" != "0" ]; then
fi


echo "Query target account token balance before submitting encrypted tx from pep module on chain fairyring_test_1"
RESULT=$($BINARY query bank balances $WALLET_1 --node tcp://localhost:16657 -o json)
echo "Query target account token balance before submitting encrypted tx from pep module on chain fairyring_test_2"
RESULT=$($BINARY query bank balances $WALLET_2 --node $CHAIN2_NODE -o json)
TARGET_BAL_DENOM=$(echo "$RESULT" | jq -r '.balances[0].denom')
TARGET_BAL=$(echo "$RESULT" | jq -r '.balances[0].amount')
echo "Target account has: $TARGET_BAL $TARGET_BAL_DENOM before encrypted bank send tx"


echo "Signing bank send tx with pep nonce: '$VALIDATOR_PEP_NONCE_BEFORE'"
echo "Sending 1 $TARGET_BAL_DENOM to target address"
$BINARY tx bank send $VALIDATOR_1 $WALLET_1 1$TARGET_BAL_DENOM --from $VALIDATOR_1 --home $CHAIN_DIR/$CHAINID_1 --chain-id $CHAINID_1 --node tcp://localhost:16657 --keyring-backend test --generate-only -o json -y > unsigned.json
SIGNED_DATA=$($BINARY tx sign unsigned.json --from $VALIDATOR_1 --offline --account-number 0 --sequence $VALIDATOR_PEP_NONCE_BEFORE --home $CHAIN_DIR/$CHAINID_1 --chain-id $CHAINID_1 --node tcp://localhost:16657 --keyring-backend test -y)
$BINARY tx bank send $VALIDATOR_2 $WALLET_2 1$TARGET_BAL_DENOM --from $VALIDATOR_2 --home $CHAIN_DIR/$CHAINID_2 --chain-id $CHAINID_2 --node $CHAIN2_NODE --keyring-backend test --generate-only -o json -y > unsigned.json
SIGNED_DATA=$($BINARY tx sign unsigned.json --from $VALIDATOR_2 --offline --account-number 0 --sequence $VALIDATOR_PEP_NONCE_BEFORE --home $CHAIN_DIR/$CHAINID_2 --chain-id $CHAINID_2 --node $CHAIN2_NODE --keyring-backend test -y)


echo "Encrypting signed tx with Pub key: '$PUB_KEY'"
CIPHER=$(./$ENCRYPTER $AGG_KEY_HEIGHT $PUB_KEY $SIGNED_DATA)
CIPHER=$($ENCRYPTER $AGG_KEY_HEIGHT $PUB_KEY $SIGNED_DATA)


rm -r unsigned.json &> /dev/null


echo "Submit encrypted tx to pep module on chain fairyring_test_1"
RESULT=$($BINARY tx pep submit-encrypted-tx $CIPHER $AGG_KEY_HEIGHT --from $VALIDATOR_1 --home $CHAIN_DIR/$CHAINID_1 --chain-id $CHAINID_1 --node tcp://localhost:16657 --broadcast-mode block --keyring-backend test -o json -y)
echo "Submit encrypted tx to pep module on chain fairyring_test_2"
RESULT=$($BINARY tx pep submit-encrypted-tx $CIPHER $AGG_KEY_HEIGHT --from $VALIDATOR_2 --home $CHAIN_DIR/$CHAINID_2 --chain-id $CHAINID_2 --node $CHAIN2_NODE --broadcast-mode block --keyring-backend test -o json -y)
EVENT_TYPE=$(echo "$RESULT" | jq -r '.logs[0].events[1].type')
TARGET_HEIGHT=$(echo "$RESULT" | jq -r '.logs[0].events[1].attributes[1].value')
if [ "$EVENT_TYPE" != "new-encrypted-tx-submitted" ] && [ "$TARGET_HEIGHT" != "$AGG_KEY_HEIGHT" ]; then
Expand All @@ -105,8 +109,8 @@ if [ "$EVENT_TYPE" != "new-encrypted-tx-submitted" ] && [ "$TARGET_HEIGHT" != "$
fi


echo "Query account pep nonce after submitting encrypted tx from pep module on chain fairyring_test_1"
RESULT=$($BINARY query pep show-pep-nonce $VALIDATOR_1 --home $CHAIN_DIR/$CHAINID_1 --chain-id $CHAINID_1 --node tcp://localhost:16657 -o json)
echo "Query account pep nonce after submitting encrypted tx from pep module on chain fairyring_test_2"
RESULT=$($BINARY query pep show-pep-nonce $VALIDATOR_2 --home $CHAIN_DIR/$CHAINID_2 --chain-id $CHAINID_2 --node $CHAIN2_NODE -o json)
VALIDATOR_PEP_NONCE=$(echo "$RESULT" | jq -r '.pepNonce.nonce')
if [ "$VALIDATOR_PEP_NONCE" != "0" ]; then
echo "ERROR: Pep module query Pep Nonce error. Expected Pep Nonce to be 0, got '$VALIDATOR_PEP_NONCE'"
Expand All @@ -115,8 +119,8 @@ if [ "$VALIDATOR_PEP_NONCE" != "0" ]; then
fi


echo "Submit valid aggregated key to pep module on chain fairyring_test_1"
RESULT=$($BINARY tx pep create-aggregated-key-share $AGG_KEY_HEIGHT $AGG_KEY $PUB_KEY --from $VALIDATOR_1 --home $CHAIN_DIR/$CHAINID_1 --chain-id $CHAINID_1 --node tcp://localhost:16657 --broadcast-mode block --keyring-backend test -o json -y)
echo "Submit valid aggregated key to pep module on chain fairyring_test_2"
RESULT=$($BINARY tx pep create-aggregated-key-share $AGG_KEY_HEIGHT $AGG_KEY --from $VALIDATOR_2 --home $CHAIN_DIR/$CHAINID_2 --chain-id $CHAINID_2 --node $CHAIN2_NODE --broadcast-mode block --keyring-backend test -o json -y)
ACTION=$(echo "$RESULT" | jq -r '.logs[0].events[0].attributes[0].value')
if [ "$ACTION" != "/fairyring.pep.MsgCreateAggregatedKeyShare" ]; then
echo "ERROR: Pep module submit aggregated key error. Expected tx action to be MsgCreateAggregatedKeyShare, got '$ACTION'"
Expand All @@ -128,30 +132,46 @@ fi
sleep 2


echo "Query latest height from pep module on chain fairyring_test_1"
RESULT=$($BINARY q pep latest-height --node tcp://localhost:16657 -o json | jq -r '.height')
echo "Query latest height from pep module on chain fairyring_test_2"
RESULT=$($BINARY q pep latest-height --node $CHAIN2_NODE -o json | jq -r '.height')
if [ "$RESULT" != "$AGG_KEY_HEIGHT" ]; then
echo "ERROR: Pep module query latest height error, Expected latest height to be same as aggregated key share height: '$AGG_KEY_HEIGHT', got '$RESULT'"
exit 1
fi


echo "Query account pep nonce after encrypted tx being processed from pep module on chain fairyring_test_1"
RESULT=$($BINARY query pep show-pep-nonce $VALIDATOR_1 --home $CHAIN_DIR/$CHAINID_1 --chain-id $CHAINID_1 --node tcp://localhost:16657 -o json)
echo "Query account pep nonce after encrypted tx being processed from pep module on chain fairyring_test_2"
RESULT=$($BINARY query pep show-pep-nonce $VALIDATOR_2 --home $CHAIN_DIR/$CHAINID_2 --chain-id $CHAINID_2 --node $CHAIN2_NODE -o json)
VALIDATOR_PEP_NONCE=$(echo "$RESULT" | jq -r '.pepNonce.nonce')
if [ "$VALIDATOR_PEP_NONCE" != "1" ]; then
echo "ERROR: Pep module query Pep Nonce error. Expected Pep Nonce to be 1, got '$VALIDATOR_PEP_NONCE'"
echo "ERROR MESSAGE: $(echo "$RESULT" | jq -r '.raw_log')"
exit 1
fi


echo "Query target account token balance after encrypted tx being executed from pep module on chain fairyring_test_1"
RESULT=$($BINARY query bank balances $WALLET_1 --node tcp://localhost:16657 -o json)
echo "Query target account token balance after encrypted tx being executed from pep module on chain fairyring_test_2"
RESULT=$($BINARY query bank balances $WALLET_2 --node $CHAIN2_NODE -o json)
TARGET_BAL_DENOM=$(echo "$RESULT" | jq -r '.balances[0].denom')
TARGET_BAL_AFTER=$(echo "$RESULT" | jq -r '.balances[0].amount')
echo "Target account has: $TARGET_BAL_AFTER $TARGET_BAL_DENOM after encrypted bank send tx being executed, balance increased $(($TARGET_BAL_AFTER - $TARGET_BAL)) $TARGET_BAL_DENOM"

echo "Submit invalid aggregated key to pep module on chain fairyring_test_2"
RESULT=$($BINARY tx pep create-aggregated-key-share $((AGG_KEY_HEIGHT+1)) 123123123 --from $VALIDATOR_2 --home $CHAIN_DIR/$CHAINID_2 --chain-id $CHAINID_2 --node $CHAIN2_NODE --broadcast-mode block --keyring-backend test -o json -y)
ACTION=$(echo "$RESULT" | jq -r '.logs[0].events[0].attributes[0].value')
if [ "$ACTION" != "/fairyring.pep.MsgCreateAggregatedKeyShare" ]; then
echo "ERROR: Pep module submit aggregated key error. Expected tx action to be MsgCreateAggregatedKeyShare, got '$ACTION'"
echo "ERROR MESSAGE: $(echo "$RESULT" | jq -r '.raw_log')"
exit 1
fi

sleep 2

echo "Query latest height from pep module on chain fairyring_test_2"
RESULT=$($BINARY q pep latest-height --node $CHAIN2_NODE -o json | jq -r '.height')
if [ "$RESULT" != "$AGG_KEY_HEIGHT" ]; then
echo "ERROR: Pep module query latest height error, Expected latest height to be same as aggregated key share height: '$AGG_KEY_HEIGHT', got '$RESULT'"
exit 1
fi

echo ""
echo "###########################################################"
Expand Down
29 changes: 29 additions & 0 deletions scripts/tests/relayer.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#!/bin/bash

echo ""
echo "###########################################################"
echo "# Setting up Relayer between two chain #"
echo "###########################################################"
echo ""


CHAINID_1=fairyring_test_1
CHAINID_2=fairyring_test_2
CONFIG_FILE=hermes_config.toml

echo "Creating client on both chain..."
hermes --config $CONFIG_FILE create client --host-chain $CHAINID_1 --reference-chain $CHAINID_2
hermes --config $CONFIG_FILE create client --host-chain $CHAINID_2 --reference-chain $CHAINID_1

echo "Creating connection..."
hermes --config $CONFIG_FILE create connection --a-chain $CHAINID_1 --a-client 07-tendermint-0 --b-client 07-tendermint-0

echo "Creating channel..."
hermes --config $CONFIG_FILE create channel --a-chain $CHAINID_1 --a-connection connection-0 --a-port pep --b-port pep --channel-version pep-1


echo ""
echo "###########################################################"
echo "# Successfully Setup Relayer between two chain #"
echo "###########################################################"
echo ""
Loading

0 comments on commit 9e92281

Please sign in to comment.