forked from forbole/juno
-
Notifications
You must be signed in to change notification settings - Fork 0
/
postgresql.go
331 lines (273 loc) · 9.58 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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
package postgresql
import (
"database/sql"
"encoding/base64"
"fmt"
"strings"
"github.com/spike-engine/juno/logging"
"github.com/cosmos/cosmos-sdk/simapp/params"
"github.com/lib/pq"
_ "github.com/lib/pq" // nolint
"github.com/spike-engine/juno/database"
"github.com/spike-engine/juno/types"
"github.com/spike-engine/juno/types/config"
)
// 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
}
// createPartitionIfNotExists creates a new partition having the given partition id if not existing
func (db *Database) createPartitionIfNotExists(table string, partitionID int64) error {
partitionTable := fmt.Sprintf("%s_%d", table, partitionID)
stmt := fmt.Sprintf(
"CREATE TABLE IF NOT EXISTS %s PARTITION OF %s FOR VALUES IN (%d)",
partitionTable,
table,
partitionID,
)
_, err := db.Sql.Exec(stmt)
if err != nil {
return err
}
return nil
}
// -------------------------------------------------------------------------------------------------------------------
// 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
}
// GetLastBlockHeight returns the last block height stored inside the database
func (db *Database) GetLastBlockHeight() (int64, error) {
stmt := `SELECT height FROM block ORDER BY height DESC LIMIT 1;`
var height int64
if err := db.Sql.QueryRow(stmt).Scan(&height); err != nil {
return 0, fmt.Errorf("error while getting last block height, error: %s", err)
}
if height == 0 {
return 0, fmt.Errorf("cannot get block height, no blocks saved")
}
return height, nil
}
// 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 {
var partitionID int64
partitionSize := config.Cfg.Database.PartitionSize
if partitionSize > 0 {
partitionID = tx.Height / partitionSize
err := db.createPartitionIfNotExists("transaction", partitionID)
if err != nil {
return err
}
}
return db.saveTxInsidePartition(tx, partitionID)
}
// saveTxInsidePartition stores the given transaction inside the partition having the given id
func (db *Database) saveTxInsidePartition(tx *types.Tx, partitionId int64) error {
sqlStatement := `
INSERT INTO transaction
(hash, height, success, messages, memo, signatures, signer_infos, fee, gas_wanted, gas_used, raw_log, logs, partition_id)
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13)
ON CONFLICT (hash, partition_id) DO UPDATE
SET height = excluded.height,
success = excluded.success,
messages = excluded.messages,
memo = excluded.memo,
signatures = excluded.signatures,
signer_infos = excluded.signer_infos,
fee = excluded.fee,
gas_wanted = excluded.gas_wanted,
gas_used = excluded.gas_used,
raw_log = excluded.raw_log,
logs = excluded.logs`
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),
partitionId,
)
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 {
var partitionID int64
partitionSize := config.Cfg.Database.PartitionSize
if partitionSize > 0 {
partitionID = msg.Height / partitionSize
err := db.createPartitionIfNotExists("message", partitionID)
if err != nil {
return err
}
}
return db.saveMessageInsidePartition(msg, partitionID)
}
// saveMessageInsidePartition stores the given message inside the partition having the provided id
func (db *Database) saveMessageInsidePartition(msg *types.Message, partitionID int64) error {
stmt := `
INSERT INTO message(transaction_hash, index, type, value, involved_accounts_addresses, height, partition_id)
VALUES ($1, $2, $3, $4, $5, $6, $7)
ON CONFLICT (transaction_hash, index, partition_id) DO UPDATE
SET height = excluded.height,
type = excluded.type,
value = excluded.value,
involved_accounts_addresses = excluded.involved_accounts_addresses`
_, err := db.Sql.Exec(stmt, msg.TxHash, msg.Index, msg.Type, msg.Value, pq.Array(msg.Addresses), msg.Height, partitionID)
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
}