Skip to content

Commit 60fbb90

Browse files
darioAnongbaGeorgeTsagk
authored andcommitted
itest: add test for address receives pagination
1 parent 12dccdd commit 60fbb90

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
@@ -881,142 +881,221 @@ func testUnknownTlvType(t *harnessTest) {
881881

882882
// testAddrReceives tests the fetching of address events.
883883
func testAddrReceives(t *harnessTest) {
884-
// First, mint an asset so we have something to create addresses for.
884+
// First, mint a few assets, so we have some to create addresses for.
885885
rpcAssets := MintAssetsConfirmBatch(
886886
t.t, t.lndHarness.Miner().Client, t.tapd,
887887
[]*mintrpc.MintAssetRequest{
888-
simpleAssets[0],
888+
simpleAssets[0], issuableAssets[0],
889889
},
890890
)
891-
asset := rpcAssets[0]
892891

893892
ctxb := context.Background()
894893
ctxt, cancel := context.WithTimeout(ctxb, defaultWaitTimeout)
895894
defer cancel()
896895

897-
// Create a second node that'll be the receiver.
896+
// We'll make a second node now that'll be the receiver of all the
897+
// assets made above.
898898
bobLnd := t.lndHarness.NewNodeWithCoins("Bob", nil)
899899
bob := setupTapdHarness(t.t, t, bobLnd, t.universeServer)
900900
defer func() {
901901
require.NoError(t.t, bob.stop(!*noDelete))
902902
}()
903903

904-
// Create an address and send assets to it.
905-
addr, events := NewAddrWithEventStream(
906-
t.t, bob, &taprpc.NewAddrRequest{
907-
AssetId: asset.AssetGenesis.AssetId,
908-
Amt: asset.Amount - 1,
909-
AssetVersion: asset.Version,
910-
},
911-
)
904+
timeBeforeSend := time.Now()
912905

913-
AssertAddrCreated(t.t, bob, asset, addr)
906+
const numAddresses = 6
907+
for i := range numAddresses {
908+
// Use different assets for variety.
909+
assetIdx := i % len(rpcAssets)
910+
asset := rpcAssets[assetIdx]
914911

915-
// Record the time before sending.
916-
timeBeforeSend := time.Now()
912+
addr, events := NewAddrWithEventStream(
913+
t.t, bob, &taprpc.NewAddrRequest{
914+
AssetId: asset.AssetGenesis.AssetId,
915+
Amt: uint64(10),
916+
AssetVersion: asset.Version,
917+
},
918+
)
917919

918-
// Send assets to the address.
919-
sendResp, sendEvents := sendAssetsToAddr(t, t.tapd, addr)
920-
require.NotNil(t.t, sendResp)
920+
AssertAddrCreated(t.t, bob, asset, addr)
921921

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

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

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

931-
// Record the time after sending.
932-
timeAfterSend := time.Now()
930+
// Eventually the event should be marked as confirmed.
931+
AssertAddrEvent(t.t, bob, addr, 1, statusConfirmed)
933932

934-
// Wait for the receive to complete.
935-
AssertNonInteractiveRecvComplete(t.t, bob, 1)
936-
AssertSendEventsComplete(t.t, addr.ScriptKey, sendEvents)
937-
AssertReceiveEvents(t.t, addr, events)
933+
// Make sure we have imported and finalized all proofs.
934+
AssertNonInteractiveRecvComplete(t.t, bob, i+1)
935+
AssertSendEventsComplete(t.t, addr.ScriptKey, sendEvents)
938936

939-
// Test 1: Get all events without timestamp filtering.
937+
// Make sure the receiver has received all events in order for
938+
// the address.
939+
AssertReceiveEvents(t.t, addr, events)
940+
}
941+
942+
timeAfterSend := time.Now()
943+
944+
// Test all events.
940945
resp, err := bob.AddrReceives(
941946
ctxt, &taprpc.AddrReceivesRequest{},
942947
)
943948
require.NoError(t.t, err)
944-
require.Len(t.t, resp.Events, 1)
949+
require.Len(t.t, resp.Events, numAddresses)
945950

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

956-
// Test 3: Filter by start timestamp exactly at the send time
957-
// (should return the event).
958-
resp, err = bob.AddrReceives(
959-
ctxt, &taprpc.AddrReceivesRequest{
960-
StartTimestamp: uint64(timeBeforeSend.Unix()),
1007+
// Test pagination through all results.
1008+
var allPaginatedEvents []*taprpc.AddrEvent
1009+
offset := int32(0)
1010+
limit := int32(3)
1011+
1012+
for {
1013+
resp, err := bob.AddrReceives(ctxt,
1014+
&taprpc.AddrReceivesRequest{
1015+
Offset: offset,
1016+
Limit: limit,
1017+
},
1018+
)
1019+
require.NoError(t.t, err)
1020+
1021+
if len(resp.Events) == 0 {
1022+
break
1023+
}
1024+
1025+
allPaginatedEvents = append(allPaginatedEvents, resp.Events...)
1026+
offset += limit
1027+
}
1028+
1029+
// Should have collected all events.
1030+
require.Len(t.t, allPaginatedEvents, numAddresses)
1031+
1032+
// Test negative offset and limit error.
1033+
_, err = bob.AddrReceives(ctxt,
1034+
&taprpc.AddrReceivesRequest{
1035+
Offset: -5,
9611036
},
9621037
)
963-
require.NoError(t.t, err)
964-
require.Len(t.t, resp.Events, 1)
1038+
require.Error(t.t, err)
1039+
require.Contains(t.t, err.Error(), "offset must be non-negative")
9651040

966-
// Test 4: Filter by address and start timestamp.
1041+
_, err = bob.AddrReceives(ctxt,
1042+
&taprpc.AddrReceivesRequest{
1043+
Limit: -5,
1044+
},
1045+
)
1046+
require.Error(t.t, err)
1047+
require.Contains(t.t, err.Error(), "limit must be non-negative")
1048+
1049+
// Test filter by start timestamp before the send
1050+
// (should return events).
9671051
resp, err = bob.AddrReceives(
9681052
ctxt, &taprpc.AddrReceivesRequest{
969-
FilterAddr: addr.Encoded,
970-
StartTimestamp: uint64(timeBeforeSend.Unix()),
1053+
StartTimestamp: uint64(timeBeforeSend.Unix() - 1),
9711054
},
9721055
)
9731056
require.NoError(t.t, err)
974-
require.Len(t.t, resp.Events, 1)
975-
require.Equal(
976-
t.t, addr.Encoded, resp.Events[0].Addr.Encoded,
977-
)
1057+
require.Len(t.t, resp.Events, numAddresses)
9781058

979-
// Test 5: Filter by address and start timestamp after send
980-
// (should return no events).
1059+
// Test filter by start timestamp exactly at the send time
1060+
// (should return all events).
9811061
resp, err = bob.AddrReceives(
9821062
ctxt, &taprpc.AddrReceivesRequest{
983-
FilterAddr: addr.Encoded,
984-
StartTimestamp: uint64(timeAfterSend.Unix() + 1),
1063+
StartTimestamp: uint64(timeBeforeSend.Unix()),
9851064
},
9861065
)
9871066
require.NoError(t.t, err)
988-
require.Len(t.t, resp.Events, 0)
1067+
require.Len(t.t, resp.Events, numAddresses)
9891068

990-
// Test 6: Filter by end timestamp before the send
1069+
// Test filter by end timestamp before the send
9911070
// (should return no events).
9921071
resp, err = bob.AddrReceives(
9931072
ctxt, &taprpc.AddrReceivesRequest{
994-
EndTimestamp: uint64(timeBeforeSend.Unix()),
1073+
EndTimestamp: uint64(timeBeforeSend.Unix() - 1),
9951074
},
9961075
)
9971076
require.NoError(t.t, err)
9981077
require.Len(t.t, resp.Events, 0)
9991078

1000-
// Test 7: Filter by end timestamp after the send
1001-
// (should return the event).
1079+
// Test filter by end timestamp after the send
1080+
// (should return all events).
10021081
resp, err = bob.AddrReceives(
10031082
ctxt, &taprpc.AddrReceivesRequest{
10041083
EndTimestamp: uint64(timeAfterSend.Unix() + 1),
10051084
},
10061085
)
10071086
require.NoError(t.t, err)
1008-
require.Len(t.t, resp.Events, 1)
1087+
require.Len(t.t, resp.Events, numAddresses)
10091088

1010-
// Test 8: Filter by both start and end timestamp
1011-
// (should return the event).
1089+
// Test filter by both start and end timestamp
1090+
// (should return all events).
10121091
resp, err = bob.AddrReceives(
10131092
ctxt, &taprpc.AddrReceivesRequest{
10141093
StartTimestamp: uint64(timeBeforeSend.Unix()),
10151094
EndTimestamp: uint64(timeAfterSend.Unix() + 1),
10161095
},
10171096
)
10181097
require.NoError(t.t, err)
1019-
require.Len(t.t, resp.Events, 1)
1098+
require.Len(t.t, resp.Events, numAddresses)
10201099
}
10211100

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

0 commit comments

Comments
 (0)