2019-05-13 09:25:46 +00:00
|
|
|
package mailserver
|
|
|
|
|
|
|
|
import (
|
|
|
|
"database/sql"
|
2020-01-21 07:11:24 +00:00
|
|
|
"errors"
|
2019-05-13 09:25:46 +00:00
|
|
|
"fmt"
|
|
|
|
"time"
|
|
|
|
|
2020-01-21 07:11:24 +00:00
|
|
|
"github.com/lib/pq"
|
|
|
|
|
2019-05-13 09:25:46 +00:00
|
|
|
// Import postgres driver
|
|
|
|
_ "github.com/lib/pq"
|
2019-06-09 07:24:20 +00:00
|
|
|
"github.com/status-im/migrate/v4"
|
|
|
|
"github.com/status-im/migrate/v4/database/postgres"
|
2019-10-28 13:50:33 +00:00
|
|
|
bindata "github.com/status-im/migrate/v4/source/go_bindata"
|
2020-01-02 09:10:19 +00:00
|
|
|
|
2024-09-26 22:37:32 +00:00
|
|
|
"github.com/status-im/status-go/common"
|
2019-05-13 09:25:46 +00:00
|
|
|
"github.com/status-im/status-go/mailserver/migrations"
|
|
|
|
|
|
|
|
"github.com/ethereum/go-ethereum/log"
|
|
|
|
"github.com/ethereum/go-ethereum/rlp"
|
2020-01-02 09:10:19 +00:00
|
|
|
|
2019-11-23 17:57:05 +00:00
|
|
|
"github.com/status-im/status-go/eth-node/types"
|
2021-05-13 16:05:34 +00:00
|
|
|
waku "github.com/status-im/status-go/waku/common"
|
2019-05-13 09:25:46 +00:00
|
|
|
)
|
|
|
|
|
2020-01-21 07:11:24 +00:00
|
|
|
type PostgresDB struct {
|
2020-04-07 08:21:39 +00:00
|
|
|
db *sql.DB
|
|
|
|
name string
|
|
|
|
done chan struct{}
|
2020-01-21 07:11:24 +00:00
|
|
|
}
|
|
|
|
|
2020-01-08 11:12:23 +00:00
|
|
|
func NewPostgresDB(uri string) (*PostgresDB, error) {
|
|
|
|
db, err := sql.Open("postgres", uri)
|
2019-05-13 09:25:46 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-04-07 08:21:39 +00:00
|
|
|
instance := &PostgresDB{
|
|
|
|
db: db,
|
|
|
|
done: make(chan struct{}),
|
|
|
|
}
|
2019-05-13 09:25:46 +00:00
|
|
|
if err := instance.setup(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-04-07 08:21:39 +00:00
|
|
|
// name is used for metrics labels
|
|
|
|
if name, err := instance.getDBName(uri); err == nil {
|
|
|
|
instance.name = name
|
|
|
|
}
|
|
|
|
|
|
|
|
// initialize the metric value
|
|
|
|
instance.updateArchivedEnvelopesCount()
|
|
|
|
// checking count on every insert is inefficient
|
|
|
|
go func() {
|
2024-09-26 22:37:32 +00:00
|
|
|
defer common.LogOnPanic()
|
2020-04-07 08:21:39 +00:00
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-instance.done:
|
|
|
|
return
|
|
|
|
case <-time.After(time.Second * envelopeCountCheckInterval):
|
|
|
|
instance.updateArchivedEnvelopesCount()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
2019-05-13 09:25:46 +00:00
|
|
|
return instance, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type postgresIterator struct {
|
|
|
|
*sql.Rows
|
|
|
|
}
|
|
|
|
|
2020-04-07 08:21:39 +00:00
|
|
|
func (i *PostgresDB) getDBName(uri string) (string, error) {
|
|
|
|
query := "SELECT current_database()"
|
|
|
|
var dbName string
|
|
|
|
return dbName, i.db.QueryRow(query).Scan(&dbName)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (i *PostgresDB) envelopesCount() (int, error) {
|
|
|
|
query := "SELECT count(*) FROM envelopes"
|
|
|
|
var count int
|
|
|
|
return count, i.db.QueryRow(query).Scan(&count)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (i *PostgresDB) updateArchivedEnvelopesCount() {
|
|
|
|
if count, err := i.envelopesCount(); err != nil {
|
|
|
|
log.Warn("db query for envelopes count failed", "err", err)
|
|
|
|
} else {
|
|
|
|
archivedEnvelopesGauge.WithLabelValues(i.name).Set(float64(count))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-13 09:25:46 +00:00
|
|
|
func (i *postgresIterator) DBKey() (*DBKey, error) {
|
|
|
|
var value []byte
|
|
|
|
var id []byte
|
|
|
|
if err := i.Scan(&id, &value); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &DBKey{raw: id}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (i *postgresIterator) Error() error {
|
2020-01-21 07:11:24 +00:00
|
|
|
return i.Err()
|
2019-05-13 09:25:46 +00:00
|
|
|
}
|
|
|
|
|
2020-01-21 07:11:24 +00:00
|
|
|
func (i *postgresIterator) Release() error {
|
|
|
|
return i.Close()
|
2019-05-13 09:25:46 +00:00
|
|
|
}
|
|
|
|
|
2021-08-24 11:51:22 +00:00
|
|
|
func (i *postgresIterator) GetEnvelopeByBloomFilter(bloom []byte) ([]byte, error) {
|
|
|
|
var value []byte
|
|
|
|
var id []byte
|
|
|
|
if err := i.Scan(&id, &value); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return value, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (i *postgresIterator) GetEnvelopeByTopicsMap(topics map[types.TopicType]bool) ([]byte, error) {
|
2019-05-13 09:25:46 +00:00
|
|
|
var value []byte
|
|
|
|
var id []byte
|
|
|
|
if err := i.Scan(&id, &value); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return value, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (i *PostgresDB) BuildIterator(query CursorQuery) (Iterator, error) {
|
2020-01-21 07:11:24 +00:00
|
|
|
var args []interface{}
|
|
|
|
|
|
|
|
stmtString := "SELECT id, data FROM envelopes"
|
|
|
|
|
2022-07-26 11:34:11 +00:00
|
|
|
var historyRange string
|
2019-05-13 09:25:46 +00:00
|
|
|
if len(query.cursor) > 0 {
|
2020-01-21 07:11:24 +00:00
|
|
|
args = append(args, query.start, query.cursor)
|
2019-05-13 09:25:46 +00:00
|
|
|
// If we have a cursor, we don't want to include that envelope in the result set
|
2020-01-21 07:11:24 +00:00
|
|
|
stmtString += " " + "WHERE id >= $1 AND id < $2"
|
2022-07-26 11:34:11 +00:00
|
|
|
historyRange = "partial" //nolint: goconst
|
2020-01-21 07:11:24 +00:00
|
|
|
} else {
|
|
|
|
args = append(args, query.start, query.end)
|
|
|
|
stmtString += " " + "WHERE id >= $1 AND id <= $2"
|
2022-07-26 11:34:11 +00:00
|
|
|
historyRange = "full" //nolint: goconst
|
2020-01-21 07:11:24 +00:00
|
|
|
}
|
2019-05-13 09:25:46 +00:00
|
|
|
|
2022-07-26 11:34:11 +00:00
|
|
|
var filterRange string
|
2020-01-21 07:11:24 +00:00
|
|
|
if len(query.topics) > 0 {
|
|
|
|
args = append(args, pq.Array(query.topics))
|
|
|
|
stmtString += " " + "AND topic = any($3)"
|
2022-07-26 11:34:11 +00:00
|
|
|
filterRange = "partial" //nolint: goconst
|
2019-05-13 09:25:46 +00:00
|
|
|
} else {
|
2020-01-21 07:11:24 +00:00
|
|
|
stmtString += " " + fmt.Sprintf("AND bloom & b'%s'::bit(512) = bloom", toBitString(query.bloom))
|
2022-07-26 11:34:11 +00:00
|
|
|
filterRange = "full" //nolint: goconst
|
2019-05-13 09:25:46 +00:00
|
|
|
}
|
|
|
|
|
2020-01-21 07:11:24 +00:00
|
|
|
// Positional argument depends on the fact whether the query uses topics or bloom filter.
|
|
|
|
// If topic is used, the list of topics is passed as an argument to the query.
|
|
|
|
// If bloom filter is used, it is included into the query statement.
|
|
|
|
args = append(args, query.limit)
|
|
|
|
stmtString += " " + fmt.Sprintf("ORDER BY ID DESC LIMIT $%d", len(args))
|
|
|
|
|
2019-05-13 09:25:46 +00:00
|
|
|
stmt, err := i.db.Prepare(stmtString)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2022-07-26 11:34:11 +00:00
|
|
|
|
|
|
|
envelopeQueriesCounter.WithLabelValues(filterRange, historyRange).Inc()
|
2020-01-21 07:11:24 +00:00
|
|
|
rows, err := stmt.Query(args...)
|
2019-05-13 09:25:46 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2022-07-26 11:34:11 +00:00
|
|
|
|
2019-05-13 09:25:46 +00:00
|
|
|
return &postgresIterator{rows}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (i *PostgresDB) setup() error {
|
|
|
|
resources := bindata.Resource(
|
|
|
|
migrations.AssetNames(),
|
2021-09-01 12:02:18 +00:00
|
|
|
migrations.Asset,
|
2019-05-13 09:25:46 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
source, err := bindata.WithInstance(resources)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
driver, err := postgres.WithInstance(i.db, &postgres.Config{})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
m, err := migrate.NewWithInstance(
|
|
|
|
"go-bindata",
|
|
|
|
source,
|
|
|
|
"postgres",
|
|
|
|
driver)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err = m.Up(); err != migrate.ErrNoChange {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (i *PostgresDB) Close() error {
|
2020-04-07 08:21:39 +00:00
|
|
|
select {
|
|
|
|
case <-i.done:
|
|
|
|
default:
|
|
|
|
close(i.done)
|
|
|
|
}
|
2019-05-13 09:25:46 +00:00
|
|
|
return i.db.Close()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (i *PostgresDB) GetEnvelope(key *DBKey) ([]byte, error) {
|
|
|
|
statement := `SELECT data FROM envelopes WHERE id = $1`
|
|
|
|
|
|
|
|
stmt, err := i.db.Prepare(statement)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer stmt.Close()
|
|
|
|
|
|
|
|
var envelope []byte
|
|
|
|
|
|
|
|
if err = stmt.QueryRow(key.Bytes()).Scan(&envelope); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return envelope, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (i *PostgresDB) Prune(t time.Time, batch int) (int, error) {
|
2019-11-23 17:57:05 +00:00
|
|
|
var zero types.Hash
|
|
|
|
var emptyTopic types.TopicType
|
2019-05-13 09:25:46 +00:00
|
|
|
kl := NewDBKey(0, emptyTopic, zero)
|
|
|
|
ku := NewDBKey(uint32(t.Unix()), emptyTopic, zero)
|
|
|
|
statement := "DELETE FROM envelopes WHERE id BETWEEN $1 AND $2"
|
|
|
|
|
|
|
|
stmt, err := i.db.Prepare(statement)
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
defer stmt.Close()
|
|
|
|
|
2020-01-13 20:13:28 +00:00
|
|
|
result, err := stmt.Exec(kl.Bytes(), ku.Bytes())
|
|
|
|
if err != nil {
|
2019-05-13 09:25:46 +00:00
|
|
|
return 0, err
|
|
|
|
}
|
2020-01-13 20:13:28 +00:00
|
|
|
rows, err := result.RowsAffected()
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
return int(rows), nil
|
2019-05-13 09:25:46 +00:00
|
|
|
}
|
|
|
|
|
2020-01-08 11:12:23 +00:00
|
|
|
func (i *PostgresDB) SaveEnvelope(env types.Envelope) error {
|
|
|
|
topic := env.Topic()
|
|
|
|
key := NewDBKey(env.Expiry()-env.TTL(), topic, env.Hash())
|
2020-01-21 07:11:24 +00:00
|
|
|
rawEnvelope, err := rlp.EncodeToBytes(env.Unwrap())
|
2019-05-13 09:25:46 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Error(fmt.Sprintf("rlp.EncodeToBytes failed: %s", err))
|
2020-04-07 08:21:39 +00:00
|
|
|
archivedErrorsCounter.WithLabelValues(i.name).Inc()
|
2019-05-13 09:25:46 +00:00
|
|
|
return err
|
|
|
|
}
|
2020-01-21 07:11:24 +00:00
|
|
|
if rawEnvelope == nil {
|
2020-04-07 08:21:39 +00:00
|
|
|
archivedErrorsCounter.WithLabelValues(i.name).Inc()
|
2020-01-21 07:11:24 +00:00
|
|
|
return errors.New("failed to encode envelope to bytes")
|
|
|
|
}
|
2019-05-13 09:25:46 +00:00
|
|
|
|
|
|
|
statement := "INSERT INTO envelopes (id, data, topic, bloom) VALUES ($1, $2, $3, B'"
|
|
|
|
statement += toBitString(env.Bloom())
|
|
|
|
statement += "'::bit(512)) ON CONFLICT (id) DO NOTHING;"
|
|
|
|
stmt, err := i.db.Prepare(statement)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer stmt.Close()
|
|
|
|
|
|
|
|
_, err = stmt.Exec(
|
|
|
|
key.Bytes(),
|
|
|
|
rawEnvelope,
|
2019-10-28 13:50:33 +00:00
|
|
|
topicToByte(topic),
|
2019-05-13 09:25:46 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
if err != nil {
|
2020-04-07 08:21:39 +00:00
|
|
|
archivedErrorsCounter.WithLabelValues(i.name).Inc()
|
2019-05-13 09:25:46 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-04-07 08:21:39 +00:00
|
|
|
archivedEnvelopesGauge.WithLabelValues(i.name).Inc()
|
|
|
|
archivedEnvelopeSizeMeter.WithLabelValues(i.name).Observe(
|
2021-05-13 16:05:34 +00:00
|
|
|
float64(waku.EnvelopeHeaderLength + env.Size()))
|
2019-05-13 09:25:46 +00:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-11-23 17:57:05 +00:00
|
|
|
func topicToByte(t types.TopicType) []byte {
|
2019-05-13 09:25:46 +00:00
|
|
|
return []byte{t[0], t[1], t[2], t[3]}
|
|
|
|
}
|
|
|
|
|
|
|
|
func toBitString(bloom []byte) string {
|
|
|
|
val := ""
|
|
|
|
for _, n := range bloom {
|
|
|
|
val += fmt.Sprintf("%08b", n)
|
|
|
|
}
|
|
|
|
return val
|
|
|
|
}
|