From 3780897e91245faf820dab0c3efd404ab92ad8bf Mon Sep 17 00:00:00 2001 From: Charlie Chen Date: Mon, 26 Aug 2024 23:42:24 -0500 Subject: [PATCH] replace GetRecentBlockhash with GetLatestBlockhash; add check on Vsize; misc --- e2e/runner/solana.go | 2 +- zetaclient/chains/bitcoin/fee.go | 7 +++++++ zetaclient/chains/bitcoin/rpc/rpc.go | 5 +++++ zetaclient/chains/interfaces/interfaces.go | 2 +- zetaclient/chains/solana/signer/withdraw.go | 4 ++-- zetaclient/testutils/mocks/solana_rpc.go | 16 ++++++++-------- 6 files changed, 24 insertions(+), 12 deletions(-) diff --git a/e2e/runner/solana.go b/e2e/runner/solana.go index ba6f240279..30e089406e 100644 --- a/e2e/runner/solana.go +++ b/e2e/runner/solana.go @@ -65,7 +65,7 @@ func (r *E2ERunner) CreateSignedTransaction( privateKey solana.PrivateKey, ) *solana.Transaction { // get a recent blockhash - recent, err := r.SolanaClient.GetRecentBlockhash(r.Ctx, rpc.CommitmentFinalized) + recent, err := r.SolanaClient.GetLatestBlockhash(r.Ctx, rpc.CommitmentFinalized) require.NoError(r, err) // create the initialize transaction diff --git a/zetaclient/chains/bitcoin/fee.go b/zetaclient/chains/bitcoin/fee.go index 6be21fdcad..3293727b47 100644 --- a/zetaclient/chains/bitcoin/fee.go +++ b/zetaclient/chains/bitcoin/fee.go @@ -49,6 +49,7 @@ const ( DynamicDepositorFeeHeight = 834500 // DynamicDepositorFeeHeightV2 is the mainnet height from which dynamic depositor fee V2 is applied + // Height 863400 is approximately a month away (2024-09-28) from the time of writing, allowing enough time for the upgrade DynamicDepositorFeeHeightV2 = 863400 ) @@ -281,6 +282,12 @@ func CalcDepositorFeeV2( // GetRecentFeeRate gets the highest fee rate from recent blocks // Note: this method should be used for testnet ONLY func GetRecentFeeRate(rpcClient interfaces.BTCRPCClient, netParams *chaincfg.Params) (uint64, error) { + // should avoid using this method for mainnet + if netParams.Name == chaincfg.MainNetParams.Name { + return 0, errors.New("GetRecentFeeRate should not be used for mainnet") + } + + // get the current block number blockNumber, err := rpcClient.GetBlockCount() if err != nil { return 0, err diff --git a/zetaclient/chains/bitcoin/rpc/rpc.go b/zetaclient/chains/bitcoin/rpc/rpc.go index 9b9b36842f..29972aa26d 100644 --- a/zetaclient/chains/bitcoin/rpc/rpc.go +++ b/zetaclient/chains/bitcoin/rpc/rpc.go @@ -130,6 +130,11 @@ func GetTransactionFeeAndRate(rpcClient interfaces.BTCRPCClient, rawResult *btcj totalOutputValue int64 ) + // make sure the tx Vsize is not zero (should not happen) + if rawResult.Vsize <= 0 { + return 0, 0, fmt.Errorf("tx %s has non-positive Vsize: %d", rawResult.Txid, rawResult.Vsize) + } + // sum up total input value for _, vin := range rawResult.Vin { prevTx, err := GetRawTxByHash(rpcClient, vin.Txid) diff --git a/zetaclient/chains/interfaces/interfaces.go b/zetaclient/chains/interfaces/interfaces.go index 5e1148f81b..f3189deee2 100644 --- a/zetaclient/chains/interfaces/interfaces.go +++ b/zetaclient/chains/interfaces/interfaces.go @@ -200,7 +200,7 @@ type SolanaRPCClient interface { account solana.PublicKey, commitment solrpc.CommitmentType, ) (*solrpc.GetBalanceResult, error) - GetRecentBlockhash(ctx context.Context, commitment solrpc.CommitmentType) (*solrpc.GetRecentBlockhashResult, error) + GetLatestBlockhash(ctx context.Context, commitment solrpc.CommitmentType) (*solrpc.GetLatestBlockhashResult, error) GetRecentPrioritizationFees( ctx context.Context, accounts solana.PublicKeySlice, diff --git a/zetaclient/chains/solana/signer/withdraw.go b/zetaclient/chains/solana/signer/withdraw.go index f44dc3fc30..8b91dc8f23 100644 --- a/zetaclient/chains/solana/signer/withdraw.go +++ b/zetaclient/chains/solana/signer/withdraw.go @@ -69,9 +69,9 @@ func (signer *Signer) SignWithdrawTx(ctx context.Context, msg contracts.MsgWithd attachWithdrawAccounts(&inst, privkey.PublicKey(), signer.pda, msg.To(), signer.gatewayID) // get a recent blockhash - recent, err := signer.client.GetRecentBlockhash(ctx, rpc.CommitmentFinalized) + recent, err := signer.client.GetLatestBlockhash(ctx, rpc.CommitmentFinalized) if err != nil { - return nil, errors.Wrap(err, "GetRecentBlockhash error") + return nil, errors.Wrap(err, "GetLatestBlockhash error") } // create a transaction that wraps the instruction diff --git a/zetaclient/testutils/mocks/solana_rpc.go b/zetaclient/testutils/mocks/solana_rpc.go index fad147037c..26b923abb3 100644 --- a/zetaclient/testutils/mocks/solana_rpc.go +++ b/zetaclient/testutils/mocks/solana_rpc.go @@ -1,4 +1,4 @@ -// Code generated by mockery v2.38.0. DO NOT EDIT. +// Code generated by mockery v2.42.2. DO NOT EDIT. package mocks @@ -135,24 +135,24 @@ func (_m *SolanaRPCClient) GetHealth(ctx context.Context) (string, error) { return r0, r1 } -// GetRecentBlockhash provides a mock function with given fields: ctx, commitment -func (_m *SolanaRPCClient) GetRecentBlockhash(ctx context.Context, commitment rpc.CommitmentType) (*rpc.GetRecentBlockhashResult, error) { +// GetLatestBlockhash provides a mock function with given fields: ctx, commitment +func (_m *SolanaRPCClient) GetLatestBlockhash(ctx context.Context, commitment rpc.CommitmentType) (*rpc.GetLatestBlockhashResult, error) { ret := _m.Called(ctx, commitment) if len(ret) == 0 { - panic("no return value specified for GetRecentBlockhash") + panic("no return value specified for GetLatestBlockhash") } - var r0 *rpc.GetRecentBlockhashResult + var r0 *rpc.GetLatestBlockhashResult var r1 error - if rf, ok := ret.Get(0).(func(context.Context, rpc.CommitmentType) (*rpc.GetRecentBlockhashResult, error)); ok { + if rf, ok := ret.Get(0).(func(context.Context, rpc.CommitmentType) (*rpc.GetLatestBlockhashResult, error)); ok { return rf(ctx, commitment) } - if rf, ok := ret.Get(0).(func(context.Context, rpc.CommitmentType) *rpc.GetRecentBlockhashResult); ok { + if rf, ok := ret.Get(0).(func(context.Context, rpc.CommitmentType) *rpc.GetLatestBlockhashResult); ok { r0 = rf(ctx, commitment) } else { if ret.Get(0) != nil { - r0 = ret.Get(0).(*rpc.GetRecentBlockhashResult) + r0 = ret.Get(0).(*rpc.GetLatestBlockhashResult) } }