Skip to content

Commit

Permalink
disable for mainnet
Browse files Browse the repository at this point in the history
  • Loading branch information
lumtis committed Oct 2, 2024
1 parent 933fbf7 commit 40785e7
Show file tree
Hide file tree
Showing 2 changed files with 103 additions and 22 deletions.
25 changes: 21 additions & 4 deletions zetaclient/chains/bitcoin/observer/inbound.go
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,7 @@ func (ob *Observer) CheckReceiptForBtcTxHash(ctx context.Context, txHash string,
}

// #nosec G115 always positive
event, err := GetBtcEventWithWitness(
event, err := GetBtcEvent(
ob.btcClient,
*tx,
tss,
Expand Down Expand Up @@ -369,7 +369,7 @@ func FilterAndParseIncomingTx(
return nil, errors.Wrapf(err, "error calculating depositor fee for inbound %s", tx.Txid)
}

event, err := GetBtcEventWithWitness(rpcClient, tx, tssAddress, blockNumber, logger, netParams, depositorFee)
event, err := GetBtcEvent(rpcClient, tx, tssAddress, blockNumber, logger, netParams, depositorFee)
if err != nil {
// unable to parse the tx, the caller should retry
return nil, errors.Wrapf(err, "error getting btc event for tx %s in block %d", tx.Txid, blockNumber)
Expand Down Expand Up @@ -431,10 +431,27 @@ func (ob *Observer) DoesInboundContainsRestrictedAddress(inTx *BTCInboundEvent)
return false
}

// GetBtcEvent either returns a valid BTCInboundEvent or nil
// GetBtcEvent returns a valid BTCInboundEvent or nil
// it uses witness data to extract the sender address, except for mainnet
func GetBtcEvent(
rpcClient interfaces.BTCRPCClient,
tx btcjson.TxRawResult,
tssAddress string,
blockNumber uint64,
logger zerolog.Logger,
netParams *chaincfg.Params,
depositorFee float64,
) (*BTCInboundEvent, error) {
if netParams.Name == chaincfg.MainNetParams.Name {
return GetBtcEventWithoutWitness(rpcClient, tx, tssAddress, blockNumber, logger, netParams, depositorFee)
}
return GetBtcEventWithWitness(rpcClient, tx, tssAddress, blockNumber, logger, netParams, depositorFee)
}

// GetBtcEventWithoutWitness either returns a valid BTCInboundEvent or nil
// Note: the caller should retry the tx on error (e.g., GetSenderAddressByVin failed)
// TODO(revamp): simplify this function
func GetBtcEvent(
func GetBtcEventWithoutWitness(
rpcClient interfaces.BTCRPCClient,
tx btcjson.TxRawResult,
tssAddress string,
Expand Down
100 changes: 82 additions & 18 deletions zetaclient/chains/bitcoin/observer/inbound_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -189,13 +189,13 @@ func TestGetSenderAddressByVin(t *testing.T) {
})
}

func TestGetBtcEvent(t *testing.T) {
func TestGetBtcEventWithoutWitness(t *testing.T) {
// load archived inbound P2WPKH raw result
// https://mempool.space/tx/847139aa65aa4a5ee896375951cbf7417cfc8a4d6f277ec11f40cd87319f04aa
txHash := "847139aa65aa4a5ee896375951cbf7417cfc8a4d6f277ec11f40cd87319f04aa"
chain := chains.BitcoinMainnet

// GetBtcEvent arguments
// GetBtcEventWithoutWitness arguments
tx := testutils.LoadBTCInboundRawResult(t, TestDataDir, chain.ChainId, txHash, false)
tssAddress := testutils.TSSAddressBTCMainnet
blockNumber := uint64(835640)
Expand Down Expand Up @@ -227,7 +227,7 @@ func TestGetBtcEvent(t *testing.T) {
rpcClient := testrpc.CreateBTCRPCAndLoadTx(t, TestDataDir, chain.ChainId, preHash)

// get BTC event
event, err := observer.GetBtcEvent(rpcClient, *tx, tssAddress, blockNumber, log.Logger, net, depositorFee)
event, err := observer.GetBtcEventWithoutWitness(rpcClient, *tx, tssAddress, blockNumber, log.Logger, net, depositorFee)
require.NoError(t, err)
require.Equal(t, eventExpected, event)
})
Expand All @@ -243,7 +243,7 @@ func TestGetBtcEvent(t *testing.T) {
rpcClient := testrpc.CreateBTCRPCAndLoadTx(t, TestDataDir, chain.ChainId, preHash)

// get BTC event
event, err := observer.GetBtcEvent(rpcClient, *tx, tssAddress, blockNumber, log.Logger, net, depositorFee)
event, err := observer.GetBtcEventWithoutWitness(rpcClient, *tx, tssAddress, blockNumber, log.Logger, net, depositorFee)
require.NoError(t, err)
require.Equal(t, eventExpected, event)
})
Expand All @@ -259,7 +259,7 @@ func TestGetBtcEvent(t *testing.T) {
rpcClient := testrpc.CreateBTCRPCAndLoadTx(t, TestDataDir, chain.ChainId, preHash)

// get BTC event
event, err := observer.GetBtcEvent(rpcClient, *tx, tssAddress, blockNumber, log.Logger, net, depositorFee)
event, err := observer.GetBtcEventWithoutWitness(rpcClient, *tx, tssAddress, blockNumber, log.Logger, net, depositorFee)
require.NoError(t, err)
require.Equal(t, eventExpected, event)
})
Expand All @@ -275,7 +275,7 @@ func TestGetBtcEvent(t *testing.T) {
rpcClient := testrpc.CreateBTCRPCAndLoadTx(t, TestDataDir, chain.ChainId, preHash)

// get BTC event
event, err := observer.GetBtcEvent(rpcClient, *tx, tssAddress, blockNumber, log.Logger, net, depositorFee)
event, err := observer.GetBtcEventWithoutWitness(rpcClient, *tx, tssAddress, blockNumber, log.Logger, net, depositorFee)
require.NoError(t, err)
require.Equal(t, eventExpected, event)
})
Expand All @@ -291,7 +291,7 @@ func TestGetBtcEvent(t *testing.T) {
rpcClient := testrpc.CreateBTCRPCAndLoadTx(t, TestDataDir, chain.ChainId, preHash)

// get BTC event
event, err := observer.GetBtcEvent(rpcClient, *tx, tssAddress, blockNumber, log.Logger, net, depositorFee)
event, err := observer.GetBtcEventWithoutWitness(rpcClient, *tx, tssAddress, blockNumber, log.Logger, net, depositorFee)
require.NoError(t, err)
require.Equal(t, eventExpected, event)
})
Expand All @@ -303,7 +303,7 @@ func TestGetBtcEvent(t *testing.T) {

// get BTC event
rpcClient := mocks.NewBTCRPCClient(t)
event, err := observer.GetBtcEvent(rpcClient, *tx, tssAddress, blockNumber, log.Logger, net, depositorFee)
event, err := observer.GetBtcEventWithoutWitness(rpcClient, *tx, tssAddress, blockNumber, log.Logger, net, depositorFee)
require.NoError(t, err)
require.Nil(t, event)
})
Expand All @@ -315,13 +315,13 @@ func TestGetBtcEvent(t *testing.T) {

// modify the tx to have Vout[0] a P2SH output
tx.Vout[0].ScriptPubKey.Hex = strings.Replace(tx.Vout[0].ScriptPubKey.Hex, "0014", "a914", 1)
event, err := observer.GetBtcEvent(rpcClient, *tx, tssAddress, blockNumber, log.Logger, net, depositorFee)
event, err := observer.GetBtcEventWithoutWitness(rpcClient, *tx, tssAddress, blockNumber, log.Logger, net, depositorFee)
require.NoError(t, err)
require.Nil(t, event)

// append 1 byte to script to make it longer than 22 bytes
tx.Vout[0].ScriptPubKey.Hex = tx.Vout[0].ScriptPubKey.Hex + "00"
event, err = observer.GetBtcEvent(rpcClient, *tx, tssAddress, blockNumber, log.Logger, net, depositorFee)
event, err = observer.GetBtcEventWithoutWitness(rpcClient, *tx, tssAddress, blockNumber, log.Logger, net, depositorFee)
require.NoError(t, err)
require.Nil(t, event)
})
Expand All @@ -333,7 +333,7 @@ func TestGetBtcEvent(t *testing.T) {

// get BTC event
rpcClient := mocks.NewBTCRPCClient(t)
event, err := observer.GetBtcEvent(rpcClient, *tx, tssAddress, blockNumber, log.Logger, net, depositorFee)
event, err := observer.GetBtcEventWithoutWitness(rpcClient, *tx, tssAddress, blockNumber, log.Logger, net, depositorFee)
require.NoError(t, err)
require.Nil(t, event)
})
Expand All @@ -345,7 +345,7 @@ func TestGetBtcEvent(t *testing.T) {

// get BTC event
rpcClient := mocks.NewBTCRPCClient(t)
event, err := observer.GetBtcEvent(rpcClient, *tx, tssAddress, blockNumber, log.Logger, net, depositorFee)
event, err := observer.GetBtcEventWithoutWitness(rpcClient, *tx, tssAddress, blockNumber, log.Logger, net, depositorFee)
require.NoError(t, err)
require.Nil(t, event)
})
Expand All @@ -357,7 +357,7 @@ func TestGetBtcEvent(t *testing.T) {

// get BTC event
rpcClient := mocks.NewBTCRPCClient(t)
event, err := observer.GetBtcEvent(rpcClient, *tx, tssAddress, blockNumber, log.Logger, net, depositorFee)
event, err := observer.GetBtcEventWithoutWitness(rpcClient, *tx, tssAddress, blockNumber, log.Logger, net, depositorFee)
require.NoError(t, err)
require.Nil(t, event)
})
Expand All @@ -369,7 +369,7 @@ func TestGetBtcEvent(t *testing.T) {

// get BTC event
rpcClient := mocks.NewBTCRPCClient(t)
event, err := observer.GetBtcEvent(rpcClient, *tx, tssAddress, blockNumber, log.Logger, net, depositorFee)
event, err := observer.GetBtcEventWithoutWitness(rpcClient, *tx, tssAddress, blockNumber, log.Logger, net, depositorFee)
require.NoError(t, err)
require.Nil(t, event)
})
Expand All @@ -392,7 +392,7 @@ func TestGetBtcEvent(t *testing.T) {
rpcClient.On("GetRawTransaction", mock.Anything).Return(btcutil.NewTx(msgTx), nil)

// get BTC event
event, err := observer.GetBtcEvent(rpcClient, *tx, tssAddress, blockNumber, log.Logger, net, depositorFee)
event, err := observer.GetBtcEventWithoutWitness(rpcClient, *tx, tssAddress, blockNumber, log.Logger, net, depositorFee)
require.NoError(t, err)
require.Nil(t, event)
})
Expand All @@ -417,7 +417,7 @@ func TestGetBtcEventErrors(t *testing.T) {

// get BTC event
rpcClient := mocks.NewBTCRPCClient(t)
event, err := observer.GetBtcEvent(rpcClient, *tx, tssAddress, blockNumber, log.Logger, net, depositorFee)
event, err := observer.GetBtcEventWithoutWitness(rpcClient, *tx, tssAddress, blockNumber, log.Logger, net, depositorFee)
require.Error(t, err)
require.Nil(t, event)
})
Expand All @@ -429,7 +429,7 @@ func TestGetBtcEventErrors(t *testing.T) {

// get BTC event
rpcClient := mocks.NewBTCRPCClient(t)
event, err := observer.GetBtcEvent(rpcClient, *tx, tssAddress, blockNumber, log.Logger, net, depositorFee)
event, err := observer.GetBtcEventWithoutWitness(rpcClient, *tx, tssAddress, blockNumber, log.Logger, net, depositorFee)
require.ErrorContains(t, err, "no input found")
require.Nil(t, event)
})
Expand All @@ -443,8 +443,72 @@ func TestGetBtcEventErrors(t *testing.T) {
rpcClient.On("GetRawTransaction", mock.Anything).Return(nil, errors.New("rpc error"))

// get BTC event
event, err := observer.GetBtcEvent(rpcClient, *tx, tssAddress, blockNumber, log.Logger, net, depositorFee)
event, err := observer.GetBtcEventWithoutWitness(rpcClient, *tx, tssAddress, blockNumber, log.Logger, net, depositorFee)
require.ErrorContains(t, err, "error getting sender address")
require.Nil(t, event)
})
}

