-
Notifications
You must be signed in to change notification settings - Fork 1
/
action.go
256 lines (217 loc) · 7.68 KB
/
action.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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
package aurora
import (
"context"
"net/http"
"net/url"
"strings"
"github.com/diamnet/go/protocols/aurora"
"github.com/diamnet/go/services/aurora/internal/actions"
"github.com/diamnet/go/services/aurora/internal/db2"
"github.com/diamnet/go/services/aurora/internal/db2/core"
"github.com/diamnet/go/services/aurora/internal/db2/history"
"github.com/diamnet/go/services/aurora/internal/httpx"
"github.com/diamnet/go/services/aurora/internal/ledger"
hProblem "github.com/diamnet/go/services/aurora/internal/render/problem"
"github.com/diamnet/go/services/aurora/internal/render/sse"
"github.com/diamnet/go/services/aurora/internal/resourceadapter"
"github.com/diamnet/go/services/aurora/internal/toid"
"github.com/diamnet/go/support/errors"
"github.com/diamnet/go/support/log"
"github.com/diamnet/go/support/render/httpjson"
"github.com/diamnet/go/support/render/problem"
)
// Action is the "base type" for all actions in aurora. It provides
// structs that embed it with access to the App struct.
//
// Additionally, this type is a trigger for go-codegen and causes
// the file at Action.tmpl to be instantiated for each struct that
// embeds Action.
type Action struct {
actions.Base
App *App
Log *log.Entry
hq *history.Q
cq *core.Q
}
// CoreQ provides access to queries that access the diamnet core database.
func (action *Action) CoreQ() *core.Q {
if action.cq == nil {
action.cq = &core.Q{Session: action.App.CoreSession(action.R.Context())}
}
return action.cq
}
// HistoryQ provides access to queries that access the history portion of
// aurora's database.
func (action *Action) HistoryQ() *history.Q {
if action.hq == nil {
action.hq = &history.Q{Session: action.App.AuroraSession(action.R.Context())}
}
return action.hq
}
// Prepare sets the action's App field based upon the context
func (action *Action) Prepare(w http.ResponseWriter, r *http.Request) {
base := &action.Base
action.App = AppFromContext(r.Context())
base.Prepare(w, r, action.App.ctx, action.App.config.SSEUpdateFrequency)
if action.R.Context() != nil {
action.Log = log.Ctx(action.R.Context())
} else {
action.Log = log.DefaultLogger
}
}
// ValidateCursorAsDefault ensures that the cursor parameter is valid in the way
// it is normally used, i.e. it is either the string "now" or a string of
// numerals that can be parsed as an int64.
func (action *Action) ValidateCursorAsDefault() {
if action.Err != nil {
return
}
if action.GetString(actions.ParamCursor) == "now" {
return
}
action.GetInt64(actions.ParamCursor)
}
// ValidateCursorWithinHistory compares the requested page of data against the
// ledger state of the history database. In the event that the cursor is
// guaranteed to return no results, we return a 410 GONE http response.
func (action *Action) ValidateCursorWithinHistory() {
if action.Err != nil {
return
}
pq := action.GetPageQuery()
if action.Err != nil {
return
}
// an ascending query should never return a gone response: An ascending query
// prior to known history should return results at the beginning of history,
// and an ascending query beyond the end of history should not error out but
// rather return an empty page (allowing code that tracks the procession of
// some resource more easily).
if pq.Order != "desc" {
return
}
var cursor int64
var err error
// HACK: checking for the presence of "-" to see whether we should use
// CursorInt64 or CursorInt64Pair is gross.
if strings.Contains(pq.Cursor, "-") {
cursor, _, err = pq.CursorInt64Pair("-")
} else {
cursor, err = pq.CursorInt64()
}
if err != nil {
action.SetInvalidField("cursor", errors.New("invalid value"))
return
}
elder := toid.New(ledger.CurrentState().HistoryElder, 0, 0)
if cursor <= elder.ToInt64() {
action.Err = &hProblem.BeforeHistory
}
}
// EnsureHistoryFreshness halts processing and raises
func (action *Action) EnsureHistoryFreshness() {
if action.Err != nil {
return
}
if action.App.IsHistoryStale() {
ls := ledger.CurrentState()
err := hProblem.StaleHistory
err.Extras = map[string]interface{}{
"history_latest_ledger": ls.HistoryLatest,
"core_latest_ledger": ls.CoreLatest,
}
action.Err = &err
}
}
// FullURL returns the full url for this request
func (action *Action) FullURL() *url.URL {
result := action.baseURL()
result.Path = action.R.URL.Path
result.RawQuery = action.R.URL.RawQuery
return result
}
// baseURL returns the base url for this request, defined as a url containing
// the Host and Scheme portions of the request uri.
func (action *Action) baseURL() *url.URL {
return httpx.BaseURL(action.R.Context())
}
// Fields of this struct are exported for json marshaling/unmarshaling in
// support/render/hal package.
type indexActionQueryParams struct {
AccountID string
LedgerID int32
PagingParams db2.PageQuery
IncludeFailedTxs bool
Signer string
}
// Fields of this struct are exported for json marshaling/unmarshaling in
// support/render/hal package.
type showActionQueryParams struct {
AccountID string
TxHash string
}
// getAccountInfo returns the information about an account based on the provided param.
func (w *web) getAccountInfo(ctx context.Context, qp *showActionQueryParams) (interface{}, error) {
return actions.AccountInfo(ctx, &core.Q{w.coreSession(ctx)}, qp.AccountID)
}
// getAccountPage returns a page containing the account records.
func (w *web) getAccountPage(ctx context.Context, qp *indexActionQueryParams) (interface{}, error) {
auroraSession, err := w.auroraSession(ctx)
if err != nil {
return nil, errors.Wrap(err, "getting aurora db session")
}
return actions.AccountPage(ctx, &history.Q{auroraSession}, qp.Signer, qp.PagingParams)
}
// getTransactionPage returns a page containing the transaction records of an account or a ledger.
func (w *web) getTransactionPage(ctx context.Context, qp *indexActionQueryParams) (interface{}, error) {
auroraSession, err := w.auroraSession(ctx)
if err != nil {
return nil, errors.Wrap(err, "getting aurora db session")
}
return actions.TransactionPage(ctx, &history.Q{auroraSession}, qp.AccountID, qp.LedgerID, qp.IncludeFailedTxs, qp.PagingParams)
}
// getTransactionRecord returns a single transaction resource.
func (w *web) getTransactionResource(ctx context.Context, qp *showActionQueryParams) (interface{}, error) {
auroraSession, err := w.auroraSession(ctx)
if err != nil {
return nil, errors.Wrap(err, "getting aurora db session")
}
return actions.TransactionResource(ctx, &history.Q{auroraSession}, qp.TxHash)
}
// streamTransactions streams the transaction records of an account or a ledger.
func (w *web) streamTransactions(ctx context.Context, s *sse.Stream, qp *indexActionQueryParams) error {
auroraSession, err := w.auroraSession(ctx)
if err != nil {
return errors.Wrap(err, "getting aurora db session")
}
return actions.StreamTransactions(ctx, s, &history.Q{auroraSession}, qp.AccountID, qp.LedgerID, qp.IncludeFailedTxs, qp.PagingParams)
}
// getOfferRecord returns a single offer resource.
func getOfferResource(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
offerID, err := getInt64ParamFromURL(r, "id")
if err != nil {
problem.Render(ctx, w, errors.Wrap(err, "couldn't parse offer id"))
return
}
app := AppFromContext(ctx)
record, err := app.HistoryQ().GetOfferByID(offerID)
if err != nil {
problem.Render(ctx, w, err)
return
}
ledger := new(history.Ledger)
err = app.HistoryQ().LedgerBySequence(
ledger,
int32(record.LastModifiedLedger),
)
if app.HistoryQ().NoRows(err) {
ledger = nil
} else if err != nil {
problem.Render(ctx, w, err)
return
}
var offerResponse aurora.Offer
resourceadapter.PopulateHistoryOffer(ctx, &offerResponse, record, ledger)
httpjson.Render(w, offerResponse, httpjson.HALJSON)
}