Skip to content

Commit 8176896

Browse files
Statuses
1 parent cd567a9 commit 8176896

15 files changed

Lines changed: 368 additions & 29 deletions

File tree

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ require (
1717
github.com/goccy/go-json v0.10.5
1818
github.com/opus-domini/fast-shot v1.1.4
1919
github.com/pkg/errors v0.9.1
20+
github.com/rs/zerolog v1.33.0
2021
github.com/stretchr/testify v1.10.0
2122
github.com/uptrace/bun v1.1.17
2223
go.uber.org/mock v0.5.0
@@ -94,7 +95,6 @@ require (
9495
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect
9596
github.com/power-devops/perfstat v0.0.0-20210106213030-5aafc221ea8c // indirect
9697
github.com/rogpeppe/go-internal v1.12.0 // indirect
97-
github.com/rs/zerolog v1.33.0 // indirect
9898
github.com/shirou/gopsutil/v3 v3.23.12 // indirect
9999
github.com/shoenig/go-m1cpu v0.1.6 // indirect
100100
github.com/sirupsen/logrus v1.9.3 // indirect

pkg/api/types.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,5 @@ type Change struct {
1010
Address string `json:"address"`
1111
ImageURL string `json:"image_url,omitempty"`
1212
ChangeID int64 `json:"change_id"`
13+
Status string `json:"status"`
1314
}

pkg/module/module.go

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,12 @@ import (
2020
type Module struct {
2121
modules.BaseModule
2222

23-
celestials celestials.API
24-
address IdByHash
25-
states storage.ICelestialState
26-
tx sdk.Transactable
27-
state storage.CelestialState
23+
celestialsApi celestials.API
24+
address IdByHash
25+
states storage.ICelestialState
26+
celestials storage.ICelestial
27+
tx sdk.Transactable
28+
state storage.CelestialState
2829

2930
celestialsDatasource config.DataSource
3031
indexerName string
@@ -38,6 +39,7 @@ type Module struct {
3839
func New(
3940
celestialsDatasource config.DataSource,
4041
address IdByHash,
42+
celestials storage.ICelestial,
4143
state storage.ICelestialState,
4244
tx sdk.Transactable,
4345
indexerName string,
@@ -47,9 +49,10 @@ func New(
4749
module := Module{
4850
BaseModule: modules.New("celestials"),
4951
address: address,
52+
celestials: celestials,
5053
states: state,
5154
tx: tx,
52-
celestials: v1.New(celestialsDatasource.URL),
55+
celestialsApi: v1.New(celestialsDatasource.URL),
5356
indexerName: indexerName,
5457
network: network,
5558
indexPeriod: time.Minute,
@@ -124,7 +127,7 @@ func (m *Module) getChanges(ctx context.Context) (celestials.Changes, error) {
124127
requestCtx, cancel := context.WithTimeout(ctx, time.Second*time.Duration(m.celestialsDatasource.Timeout))
125128
defer cancel()
126129

127-
return m.celestials.Changes(
130+
return m.celestialsApi.Changes(
128131
requestCtx,
129132
m.network,
130133
celestials.WithFromChangeId(m.state.ChangeId),
@@ -145,6 +148,7 @@ func (m *Module) sync(ctx context.Context) error {
145148
}
146149

147150
cids := make(map[string]storage.Celestial)
151+
addressIds := make(map[uint64]struct{})
148152

149153
for i := range changes.Changes {
150154
if m.state.ChangeId >= changes.Changes[i].ChangeID {
@@ -169,15 +173,25 @@ func (m *Module) sync(ctx context.Context) error {
169173
return errors.Errorf("can't find address %s", changes.Changes[i].Address)
170174
}
171175

176+
status, err := storage.ParseStatus(changes.Changes[i].Status)
177+
if err != nil {
178+
return err
179+
}
180+
181+
if status == storage.StatusPRIMARY {
182+
addressIds[addressId[0]] = struct{}{}
183+
}
184+
172185
cids[changes.Changes[i].CelestialID] = storage.Celestial{
173186
Id: changes.Changes[i].CelestialID,
174187
ImageUrl: changes.Changes[i].ImageURL,
175188
AddressId: addressId[0],
176189
ChangeId: changes.Changes[i].ChangeID,
190+
Status: status,
177191
}
178192
}
179193

180-
if err := m.save(ctx, cids); err != nil {
194+
if err := m.save(ctx, cids, addressIds); err != nil {
181195
return errors.Wrap(err, "save")
182196
}
183197
end = len(changes.Changes) < int(m.limit)
@@ -187,7 +201,7 @@ func (m *Module) sync(ctx context.Context) error {
187201
return nil
188202
}
189203

190-
func (m *Module) save(ctx context.Context, cids map[string]storage.Celestial) error {
204+
func (m *Module) save(ctx context.Context, cids map[string]storage.Celestial, addressIds map[uint64]struct{}) error {
191205
requestCtx, cancel := context.WithTimeout(ctx, m.databaseTimeout)
192206
defer cancel()
193207

@@ -197,6 +211,10 @@ func (m *Module) save(ctx context.Context, cids map[string]storage.Celestial) er
197211
}
198212
defer tx.Close(requestCtx)
199213

214+
if err := tx.UpdateStatusForAddress(ctx, maps.Keys(addressIds)); err != nil {
215+
return tx.HandleError(requestCtx, errors.Wrap(err, "update primary statuses"))
216+
}
217+
200218
if err := tx.SaveCelestials(requestCtx, maps.Values(cids)); err != nil {
201219
return tx.HandleError(requestCtx, errors.Wrap(err, "save celestials"))
202220
}

pkg/module/module_test.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,9 @@ func (s *ModuleTestSuite) SetupSuite() {
5151
s.psqlContainer = psqlContainer
5252

5353
init := func(ctx context.Context, conn *database.Bun) error {
54+
if err := pg.CreateTypes(ctx, conn); err != nil {
55+
return err
56+
}
5457
if err := database.CreateTables(ctx, conn, new(storage.Celestial), new(storage.CelestialState)); err != nil {
5558
if err := conn.Close(); err != nil {
5659
return err
@@ -118,6 +121,7 @@ func (s *ModuleTestSuite) TestSync() {
118121
Address: "celestia1mm8yykm46ec3t0dgwls70g0jvtm055wk9ayal8",
119122
ImageURL: "image_url",
120123
ChangeID: 4,
124+
Status: "PRIMARY",
121125
},
122126
},
123127
}, nil)
@@ -140,14 +144,15 @@ func (s *ModuleTestSuite) TestSync() {
140144
m := New(
141145
cfgDs,
142146
s.address,
147+
s.celestials,
143148
s.celestialState,
144149
s.storage.Transactable,
145150
testIndexerName,
146151
network,
147152
WithAddressPrefix("celestia"),
148153
WithLimit(10),
149154
)
150-
m.celestials = s.api
155+
m.celestialsApi = s.api
151156

152157
err = m.getState(ctx)
153158
s.Require().NoError(err)

pkg/storage/celestial.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,17 @@ import (
1111
type ICelestial interface {
1212
ById(ctx context.Context, id string) (Celestial, error)
1313
ByAddressId(ctx context.Context, addressId uint64, limit, offset int) ([]Celestial, error)
14+
Primary(ctx context.Context, addressId uint64) (Celestial, error)
1415
}
1516

1617
type Celestial struct {
1718
bun.BaseModel `bun:"celestial" comment:"Table with celestial ids."`
1819

19-
Id string `bun:"id,pk,notnull" comment:"Celestial id"`
20-
AddressId uint64 `bun:"address_id" comment:"Internal address identity for connected address"`
21-
ImageUrl string `bun:"image_url" comment:"Image url"`
22-
ChangeId int64 `bun:"change_id" comment:"Id of the last change of celestial id"`
20+
Id string `bun:"id,pk,notnull" comment:"Celestial id"`
21+
AddressId uint64 `bun:"address_id" comment:"Internal address identity for connected address"`
22+
ImageUrl string `bun:"image_url" comment:"Image url"`
23+
ChangeId int64 `bun:"change_id" comment:"Id of the last change of celestial id"`
24+
Status Status `bun:"status,type:celestials_status" comment:"Status of celestial domain"`
2325
}
2426

2527
func (Celestial) TableName() string {

pkg/storage/mock/celestial.go

Lines changed: 39 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/storage/mock/tx.go

Lines changed: 50 additions & 11 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/storage/postgres/celestials.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,3 +40,12 @@ func (c *Celestials) ByAddressId(ctx context.Context, addressId uint64, limit, o
4040
err = query.Limit(limit).Scan(ctx)
4141
return
4242
}
43+
44+
func (c *Celestials) Primary(ctx context.Context, addressId uint64) (result storage.Celestial, err error) {
45+
err = c.DB().NewSelect().
46+
Model(&result).
47+
Where("address_id = ?", addressId).
48+
Where("status = ?", storage.StatusPRIMARY).
49+
Scan(ctx)
50+
return
51+
}

0 commit comments

Comments
 (0)