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

refactor: configure solana chain during e2e setup #2990

Merged
merged 2 commits into from
Oct 11, 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
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())
swift1337 marked this conversation as resolved.
Show resolved Hide resolved
}
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)
}
gartnera marked this conversation as resolved.
Show resolved Hide resolved

func (r *E2ERunner) ensureSolanaChainParams() error {
gartnera marked this conversation as resolved.
Show resolved Hide resolved
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")
gartnera marked this conversation as resolved.
Show resolved Hide resolved
}
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
6 changes: 1 addition & 5 deletions zetaclient/context/app_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,6 @@ func TestAppContext(t *testing.T) {
btcParams := types.GetDefaultBtcMainnetChainParams()
btcParams.IsSupported = true

solParams := types.GetDefaultSolanaLocalnetChainParams()
solParams.IsSupported = true

fancyL2 := chains.Chain{
ChainId: 123,
Network: 0,
Expand Down Expand Up @@ -80,7 +77,6 @@ func TestAppContext(t *testing.T) {
chainParams := map[int64]*types.ChainParams{
chains.Ethereum.ChainId: ethParams,
chains.BitcoinMainnet.ChainId: btcParams,
chains.SolanaLocalnet.ChainId: solParams,
fancyL2.ChainId: fancyL2Params,
}

Expand Down Expand Up @@ -117,7 +113,7 @@ func TestAppContext(t *testing.T) {
assert.Equal(t, fancyL2Params, fancyL2Chain.Params())

// Check chain IDs
expectedIDs := []int64{ethParams.ChainId, btcParams.ChainId, solParams.ChainId, fancyL2.ChainId}
expectedIDs := []int64{ethParams.ChainId, btcParams.ChainId, fancyL2.ChainId}
swift1337 marked this conversation as resolved.
Show resolved Hide resolved
assert.ElementsMatch(t, expectedIDs, appContext.ListChainIDs())

// Check config
Expand Down
Loading