-
Notifications
You must be signed in to change notification settings - Fork 11
/
core.go
211 lines (185 loc) 路 5.8 KB
/
core.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
// SPDX-FileCopyrightText: 2024 PK Lab AG <contact@pklab.io>
// SPDX-License-Identifier: MIT
package postgres
import (
"context"
"github.com/celenium-io/celestia-indexer/internal/stats"
models "github.com/celenium-io/celestia-indexer/internal/storage"
"github.com/dipdup-net/go-lib/config"
"github.com/dipdup-net/go-lib/database"
"github.com/dipdup-net/indexer-sdk/pkg/storage"
"github.com/dipdup-net/indexer-sdk/pkg/storage/postgres"
"github.com/pkg/errors"
"github.com/uptrace/bun"
"go.opentelemetry.io/otel/trace"
)
// Storage -
type Storage struct {
*postgres.Storage
cfg config.Database
scriptsDir string
Blocks models.IBlock
BlockStats models.IBlockStats
BlockSignatures models.IBlockSignature
BlobLogs models.IBlobLog
Constants models.IConstant
DenomMetadata models.IDenomMetadata
Tx models.ITx
Message models.IMessage
Event models.IEvent
Address models.IAddress
VestingAccounts models.IVestingAccount
VestingPeriods models.IVestingPeriod
Namespace models.INamespace
Price models.IPrice
State models.IState
Stats models.IStats
Search models.ISearch
Validator models.IValidator
StakingLogs models.IStakingLog
Delegation models.IDelegation
Redelegation models.IRedelegation
Undelegation models.IUndelegation
Jails models.IJail
Rollup models.IRollup
Grants models.IGrant
Notificator *Notificator
export models.Export
}
// Create -
func Create(ctx context.Context, cfg config.Database, scriptsDir string) (Storage, error) {
strg, err := postgres.Create(ctx, cfg, initDatabase)
if err != nil {
return Storage{}, err
}
export := NewExport(cfg)
s := Storage{
cfg: cfg,
scriptsDir: scriptsDir,
Storage: strg,
Blocks: NewBlocks(strg.Connection()),
BlockStats: NewBlockStats(strg.Connection()),
BlockSignatures: NewBlockSignature(strg.Connection()),
BlobLogs: NewBlobLog(strg.Connection(), export),
Constants: NewConstant(strg.Connection()),
DenomMetadata: NewDenomMetadata(strg.Connection()),
Message: NewMessage(strg.Connection()),
Event: NewEvent(strg.Connection()),
Address: NewAddress(strg.Connection()),
VestingAccounts: NewVestingAccount(strg.Connection()),
VestingPeriods: NewVestingPeriod(strg.Connection()),
Price: NewPrice(strg.Connection()),
Tx: NewTx(strg.Connection()),
State: NewState(strg.Connection()),
Namespace: NewNamespace(strg.Connection()),
Stats: NewStats(strg.Connection()),
Search: NewSearch(strg.Connection()),
Validator: NewValidator(strg.Connection()),
StakingLogs: NewStakingLog(strg.Connection()),
Delegation: NewDelegation(strg.Connection()),
Redelegation: NewRedelegation(strg.Connection()),
Undelegation: NewUndelegation(strg.Connection()),
Jails: NewJail(strg.Connection()),
Rollup: NewRollup(strg.Connection()),
Grants: NewGrant(strg.Connection()),
Notificator: NewNotificator(cfg, strg.Connection().DB()),
export: export,
}
if err := s.createScripts(ctx, "functions", false); err != nil {
return s, errors.Wrap(err, "creating views")
}
if err := s.createScripts(ctx, "views", true); err != nil {
return s, errors.Wrap(err, "creating views")
}
return s, nil
}
func initDatabase(ctx context.Context, conn *database.Bun) error {
if err := createExtensions(ctx, conn); err != nil {
return errors.Wrap(err, "create extensions")
}
if err := createTypes(ctx, conn); err != nil {
return errors.Wrap(err, "creating custom types")
}
// register many-to-many relationships
conn.DB().RegisterModel(
(*models.NamespaceMessage)(nil),
(*models.MsgAddress)(nil),
(*models.RollupProvider)(nil),
)
if err := database.CreateTables(ctx, conn, models.Models...); err != nil {
if err := conn.Close(); err != nil {
return err
}
return err
}
if err := database.MakeComments(ctx, conn, models.Models...); err != nil {
if err := conn.Close(); err != nil {
return err
}
return errors.Wrap(err, "make comments")
}
if err := createHypertables(ctx, conn); err != nil {
if err := conn.Close(); err != nil {
return err
}
return errors.Wrap(err, "create hypertables")
}
return createIndices(ctx, conn)
}
func (s Storage) CreateListener() models.Listener {
return NewNotificator(s.cfg, s.Notificator.db)
}
func createHypertables(ctx context.Context, conn *database.Bun) error {
return conn.DB().RunInTx(ctx, nil, func(ctx context.Context, tx bun.Tx) error {
for _, model := range []storage.Model{
&models.Block{},
&models.BlockStats{},
&models.Tx{},
&models.Message{},
&models.Event{},
&models.NamespaceMessage{},
&models.BlobLog{},
&models.Jail{},
&models.StakingLog{},
&models.Price{},
} {
if _, err := tx.ExecContext(ctx,
`SELECT create_hypertable(?, 'time', chunk_time_interval => INTERVAL '1 month', if_not_exists => TRUE);`,
model.TableName(),
); err != nil {
return err
}
if err := stats.InitModel(model); err != nil {
return err
}
}
if err := stats.InitModel(&models.Validator{}); err != nil {
return err
}
return nil
})
}
func createExtensions(ctx context.Context, conn *database.Bun) error {
return conn.DB().RunInTx(ctx, nil, func(ctx context.Context, tx bun.Tx) error {
_, err := tx.ExecContext(ctx, "CREATE EXTENSION IF NOT EXISTS pg_trgm;")
return err
})
}
func (s Storage) Close() error {
if err := s.export.Close(); err != nil {
return err
}
if err := s.Storage.Close(); err != nil {
return err
}
return nil
}
func (s Storage) SetTracer(tp trace.TracerProvider) {
s.Connection().DB().AddQueryHook(
NewSentryHook(
s.cfg.Database,
tp.Tracer("db"),
true,
),
)
}