Skip to content

Commit

Permalink
Merge pull request #109 from Fairblock/fix/target-height-submit-encry…
Browse files Browse the repository at this point in the history
…pted-tx

Fix/target height submit encrypted tx
  • Loading branch information
p0p3yee authored Feb 20, 2024
2 parents c28285e + 323108c commit f6666a5
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 0 deletions.
1 change: 1 addition & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ jobs:
- name: Publish the Release
uses: softprops/action-gh-release@v1
with:
tag_name: ${{ steps.vars.outputs.tag_name }}
prerelease: true
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
33 changes: 33 additions & 0 deletions scripts/tests/pep.sh
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,15 @@ if [ "$PUB_KEY" == "" ]; then
fi


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

echo "Pub Key expires at: $PUB_KEY_EXPIRY"

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_2 --gas-prices 1ufairy --gas 300000 --home $CHAIN_DIR/$CHAINID_2 --chain-id $CHAINID_2 --node $CHAIN2_NODE --broadcast-mode sync --keyring-backend test -o json -y)
Expand All @@ -73,6 +82,30 @@ if [[ "$ERROR_MSG" != *"Invalid target block height"* ]]; then
fi


echo "Submit encrypted tx with block height much higher than queued public key to pep module on chain fairyring_test_2"
RESULT=$($BINARY tx pep submit-encrypted-tx 0000 500000 --from $VALIDATOR_2 --gas-prices 1ufairy --gas 300000 --home $CHAIN_DIR/$CHAINID_2 --chain-id $CHAINID_2 --node $CHAIN2_NODE --broadcast-mode sync --keyring-backend test -o json -y)
check_tx_code $RESULT
RESULT=$(wait_for_tx $RESULT)
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'"
echo "ERROR MESSAGE: $(echo "$RESULT" | jq -r '.raw_log')"
exit 1
fi


echo "Submit encrypted tx with block height 1 block higher than the pub key expiry height to pep module on chain fairyring_test_2"
RESULT=$($BINARY tx pep submit-encrypted-tx 0000 $((PUB_KEY_EXPIRY + 1)) --from $VALIDATOR_2 --gas-prices 1ufairy --gas 300000 --home $CHAIN_DIR/$CHAINID_2 --chain-id $CHAINID_2 --node $CHAIN2_NODE --broadcast-mode sync --keyring-backend test -o json -y)
check_tx_code $RESULT
RESULT=$(wait_for_tx $RESULT)
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'"
echo "ERROR MESSAGE: $(echo "$RESULT" | jq -r '.raw_log')"
exit 1
fi


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')
Expand Down
31 changes: 31 additions & 0 deletions x/pep/keeper/msg_server_submit_encrypted_tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,38 @@ func (k msgServer) SubmitEncryptedTx(goCtx context.Context, msg *types.MsgSubmit
sdk.NewAttribute(types.EncryptedTxRevertedEventIndex, "0"),
),
)
return nil, types.ErrInvalidTargetBlockHeight
}

var maxHeight uint64
queuedKey, found := k.GetQueuedPubKey(ctx)
if !found {
activeKey, foundActiveKey := k.GetActivePubKey(ctx)
if !foundActiveKey {
ctx.EventManager().EmitEvent(
sdk.NewEvent(types.EncryptedTxRevertedEventType,
sdk.NewAttribute(types.EncryptedTxRevertedEventCreator, msg.Creator),
sdk.NewAttribute(types.EncryptedTxRevertedEventHeight, strconv.FormatUint(msg.TargetBlockHeight, 10)),
sdk.NewAttribute(types.EncryptedTxRevertedEventReason, "Active Public key not found"),
sdk.NewAttribute(types.EncryptedTxRevertedEventIndex, "0"),
),
)
return nil, types.ErrActivePubKeyNotFound
}
maxHeight = activeKey.Expiry
} else {
maxHeight = queuedKey.Expiry
}

if msg.TargetBlockHeight > maxHeight {
ctx.EventManager().EmitEvent(
sdk.NewEvent(types.EncryptedTxRevertedEventType,
sdk.NewAttribute(types.EncryptedTxRevertedEventCreator, msg.Creator),
sdk.NewAttribute(types.EncryptedTxRevertedEventHeight, strconv.FormatUint(msg.TargetBlockHeight, 10)),
sdk.NewAttribute(types.EncryptedTxRevertedEventReason, "Target block height is higher than queued public key expiry height"),
sdk.NewAttribute(types.EncryptedTxRevertedEventIndex, "0"),
),
)
return nil, types.ErrInvalidTargetBlockHeight
}

Expand Down
1 change: 1 addition & 0 deletions x/pep/types/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,5 @@ var (
ErrInvalidVersion = sdkerrors.Register(ModuleName, 1501, "invalid version")
ErrInvalidTargetBlockHeight = sdkerrors.Register(ModuleName, 1600, "Invalid target block height")
ErrInvalidMsgCreator = sdkerrors.Register(ModuleName, 1700, "Invalid msg creator address")
ErrActivePubKeyNotFound = sdkerrors.Register(ModuleName, 1800, "Active public key not found")
)

0 comments on commit f6666a5

Please sign in to comment.