Skip to content

Commit

Permalink
add tests for operator commands
Browse files Browse the repository at this point in the history
Signed-off-by: Jun Kimura <[email protected]>
  • Loading branch information
bluele committed Jun 17, 2024
1 parent 74087d4 commit 97a3705
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 14 deletions.
50 changes: 36 additions & 14 deletions relay/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,14 @@ import (
)

const (
flagSrc = "src"
flagHeight = "height"
flagELCClientID = "elc_client_id"
flagNewOperators = "new_operators"
flagNonce = "nonce"
flagThresholdNumerator = "threshold_numerator"
flagThresholdDenominator = "threshold_denominator"
flagSrc = "src"
flagHeight = "height"
flagELCClientID = "elc_client_id"
flagNewOperators = "new_operators"
flagNonce = "nonce"
flagThresholdNumerator = "threshold_numerator"
flagThresholdDenominator = "threshold_denominator"
flagPermissionlessOperators = "permissionless_operators"
)

func LCPCmd(ctx *config.Context) *cobra.Command {
Expand Down Expand Up @@ -304,25 +305,38 @@ func updateOperatorsCmd(ctx *config.Context) *cobra.Command {
counterparty = c[src]
}
prover := target.Prover.(*Prover)

newOperators := viper.GetStringSlice(flagNewOperators)
threshold := Fraction{
Numerator: viper.GetUint64(flagThresholdNumerator),
Denominator: viper.GetUint64(flagThresholdDenominator),
viper.GetBool(flagPermissionlessOperators)
if len(newOperators) == 0 && !viper.GetBool(flagPermissionlessOperators) {
return fmt.Errorf("either new operators or permissionless operators must be provided")
} else if len(newOperators) > 0 && viper.GetBool(flagPermissionlessOperators) {
return fmt.Errorf("both new operators and permissionless operators cannot be provided")
}
nonce := viper.GetUint64(flagNonce)

var newOpAddrs []common.Address
for _, op := range newOperators {
if !common.IsHexAddress(op) {
return fmt.Errorf("invalid operator address: %s", op)
}
newOpAddrs = append(newOpAddrs, common.HexToAddress(op))
}
threshold := Fraction{
Numerator: viper.GetUint64(flagThresholdNumerator),
Denominator: viper.GetUint64(flagThresholdDenominator),
}
nonce := viper.GetUint64(flagNonce)
return prover.updateOperators(counterparty, nonce, newOpAddrs, threshold)
},
}
cmd = thresholdFlag(nonceFlag(newOperatorsFlag(srcFlag(cmd))))
cmd.MarkFlagRequired(flagNewOperators)
cmd = thresholdFlag(
nonceFlag(
permissionlessOperatorsFlag(
newOperatorsFlag(
srcFlag(cmd),
),
),
),
)
cmd.MarkFlagRequired(flagThresholdNumerator)
cmd.MarkFlagRequired(flagThresholdDenominator)
cmd.MarkFlagRequired(flagNonce)
Expand Down Expand Up @@ -380,3 +394,11 @@ func thresholdFlag(cmd *cobra.Command) *cobra.Command {
}
return cmd
}

func permissionlessOperatorsFlag(cmd *cobra.Command) *cobra.Command {
cmd.Flags().BoolP(flagPermissionlessOperators, "", false, "a boolean value whether the new operators are permissionless")
if err := viper.BindPFlag(flagPermissionlessOperators, cmd.Flags().Lookup(flagPermissionlessOperators)); err != nil {
panic(err)
}
return cmd
}
1 change: 1 addition & 0 deletions scripts/run_e2e_test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,6 @@ make -C tests/e2e/cases/tm2tm restore

make -C tests/e2e/cases/tm2tm test-relay
make -C tests/e2e/cases/tm2tm test-elc-cmd
make -C tests/e2e/cases/tm2tm test-operators
make -C tests/e2e/cases/tm2tm network-down
kill $LCP_PID
4 changes: 4 additions & 0 deletions tests/e2e/cases/tm2tm/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,7 @@ restore:
.PHONY: test-elc-cmd
test-elc-cmd:
RLY_BIN=$(RLY_BIN) ./scripts/test-elc-cmd

.PHONY: test-operators
test-operators:
RLY_BIN=$(RLY_BIN) ./scripts/test-operators
50 changes: 50 additions & 0 deletions tests/e2e/cases/tm2tm/scripts/test-operators
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
#!/bin/bash
set -eu

RLY="${RLY_BIN} --debug"

UPDATE_CMD="${RLY} lcp update-operators ibc01"

for IS_SRC in true false; do
echo "src=${IS_SRC}"
if [ "${IS_SRC}" = true ]; then
NEW_OPERATOR=0xcb96F8d6C2d543102184d679D7829b39434E4EEc
else
NEW_OPERATOR=0x9722414d09f43fb02235d739B50F4C027F43e657
fi

output=$(${UPDATE_CMD} --src=${IS_SRC} --nonce 2 --new_operators ${NEW_OPERATOR} --threshold_denominator 1 --threshold_numerator 1 2>&1 || true)
if [[ $output == *"invalid nonce"* ]]; then
echo "01: OK"
else
echo "01: Unexpected error message: $output"
exit 1
fi

output=$(${UPDATE_CMD} --src=${IS_SRC} --nonce 1 --new_operators ${NEW_OPERATOR} --threshold_denominator 0 --threshold_numerator 1 2>&1 || true)
if [[ $output == *"invalid threshold"* ]]; then
echo "02: OK"
else
echo "02: Unexpected error message: $output"
exit 1
fi

output=$(${UPDATE_CMD} --src=${IS_SRC} --nonce 1 --new_operators ${NEW_OPERATOR} --threshold_denominator 1 --threshold_numerator 0 2>&1 || true)
if [[ $output == *"invalid threshold"* ]]; then
echo "03: OK"
else
echo "03: Unexpected error message: $output"
exit 1
fi

output=$(${UPDATE_CMD} --src=${IS_SRC} --nonce 1 --new_operators ${NEW_OPERATOR} --threshold_denominator 1 --threshold_numerator 2 2>&1 || true)
if [[ $output == *"new operators threshold numerator cannot be greater than denominator"* ]]; then
echo "04: OK"
else
echo "04: Unexpected error message: $output"
exit 1
fi

# should be successful
${UPDATE_CMD} --src=${IS_SRC} --nonce 1 --new_operators ${NEW_OPERATOR} --threshold_denominator 1 --threshold_numerator 1
done

0 comments on commit 97a3705

Please sign in to comment.