Skip to content

Commit 4422507

Browse files
committed
itest: test address events
1 parent 8490249 commit 4422507

File tree

1 file changed

+144
-65
lines changed

1 file changed

+144
-65
lines changed

itest/addrs_test.go

Lines changed: 144 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -875,142 +875,221 @@ func testUnknownTlvType(t *harnessTest) {
875875

876876
// testAddrReceives tests the fetching of address events.
877877
func testAddrReceives(t *harnessTest) {
878-
// First, mint an asset so we have something to create addresses for.
878+
// First, mint a few assets, so we have some to create addresses for.
879879
rpcAssets := MintAssetsConfirmBatch(
880880
t.t, t.lndHarness.Miner().Client, t.tapd,
881881
[]*mintrpc.MintAssetRequest{
882-
simpleAssets[0],
882+
simpleAssets[0], issuableAssets[0],
883883
},
884884
)
885-
asset := rpcAssets[0]
886885

887886
ctxb := context.Background()
888887
ctxt, cancel := context.WithTimeout(ctxb, defaultWaitTimeout)
889888
defer cancel()
890889

891-
// Create a second node that'll be the receiver.
890+
// We'll make a second node now that'll be the receiver of all the
891+
// assets made above.
892892
bobLnd := t.lndHarness.NewNodeWithCoins("Bob", nil)
893893
bob := setupTapdHarness(t.t, t, bobLnd, t.universeServer)
894894
defer func() {
895895
require.NoError(t.t, bob.stop(!*noDelete))
896896
}()
897897

898-
// Create an address and send assets to it.
899-
addr, events := NewAddrWithEventStream(
900-
t.t, bob, &taprpc.NewAddrRequest{
901-
AssetId: asset.AssetGenesis.AssetId,
902-
Amt: asset.Amount - 1,
903-
AssetVersion: asset.Version,
904-
},
905-
)
898+
timeBeforeSend := time.Now()
906899

907-
AssertAddrCreated(t.t, bob, asset, addr)
900+
const numAddresses = 6
901+
for i := range numAddresses {
902+
// Use different assets for variety.
903+
assetIdx := i % len(rpcAssets)
904+
asset := rpcAssets[assetIdx]
908905

909-
// Record the time before sending.
910-
timeBeforeSend := time.Now()
906+
addr, events := NewAddrWithEventStream(
907+
t.t, bob, &taprpc.NewAddrRequest{
908+
AssetId: asset.AssetGenesis.AssetId,
909+
Amt: uint64(10),
910+
AssetVersion: asset.Version,
911+
},
912+
)
911913

912-
// Send assets to the address.
913-
sendResp, sendEvents := sendAssetsToAddr(t, t.tapd, addr)
914-
require.NotNil(t.t, sendResp)
914+
AssertAddrCreated(t.t, bob, asset, addr)
915915

916-
// Wait for the event to be detected.
917-
AssertAddrEvent(t.t, bob, addr, 1, statusDetected)
916+
// Send assets to the address.
917+
_, sendEvents := sendAssetsToAddr(t, t.tapd, addr)
918918

919-
// Mine a block to confirm the transaction.
920-
MineBlocks(t.t, t.lndHarness.Miner().Client, 1, 1)
919+
AssertAddrEvent(t.t, bob, addr, 1, statusDetected)
921920

922-
// Wait for the event to be confirmed.
923-
AssertAddrEvent(t.t, bob, addr, 1, statusConfirmed)
921+
// Mine a block to make sure the events are marked as confirmed.
922+
MineBlocks(t.t, t.lndHarness.Miner().Client, 1, 1)
924923

925-
// Record the time after sending.
926-
timeAfterSend := time.Now()
924+
// Eventually the event should be marked as confirmed.
925+
AssertAddrEvent(t.t, bob, addr, 1, statusConfirmed)
927926

928-
// Wait for the receive to complete.
929-
AssertNonInteractiveRecvComplete(t.t, bob, 1)
930-
AssertSendEventsComplete(t.t, addr.ScriptKey, sendEvents)
931-
AssertReceiveEvents(t.t, addr, events)
927+
// Make sure we have imported and finalized all proofs.
928+
AssertNonInteractiveRecvComplete(t.t, bob, i+1)
929+
AssertSendEventsComplete(t.t, addr.ScriptKey, sendEvents)
932930

933-
// Test 1: Get all events without timestamp filtering.
931+
// Make sure the receiver has received all events in order for
932+
// the address.
933+
AssertReceiveEvents(t.t, addr, events)
934+
}
935+
936+
timeAfterSend := time.Now()
937+
938+
// Test all events.
934939
resp, err := bob.AddrReceives(
935940
ctxt, &taprpc.AddrReceivesRequest{},
936941
)
937942
require.NoError(t.t, err)
938-
require.Len(t.t, resp.Events, 1)
943+
require.Len(t.t, resp.Events, numAddresses)
939944

940-
// Test 2: Filter by start timestamp before the send
941-
// (should return events).
942-
resp, err = bob.AddrReceives(
943-
ctxt, &taprpc.AddrReceivesRequest{
944-
StartTimestamp: uint64(timeBeforeSend.Unix()),
945+
// Test limit.
946+
resp, err = bob.AddrReceives(ctxt, &taprpc.AddrReceivesRequest{
947+
Limit: 3,
948+
})
949+
require.NoError(t.t, err)
950+
require.Len(t.t, resp.Events, 3)
951+
952+
// Test offset.
953+
resp, err = bob.AddrReceives(ctxt, &taprpc.AddrReceivesRequest{
954+
Offset: 2,
955+
Limit: 3,
956+
})
957+
require.NoError(t.t, err)
958+
require.Len(t.t, resp.Events, 3)
959+
960+
// Test ascending direction (default).
961+
resp, err = bob.AddrReceives(ctxt, &taprpc.AddrReceivesRequest{
962+
Limit: 5,
963+
})
964+
require.NoError(t.t, err)
965+
require.Len(t.t, resp.Events, 5)
966+
967+
// Verify ascending order by checking creation times.
968+
for i := 1; i < len(resp.Events); i++ {
969+
require.LessOrEqual(t.t,
970+
resp.Events[i-1].CreationTimeUnixSeconds,
971+
resp.Events[i].CreationTimeUnixSeconds,
972+
)
973+
}
974+
975+
// Test descending direction.
976+
resp, err = bob.AddrReceives(ctxt, &taprpc.AddrReceivesRequest{
977+
Limit: 5,
978+
Direction: taprpc.SortDirection_SORT_DIRECTION_DESC,
979+
})
980+
require.NoError(t.t, err)
981+
require.Len(t.t, resp.Events, 5)
982+
983+
// Verify descending order by checking creation times.
984+
for i := 1; i < len(resp.Events); i++ {
985+
require.GreaterOrEqual(t.t,
986+
resp.Events[i-1].CreationTimeUnixSeconds,
987+
resp.Events[i].CreationTimeUnixSeconds,
988+
)
989+
}
990+
991+
// Test offset out of bounds.
992+
resp, err = bob.AddrReceives(ctxt,
993+
&taprpc.AddrReceivesRequest{
994+
Offset: 100,
995+
Limit: 10,
945996
},
946997
)
947998
require.NoError(t.t, err)
948-
require.Len(t.t, resp.Events, 1)
999+
require.Len(t.t, resp.Events, 0)
9491000

950-
// Test 3: Filter by start timestamp exactly at the send time
951-
// (should return the event).
952-
resp, err = bob.AddrReceives(
953-
ctxt, &taprpc.AddrReceivesRequest{
954-
StartTimestamp: uint64(timeBeforeSend.Unix()),
1001+
// Test pagination through all results.
1002+
var allPaginatedEvents []*taprpc.AddrEvent
1003+
offset := int32(0)
1004+
limit := int32(3)
1005+
1006+
for {
1007+
resp, err := bob.AddrReceives(ctxt,
1008+
&taprpc.AddrReceivesRequest{
1009+
Offset: offset,
1010+
Limit: limit,
1011+
},
1012+
)
1013+
require.NoError(t.t, err)
1014+
1015+
if len(resp.Events) == 0 {
1016+
break
1017+
}
1018+
1019+
allPaginatedEvents = append(allPaginatedEvents, resp.Events...)
1020+
offset += limit
1021+
}
1022+
1023+
// Should have collected all events.
1024+
require.Len(t.t, allPaginatedEvents, numAddresses)
1025+
1026+
// Test negative offset and limit error.
1027+
_, err = bob.AddrReceives(ctxt,
1028+
&taprpc.AddrReceivesRequest{
1029+
Offset: -5,
9551030
},
9561031
)
957-
require.NoError(t.t, err)
958-
require.Len(t.t, resp.Events, 1)
1032+
require.Error(t.t, err)
1033+
require.Contains(t.t, err.Error(), "offset must be non-negative")
9591034

960-
// Test 4: Filter by address and start timestamp.
1035+
_, err = bob.AddrReceives(ctxt,
1036+
&taprpc.AddrReceivesRequest{
1037+
Limit: -5,
1038+
},
1039+
)
1040+
require.Error(t.t, err)
1041+
require.Contains(t.t, err.Error(), "limit must be non-negative")
1042+
1043+
// Test filter by start timestamp before the send
1044+
// (should return events).
9611045
resp, err = bob.AddrReceives(
9621046
ctxt, &taprpc.AddrReceivesRequest{
963-
FilterAddr: addr.Encoded,
964-
StartTimestamp: uint64(timeBeforeSend.Unix()),
1047+
StartTimestamp: uint64(timeBeforeSend.Unix() - 1),
9651048
},
9661049
)
9671050
require.NoError(t.t, err)
968-
require.Len(t.t, resp.Events, 1)
969-
require.Equal(
970-
t.t, addr.Encoded, resp.Events[0].Addr.Encoded,
971-
)
1051+
require.Len(t.t, resp.Events, numAddresses)
9721052

973-
// Test 5: Filter by address and start timestamp after send
974-
// (should return no events).
1053+
// Test filter by start timestamp exactly at the send time
1054+
// (should return all events).
9751055
resp, err = bob.AddrReceives(
9761056
ctxt, &taprpc.AddrReceivesRequest{
977-
FilterAddr: addr.Encoded,
978-
StartTimestamp: uint64(timeAfterSend.Unix() + 1),
1057+
StartTimestamp: uint64(timeBeforeSend.Unix()),
9791058
},
9801059
)
9811060
require.NoError(t.t, err)
982-
require.Len(t.t, resp.Events, 0)
1061+
require.Len(t.t, resp.Events, numAddresses)
9831062

984-
// Test 6: Filter by end timestamp before the send
1063+
// Test filter by end timestamp before the send
9851064
// (should return no events).
9861065
resp, err = bob.AddrReceives(
9871066
ctxt, &taprpc.AddrReceivesRequest{
988-
EndTimestamp: uint64(timeBeforeSend.Unix()),
1067+
EndTimestamp: uint64(timeBeforeSend.Unix() - 1),
9891068
},
9901069
)
9911070
require.NoError(t.t, err)
9921071
require.Len(t.t, resp.Events, 0)
9931072

994-
// Test 7: Filter by end timestamp after the send
995-
// (should return the event).
1073+
// Test filter by end timestamp after the send
1074+
// (should return all events).
9961075
resp, err = bob.AddrReceives(
9971076
ctxt, &taprpc.AddrReceivesRequest{
9981077
EndTimestamp: uint64(timeAfterSend.Unix() + 1),
9991078
},
10001079
)
10011080
require.NoError(t.t, err)
1002-
require.Len(t.t, resp.Events, 1)
1081+
require.Len(t.t, resp.Events, numAddresses)
10031082

1004-
// Test 8: Filter by both start and end timestamp
1005-
// (should return the event).
1083+
// Test filter by both start and end timestamp
1084+
// (should return all events).
10061085
resp, err = bob.AddrReceives(
10071086
ctxt, &taprpc.AddrReceivesRequest{
10081087
StartTimestamp: uint64(timeBeforeSend.Unix()),
10091088
EndTimestamp: uint64(timeAfterSend.Unix() + 1),
10101089
},
10111090
)
10121091
require.NoError(t.t, err)
1013-
require.Len(t.t, resp.Events, 1)
1092+
require.Len(t.t, resp.Events, numAddresses)
10141093
}
10151094

10161095
// sendProof manually exports a proof from the given source node and imports it

0 commit comments

Comments
 (0)