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

Custom req #185

Merged
merged 2 commits into from
Aug 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
41 changes: 26 additions & 15 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,19 @@ import (
"context"
upgradetypes "cosmossdk.io/x/upgrade/types"
"fmt"
wasmtypes "github.com/CosmWasm/wasmd/x/wasm/types"
keysharemodule "github.com/Fairblock/fairyring/x/keyshare/module"
keysharemoduletypes "github.com/Fairblock/fairyring/x/keyshare/types"
pepmodule "github.com/Fairblock/fairyring/x/pep/module"
peptypes "github.com/Fairblock/fairyring/x/pep/types"
"github.com/cosmos/ibc-go/modules/capability"
capabilitytypes "github.com/cosmos/ibc-go/modules/capability/types"
interchainaccountsmodule "github.com/cosmos/ibc-go/v8/modules/apps/27-interchain-accounts"
interchainaccountstypes "github.com/cosmos/ibc-go/v8/modules/apps/27-interchain-accounts/types"
ibcfeemodule "github.com/cosmos/ibc-go/v8/modules/apps/29-fee"
ibcfeetypes "github.com/cosmos/ibc-go/v8/modules/apps/29-fee/types"
"github.com/cosmos/ibc-go/v8/modules/apps/transfer"
transfertypes "github.com/cosmos/ibc-go/v8/modules/apps/transfer/types"
"io"
"os"
"path/filepath"
Expand Down Expand Up @@ -80,6 +93,7 @@ import (
_ "github.com/cosmos/ibc-go/v8/modules/apps/29-fee" // import for side-effects
ibcfeekeeper "github.com/cosmos/ibc-go/v8/modules/apps/29-fee/keeper"
ibctransferkeeper "github.com/cosmos/ibc-go/v8/modules/apps/transfer/keeper"
ibcmodule "github.com/cosmos/ibc-go/v8/modules/core"
ibckeeper "github.com/cosmos/ibc-go/v8/modules/core/keeper"
"github.com/skip-mev/block-sdk/v2/block"
"github.com/skip-mev/block-sdk/v2/block/base"
Expand Down Expand Up @@ -463,26 +477,23 @@ func New(
app.sm.RegisterStoreDecoders()

app.UpgradeKeeper.SetUpgradeHandler(
"v0.8.2-to-custom-req-id",
"v0.8.2-to-0.8.3-release",
func(ctx context.Context, plan upgradetypes.Plan, fromVM module.VersionMap) (module.VersionMap, error) {

fromVM[capabilitytypes.ModuleName] = capability.AppModule{}.ConsensusVersion()
fromVM[peptypes.ModuleName] = pepmodule.AppModule{}.ConsensusVersion()
fromVM[keysharemoduletypes.ModuleName] = keysharemodule.AppModule{}.ConsensusVersion()
fromVM[ibcmodule.AppModule{}.Name()] = ibcmodule.AppModule{}.ConsensusVersion()
fromVM[ibcfeetypes.ModuleName] = ibcfeemodule.AppModule{}.ConsensusVersion()
fromVM[wasmtypes.ModuleName] = wasm.AppModule{}.ConsensusVersion()
fromVM[transfertypes.ModuleName] = transfer.AppModule{}.ConsensusVersion()
fromVM[interchainaccountstypes.ModuleName] = interchainaccountsmodule.AppModule{}.ConsensusVersion()
// 07-tendermint, 06-solomachine

return app.ModuleManager.RunMigrations(ctx, app.Configurator(), fromVM)
},
)

upgradeInfo, err := app.UpgradeKeeper.ReadUpgradeInfoFromDisk()
if err != nil {
panic(err)
}

if upgradeInfo.Name == "v0.8.2-to-custom-req-id" && !app.UpgradeKeeper.IsSkipHeight(upgradeInfo.Height) {
storeUpgrades := storetypes.StoreUpgrades{
Deleted: []string{"capability"},
}

// configure store loader that checks if version == upgradeHeight and applies store upgrades
app.SetStoreLoader(upgradetypes.UpgradeStoreLoader(upgradeInfo.Height, &storeUpgrades))
}

// A custom InitChainer can be set if extra pre-init-genesis logic is required.
// By default, when using app wiring enabled module, this is not required.
// For instance, the upgrade module will set automatically the module version map in its init genesis thanks to app wiring.
Expand Down
2 changes: 1 addition & 1 deletion cmd/fairyringd/cmd/encrypter/encrypter.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"fmt"

enc "github.com/FairBlock/DistributedIBE/encryption"
"github.com/Fairblock/fairyring/x/keyshare/types"
"github.com/Fairblock/fairyring/x/pep/types"
"github.com/cosmos/cosmos-sdk/client"
bls "github.com/drand/kyber-bls12381"
"github.com/spf13/cobra"
Expand Down
117 changes: 117 additions & 0 deletions scripts/tests/pep.sh
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,8 @@ fi

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)
echo $RESULT
echo $TARGET_BAL_DENOM
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"
Expand Down Expand Up @@ -435,6 +437,121 @@ if [ "$TARGET_BAL_AFTER" == "$TARGET_BAL" ]; then
exit 1
fi


echo "#############################################"
echo "Testing general keyshare on destination chain"
echo "#############################################"

echo "Creating new General Enc Request in pep module on chain $CHAINID_2"
RESULT=$($BINARY tx pep request-general-keyshare 30s testing12345 --from $WALLET_2 --gas-prices 1ufairy --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

while true; do
echo "Query general keyshare request on chain $CHAINID_2"
LIST_KEYSHARE_REQ=$($BINARY query pep list-keyshare-req --node $CHAIN2_NODE -o json)
echo $LIST_KEYSHARE_REQ | jq
IDENTITY=$(echo $LIST_KEYSHARE_REQ | jq -r '.keyshares[0].identity')
REQ_ID=$(echo $LIST_KEYSHARE_REQ | jq -r '.keyshares[0].request_id')
echo "Identity for keyshare request 1 is: $IDENTITY"
echo "Request ID for keyshare request 1 is: $REQ_ID"
if [[ "$IDENTITY" != "null" ]]; then
echo "Found Identity & Request ID"
break
fi
sleep 10
done

echo "Query account pep nonce before submitting encrypted tx from pep module on chain $CHAINID_2"
RESULT=$($BINARY query pep show-pep-nonce $WALLET_2 --home $CHAIN_DIR/$CHAINID_2 --chain-id $CHAINID_2 --node $CHAIN2_NODE -o json)
PEP_NONCE_BEFORE=$(echo "$RESULT" | jq -r '.pepNonce.nonce')

echo "Query target account token balance before submitting encrypted tx from pep module on chain $CHAINID_2"
RESULT=$($BINARY query bank balances $VALIDATOR_2 --node $CHAIN2_NODE -o json)
echo $RESULT
TARGET_BAL_DENOM=$(echo "$RESULT" | jq -r '.balances[0].denom')
echo $TARGET_BAL_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: '$PEP_NONCE_BEFORE'"
echo "Sending 1 $TARGET_BAL_DENOM to target address"
$BINARY tx bank send $WALLET_2 $VALIDATOR_2 5$TARGET_BAL_DENOM --from $WALLET_2 --gas-prices 1ufairy --gas 300000 --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 $WALLET_2 --offline --account-number 1 --sequence $PEP_NONCE_BEFORE --gas-prices 1ufairy --home $CHAIN_DIR/$CHAINID_2 --chain-id $CHAINID_2 --node $CHAIN2_NODE --keyring-backend test -y)

PEP_NONCE_2ND=$(($PEP_NONCE_BEFORE+1))
echo "Signing second bank send tx with pep nonce: '$PEP_NONCE_2ND'"
echo "Sending 1 $TARGET_BAL_DENOM to target address"
$BINARY tx bank send $WALLET_2 $VALIDATOR_2 5$TARGET_BAL_DENOM --from $WALLET_2 --gas-prices 1ufairy --gas 300000 --home $CHAIN_DIR/$CHAINID_2 --chain-id $CHAINID_2 --node $CHAIN2_NODE --keyring-backend test --generate-only -o json -y > unsigned2.json
SIGNED_DATA_2=$($BINARY tx sign unsigned2.json --from $WALLET_2 --offline --account-number 1 --sequence $PEP_NONCE_2ND --gas-prices 1ufairy --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'"
echo $IDENTITY $SIGNED_DATA
echo $IDENTITY $SIGNED_DATA_2
CIPHER=$($BINARY encrypt "$IDENTITY" "$SIGNED_DATA" --node $CHAIN2_NODE)
CIPHER2=$($BINARY encrypt "$IDENTITY" "$SIGNED_DATA_2" --node $CHAIN2_NODE)

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

sleep 10

echo "Submit general encrypted tx to pep module on chain $CHAINID_2"
RESULT=$($BINARY tx pep submit-general-encrypted-tx $CIPHER $REQ_ID --from $WALLET_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)
echo "$RESULT"
check_tx_code $RESULT

sleep 6

echo "Query Keyshare request and check for encrypted tx"
TX=$($BINARY query pep show-keyshare-req $REQ_ID --node $CHAIN2_NODE -o json | jq -r '.keyshare.tx_list.encryptedTx[0].data')
if [ "$TX" != "$CIPHER" ]; then
echo "Submitting general encrypted tx failed. Expected: $CIPHER, got $TX"
exit 1
fi

echo "Submit 2nd general encrypted tx to pep module on chain $CHAINID_2"
RESULT=$($BINARY tx pep submit-general-encrypted-tx $CIPHER2 $REQ_ID --from $WALLET_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)
echo "$RESULT"
check_tx_code $RESULT

sleep 6

echo "Request Generation of Aggr keyshare"
RESULT=$($BINARY tx pep get-general-keyshare $REQ_ID --from $WALLET_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)
echo "$RESULT"
check_tx_code $RESULT

sleep 6

EXTRACTED_RESULT=$($BINARY share-generation derive $GENERATED_SHARE 1 $IDENTITY)
EXTRACTED_SHARE=$(echo "$EXTRACTED_RESULT" | jq -r '.KeyShare')

while true; do
echo "Submitting General Key Share"

RESULT=$($BINARY tx keyshare create-general-key-share "private-gov-identity" $IDENTITY $EXTRACTED_SHARE 1 --from $VALIDATOR_1 --gas-prices 1ufairy --gas 300000 --home $CHAIN_DIR/$CHAINID_1 --chain-id $CHAINID_1 --node $CHAIN1_NODE --broadcast-mode sync --keyring-backend test -o json -y)
echo "$RESULT"
check_tx_err $RESULT
if [ $? -eq 0 ]; then
break
fi
done

sleep 30

echo "Query target account token balance after general encrypted tx being executed from pep module on chain $CHAINID_2"
RESULT=$($BINARY query bank balances $VALIDATOR_2 --node $CHAIN2_NODE -o json)
echo $RESULT
TARGET_BAL_DENOM=$(echo "$RESULT" | jq -r '.balances[0].denom')
echo $TARGET_BAL_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"
if [ "$TARGET_BAL_AFTER" == "$TARGET_BAL" ]; then
echo "ERROR: Pep module encrypted tx execution error. Expected Target Balance to be updated, got same balance: '$TARGET_BAL_AFTER $TARGET_BAL_DENOM'"
exit 1
fi

echo ""
echo "###########################################################"
echo "# SUCCESSFULLY TESTED #"
Expand Down
54 changes: 0 additions & 54 deletions scripts/tests/priv_gov.sh
Original file line number Diff line number Diff line change
Expand Up @@ -50,49 +50,6 @@ wait_for_tx () {
fi
echo "$RESULT"
}
#
#echo "Creating a new proposal on destination chain"
#RESULT=$($BINARY tx gov submit-proposal ./scripts/tests/draft_proposal.json --from $VALIDATOR_2 --gas-prices 1ufairy --home $CHAIN_DIR/$CHAINID_2 --chain-id $CHAINID_2 --node tcp://localhost:26657 --broadcast-mode sync --keyring-backend test -o json -y)
#check_tx_code $RESULT
#RESULT=$(wait_for_tx $RESULT "destination")
#
## waiting for identity to be updated
#sleep 25
#PROPOSAL=$(fairyringd q gov proposals --home $CHAIN_DIR/$CHAINID_1 -o json | jq '.proposals[0]')
#IDENTITY=$(echo "$PROPOSAL" | jq -r '.identity')
#PUBKEY=$(echo "$PROPOSAL" | jq -r '.pubkey')
#
#if [ -z "$IDENTITY" ]; then
# echo "ERROR: The identity is blank"
# echo "$PROPOSAL"
# exit 1
#elif [ -z "$PUBKEY" ]; then
# echo "The pubkey is blank"
# echo "$PROPOSAL"
# exit 1
#else
# echo "Successfully created proposal on destination chain"
#fi
#
#
#echo "Submitting encrypted vote on destination chain"
#echo "Encrypting vote with Pub key: '$PUBKEY' and Identity: $IDENTITY"
#ENCVOTE=$($ENCRYPTER "yes" 100 $IDENTITY $PUBKEY)
#echo "$ENCVOTE"
#
#RESULT=$(fairyringd tx gov vote-encrypted 1 $ENCVOTE --from val2 --home $CHAIN_DIR/$CHAINID_1 --keyring-backend test --gas-prices 1ufairy -o json -y)
#check_tx_code $RESULT
#RESULT=$(wait_for_tx $RESULT "destination")
#
#VOTE=$(fairyringd q gov votes 1 --home $CHAIN_DIR/$CHAINID_1 -o json | jq '.votes[0]')
#VOTEDATA=$(echo "$VOTE" | jq -r '.encrypted_vote_data')
#
#if [ -z "$VOTEDATA" ]; then
# echo "ERROR: Encrypted vote option is blank"
# echo "$VOTE"
# exit 1
#fi
#echo "Successfully submitted encrypted vote on destination chain"

echo "PUBKEY: $1"
echo "SHARE: $2"
Expand Down Expand Up @@ -171,17 +128,6 @@ done
RESULT=$(wait_for_tx $RESULT "source")
sleep 25

#echo "Checking Status of proposal on Destination chain"
#PROPOSAL=$(fairyringd q gov proposals --home $CHAIN_DIR/$CHAINID_1 -o json | jq '.proposals[0]')
#STATUS=$(echo "$PROPOSAL" | jq -r '.status')
#
#if [ "$STATUS" != "PROPOSAL_STATUS_PASSED" ]; then
# echo "ERROR: Failed to pass proposal on destination chain"
# echo "$PROPOSAL"
# exit 1
#fi
#echo "Successfully passed proposal with enc vote on destination chain"

echo "Checking Status of proposal on Source chain"
sleep 5
PROPOSAL=$(fairyringd q gov proposals --home $CHAIN_DIR/$CHAINID_1 --node tcp://localhost:16657 -o json | jq '.proposals[0]')
Expand Down
13 changes: 8 additions & 5 deletions x/keyshare/keeper/process_queues.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package keeper

import (
"errors"
"fmt"
"math"
"strconv"

Expand All @@ -17,11 +18,12 @@ func (k Keeper) ProcessPepRequestQueue(ctx sdk.Context) error {
}

reqs := k.pepKeeper.GetAllGenEncTxReqQueueEntry(ctx)
k.Logger().Info(fmt.Sprintf("PROCESSING PEP REQUEST QUEUE: %v", reqs))
for _, req := range reqs {
if req.EstimatedDelay == nil {
k.pepKeeper.RemoveReqQueueEntry(ctx, req.GetRequestId())
k.Logger().Info("[ProcessPepRequestQueue] Estimated delay has not been set")
continue
// return errors.New("estimated delay has not been set")
}
delay := req.EstimatedDelay
blockDelay := uint64(math.Ceil(delay.Seconds() / types.AvgBlockTime))
Expand All @@ -30,19 +32,19 @@ func (k Keeper) ProcessPepRequestQueue(ctx sdk.Context) error {
if executionHeight > activePubKey.Expiry {
queuedPubKey, found := k.GetQueuedPubKey(ctx)
if !found {
k.Logger().Info("[ProcessPepRequestQueue] Queued Pub Key not found")
k.pepKeeper.RemoveReqQueueEntry(ctx, req.GetRequestId())
continue
// return errors.New("estimated delay too long")
}
if executionHeight > queuedPubKey.Expiry {
k.Logger().Info("[ProcessPepRequestQueue] Estimated delay too long")
k.pepKeeper.RemoveReqQueueEntry(ctx, req.GetRequestId())
continue
// return errors.New("estimated delay too long")
}
activePubKey = types.ActivePubKey(queuedPubKey)
}

id := types.IdentityFromRequestID(req.GetRequestId())
id := req.GetRequestId()

var keyshareRequest types.KeyShareRequest

Expand All @@ -69,6 +71,7 @@ func (k Keeper) ProcessPepRequestQueue(ctx sdk.Context) error {

func (k Keeper) ProcessPepSignalQueue(ctx sdk.Context) error {
reqs := k.pepKeeper.GetAllGenEncTxSignalQueueEntry(ctx)
k.Logger().Info(fmt.Sprintf("PROCESSING PEP SIGNAL QUEUE: %v", reqs))
for _, req := range reqs {
if req.Identity != "" {
keyshareReq, found := k.GetKeyShareRequest(ctx, req.Identity)
Expand Down Expand Up @@ -134,7 +137,7 @@ func (k Keeper) ProcessGovRequestQueue(ctx sdk.Context) error {
reqCount, _ := strconv.ParseUint(reqCountString, 10, 64)
reqCount = reqCount + 1

id := types.IdentityFromRequestCount(reqCount)
id := req.GetRequestId()

var keyshareRequest types.KeyShareRequest

Expand Down
12 changes: 2 additions & 10 deletions x/keyshare/keeper/request_aggr_key_share.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,8 @@ package keeper

import (
"errors"
"math"
"strconv"

"github.com/Fairblock/fairyring/x/keyshare/types"
"math"

sdkerrors "cosmossdk.io/errors"
sdk "github.com/cosmos/cosmos-sdk/types"
Expand Down Expand Up @@ -72,11 +70,7 @@ func (k Keeper) OnRecvRequestAggrKeysharePacket(
isProposalID = false
}

reqCountString := k.GetRequestCount(ctx)
reqCount, _ := strconv.ParseUint(reqCountString, 10, 64)
reqCount = reqCount + 1

id := types.IdentityFromRequestCount(reqCount)
id := data.GetRequestId()

var keyshareRequest types.KeyShareRequest

Expand All @@ -103,8 +97,6 @@ func (k Keeper) OnRecvRequestAggrKeysharePacket(

k.SetKeyShareRequest(ctx, keyshareRequest)

k.SetRequestCount(ctx, reqCount)

packetAck.Identity = id
packetAck.Pubkey = activePubKey.PublicKey

Expand Down
15 changes: 0 additions & 15 deletions x/keyshare/types/key_key_share_request.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,21 +13,6 @@ const (
KeyShareRequestKeyPrefix = "KeyshareRequest/value/"
)

func IdentityFromRequestCount(
reqCount uint64,
) string {
reqNumber := strconv.FormatUint(reqCount, 10)
identity := reqNumber + "/rq"
return identity
}

func IdentityFromRequestID(
reqID string,
) string {
identity := reqID + "/id"
return identity
}

func RequestCountFromIdentity(
identity string,
) uint64 {
Expand Down
Loading