-
Notifications
You must be signed in to change notification settings - Fork 0
/
actions_ledger_index.go
119 lines (109 loc) · 3.71 KB
/
actions_ledger_index.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
package horizon
import (
"gitlab.com/distributed_lab/logan/v3/errors"
"gitlab.com/tokend/go/xdr"
"gitlab.com/tokend/horizon/db2"
"gitlab.com/tokend/horizon/db2/history"
"gitlab.com/tokend/horizon/render/hal"
"gitlab.com/tokend/horizon/render/problem"
"gitlab.com/tokend/horizon/resource"
"golang.org/x/net/context"
)
// LedgerOperationsIndexAction is an Action-based struct designed to co
type LedgerOperationsIndexAction struct {
Action
Types []xdr.OperationType
PagingParams db2.PageQuery
Records resource.Data
Page hal.Page
}
// JSON is a method for actions.JSON
func (action *LedgerOperationsIndexAction) JSON() {
action.Do(
action.loadParams,
action.loadRecords,
action.loadPage,
func() {
hal.Render(action.W, action.Page)
},
)
}
func (action *LedgerOperationsIndexAction) loadParams() {
action.PagingParams = action.GetPageQuery()
}
func collectLedgers(historyQ history.QInterface, pagingParams db2.PageQuery, ctx context.Context) (result []resource.DataLedger, err error) {
var ledgers []history.Ledger
err = historyQ.Ledgers().Page(pagingParams).Select(&ledgers)
if err != nil {
return nil, errors.Wrap(err, "failed to select ledgers by paging params")
}
for _, ledger := range ledgers {
dataLedger := resource.DataLedger{
ClosedAt: ledger.ClosedAt,
Sequence: ledger.Sequence,
LedgerHash: ledger.LedgerHash,
Transactions: nil,
}
dataLedger.Transactions, err = collectLedgerTransactions(historyQ, ledger, ctx)
if err != nil {
return nil, errors.Wrap(err, "failed to collect ledger transactions")
}
result = append(result, dataLedger)
}
return result, nil
}
func collectLedgerTransactions(historyQ history.QInterface, ledger history.Ledger, ctx context.Context) (result []resource.DataLedgerTransaction, err error) {
var transactions []history.Transaction
err = historyQ.Transactions().ForLedger(int32(ledger.Sequence)).Select(&transactions)
if err != nil {
return nil, errors.Wrap(err, "failed to select transactions for ledger")
}
for _, transaction := range transactions {
dataTransaction := resource.DataLedgerTransaction{
ID: transaction.ID,
Operations: nil,
}
dataTransaction.Operations, err = collectTransactionOperations(historyQ, transaction, ctx)
if err != nil {
return nil, errors.Wrap(err, "failed to collect operations")
}
result = append(result, dataTransaction)
}
return result, nil
}
func collectTransactionOperations(historyQ history.QInterface, transaction history.Transaction, ctx context.Context) (result []hal.Pageable, err error) {
var operations []history.Operation
err = historyQ.Operations().ForTx(transaction.TransactionHash).Select(&operations)
if err != nil {
return nil, errors.Wrap(err, "failed to select operations for tx")
}
for _, operation := range operations {
dataOperation, err := resource.NewPublicOperation(ctx, operation, nil)
if err != nil {
return nil, errors.Wrap(err, "failed to populate public operation")
}
result = append(result, dataOperation)
}
return result, nil
}
func (action *LedgerOperationsIndexAction) loadRecords() {
var data resource.Data
var err error
data.Ledgers, err = collectLedgers(action.HistoryQ(), action.PagingParams, action.Ctx)
if err != nil {
action.Log.WithError(err).Error("failed to collect records")
action.Err = &problem.ServerError
}
action.Records = data
}
func (action *LedgerOperationsIndexAction) loadPage() {
for _, record := range action.Records.Ledgers {
action.Page.Add(record)
}
action.Page.BaseURL = action.BaseURL()
action.Page.BasePath = action.Path()
action.Page.Limit = action.PagingParams.Limit
action.Page.Cursor = action.PagingParams.Cursor
action.Page.Order = action.PagingParams.Order
action.Page.PopulateLinks()
}