-
Notifications
You must be signed in to change notification settings - Fork 19
/
transactions.go
173 lines (145 loc) · 5.53 KB
/
transactions.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
// Copyright (C) 2023 Gobalsky Labs Limited
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
package store
import (
"context"
"errors"
"fmt"
"sort"
"strings"
"github.com/georgysavva/scany/pgxscan"
"code.vegaprotocol.io/vega/blockexplorer/entities"
pb "code.vegaprotocol.io/vega/protos/blockexplorer/api/v1"
)
var (
ErrTxNotFound = errors.New("transaction not found")
ErrMultipleTxFound = errors.New("multiple transactions found")
)
func (s *Store) GetTransaction(ctx context.Context, txID string) (*pb.Transaction, error) {
txID = strings.ToUpper(txID)
query := `SELECT t.rowid, b.height as block_id, t.index, t.created_at, t.tx_hash, t.tx_result, t.cmd_type, t.submitter FROM tx_results t JOIN blocks b ON t.block_id = b.rowid WHERE t.tx_hash=$1`
var rows []entities.TxResultRow
if err := pgxscan.Select(ctx, s.pool, &rows, query, txID); err != nil {
return nil, fmt.Errorf("querying tx_results:%w", err)
}
if len(rows) == 0 {
return nil, ErrTxNotFound
}
if len(rows) > 1 {
return nil, ErrMultipleTxFound
}
tx, err := rows[0].ToProto()
if err != nil {
return nil, err
}
return tx, nil
}
func (s *Store) ListTransactions(ctx context.Context,
filters map[string]string,
cmdTypes, exclCmdTypes, parties []string,
first uint32,
after *entities.TxCursor,
last uint32,
before *entities.TxCursor,
) ([]*pb.Transaction, error) {
query := `SELECT t.rowid, b.height as block_id, t.index, t.created_at, t.tx_hash, t.tx_result, t.cmd_type, t.submitter FROM tx_results t JOIN blocks b ON t.block_id = b.rowid`
args := []interface{}{}
predicates := []string{}
// by default we want the most recent transactions so we'll set the limit to first
// and sort order to desc
limit := first
sortOrder := "desc"
// if we have a before cursor we want the results ordered earliest to latest
// so the limit will be set to last and sort order to asc
if before != nil {
block := nextBindVar(&args, before.BlockNumber)
index := nextBindVar(&args, before.TxIndex)
predicate := fmt.Sprintf("(b.height, t.index) > (%s, %s)", block, index)
predicates = append(predicates, predicate)
limit = last
sortOrder = "asc"
}
if after != nil {
block := nextBindVar(&args, after.BlockNumber)
index := nextBindVar(&args, after.TxIndex)
predicate := fmt.Sprintf("(b.height, t.index) < (%s, %s)", block, index)
predicates = append(predicates, predicate)
}
// just in case we have no before cursor, but we want to have the last N transactions in the data set
// i.e. the earliest transactions, sorting ascending
if last > 0 && first == 0 && after == nil && before == nil {
limit = last
sortOrder = "asc"
}
if len(cmdTypes) > 0 {
predicates = append(predicates, fmt.Sprintf("t.cmd_type = ANY(%s)", nextBindVar(&args, cmdTypes)))
}
if len(exclCmdTypes) > 0 {
predicates = append(predicates, fmt.Sprintf("t.cmd_type != ALL(%s)", nextBindVar(&args, exclCmdTypes)))
}
if len(parties) > 0 {
partiesBytes := make([][]byte, len(parties))
for i, p := range parties {
partiesBytes[i] = []byte(p)
}
predicates = append(predicates, fmt.Sprintf("t.submitter = ANY(%s)", nextBindVar(&args, partiesBytes)))
}
for key, value := range filters {
var predicate string
if key == "tx.submitter" {
// tx.submitter is lifted out of attributes and into tx_results by a trigger for faster access
predicate = fmt.Sprintf("t.submitter=%s", nextBindVar(&args, value))
} else if key == "cmd.type" {
predicate = fmt.Sprintf("t.cmd_type=%s", nextBindVar(&args, value))
} else if key == "block.height" {
// much quicker to filter block height by joining to the block table than looking in attributes
predicate = fmt.Sprintf("b.height = %s", nextBindVar(&args, value))
} else {
predicate = fmt.Sprintf(`
EXISTS (SELECT 1 FROM events e JOIN attributes a ON e.rowid = a.event_id
WHERE e.tx_id = t.rowid
AND a.composite_key = %s
AND a.value = %s)`, nextBindVar(&args, key), nextBindVar(&args, value))
}
predicates = append(predicates, predicate)
}
if len(predicates) > 0 {
query = fmt.Sprintf("%s WHERE %s", query, strings.Join(predicates, " AND "))
}
query = fmt.Sprintf("%s ORDER BY t.block_id %s, t.index %s", query, sortOrder, sortOrder)
query = fmt.Sprintf("%s LIMIT %d", query, limit)
var rows []entities.TxResultRow
if err := pgxscan.Select(ctx, s.pool, &rows, query, args...); err != nil {
return nil, fmt.Errorf("querying tx_results:%w", err)
}
txs := make([]*pb.Transaction, 0, len(rows))
for _, row := range rows {
tx, err := row.ToProto()
if err != nil {
s.log.Warn(fmt.Sprintf("unable to decode transaction %s: %v", row.TxHash, err))
continue
}
txs = append(txs, tx)
}
// make sure the results are always order in the same direction, i.e. newest first, regardless of the order of the
// results from the database.
sort.Slice(txs, func(i, j int) bool {
if txs[i].Block == txs[j].Block {
return txs[i].Index > txs[j].Index
}
return txs[i].Block > txs[j].Block
})
return txs, nil
}