Skip to content

Commit

Permalink
Merge branch 'develop' into chore/fix-changelog
Browse files Browse the repository at this point in the history
  • Loading branch information
lumtis authored May 23, 2024
2 parents f1f1854 + 714beab commit 5facef4
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 3 deletions.
1 change: 1 addition & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
* [2125](https://github.com/zeta-chain/node/pull/2125) - fix develop upgrade test
* [2222](https://github.com/zeta-chain/node/pull/2222) - removed `maxHeightDiff` to let observer scan from Bitcoin height where it left off
* [2233](https://github.com/zeta-chain/node/pull/2233) - fix `IsSupported` flag not properly updated in zetaclient's context
* [2243](https://github.com/zeta-chain/node/pull/2243) - fix incorrect bitcoin outbound height in the CCTX outbound parameter

### CI

Expand Down
30 changes: 28 additions & 2 deletions zetaclient/chains/bitcoin/observer/live_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"encoding/hex"
"fmt"
"math/big"
"os"
"strings"
"testing"
"time"
Expand Down Expand Up @@ -86,10 +87,13 @@ func (suite *BitcoinObserverTestSuite) TearDownSuite() {

func getRPCClient(chainID int64) (*rpcclient.Client, error) {
var connCfg *rpcclient.ConnConfig
rpcMainnet := os.Getenv("BTC_RPC_MAINNET")
rpcTestnet := os.Getenv("BTC_RPC_TESTNET")

// mainnet
if chainID == 8332 {
connCfg = &rpcclient.ConnConfig{
Host: "127.0.0.1:8332", // mainnet endpoint goes here
Host: rpcMainnet, // mainnet endpoint goes here
User: "user",
Pass: "pass",
Params: "mainnet",
Expand All @@ -100,7 +104,7 @@ func getRPCClient(chainID int64) (*rpcclient.Client, error) {
// testnet3
if chainID == 18332 {
connCfg = &rpcclient.ConnConfig{
Host: "127.0.0.1:8332", // testnet endpoint goes here
Host: rpcTestnet, // testnet endpoint goes here
User: "user",
Pass: "pass",
Params: "testnet3",
Expand Down Expand Up @@ -219,12 +223,34 @@ func (suite *BitcoinObserverTestSuite) Test3() {
func TestBitcoinObserverLive(t *testing.T) {
// suite.Run(t, new(BitcoinClientTestSuite))

// LiveTestGetBlockHeightByHash(t)
// LiveTestBitcoinFeeRate(t)
// LiveTestAvgFeeRateMainnetMempoolSpace(t)
// LiveTestAvgFeeRateTestnetMempoolSpace(t)
// LiveTestGetSenderByVin(t)
}

// LiveTestGetBlockHeightByHash queries Bitcoin block height by hash
func LiveTestGetBlockHeightByHash(t *testing.T) {
// setup Bitcoin client
client, err := getRPCClient(8332)
require.NoError(t, err)

// the block hashes to test
expectedHeight := int64(835053)
hash := "00000000000000000000994a5d12976ec5bda078a7b9c27981f0a4e7a6d46d23"
invalidHash := "invalidhash"

// get block by invalid hash
_, err = GetBlockHeightByHash(client, invalidHash)
require.ErrorContains(t, err, "error decoding block hash")

// get block height by block hash
height, err := GetBlockHeightByHash(client, hash)
require.NoError(t, err)
require.Equal(t, expectedHeight, height)
}

// LiveTestBitcoinFeeRate query Bitcoin mainnet fee rate every 5 minutes
// and compares Conservative and Economical fee rates for different block targets (1 and 2)
func LiveTestBitcoinFeeRate(t *testing.T) {
Expand Down
20 changes: 20 additions & 0 deletions zetaclient/chains/bitcoin/observer/observer.go
Original file line number Diff line number Diff line change
Expand Up @@ -634,6 +634,26 @@ func GetTxResultByHash(
return hash, txResult, nil
}

// GetBlockHeightByTxHash gets the block height by block hash
func GetBlockHeightByHash(
rpcClient interfaces.BTCRPCClient,
hash string,
) (int64, error) {
// decode the block hash
var blockHash chainhash.Hash
err := chainhash.Decode(&blockHash, hash)
if err != nil {
return 0, errors.Wrapf(err, "GetBlockHeightByHash: error decoding block hash %s", hash)
}

// get block by hash
block, err := rpcClient.GetBlockVerbose(&blockHash)
if err != nil {
return 0, errors.Wrapf(err, "GetBlockHeightByHash: error GetBlockVerbose %s", hash)
}
return block.Height, nil
}

// GetRawTxResult gets the raw tx result
func GetRawTxResult(
rpcClient interfaces.BTCRPCClient,
Expand Down
12 changes: 11 additions & 1 deletion zetaclient/chains/bitcoin/observer/outbound.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,12 +161,22 @@ func (ob *Observer) IsOutboundProcessed(cctx *crosschaintypes.CrossChainTx, logg
return true, false, nil
}

// Get outbound block height
blockHeight, err := GetBlockHeightByHash(ob.rpcClient, res.BlockHash)
if err != nil {
return true, false, errors.Wrapf(
err,
"IsOutboundProcessed: error getting block height by hash %s",
res.BlockHash,
)
}

logger.Debug().Msgf("Bitcoin outbound confirmed: txid %s, amount %s\n", res.TxID, amountInSat.String())
zetaHash, ballot, err := ob.zetacoreClient.PostVoteOutbound(
sendHash,
res.TxID,
// #nosec G701 always positive
uint64(res.BlockIndex),
uint64(blockHeight),
0, // gas used not used with Bitcoin
nil, // gas price not used with Bitcoin
0, // gas limit not used with Bitcoin
Expand Down

0 comments on commit 5facef4

Please sign in to comment.