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

test: e2e add withdraw emissions #3151

Merged
merged 5 commits into from
Nov 22, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
4 changes: 4 additions & 0 deletions cmd/zetae2e/config/local.yml
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,10 @@ additional_accounts:
bech32_address: "zeta1nry9yeg6njhjrp2ctppa8558vqxal9fxk69zxg"
evm_address: "0x98c852651A9CAF2185585843d3D287600Ddf9526"
private_key: "bf9456c679bb5a952a9a137fcfc920e0413efdb97c36de1e57455763084230cb"
user_emissions_withdraw:
lumtis marked this conversation as resolved.
Show resolved Hide resolved
bech32_address: "zeta1n9zhyn4unvaee3ey40k7x7f5nmj7zet6qr5kl7"
evm_address: "0x9945724EBc9B3B9cc724abedE379349EE5E1657a"
private_key: "9d524fe318c0eb5f80d8b246993a9f15f924db24d4b8b873839b13bc30040d03"
policy_accounts:
emergency_policy_account:
bech32_address: "zeta16m2cnrdwtgweq4njc6t470vl325gw4kp6s7tap"
Expand Down
4 changes: 4 additions & 0 deletions cmd/zetae2e/config/localnet.yml
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,10 @@ additional_accounts:
bech32_address: "zeta1nry9yeg6njhjrp2ctppa8558vqxal9fxk69zxg"
evm_address: "0x98c852651A9CAF2185585843d3D287600Ddf9526"
private_key: "bf9456c679bb5a952a9a137fcfc920e0413efdb97c36de1e57455763084230cb"
user_emissions_withdraw:
bech32_address: "zeta1n9zhyn4unvaee3ey40k7x7f5nmj7zet6qr5kl7"
evm_address: "0x9945724EBc9B3B9cc724abedE379349EE5E1657a"
private_key: "9d524fe318c0eb5f80d8b246993a9f15f924db24d4b8b873839b13bc30040d03"
policy_accounts:
emergency_policy_account:
bech32_address: "zeta16m2cnrdwtgweq4njc6t470vl325gw4kp6s7tap"
Expand Down
10 changes: 9 additions & 1 deletion cmd/zetae2e/local/local.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,11 +160,17 @@ func localE2ETest(cmd *cobra.Command, _ []string) {

zetaTxServer, err := txserver.NewZetaTxServer(
conf.RPCs.ZetaCoreRPC,
[]string{utils.EmergencyPolicyName, utils.OperationalPolicyName, utils.AdminPolicyName},
[]string{
utils.EmergencyPolicyName,
utils.OperationalPolicyName,
utils.AdminPolicyName,
utils.UserEmissionsWithdrawName,
kingpinXD marked this conversation as resolved.
Show resolved Hide resolved
},
[]string{
conf.PolicyAccounts.EmergencyPolicyAccount.RawPrivateKey.String(),
conf.PolicyAccounts.OperationalPolicyAccount.RawPrivateKey.String(),
conf.PolicyAccounts.AdminPolicyAccount.RawPrivateKey.String(),
conf.AdditionalAccounts.UserEmissionsWithdraw.RawPrivateKey.String(),
},
conf.ZetaChainID,
)
Expand Down Expand Up @@ -492,6 +498,8 @@ func localE2ETest(cmd *cobra.Command, _ []string) {
os.Exit(1)
}

noError(deployerRunner.WithdrawEmissions())

kingpinXD marked this conversation as resolved.
Show resolved Hide resolved
logger.Print("✅ e2e tests completed in %s", time.Since(testStartTime).String())

if testTSSMigration {
Expand Down
3 changes: 3 additions & 0 deletions contrib/localnet/orchestrator/start-zetae2e.sh
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,9 @@ fund_eth_from_config '.additional_accounts.user_v2_ether_revert.evm_address' 100
# unlock v2 erc20 revert tests accounts
fund_eth_from_config '.additional_accounts.user_v2_erc20_revert.evm_address' 10000 "V2 ERC20 revert tester"

# unlock emissions withdraw tests accounts
fund_eth_from_config '.additional_accounts.user_emissions_withdraw.evm_address' 10000 "emissions withdraw tester"

# unlock local solana relayer accounts
if host solana > /dev/null; then
solana_url=$(config_str '.rpcs.solana')
Expand Down
45 changes: 45 additions & 0 deletions contrib/localnet/scripts/start-zetacored.sh
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,15 @@ add_v17_message_authorizations() {
},
"expiration": null
},
{
"granter": .operator,
"grantee": .granteeAddress,
"authorization": {
"@type": "/cosmos.authz.v1beta1.GenericAuthorization",
"msg": "/zetachain.zetacore.crosschain.MsgWithdrawEmissionsss"
kingpinXD marked this conversation as resolved.
Show resolved Hide resolved
},
"expiration": null
},
{
"granter": .operator,
"grantee": .granteeAddress,
Expand Down Expand Up @@ -67,6 +76,38 @@ add_v17_message_authorizations() {
' $json_file > temp.json && mv temp.json $json_file
}


add_emissions_withdraw_authorizations() {
lumtis marked this conversation as resolved.
Show resolved Hide resolved
# Address to add emissions withdraw authorizations
address=$(yq -r '.additional_accounts.user_emissions_withdraw.bech32_address' /root/config.yml)

echo "Adding emissions withdraw authorizations for address: $address"
# Path to the JSON file
json_file="/root/.zetacored/config/genesis.json"

# Using jq to parse JSON, create new entries, and append them to the authorization array
jq --arg address "$address" '
# Store the nodeAccountList array
.app_state.observer.nodeAccountList as $list |

# Iterate over the stored list to construct new objects and append to the authorization array
.app_state.authz.authorization += [
$list[] |
{
"granter": .operator,
"grantee": $address,
"authorization": {
"@type": "/cosmos.authz.v1beta1.GenericAuthorization",
"msg": "/zetachain.zetacore.emissions.MsgWithdrawEmission"
},
"expiration": null
}
]
' $json_file > temp.json && mv temp.json $json_file
}
kingpinXD marked this conversation as resolved.
Show resolved Hide resolved



# create keys
CHAINID="athens_101-1"
KEYRING="test"
Expand Down Expand Up @@ -190,6 +231,7 @@ then
# 2. Add the observers, authorizations, required params and accounts to the genesis.json
zetacored collect-observer-info
zetacored add-observer-list --keygen-block 25
add_emissions_withdraw_authorizations
kingpinXD marked this conversation as resolved.
Show resolved Hide resolved

# Check for the existence of "AddToOutTxTracker" string in the genesis file
# If this message is found in the genesis, it means add-observer-list has been run with the v16 binary for upgrade tests
Expand Down Expand Up @@ -272,6 +314,9 @@ then
# v2 erc20 revert tester
address=$(yq -r '.additional_accounts.user_v2_erc20_revert.bech32_address' /root/config.yml)
zetacored add-genesis-account "$address" 100000000000000000000000000azeta
# emissions withdraw tester
address=$(yq -r '.additional_accounts.user_emissions_withdraw.bech32_address' /root/config.yml)
zetacored add-genesis-account "$address" 100000000000000000000000000azeta

# 3. Copy the genesis.json to all the nodes .And use it to create a gentx for every node
zetacored gentx operator 1000000000000000000000azeta --chain-id=$CHAINID --keyring-backend=$KEYRING --gas-prices 20000000000azeta
Expand Down
36 changes: 21 additions & 15 deletions e2e/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,21 +61,22 @@ type Account struct {

// AdditionalAccounts are extra accounts required to run specific tests
type AdditionalAccounts struct {
UserERC20 Account `yaml:"user_erc20"`
UserZetaTest Account `yaml:"user_zeta_test"`
UserZEVMMPTest Account `yaml:"user_zevm_mp_test"`
UserBitcoinDeposit Account `yaml:"user_bitcoin_deposit"`
UserBitcoinWithdraw Account `yaml:"user_bitcoin_withdraw"`
UserSolana Account `yaml:"user_solana"`
UserEther Account `yaml:"user_ether"`
UserMisc Account `yaml:"user_misc"`
UserAdmin Account `yaml:"user_admin"`
UserMigration Account `yaml:"user_migration"` // used for TSS migration, TODO: rename (https://github.com/zeta-chain/node/issues/2780)
UserPrecompile Account `yaml:"user_precompile"`
UserV2Ether Account `yaml:"user_v2_ether"`
UserV2ERC20 Account `yaml:"user_v2_erc20"`
UserV2EtherRevert Account `yaml:"user_v2_ether_revert"`
UserV2ERC20Revert Account `yaml:"user_v2_erc20_revert"`
UserERC20 Account `yaml:"user_erc20"`
UserZetaTest Account `yaml:"user_zeta_test"`
UserZEVMMPTest Account `yaml:"user_zevm_mp_test"`
UserBitcoinDeposit Account `yaml:"user_bitcoin_deposit"`
UserBitcoinWithdraw Account `yaml:"user_bitcoin_withdraw"`
UserSolana Account `yaml:"user_solana"`
UserEther Account `yaml:"user_ether"`
UserMisc Account `yaml:"user_misc"`
UserAdmin Account `yaml:"user_admin"`
UserMigration Account `yaml:"user_migration"` // used for TSS migration, TODO: rename (https://github.com/zeta-chain/node/issues/2780)
UserPrecompile Account `yaml:"user_precompile"`
UserV2Ether Account `yaml:"user_v2_ether"`
UserV2ERC20 Account `yaml:"user_v2_erc20"`
UserV2EtherRevert Account `yaml:"user_v2_ether_revert"`
UserV2ERC20Revert Account `yaml:"user_v2_erc20_revert"`
UserEmissionsWithdraw Account `yaml:"user_emissions_withdraw"`
}

type PolicyAccounts struct {
Expand Down Expand Up @@ -248,6 +249,7 @@ func (a AdditionalAccounts) AsSlice() []Account {
a.UserV2ERC20,
a.UserV2EtherRevert,
a.UserV2ERC20Revert,
a.UserEmissionsWithdraw,
}
}

Expand Down Expand Up @@ -364,6 +366,10 @@ func (c *Config) GenerateKeys() error {
if err != nil {
return err
}
c.AdditionalAccounts.UserEmissionsWithdraw, err = generateAccount()
if err != nil {
return err
}

c.PolicyAccounts.EmergencyPolicyAccount, err = generateAccount()
if err != nil {
Expand Down
96 changes: 96 additions & 0 deletions e2e/runner/emissions.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
package runner

import (
"fmt"
"strings"

"cosmossdk.io/errors"
sdkmath "cosmossdk.io/math"
banktypes "github.com/cosmos/cosmos-sdk/x/bank/types"

"github.com/zeta-chain/node/cmd/zetacored/config"
"github.com/zeta-chain/node/e2e/txserver"
e2eutils "github.com/zeta-chain/node/e2e/utils"
emissionstypes "github.com/zeta-chain/node/x/emissions/types"
observertypes "github.com/zeta-chain/node/x/observer/types"
)

// FundEmissionsPool funds the emissions pool on ZetaChain with the same value as used originally on mainnet (20M ZETA)
func (r *E2ERunner) FundEmissionsPool() error {
r.Logger.Print("⚙️ funding the emissions pool on ZetaChain with 20M ZETA (%s)", txserver.EmissionsPoolAddress)
kingpinXD marked this conversation as resolved.
Show resolved Hide resolved

return r.ZetaTxServer.FundEmissionsPool(e2eutils.OperationalPolicyName, EmissionsPoolFunding)
}
kingpinXD marked this conversation as resolved.
Show resolved Hide resolved

// WithdrawEmissions withdraws emissions from the emission pool on ZetaChain for all observers
// This functions uses the UserEmissionsWithdrawName to create the withdraw tx.
// UserEmissionsWithdraw can sign the authz transactions because the necessary permissions are granted in the genesis file
func (r *E2ERunner) WithdrawEmissions() error {
observerSet, err := r.ObserverClient.ObserverSet(r.Ctx, &observertypes.QueryObserverSet{})
if err != nil {
return err
}

for _, observer := range observerSet.Observers {
r.Logger.Print("🏃 withdrawing emissions from the emissions pool on ZetaChain for observer %s", observer)
var (
baseDenom = config.BaseDenom
queryObserverBalance = &banktypes.QueryBalanceRequest{
Address: observer,
Denom: baseDenom,
}
)

balanceBefore, err := r.BankClient.Balance(r.Ctx, queryObserverBalance)
if err != nil {
return errors.Wrapf(err, "failed to get balance for observer before withdrawing emissions %s", observer)
}

availableAmount, err := r.EmissionsClient.ShowAvailableEmissions(
r.Ctx,
&emissionstypes.QueryShowAvailableEmissionsRequest{
Address: observer,
},
)
if err != nil {
return fmt.Errorf("failed to get available emissions for observer %s: %w", observer, err)
}

amount, found := strings.CutSuffix(availableAmount.Amount, baseDenom)
if !found {
return fmt.Errorf("invalid amount %s", availableAmount.Amount)
}
kingpinXD marked this conversation as resolved.
Show resolved Hide resolved

amountInt, ok := sdkmath.NewIntFromString(amount)
if !ok {
return fmt.Errorf("failed to convert string to int")
}

if amountInt.IsZero() {
r.Logger.Print("no emissions to withdraw for observer %s", observer)
continue
}

err = r.ZetaTxServer.WithdrawAllEmissions(amountInt, e2eutils.UserEmissionsWithdrawName, observer)
if err != nil {
return err
}

balanceAfter, err := r.BankClient.Balance(r.Ctx, queryObserverBalance)
if err != nil {
return errors.Wrapf(err, "failed to get balance for observer after withdrawing emissions %s", observer)
}

changeInBalance := balanceAfter.Balance.Sub(*balanceBefore.Balance).Amount.String()
if changeInBalance != amount {
return fmt.Errorf(
"invalid balance change for observer %s, expected %s, got %s",
observer,
amount,
changeInBalance,
)
}
kingpinXD marked this conversation as resolved.
Show resolved Hide resolved
}
kingpinXD marked this conversation as resolved.
Show resolved Hide resolved

return nil
}
3 changes: 3 additions & 0 deletions e2e/runner/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ import (
toncontracts "github.com/zeta-chain/node/pkg/contracts/ton"
authoritytypes "github.com/zeta-chain/node/x/authority/types"
crosschaintypes "github.com/zeta-chain/node/x/crosschain/types"
emissionstypes "github.com/zeta-chain/node/x/emissions/types"
fungibletypes "github.com/zeta-chain/node/x/fungible/types"
lightclienttypes "github.com/zeta-chain/node/x/lightclient/types"
observertypes "github.com/zeta-chain/node/x/observer/types"
Expand Down Expand Up @@ -98,6 +99,7 @@ type E2ERunner struct {
ObserverClient observertypes.QueryClient
LightclientClient lightclienttypes.QueryClient
DistributionClient distributiontypes.QueryClient
EmissionsClient emissionstypes.QueryClient

// optional zeta (cosmos) client
// typically only in test runners that need it
Expand Down Expand Up @@ -209,6 +211,7 @@ func NewE2ERunner(
ObserverClient: clients.Zetacore.Observer,
LightclientClient: clients.Zetacore.Lightclient,
DistributionClient: clients.Zetacore.Distribution,
EmissionsClient: clients.Zetacore.Emissions,

EVMAuth: clients.EvmAuth,
ZEVMAuth: clients.ZevmAuth,
Expand Down
7 changes: 0 additions & 7 deletions e2e/runner/setup_zeta.go
Original file line number Diff line number Diff line change
Expand Up @@ -276,10 +276,3 @@ func (r *E2ERunner) EnableHeaderVerification(chainIDList []int64) error {

return r.ZetaTxServer.EnableHeaderVerification(e2eutils.AdminPolicyName, chainIDList)
}

// FundEmissionsPool funds the emissions pool on ZetaChain with the same value as used originally on mainnet (20M ZETA)
func (r *E2ERunner) FundEmissionsPool() error {
r.Logger.Print("⚙️ funding the emissions pool on ZetaChain with 20M ZETA (%s)", txserver.EmissionsPoolAddress)

return r.ZetaTxServer.FundEmissionsPool(e2eutils.OperationalPolicyName, EmissionsPoolFunding)
}
23 changes: 23 additions & 0 deletions e2e/txserver/zeta_tx_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"strings"
"time"

sdkmath "cosmossdk.io/math"
abci "github.com/cometbft/cometbft/abci/types"
rpchttp "github.com/cometbft/cometbft/rpc/client/http"
coretypes "github.com/cometbft/cometbft/rpc/core/types"
Expand Down Expand Up @@ -585,6 +586,28 @@ func (zts ZetaTxServer) FundEmissionsPool(account string, amount *big.Int) error
return err
}

func (zts ZetaTxServer) WithdrawAllEmissions(withdrawAmount sdkmath.Int, account, observer string) error {
// retrieve account
acc, err := zts.clientCtx.Keyring.Key(account)
if err != nil {
return err
}
withdrawerAddress, err := acc.GetAddress()
if err != nil {
return err
}

msg := emissionstypes.MsgWithdrawEmission{
Creator: observer,
Amount: withdrawAmount,
}

authzMessage := authz.NewMsgExec(withdrawerAddress, []sdktypes.Msg{&msg})

_, err = zts.BroadcastTx(account, &authzMessage)
return err
}
kingpinXD marked this conversation as resolved.
Show resolved Hide resolved

// UpdateKeygen sets a new keygen height . The new height is the current height + 30
func (zts ZetaTxServer) UpdateKeygen(height int64) error {
keygenHeight := height + 30
Expand Down
8 changes: 5 additions & 3 deletions e2e/utils/zetacore.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,11 @@ import (
type CCTXClient = crosschaintypes.QueryClient

const (
EmergencyPolicyName = "emergency"
AdminPolicyName = "admin"
OperationalPolicyName = "operational"
EmergencyPolicyName = "emergency"
AdminPolicyName = "admin"
OperationalPolicyName = "operational"
UserEmissionsWithdrawName = "emissions_withdraw"

// The timeout was increased from 4 to 6 , which allows for a higher success in test runs
// However this needs to be researched as to why the increase in timeout was needed.
// https://github.com/zeta-chain/node/issues/2690
Expand Down
4 changes: 4 additions & 0 deletions pkg/rpc/clients.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
etherminttypes "github.com/zeta-chain/node/rpc/types"
authoritytypes "github.com/zeta-chain/node/x/authority/types"
crosschaintypes "github.com/zeta-chain/node/x/crosschain/types"
emissionstypes "github.com/zeta-chain/node/x/emissions/types"
fungibletypes "github.com/zeta-chain/node/x/fungible/types"
lightclienttypes "github.com/zeta-chain/node/x/lightclient/types"
observertypes "github.com/zeta-chain/node/x/observer/types"
Expand Down Expand Up @@ -51,6 +52,8 @@ type Clients struct {
Observer observertypes.QueryClient
// Lightclient is a github.com/zeta-chain/zetacore/x/lightclient/types QueryClient
Lightclient lightclienttypes.QueryClient
// EmissionsClient is a github.com/zeta-chain/zetacore/x/emissions/types QueryClient
Emissions emissionstypes.QueryClient

// Ethermint specific clients

Expand Down Expand Up @@ -79,6 +82,7 @@ func newClients(ctx client.Context) (Clients, error) {
Fungible: fungibletypes.NewQueryClient(ctx),
Observer: observertypes.NewQueryClient(ctx),
Lightclient: lightclienttypes.NewQueryClient(ctx),
Emissions: emissionstypes.NewQueryClient(ctx),
// Ethermint specific clients
Ethermint: etherminttypes.NewQueryClient(ctx),
EthermintFeeMarket: feemarkettypes.NewQueryClient(ctx),
Expand Down
Loading