@@ -2205,3 +2205,147 @@ func TestTransferOutputProofDeliveryStatus(t *testing.T) {
2205
2205
t , randBlockHash [:], assetTransfers [0 ].AnchorTxBlockHash ,
2206
2206
)
2207
2207
}
2208
+
2209
+ func TestQueryAssetBalances (t * testing.T ) {
2210
+ t .Parallel ()
2211
+
2212
+ _ , assetsStore , _ := newAssetStore (t )
2213
+ ctx := context .Background ()
2214
+
2215
+ // First, we'll generate 3 assets, two of them sharing the same anchor
2216
+ // transaction, but all having distinct asset IDs.
2217
+ const numAssets = 4
2218
+ const numGroups = 2
2219
+ assetGen := newAssetGenerator (t , numAssets , numGroups )
2220
+ assetDesc := []assetDesc {
2221
+ {
2222
+ assetGen : assetGen .assetGens [0 ],
2223
+ anchorPoint : assetGen .anchorPoints [0 ],
2224
+ keyGroup : assetGen .groupKeys [0 ],
2225
+ amt : 16 ,
2226
+ },
2227
+ {
2228
+ assetGen : assetGen .assetGens [1 ],
2229
+ anchorPoint : assetGen .anchorPoints [0 ],
2230
+ keyGroup : assetGen .groupKeys [1 ],
2231
+ amt : 10 ,
2232
+ },
2233
+ {
2234
+ assetGen : assetGen .assetGens [2 ],
2235
+ anchorPoint : assetGen .anchorPoints [1 ],
2236
+ keyGroup : assetGen .groupKeys [0 ],
2237
+ groupAnchorGen : & assetGen .assetGens [0 ],
2238
+ groupAnchorGenPoint : & assetGen .anchorPoints [0 ],
2239
+ amt : 6 ,
2240
+ },
2241
+ {
2242
+ assetGen : assetGen .assetGens [3 ],
2243
+ anchorPoint : assetGen .anchorPoints [3 ],
2244
+ noGroupKey : true ,
2245
+ amt : 4 ,
2246
+ },
2247
+ }
2248
+ assetGen .genAssets (t , assetsStore , assetDesc )
2249
+
2250
+ // Loop through assetDesc and sum the amt values
2251
+ totalBalances := uint64 (0 )
2252
+ for _ , desc := range assetDesc {
2253
+ totalBalances += desc .amt
2254
+ }
2255
+ totalGroupedBalances := totalBalances - assetDesc [3 ].amt
2256
+
2257
+ // At first, none of the assets should be leased.
2258
+ includeLeased := false
2259
+ balances , err := assetsStore .QueryBalancesByAsset (
2260
+ ctx , nil , includeLeased ,
2261
+ )
2262
+ require .NoError (t , err )
2263
+ balancesByGroup , err := assetsStore .QueryAssetBalancesByGroup (
2264
+ ctx , nil , includeLeased ,
2265
+ )
2266
+ require .NoError (t , err )
2267
+ require .Len (t , balances , numAssets )
2268
+ require .Len (t , balancesByGroup , len (assetGen .groupKeys ))
2269
+
2270
+ balanceSum := uint64 (0 )
2271
+ for _ , balance := range balances {
2272
+ balanceSum += balance .Balance
2273
+ }
2274
+ require .Equal (t , totalBalances , balanceSum )
2275
+
2276
+ balanceByGroupSum := uint64 (0 )
2277
+ for _ , balance := range balancesByGroup {
2278
+ balanceByGroupSum += balance .Balance
2279
+ }
2280
+ require .Equal (t , totalGroupedBalances , balanceByGroupSum )
2281
+
2282
+ // Now we lease the first asset for 1 hour. This will cause the second
2283
+ // one also to be leased, since it's on the same anchor transaction. The
2284
+ // second asset is in its own group, so when leased, the entire group is
2285
+ // leased.
2286
+ leaseOwner := fn.ToArray [[32 ]byte ](test .RandBytes (32 ))
2287
+ leaseExpiry := time .Now ().Add (time .Hour )
2288
+ err = assetsStore .LeaseCoins (
2289
+ ctx , leaseOwner , leaseExpiry , assetGen .anchorPoints [0 ],
2290
+ )
2291
+ require .NoError (t , err )
2292
+ // With the first two assets leased, the total balance of unleased
2293
+ // assets is the sum of the third and fourth asset.
2294
+ totalUnleasedBalances := assetDesc [2 ].amt + assetDesc [3 ].amt
2295
+ // The total of unleased grouped assets is only the third asset.
2296
+ totalUnleasedGroupedBalances := assetDesc [2 ].amt
2297
+
2298
+ // Only two assets should be returned that is not leased.
2299
+ unleasedBalances , err := assetsStore .QueryBalancesByAsset (
2300
+ ctx , nil , includeLeased ,
2301
+ )
2302
+ require .NoError (t , err )
2303
+ require .Len (t , unleasedBalances , numAssets - 2 )
2304
+
2305
+ // Only one group should be returned that is not leased.
2306
+ unleasedBalancesByGroup , err := assetsStore .QueryAssetBalancesByGroup (
2307
+ ctx , nil , includeLeased ,
2308
+ )
2309
+ require .NoError (t , err )
2310
+ require .Len (t , unleasedBalancesByGroup , numGroups - 1 )
2311
+
2312
+ unleasedBalanceSum := uint64 (0 )
2313
+ for _ , balance := range unleasedBalances {
2314
+ unleasedBalanceSum += balance .Balance
2315
+ }
2316
+ require .Equal (t , totalUnleasedBalances , unleasedBalanceSum )
2317
+
2318
+ unleasedBalanceByGroupSum := uint64 (0 )
2319
+ for _ , balance := range unleasedBalancesByGroup {
2320
+ unleasedBalanceByGroupSum += balance .Balance
2321
+ }
2322
+ require .Equal (
2323
+ t , totalUnleasedGroupedBalances , unleasedBalanceByGroupSum ,
2324
+ )
2325
+
2326
+ // Now we'll query with the leased assets included. This should return
2327
+ // the same results as when the assets where unleased.
2328
+ includeLeased = true
2329
+ includeLeasedBalances , err := assetsStore .QueryBalancesByAsset (
2330
+ ctx , nil , includeLeased ,
2331
+ )
2332
+ require .NoError (t , err )
2333
+ require .Len (t , includeLeasedBalances , numAssets )
2334
+ includeLeasedBalByGroup , err := assetsStore .QueryAssetBalancesByGroup (
2335
+ ctx , nil , includeLeased ,
2336
+ )
2337
+ require .NoError (t , err )
2338
+ require .Len (t , includeLeasedBalByGroup , len (assetGen .groupKeys ))
2339
+
2340
+ leasedBalanceSum := uint64 (0 )
2341
+ for _ , balance := range includeLeasedBalances {
2342
+ leasedBalanceSum += balance .Balance
2343
+ }
2344
+ require .Equal (t , totalBalances , leasedBalanceSum )
2345
+
2346
+ leasedBalanceByGroupSum := uint64 (0 )
2347
+ for _ , balance := range includeLeasedBalByGroup {
2348
+ leasedBalanceByGroupSum += balance .Balance
2349
+ }
2350
+ require .Equal (t , totalGroupedBalances , leasedBalanceByGroupSum )
2351
+ }
0 commit comments