/
postgresql.go
276 lines (226 loc) · 7.81 KB
/
postgresql.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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
package postgresql
import (
"database/sql"
"encoding/base64"
"fmt"
"strings"
"github.com/forbole/juno/v2/logging"
"github.com/cosmos/cosmos-sdk/simapp/params"
"github.com/lib/pq"
_ "github.com/lib/pq" // nolint
"github.com/forbole/juno/v2/database"
"github.com/forbole/juno/v2/types"
)
// Builder creates a database connection with the given database connection info
// from config. It returns a database connection handle or an error if the
// connection fails.
func Builder(ctx *database.Context) (database.Database, error) {
sslMode := "disable"
if ctx.Cfg.SSLMode != "" {
sslMode = ctx.Cfg.SSLMode
}
schema := "public"
if ctx.Cfg.Schema != "" {
schema = ctx.Cfg.Schema
}
connStr := fmt.Sprintf(
"host=%s port=%d dbname=%s user=%s sslmode=%s search_path=%s",
ctx.Cfg.Host, ctx.Cfg.Port, ctx.Cfg.Name, ctx.Cfg.User, sslMode, schema,
)
if ctx.Cfg.Password != "" {
connStr += fmt.Sprintf(" password=%s", ctx.Cfg.Password)
}
postgresDb, err := sql.Open("postgres", connStr)
if err != nil {
return nil, err
}
// Set max open connections
postgresDb.SetMaxOpenConns(ctx.Cfg.MaxOpenConnections)
postgresDb.SetMaxIdleConns(ctx.Cfg.MaxIdleConnections)
return &Database{
Sql: postgresDb,
EncodingConfig: ctx.EncodingConfig,
Logger: ctx.Logger,
}, nil
}
// type check to ensure interface is properly implemented
var _ database.Database = &Database{}
// Database defines a wrapper around a SQL database and implements functionality
// for data aggregation and exporting.
type Database struct {
Sql *sql.DB
EncodingConfig *params.EncodingConfig
Logger logging.Logger
}
// RunTx allows to run a transaction inside this database instance
func (db *Database) RunTx(fn func(tx *sql.Tx) error) error {
tx, err := db.Sql.Begin()
if err != nil {
return fmt.Errorf("error while beginning database transaction: %s", err)
}
err = fn(tx)
if err != nil {
if rbError := tx.Rollback(); rbError != nil {
return fmt.Errorf("error while rolling back database transaction; tx err: %s, rb err: %s", err, rbError)
}
return err
}
err = tx.Commit()
if err != nil {
return fmt.Errorf("error while committing transaction: %s", err)
}
return nil
}
// -------------------------------------------------------------------------------------------------------------------
// LastBlockHeight implements database.Database
func (db *Database) LastBlockHeight() (int64, error) {
var height int64
err := db.Sql.QueryRow(`SELECT coalesce(MAX(height),0) AS height FROM block;`).Scan(&height)
return height, err
}
// HasBlock implements database.Database
func (db *Database) HasBlock(height int64) (bool, error) {
var res bool
err := db.Sql.QueryRow(`SELECT EXISTS(SELECT 1 FROM block WHERE height = $1);`, height).Scan(&res)
return res, err
}
// SaveBlock implements database.Database
func (db *Database) SaveBlock(block *types.Block) error {
sqlStatement := `
INSERT INTO block (height, hash, num_txs, total_gas, proposer_address, timestamp)
VALUES ($1, $2, $3, $4, $5, $6) ON CONFLICT DO NOTHING`
proposerAddress := sql.NullString{Valid: len(block.ProposerAddress) != 0, String: block.ProposerAddress}
_, err := db.Sql.Exec(sqlStatement,
block.Height, block.Hash, block.TxNum, block.TotalGas, proposerAddress, block.Timestamp,
)
return err
}
// SaveTx implements database.Database
func (db *Database) SaveTx(tx *types.Tx) error {
sqlStatement := `
INSERT INTO transaction
(hash, height, success, messages, memo, signatures, signer_infos, fee, gas_wanted, gas_used, raw_log, logs)
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12) ON CONFLICT DO NOTHING`
var sigs = make([]string, len(tx.Signatures))
for index, sig := range tx.Signatures {
sigs[index] = base64.StdEncoding.EncodeToString(sig)
}
var msgs = make([]string, len(tx.Body.Messages))
for index, msg := range tx.Body.Messages {
bz, err := db.EncodingConfig.Marshaler.MarshalJSON(msg)
if err != nil {
return err
}
msgs[index] = string(bz)
}
msgsBz := fmt.Sprintf("[%s]", strings.Join(msgs, ","))
feeBz, err := db.EncodingConfig.Marshaler.MarshalJSON(tx.AuthInfo.Fee)
if err != nil {
return fmt.Errorf("failed to JSON encode tx fee: %s", err)
}
var sigInfos = make([]string, len(tx.AuthInfo.SignerInfos))
for index, info := range tx.AuthInfo.SignerInfos {
bz, err := db.EncodingConfig.Marshaler.MarshalJSON(info)
if err != nil {
return err
}
sigInfos[index] = string(bz)
}
sigInfoBz := fmt.Sprintf("[%s]", strings.Join(sigInfos, ","))
logsBz, err := db.EncodingConfig.Amino.MarshalJSON(tx.Logs)
if err != nil {
return err
}
_, err = db.Sql.Exec(sqlStatement,
tx.TxHash, tx.Height, tx.Successful(),
msgsBz, tx.Body.Memo, pq.Array(sigs),
sigInfoBz, string(feeBz),
tx.GasWanted, tx.GasUsed, tx.RawLog, string(logsBz),
)
return err
}
// HasValidator implements database.Database
func (db *Database) HasValidator(addr string) (bool, error) {
var res bool
stmt := `SELECT EXISTS(SELECT 1 FROM validator WHERE consensus_address = $1);`
err := db.Sql.QueryRow(stmt, addr).Scan(&res)
return res, err
}
// SaveValidators implements database.Database
func (db *Database) SaveValidators(validators []*types.Validator) error {
if len(validators) == 0 {
return nil
}
stmt := `INSERT INTO validator (consensus_address, consensus_pubkey) VALUES `
var vparams []interface{}
for i, val := range validators {
vi := i * 2
stmt += fmt.Sprintf("($%d, $%d),", vi+1, vi+2)
vparams = append(vparams, val.ConsAddr, val.ConsPubKey)
}
stmt = stmt[:len(stmt)-1] // Remove trailing ,
stmt += " ON CONFLICT DO NOTHING"
_, err := db.Sql.Exec(stmt, vparams...)
return err
}
// SaveCommitSignatures implements database.Database
func (db *Database) SaveCommitSignatures(signatures []*types.CommitSig) error {
if len(signatures) == 0 {
return nil
}
stmt := `INSERT INTO pre_commit (validator_address, height, timestamp, voting_power, proposer_priority) VALUES `
var sparams []interface{}
for i, sig := range signatures {
si := i * 5
stmt += fmt.Sprintf("($%d, $%d, $%d, $%d, $%d),", si+1, si+2, si+3, si+4, si+5)
sparams = append(sparams, sig.ValidatorAddress, sig.Height, sig.Timestamp, sig.VotingPower, sig.ProposerPriority)
}
stmt = stmt[:len(stmt)-1]
stmt += " ON CONFLICT (validator_address, timestamp) DO NOTHING"
_, err := db.Sql.Exec(stmt, sparams...)
return err
}
// SaveMessage implements database.Database
func (db *Database) SaveMessage(msg *types.Message) error {
stmt := `
INSERT INTO message(transaction_hash, index, type, value, involved_accounts_addresses)
VALUES ($1, $2, $3, $4, $5)`
_, err := db.Sql.Exec(stmt, msg.TxHash, msg.Index, msg.Type, msg.Value, pq.Array(msg.Addresses))
return err
}
// Close implements database.Database
func (db *Database) Close() {
err := db.Sql.Close()
if err != nil {
db.Logger.Error("error while closing connection", "err", err)
}
}
// -------------------------------------------------------------------------------------------------------------------
// GetLastPruned implements database.PruningDb
func (db *Database) GetLastPruned() (int64, error) {
var lastPrunedHeight int64
err := db.Sql.QueryRow(`SELECT coalesce(MAX(last_pruned_height),0) FROM pruning LIMIT 1;`).Scan(&lastPrunedHeight)
return lastPrunedHeight, err
}
// StoreLastPruned implements database.PruningDb
func (db *Database) StoreLastPruned(height int64) error {
_, err := db.Sql.Exec(`DELETE FROM pruning`)
if err != nil {
return err
}
_, err = db.Sql.Exec(`INSERT INTO pruning (last_pruned_height) VALUES ($1)`, height)
return err
}
// Prune implements database.PruningDb
func (db *Database) Prune(height int64) error {
_, err := db.Sql.Exec(`DELETE FROM pre_commit WHERE height = $1`, height)
if err != nil {
return err
}
_, err = db.Sql.Exec(`
DELETE FROM message
USING transaction
WHERE message.transaction_hash = transaction.hash AND transaction.height = $1
`, height)
return err
}