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

fix: incorrect bitcoin outbound height #2243

Merged
merged 3 commits into from
May 23, 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
22 changes: 22 additions & 0 deletions zetaclient/chains/bitcoin/observer/live_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -219,12 +219,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 @@
return hash, txResult, nil
}

// GetBlockHeightByTxHash gets the block height by block hash
func GetBlockHeightByHash(
rpcClient interfaces.BTCRPCClient,
hash string,
) (int64, error) {

Check warning on line 641 in zetaclient/chains/bitcoin/observer/observer.go

View check run for this annotation

Codecov / codecov/patch

zetaclient/chains/bitcoin/observer/observer.go#L641

Added line #L641 was not covered by tests
// 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)

Check warning on line 646 in zetaclient/chains/bitcoin/observer/observer.go

View check run for this annotation

Codecov / codecov/patch

zetaclient/chains/bitcoin/observer/observer.go#L643-L646

Added lines #L643 - L646 were not covered by tests
}

// get block by hash
block, err := rpcClient.GetBlockVerbose(&blockHash)
skosito marked this conversation as resolved.
Show resolved Hide resolved
if err != nil {
return 0, errors.Wrapf(err, "GetBlockHeightByHash: error GetBlockVerbose %s", hash)

Check warning on line 652 in zetaclient/chains/bitcoin/observer/observer.go

View check run for this annotation

Codecov / codecov/patch

zetaclient/chains/bitcoin/observer/observer.go#L650-L652

Added lines #L650 - L652 were not covered by tests
}
return block.Height, nil

Check warning on line 654 in zetaclient/chains/bitcoin/observer/observer.go

View check run for this annotation

Codecov / codecov/patch

zetaclient/chains/bitcoin/observer/observer.go#L654

Added line #L654 was not covered by tests
}

// GetRawTxResult gets the raw tx result
func GetRawTxResult(
rpcClient interfaces.BTCRPCClient,
Expand Down
8 changes: 7 additions & 1 deletion zetaclient/chains/bitcoin/observer/outbound.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,12 +161,18 @@
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)

Check warning on line 167 in zetaclient/chains/bitcoin/observer/outbound.go

View check run for this annotation

Codecov / codecov/patch

zetaclient/chains/bitcoin/observer/outbound.go#L165-L167

Added lines #L165 - L167 were not covered by tests
}

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),

Check warning on line 175 in zetaclient/chains/bitcoin/observer/outbound.go

View check run for this annotation

Codecov / codecov/patch

zetaclient/chains/bitcoin/observer/outbound.go#L175

Added line #L175 was not covered by tests
0, // gas used not used with Bitcoin
nil, // gas price not used with Bitcoin
0, // gas limit not used with Bitcoin
Expand Down
Loading