Skip to content

Commit 0a13455

Browse files
committed
itest: make script key check in AssertSendEvents optional
The script key check in AssertSendEvents is now optional. sendAsset no longer relies on the script key for event filtering.
1 parent dba1544 commit 0a13455

File tree

2 files changed

+76
-33
lines changed

2 files changed

+76
-33
lines changed

itest/addrs_test.go

Lines changed: 29 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ package itest
33
import (
44
"bytes"
55
"context"
6+
"fmt"
7+
"time"
68

79
"github.com/btcsuite/btcd/btcec/v2/schnorr"
810
"github.com/btcsuite/btcd/wire"
@@ -964,25 +966,40 @@ func sendAsset(t *harnessTest, sender *tapdHarness,
964966

965967
require.NotEmpty(t.t, options.sendAssetRequest.TapAddrs)
966968

967-
// We need the first address's scriptkey to subscribe to events.
968-
firstAddr, err := address.DecodeAddress(
969-
options.sendAssetRequest.TapAddrs[0], &address.RegressionNetTap,
970-
)
971-
require.NoError(t.t, err)
972-
scriptKey := firstAddr.ScriptKey.SerializeCompressed()
969+
// Assign a default transfer label using a Unix timestamp if none is
970+
// provided. This will be used in filtering send events.
971+
if options.sendAssetRequest.Label == "" {
972+
options.sendAssetRequest.Label = fmt.Sprintf(
973+
"%d", time.Now().UnixNano(),
974+
)
975+
}
973976

977+
// Construct send event stream.
974978
ctxc, streamCancel := context.WithCancel(ctxb)
975979
stream, err := sender.SubscribeSendEvents(
976980
ctxc, &taprpc.SubscribeSendEventsRequest{
977-
FilterScriptKey: scriptKey,
981+
FilterLabel: options.sendAssetRequest.Label,
978982
},
979983
)
980984
require.NoError(t.t, err)
985+
986+
// Formulate a subscription handler for the send event stream.
981987
sub := &EventSubscription[*taprpc.SendEvent]{
982988
ClientEventStream: stream,
983989
Cancel: streamCancel,
990+
991+
// Use the filter callback to ensure that the server side filter
992+
// is working as expected.
993+
ShouldNotify: func(e *taprpc.SendEvent) (bool, error) {
994+
require.Equal(
995+
t.t, e.TransferLabel,
996+
options.sendAssetRequest.Label,
997+
)
998+
return true, nil
999+
},
9841000
}
9851001

1002+
// Kick off the send asset request.
9861003
resp, err := sender.SendAsset(ctxt, &options.sendAssetRequest)
9871004
if options.errText != "" {
9881005
require.ErrorContains(t.t, err, options.errText)
@@ -992,8 +1009,12 @@ func sendAsset(t *harnessTest, sender *tapdHarness,
9921009
require.NoError(t.t, err)
9931010

9941011
// We'll get events up to the point where we broadcast the transaction.
1012+
//
1013+
// Don't specify a target script key here, we are already filtering
1014+
// events by the label.
1015+
var targetScriptKey []byte = nil
9951016
AssertSendEvents(
996-
t.t, scriptKey, sub,
1017+
t.t, targetScriptKey, sub,
9971018
tapfreighter.SendStateVirtualCommitmentSelect,
9981019
tapfreighter.SendStateBroadcast,
9991020
)

itest/assertions.go

Lines changed: 47 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -961,6 +961,36 @@ func AssertReceiveEvents(t *testing.T, addr *taprpc.Addr,
961961
}
962962
}
963963

964+
// makeFilterSendEventScriptKey returns a filter function that checks if the
965+
// given script key is present in the send event. If it is, the event is
966+
// included in the stream.
967+
func makeFilterSendEventScriptKey(
968+
scriptKey btcec.PublicKey) func(*taprpc.SendEvent) (bool, error) {
969+
970+
return func(event *taprpc.SendEvent) (bool, error) {
971+
for _, vPacketBytes := range event.VirtualPackets {
972+
vPkt, err := tappsbt.Decode(vPacketBytes)
973+
if err != nil {
974+
return false, err
975+
}
976+
977+
for _, vOut := range vPkt.Outputs {
978+
if vOut.ScriptKey.PubKey == nil {
979+
continue
980+
}
981+
982+
// This packet sends to the filtered script key,
983+
// we want to include this event.
984+
if vOut.ScriptKey.PubKey.IsEqual(&scriptKey) {
985+
return true, nil
986+
}
987+
}
988+
}
989+
990+
return false, nil
991+
}
992+
}
993+
964994
// AssertSendEventsComplete makes sure the two remaining events for the given
965995
// script key are sent on the stream.
966996
func AssertSendEventsComplete(t *testing.T, scriptKey []byte,
@@ -974,39 +1004,27 @@ func AssertSendEventsComplete(t *testing.T, scriptKey []byte,
9741004

9751005
// AssertSendEvents makes sure all events with incremental status are sent
9761006
// on the stream for the given script key.
977-
func AssertSendEvents(t *testing.T, scriptKey []byte,
1007+
func AssertSendEvents(t *testing.T, targetScriptKey []byte,
9781008
stream *EventSubscription[*taprpc.SendEvent], from,
9791009
to tapfreighter.SendState) {
9801010

9811011
success := make(chan struct{})
9821012
timeout := time.After(defaultWaitTimeout)
9831013
startStatus := from
9841014

985-
targetScriptKey, err := btcec.ParsePubKey(scriptKey)
986-
require.NoError(t, err)
987-
988-
sendsToKey := func(e *taprpc.SendEvent) bool {
989-
for _, vPacketBytes := range e.VirtualPackets {
990-
vPkt, err := tappsbt.Decode(vPacketBytes)
991-
require.NoError(t, err)
992-
993-
for _, vOut := range vPkt.Outputs {
994-
if vOut.ScriptKey.PubKey == nil {
995-
continue
996-
}
997-
998-
// This packet sends to the filtered script key,
999-
// we want to include this event.
1000-
if vOut.ScriptKey.PubKey.IsEqual(
1001-
targetScriptKey,
1002-
) {
1015+
// By default, if the target script key is not given we will accept all
1016+
// send events.
1017+
sendsToKey := func(*taprpc.SendEvent) (bool, error) {
1018+
return true, nil
1019+
}
10031020

1004-
return true
1005-
}
1006-
}
1007-
}
1021+
// If a target script key is given, we will only accept send events that
1022+
// relate to that key.
1023+
if targetScriptKey != nil {
1024+
targetScriptKey, err := btcec.ParsePubKey(targetScriptKey)
1025+
require.NoError(t, err)
10081026

1009-
return false
1027+
sendsToKey = makeFilterSendEventScriptKey(*targetScriptKey)
10101028
}
10111029

10121030
// To make sure we don't forever hang on receiving on the stream, we'll
@@ -1026,7 +1044,11 @@ func AssertSendEvents(t *testing.T, scriptKey []byte,
10261044
event, err := stream.Recv()
10271045
require.NoError(t, err, "receiving send event")
10281046

1029-
require.True(t, sendsToKey(event))
1047+
// Ensure that the event complies with the target script key
1048+
// check.
1049+
res, err := sendsToKey(event)
1050+
require.NoError(t, err)
1051+
require.True(t, res)
10301052

10311053
// Check the event's error field for unexpected errors. Perform
10321054
// this check before verifying the expected send state, as

0 commit comments

Comments
 (0)