@@ -5,17 +5,18 @@ import (
5
5
"context"
6
6
"encoding/hex"
7
7
"fmt"
8
+ "math"
9
+ "sort"
10
+ "strings"
11
+ "time"
12
+
8
13
"github.com/ark-network/ark/common"
9
14
"github.com/ark-network/ark/common/tree"
10
15
"github.com/ark-network/ark/server/internal/core/domain"
11
16
"github.com/ark-network/ark/server/internal/core/ports"
12
17
"github.com/btcsuite/btcd/btcec/v2/schnorr"
13
18
"github.com/btcsuite/btcd/btcutil/psbt"
14
19
"github.com/decred/dcrd/dcrec/secp256k1/v4"
15
- "math"
16
- "sort"
17
- "strings"
18
- "time"
19
20
)
20
21
21
22
const (
@@ -30,13 +31,13 @@ const (
30
31
31
32
type IndexerService interface {
32
33
GetCommitmentTxInfo (ctx context.Context , txid string ) (* CommitmentTxResp , error )
33
- GetVtxoTree (ctx context.Context , batchOutpoint Outpoint , page Page ) (* VtxoTreeResp , error )
34
- GetForfeitTxs (ctx context.Context , batchOutpoint Outpoint , page Page ) (* ForfeitTxsResp , error )
35
- GetConnectors (ctx context.Context , batchOutpoint Outpoint , page Page ) (* ConnectorResp , error )
36
- GetSpendableVtxos (ctx context.Context , address string , page Page ) (* SpendableVtxosResp , error )
37
- GetTransactionHistory (ctx context.Context , address string , start , end int64 , page Page ) (* TxHistoryResp , error )
38
- GetVtxoChain (ctx context.Context , vtxoKey Outpoint , page Page ) (* VtxoChainResp , error )
39
- GetVirtualTxs (ctx context.Context , txids []string , page Page ) (* VirtualTxsResp , error )
34
+ GetVtxoTree (ctx context.Context , batchOutpoint Outpoint , page * Page ) (* VtxoTreeResp , error )
35
+ GetForfeitTxs (ctx context.Context , batchOutpoint Outpoint , page * Page ) (* ForfeitTxsResp , error )
36
+ GetConnectors (ctx context.Context , batchOutpoint Outpoint , page * Page ) (* ConnectorResp , error )
37
+ GetSpendableVtxos (ctx context.Context , address string , page * Page ) (* SpendableVtxosResp , error )
38
+ GetTransactionHistory (ctx context.Context , address string , start , end int64 , page * Page ) (* TxHistoryResp , error )
39
+ GetVtxoChain (ctx context.Context , vtxoKey Outpoint , page * Page ) (* VtxoChainResp , error )
40
+ GetVirtualTxs (ctx context.Context , txids []string , page * Page ) (* VirtualTxsResp , error )
40
41
GetSweptCommitmentTx (ctx context.Context , txid string ) (* SweptCommitmentTxResp , error )
41
42
}
42
43
@@ -82,7 +83,7 @@ func (i *indexerService) GetCommitmentTxInfo(
82
83
}, nil
83
84
}
84
85
85
- func (i * indexerService ) GetVtxoTree (ctx context.Context , batchOutpoint Outpoint , page Page ) (* VtxoTreeResp , error ) {
86
+ func (i * indexerService ) GetVtxoTree (ctx context.Context , batchOutpoint Outpoint , page * Page ) (* VtxoTreeResp , error ) {
86
87
vtxoTree , err := i .repoManager .Rounds ().GetVtxoTreeWithTxid (ctx , batchOutpoint .Txid ) //TODO repo methods needs to be updated with multiple batches in future
87
88
if err != nil {
88
89
return nil , err
@@ -96,7 +97,7 @@ func (i *indexerService) GetVtxoTree(ctx context.Context, batchOutpoint Outpoint
96
97
}, nil
97
98
}
98
99
99
- func (i * indexerService ) GetForfeitTxs (ctx context.Context , batchOutpoint Outpoint , page Page ) (* ForfeitTxsResp , error ) {
100
+ func (i * indexerService ) GetForfeitTxs (ctx context.Context , batchOutpoint Outpoint , page * Page ) (* ForfeitTxsResp , error ) {
100
101
forfeitTxs , err := i .repoManager .Rounds ().GetRoundForfeitTxs (ctx , batchOutpoint .Txid ) //TODO batch thing
101
102
if err != nil {
102
103
return nil , err
@@ -116,21 +117,21 @@ func (i *indexerService) GetForfeitTxs(ctx context.Context, batchOutpoint Outpoi
116
117
117
118
}
118
119
119
- func (i * indexerService ) GetConnectors (ctx context.Context , batchOutpoint Outpoint , page Page ) (* ConnectorResp , error ) {
120
- round , err := i .repoManager .Rounds ().GetRoundWithTxid (ctx , batchOutpoint .Txid ) //TODO batch thing
120
+ func (i * indexerService ) GetConnectors (ctx context.Context , batchOutpoint Outpoint , page * Page ) (* ConnectorResp , error ) {
121
+ connectorTree , err := i .repoManager .Rounds ().GetRoundConnectorTree (ctx , batchOutpoint .Txid ) //TODO batch thing
121
122
if err != nil {
122
123
return nil , err
123
124
}
124
125
125
- connectors , pageResp := paginate (flattenNodes (round . Connectors ), page , maxPageSizeConnector )
126
+ nodes , pageResp := paginate (flattenNodes (connectorTree ), page , maxPageSizeVtxoTree )
126
127
127
128
return & ConnectorResp {
128
- Connectors : connectors ,
129
+ Connectors : nodes ,
129
130
Page : pageResp ,
130
131
}, nil
131
132
}
132
133
133
- func (i * indexerService ) GetSpendableVtxos (ctx context.Context , address string , page Page ) (* SpendableVtxosResp , error ) {
134
+ func (i * indexerService ) GetSpendableVtxos (ctx context.Context , address string , page * Page ) (* SpendableVtxosResp , error ) {
134
135
decodedAddress , err := common .DecodeAddress (address )
135
136
if err != nil {
136
137
return nil , err
@@ -156,7 +157,7 @@ func (i *indexerService) GetSpendableVtxos(ctx context.Context, address string,
156
157
}
157
158
158
159
func (i * indexerService ) GetTransactionHistory (
159
- ctx context.Context , address string , start , end int64 , page Page ,
160
+ ctx context.Context , address string , start , end int64 , page * Page ,
160
161
) (* TxHistoryResp , error ) {
161
162
allVtxos , err := i .repoManager .Vtxos ().GetAll (ctx )
162
163
if err != nil {
@@ -203,7 +204,7 @@ type vtxoKeyWithCreatedAt struct {
203
204
CreatedAt int64
204
205
}
205
206
206
- func (i * indexerService ) GetVtxoChain (ctx context.Context , vtxoKey Outpoint , page Page ) (* VtxoChainResp , error ) {
207
+ func (i * indexerService ) GetVtxoChain (ctx context.Context , vtxoKey Outpoint , page * Page ) (* VtxoChainResp , error ) {
207
208
chainMap := make (map [vtxoKeyWithCreatedAt ][]string )
208
209
209
210
outpoint := domain.VtxoKey {
@@ -289,7 +290,7 @@ func (i *indexerService) buildChain(
289
290
return nil
290
291
}
291
292
292
- func (i * indexerService ) GetVirtualTxs (ctx context.Context , txids []string , page Page ) (* VirtualTxsResp , error ) {
293
+ func (i * indexerService ) GetVirtualTxs (ctx context.Context , txids []string , page * Page ) (* VirtualTxsResp , error ) {
293
294
txs , err := i .repoManager .Rounds ().GetTxsWithTxids (ctx , txids )
294
295
if err != nil {
295
296
return nil , err
@@ -310,7 +311,10 @@ func (i *indexerService) GetSweptCommitmentTx(ctx context.Context, txid string)
310
311
return & SweptCommitmentTxResp {}, nil
311
312
}
312
313
313
- func paginate [T any ](items []T , params Page , maxSize int ) ([]T , PageResp ) {
314
+ func paginate [T any ](items []T , params * Page , maxSize int ) ([]T , PageResp ) {
315
+ if params == nil {
316
+ return items , PageResp {}
317
+ }
314
318
if params .PageSize <= 0 {
315
319
params .PageSize = maxSize
316
320
}
@@ -320,10 +324,11 @@ func paginate[T any](items []T, params Page, maxSize int) ([]T, PageResp) {
320
324
321
325
totalCount := len (items )
322
326
totalPages := int (math .Ceil (float64 (totalCount ) / float64 (params .PageSize )))
327
+ next := min (params .PageNum + 1 , totalPages )
323
328
324
329
resp := PageResp {
325
330
Current : params .PageNum ,
326
- Next : params . PageNum + 1 ,
331
+ Next : next ,
327
332
Total : totalPages ,
328
333
}
329
334
0 commit comments