forked from Onyx-Protocol/Onyx
-
Notifications
You must be signed in to change notification settings - Fork 0
/
outputs.go
171 lines (146 loc) · 4.22 KB
/
outputs.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
package query
import (
"bytes"
"context"
"fmt"
"math"
"github.com/lib/pq"
"chain/core/query/filter"
"chain/errors"
"chain/protocol/bc"
)
var defaultOutputsAfter = OutputsAfter{
lastBlockHeight: math.MaxInt64,
lastTxPos: math.MaxUint32,
lastIndex: math.MaxInt32,
}
type OutputsAfter struct {
lastBlockHeight uint64
lastTxPos uint32
lastIndex int
}
func (cur OutputsAfter) String() string {
return fmt.Sprintf("%d:%d:%d", cur.lastBlockHeight, cur.lastTxPos, cur.lastIndex)
}
func DecodeOutputsAfter(str string) (c *OutputsAfter, err error) {
var lastBlockHeight, lastTxPos, lastIndex uint64
_, err = fmt.Sscanf(str, "%d:%d:%d", &lastBlockHeight, &lastTxPos, &lastIndex)
if err != nil {
return c, errors.Sub(ErrBadAfter, err)
}
if lastBlockHeight > math.MaxInt64 ||
lastTxPos > math.MaxUint32 ||
lastIndex > math.MaxInt32 {
return nil, errors.Wrap(ErrBadAfter)
}
return &OutputsAfter{
lastBlockHeight: lastBlockHeight,
lastTxPos: uint32(lastTxPos),
lastIndex: int(lastIndex),
}, nil
}
func (ind *Indexer) Outputs(ctx context.Context, filt string, vals []interface{}, timestampMS uint64, after *OutputsAfter, limit int) ([]*AnnotatedOutput, *OutputsAfter, error) {
p, err := filter.Parse(filt, outputsTable, vals)
if err != nil {
return nil, nil, err
}
if len(vals) != p.Parameters {
return nil, nil, ErrParameterCountMismatch
}
expr, err := filter.AsSQL(p, outputsTable, vals)
if err != nil {
return nil, nil, err
}
queryStr, queryArgs := constructOutputsQuery(expr, vals, timestampMS, after, limit)
rows, err := ind.db.QueryContext(ctx, queryStr, queryArgs...)
if err != nil {
return nil, nil, err
}
defer rows.Close()
var newAfter = defaultOutputsAfter
if after != nil {
newAfter = *after
}
outputs := make([]*AnnotatedOutput, 0, limit)
for rows.Next() {
var (
blockHeight uint64
txPos uint32
txID = new(bc.Hash)
accountID *string
accountAlias *string
out = new(AnnotatedOutput)
)
err = rows.Scan(
&blockHeight,
&txPos,
&out.Position,
txID,
&out.OutputID,
&out.Type,
&out.Purpose,
&out.AssetID,
&out.AssetAlias,
&out.AssetDefinition,
&out.AssetTags,
&out.AssetIsLocal,
&out.Amount,
&accountID,
&accountAlias,
&out.AccountTags,
&out.ControlProgram,
&out.ReferenceData,
&out.IsLocal,
)
if err != nil {
return nil, nil, errors.Wrap(err, "scanning annotated output")
}
out.TransactionID = txID
// Set nullable fields.
if accountID != nil {
out.AccountID = *accountID
}
if accountAlias != nil {
out.AccountAlias = *accountAlias
}
outputs = append(outputs, out)
newAfter.lastBlockHeight = blockHeight
newAfter.lastTxPos = txPos
newAfter.lastIndex = out.Position
}
err = rows.Err()
if err != nil {
return nil, nil, err
}
return outputs, &newAfter, nil
}
func constructOutputsQuery(where string, vals []interface{}, timestampMS uint64, after *OutputsAfter, limit int) (string, []interface{}) {
var buf bytes.Buffer
buf.WriteString("SELECT ")
buf.WriteString("block_height, tx_pos, output_index, tx_hash, output_id, type, purpose, ")
buf.WriteString("asset_id, asset_alias, asset_definition, asset_tags, asset_local, ")
buf.WriteString("amount, account_id, account_alias, account_tags, control_program, ")
buf.WriteString("reference_data, local")
buf.WriteString(" FROM ")
buf.WriteString(pq.QuoteIdentifier("annotated_outputs"))
buf.WriteString(" AS out WHERE ")
if where != "" {
buf.WriteString("(")
buf.WriteString(where)
buf.WriteString(") AND ")
}
vals = append(vals, timestampMS)
timestampValIndex := len(vals)
buf.WriteString(fmt.Sprintf("timespan @> $%d::int8", timestampValIndex))
if after != nil {
vals = append(vals, after.lastBlockHeight)
lastBlockHeightValIndex := len(vals)
vals = append(vals, after.lastTxPos)
lastTxPosValIndex := len(vals)
vals = append(vals, after.lastIndex)
lastIndexValIndex := len(vals)
buf.WriteString(fmt.Sprintf(" AND (block_height, tx_pos, output_index) < ($%d, $%d, $%d)", lastBlockHeightValIndex, lastTxPosValIndex, lastIndexValIndex))
}
buf.WriteString(fmt.Sprintf(" ORDER BY block_height DESC, tx_pos DESC, output_index DESC LIMIT %d", limit))
return buf.String(), vals
}