func TestGetBtcEvent(t *testing.T) {
t.Run("should not decode inbound event with witness with mainnet chain", func(t *testing.T) {
// load archived inbound P2WPKH raw result
// https://mempool.space/tx/847139aa65aa4a5ee896375951cbf7417cfc8a4d6f277ec11f40cd87319f04aa
chain := chains.BitcoinMainnet
tssAddress := testutils.TSSAddressBTCMainnet
blockNumber := uint64(835640)
net := &chaincfg.MainNetParams
// 2.992e-05, see avgFeeRate https://mempool.space/api/v1/blocks/835640
depositorFee := bitcoin.DepositorFee(22 * clientcommon.BTCOutboundGasPriceMultiplier)
txHash2 := "37777defed8717c581b4c0509329550e344bdc14ac38f71fc050096887e535c8"
tx := testutils.LoadBTCInboundRawResult(t, TestDataDir, chain.ChainId, txHash2, false)
rpcClient := mocks.NewBTCRPCClient(t)
// get BTC event
event, err := observer.GetBtcEvent(
rpcClient,
*tx,
tssAddress,
blockNumber,
log.Logger,
net,
depositorFee,
)
require.NoError(t, err)
require.Equal(t, (*observer.BTCInboundEvent)(nil), event)
})

t.Run("should support legacy BTC inbound event parsing for mainnet", func(t *testing.T) {
// load archived inbound P2WPKH raw result
// https://mempool.space/tx/847139aa65aa4a5ee896375951cbf7417cfc8a4d6f277ec11f40cd87319f04aa
txHash := "847139aa65aa4a5ee896375951cbf7417cfc8a4d6f277ec11f40cd87319f04aa"
chain := chains.BitcoinMainnet
// GetBtcEventWithoutWitness arguments
tx := testutils.LoadBTCInboundRawResult(t, TestDataDir, chain.ChainId, txHash, false)
tssAddress := testutils.TSSAddressBTCMainnet
blockNumber := uint64(835640)
net := &chaincfg.MainNetParams
// 2.992e-05, see avgFeeRate https://mempool.space/api/v1/blocks/835640
depositorFee := bitcoin.DepositorFee(22 * clientcommon.BTCOutboundGasPriceMultiplier)
// expected result
memo, err := hex.DecodeString(tx.Vout[1].ScriptPubKey.Hex[4:])
require.NoError(t, err)
eventExpected := &observer.BTCInboundEvent{
FromAddress: "bc1q68kxnq52ahz5vd6c8czevsawu0ux9nfrzzrh6e",
ToAddress: tssAddress,
Value: tx.Vout[0].Value - depositorFee, // 7008 sataoshis
MemoBytes: memo,
BlockNumber: blockNumber,
TxHash: tx.Txid,
}
// https://mempool.space/tx/c5d224963832fc0b9a597251c2342a17b25e481a88cc9119008e8f8296652697
preHash := "c5d224963832fc0b9a597251c2342a17b25e481a88cc9119008e8f8296652697"
tx.Vin[0].Txid = preHash
tx.Vin[0].Vout = 2
eventExpected.FromAddress = "bc1q68kxnq52ahz5vd6c8czevsawu0ux9nfrzzrh6e"
// load previous raw tx so so mock rpc client can return it
rpcClient := mocks.NewBTCRPCClient(t)
// get BTC event
event, err := observer.GetBtcEvent(rpcClient, *tx, tssAddress, blockNumber, log.Logger, net, depositorFee)
require.NoError(t, err)
require.Equal(t, eventExpected, event)
})
}

0 comments on commit 40785e7

Please sign in to comment.