Skip to content

Commit

Permalink
refactor: configure solana chain during e2e setup (#2990)
Browse files Browse the repository at this point in the history
* refactor: configure solana chain during setup

* add sol back to app_test.go
  • Loading branch information
gartnera authored Oct 11, 2024
1 parent 95186e0 commit 404bd96
Show file tree
Hide file tree
Showing 8 changed files with 78 additions and 39 deletions.
2 changes: 1 addition & 1 deletion cmd/zetae2e/local/local.go
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ func localE2ETest(cmd *cobra.Command, _ []string) {
deployerRunner.ERC20CustodyAddr = deployerRunner.ERC20CustodyV2Addr

if testSolana {
deployerRunner.SetSolanaContracts(conf.AdditionalAccounts.UserSolana.SolanaPrivateKey.String())
deployerRunner.SetupSolana(conf.AdditionalAccounts.UserSolana.SolanaPrivateKey.String())
}
noError(deployerRunner.FundEmissionsPool())

Expand Down
7 changes: 5 additions & 2 deletions e2e/e2etests/test_solana_withdraw.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,22 @@ import (
func TestSolanaWithdraw(r *runner.E2ERunner, args []string) {
require.Len(r, args, 1)

withdrawAmount := parseBigInt(r, args[0])

// get ERC20 SOL balance before withdraw
balanceBefore, err := r.SOLZRC20.BalanceOf(&bind.CallOpts{}, r.EVMAddress())
require.NoError(r, err)
r.Logger.Info("runner balance of SOL before withdraw: %d", balanceBefore)

require.Equal(r, 1, balanceBefore.Cmp(withdrawAmount), "Insufficient balance for withdrawal")

// parse withdraw amount (in lamports), approve amount is 1 SOL
approvedAmount := new(big.Int).SetUint64(solana.LAMPORTS_PER_SOL)
withdrawAmount := parseBigInt(r, args[0])
require.Equal(
r,
-1,
withdrawAmount.Cmp(approvedAmount),
"Withdrawal amount must be less than the approved amount (1e9).",
"Withdrawal amount must be less than the approved amount (1e9)",
)

// load deployer private key
Expand Down
70 changes: 68 additions & 2 deletions e2e/runner/setup_solana.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,20 @@
package runner

import (
"time"

ethcommon "github.com/ethereum/go-ethereum/common"
"github.com/gagliardetto/solana-go"
"github.com/gagliardetto/solana-go/rpc"
"github.com/near/borsh-go"
"github.com/pkg/errors"
"github.com/stretchr/testify/require"

"github.com/zeta-chain/node/e2e/utils"
"github.com/zeta-chain/node/pkg/chains"
"github.com/zeta-chain/node/pkg/constant"
solanacontracts "github.com/zeta-chain/node/pkg/contracts/solana"
observertypes "github.com/zeta-chain/node/x/observer/types"
)

// SetupSolanaAccount imports the deployer's private key
Expand All @@ -20,8 +26,8 @@ func (r *E2ERunner) SetupSolanaAccount() {
r.Logger.Info("SolanaDeployerAddress: %s", r.SolanaDeployerAddress)
}

// SetSolanaContracts set Solana contracts
func (r *E2ERunner) SetSolanaContracts(deployerPrivateKey string) {
// SetupSolana sets Solana contracts and params
func (r *E2ERunner) SetupSolana(deployerPrivateKey string) {
r.Logger.Print("⚙️ initializing gateway program on Solana")

// set Solana contracts
Expand Down Expand Up @@ -78,4 +84,64 @@ func (r *E2ERunner) SetSolanaContracts(deployerPrivateKey string) {
balance, err := r.SolanaClient.GetBalance(r.Ctx, pdaComputed, rpc.CommitmentConfirmed)
require.NoError(r, err)
r.Logger.Info("initial PDA balance: %d lamports", balance.Value)

err = r.ensureSolanaChainParams()
require.NoError(r, err)
}

func (r *E2ERunner) ensureSolanaChainParams() error {
if r.ZetaTxServer == nil {
return errors.New("ZetaTxServer is not initialized")
}

creator := r.ZetaTxServer.MustGetAccountAddressFromName(utils.OperationalPolicyName)

chainID := chains.SolanaLocalnet.ChainId

chainParams := &observertypes.ChainParams{
ChainId: chainID,
ConfirmationCount: 32,
ZetaTokenContractAddress: constant.EVMZeroAddress,
ConnectorContractAddress: constant.EVMZeroAddress,
Erc20CustodyContractAddress: constant.EVMZeroAddress,
GasPriceTicker: 5,
WatchUtxoTicker: 0,
InboundTicker: 2,
OutboundTicker: 2,
OutboundScheduleInterval: 2,
OutboundScheduleLookahead: 5,
BallotThreshold: observertypes.DefaultBallotThreshold,
MinObserverDelegation: observertypes.DefaultMinObserverDelegation,
IsSupported: true,
GatewayAddress: solanacontracts.SolanaGatewayProgramID,
}

updateMsg := observertypes.NewMsgUpdateChainParams(creator, chainParams)

if _, err := r.ZetaTxServer.BroadcastTx(utils.OperationalPolicyName, updateMsg); err != nil {
return errors.Wrap(err, "unable to broadcast solana chain params tx")
}

resetMsg := observertypes.NewMsgResetChainNonces(creator, chainID, 0, 0)
if _, err := r.ZetaTxServer.BroadcastTx(utils.OperationalPolicyName, resetMsg); err != nil {
return errors.Wrap(err, "unable to broadcast solana chain nonce reset tx")
}

r.Logger.Print("⚙️ voted for adding solana chain params (localnet). Waiting for confirmation")

query := &observertypes.QueryGetChainParamsForChainRequest{ChainId: chainID}

const duration = 2 * time.Second

for i := 0; i < 10; i++ {
_, err := r.ObserverClient.GetChainParamsForChain(r.Ctx, query)
if err == nil {
r.Logger.Print("⚙️ solana chain params are set")
return nil
}

time.Sleep(duration)
}

return errors.New("unable to set Solana chain params")
}
4 changes: 2 additions & 2 deletions e2e/runner/solana.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ func (r *E2ERunner) WithdrawSOLZRC20(
tx, err := r.SOLZRC20.Approve(r.ZEVMAuth, r.SOLZRC20Addr, approveAmount)
require.NoError(r, err)
receipt := utils.MustWaitForTxReceipt(r.Ctx, r.ZEVMClient, tx, r.Logger, r.ReceiptTimeout)
utils.RequireTxSuccessful(r, receipt)
utils.RequireTxSuccessful(r, receipt, "approve")

// withdraw
tx, err = r.SOLZRC20.Withdraw(r.ZEVMAuth, []byte(to.String()), amount)
Expand All @@ -162,7 +162,7 @@ func (r *E2ERunner) WithdrawSOLZRC20(

// wait for tx receipt
receipt = utils.MustWaitForTxReceipt(r.Ctx, r.ZEVMClient, tx, r.Logger, r.ReceiptTimeout)
utils.RequireTxSuccessful(r, receipt)
utils.RequireTxSuccessful(r, receipt, "withdraw")
r.Logger.Info("Receipt txhash %s status %d", receipt.TxHash, receipt.Status)

// wait for the cctx to be mined
Expand Down
3 changes: 0 additions & 3 deletions x/observer/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,12 @@ func InitGenesis(ctx sdk.Context, k keeper.Keeper, genState types.GenesisState)
btcChainParams.IsSupported = true
goerliChainParams := types.GetDefaultGoerliLocalnetChainParams()
goerliChainParams.IsSupported = true
solanaChainParams := types.GetDefaultSolanaLocalnetChainParams()
solanaChainParams.IsSupported = true
zetaPrivnetChainParams := types.GetDefaultZetaPrivnetChainParams()
zetaPrivnetChainParams.IsSupported = true
k.SetChainParamsList(ctx, types.ChainParamsList{
ChainParams: []*types.ChainParams{
btcChainParams,
goerliChainParams,
solanaChainParams,
zetaPrivnetChainParams,
},
})
Expand Down
6 changes: 0 additions & 6 deletions x/observer/genesis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,15 +68,12 @@ func TestGenesis(t *testing.T) {
btcChainParams.IsSupported = true
goerliChainParams := types.GetDefaultGoerliLocalnetChainParams()
goerliChainParams.IsSupported = true
solanaChainParams := types.GetDefaultSolanaLocalnetChainParams()
solanaChainParams.IsSupported = true
zetaPrivnetChainParams := types.GetDefaultZetaPrivnetChainParams()
zetaPrivnetChainParams.IsSupported = true
localnetChainParams := types.ChainParamsList{
ChainParams: []*types.ChainParams{
btcChainParams,
goerliChainParams,
solanaChainParams,
zetaPrivnetChainParams,
},
}
Expand Down Expand Up @@ -107,15 +104,12 @@ func TestGenesis(t *testing.T) {
btcChainParams.IsSupported = true
goerliChainParams := types.GetDefaultGoerliLocalnetChainParams()
goerliChainParams.IsSupported = true
solanaChainParams := types.GetDefaultSolanaLocalnetChainParams()
solanaChainParams.IsSupported = true
zetaPrivnetChainParams := types.GetDefaultZetaPrivnetChainParams()
zetaPrivnetChainParams.IsSupported = true
localnetChainParams := types.ChainParamsList{
ChainParams: []*types.ChainParams{
btcChainParams,
goerliChainParams,
solanaChainParams,
zetaPrivnetChainParams,
},
}
Expand Down
21 changes: 0 additions & 21 deletions x/observer/types/chain_params.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (

"github.com/zeta-chain/node/pkg/chains"
"github.com/zeta-chain/node/pkg/constant"
solanacontracts "github.com/zeta-chain/node/pkg/contracts/solana"
)

var (
Expand Down Expand Up @@ -148,7 +147,6 @@ func GetDefaultChainParams() ChainParamsList {
GetDefaultMumbaiTestnetChainParams(),
GetDefaultBtcTestnetChainParams(),
GetDefaultBtcRegtestChainParams(),
GetDefaultSolanaLocalnetChainParams(),
GetDefaultGoerliLocalnetChainParams(),
},
}
Expand Down Expand Up @@ -299,25 +297,6 @@ func GetDefaultBtcRegtestChainParams() *ChainParams {
IsSupported: false,
}
}
func GetDefaultSolanaLocalnetChainParams() *ChainParams {
return &ChainParams{
ChainId: chains.SolanaLocalnet.ChainId,
ConfirmationCount: 32,
ZetaTokenContractAddress: constant.EVMZeroAddress,
ConnectorContractAddress: constant.EVMZeroAddress,
Erc20CustodyContractAddress: constant.EVMZeroAddress,
GasPriceTicker: 5,
WatchUtxoTicker: 0,
InboundTicker: 2,
OutboundTicker: 2,
OutboundScheduleInterval: 2,
OutboundScheduleLookahead: 5,
BallotThreshold: DefaultBallotThreshold,
MinObserverDelegation: DefaultMinObserverDelegation,
IsSupported: false,
GatewayAddress: solanacontracts.SolanaGatewayProgramID,
}
}
func GetDefaultGoerliLocalnetChainParams() *ChainParams {
return &ChainParams{
ChainId: chains.GoerliLocalnet.ChainId,
Expand Down
4 changes: 2 additions & 2 deletions zetaclient/context/app_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/zeta-chain/node/pkg/chains"
"github.com/zeta-chain/node/testutil/sample"
"github.com/zeta-chain/node/x/observer/types"
"github.com/zeta-chain/node/zetaclient/config"
"golang.org/x/exp/maps"
Expand Down Expand Up @@ -38,8 +39,7 @@ func TestAppContext(t *testing.T) {
btcParams := types.GetDefaultBtcMainnetChainParams()
btcParams.IsSupported = true

solParams := types.GetDefaultSolanaLocalnetChainParams()
solParams.IsSupported = true
solParams := sample.ChainParamsSupported(chains.SolanaLocalnet.ChainId)

fancyL2 := chains.Chain{
ChainId: 123,
Expand Down

0 comments on commit 404bd96

Please sign in to comment.