Skip to content

Commit 021461d

Browse files
Address handler
1 parent 8176896 commit 021461d

7 files changed

Lines changed: 213 additions & 147 deletions

File tree

go.mod

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,9 @@ module github.com/celenium-io/celestial-module
22

33
go 1.23.2
44

5-
replace (
6-
github.com/cosmos/cosmos-sdk => github.com/celestiaorg/cosmos-sdk v1.25.1-sdk-v0.46.16
7-
github.com/gogo/protobuf => github.com/regen-network/protobuf v1.3.3-alpha.regen.1
8-
github.com/syndtr/goleveldb => github.com/syndtr/goleveldb v1.0.1-0.20210819022825-2ae1ddf74ef7
9-
github.com/tendermint/tendermint => github.com/celestiaorg/celestia-core v1.43.0-tm-v0.34.35
10-
)
5+
replace github.com/gogo/protobuf => github.com/regen-network/protobuf v1.3.3-alpha.regen.1
116

127
require (
13-
github.com/cosmos/cosmos-sdk v0.46.16
148
github.com/dipdup-net/go-lib v0.4.7
159
github.com/dipdup-net/indexer-sdk v0.0.6
1610
github.com/go-testfixtures/testfixtures/v3 v3.14.0
@@ -47,7 +41,6 @@ require (
4741
github.com/containerd/containerd v1.7.18 // indirect
4842
github.com/containerd/errdefs v0.1.0 // indirect
4943
github.com/containerd/log v0.1.0 // indirect
50-
github.com/cosmos/btcutil v1.0.5 // indirect
5144
github.com/cpuguy83/dockercfg v0.3.1 // indirect
5245
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect
5346
github.com/dipdup-io/workerpool v0.0.4 // indirect

go.sum

Lines changed: 182 additions & 4 deletions
Large diffs are not rendered by default.

pkg/module/address.go

Lines changed: 0 additions & 8 deletions
This file was deleted.

pkg/module/mock/address.go

Lines changed: 0 additions & 84 deletions
This file was deleted.

pkg/module/module.go

Lines changed: 27 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -10,35 +10,36 @@ import (
1010
v1 "github.com/celenium-io/celestial-module/pkg/api/v1"
1111
"github.com/celenium-io/celestial-module/pkg/storage"
1212
"github.com/celenium-io/celestial-module/pkg/storage/postgres"
13-
"github.com/cosmos/cosmos-sdk/types/bech32"
1413
"github.com/dipdup-net/go-lib/config"
1514
"github.com/dipdup-net/indexer-sdk/pkg/modules"
1615
sdk "github.com/dipdup-net/indexer-sdk/pkg/storage"
1716
"github.com/pkg/errors"
17+
"github.com/rs/zerolog/log"
1818
)
1919

20+
type AddressHandler func(ctx context.Context, address string) (uint64, error)
21+
2022
type Module struct {
2123
modules.BaseModule
2224

23-
celestialsApi celestials.API
24-
address IdByHash
25-
states storage.ICelestialState
26-
celestials storage.ICelestial
27-
tx sdk.Transactable
28-
state storage.CelestialState
25+
celestialsApi celestials.API
26+
addressHandler AddressHandler
27+
states storage.ICelestialState
28+
celestials storage.ICelestial
29+
tx sdk.Transactable
30+
state storage.CelestialState
2931

3032
celestialsDatasource config.DataSource
3133
indexerName string
3234
network string
33-
prefix string
3435
indexPeriod time.Duration
3536
databaseTimeout time.Duration
3637
limit int64
3738
}
3839

3940
func New(
4041
celestialsDatasource config.DataSource,
41-
address IdByHash,
42+
addressHandler AddressHandler,
4243
celestials storage.ICelestial,
4344
state storage.ICelestialState,
4445
tx sdk.Transactable,
@@ -48,7 +49,6 @@ func New(
4849
) *Module {
4950
module := Module{
5051
BaseModule: modules.New("celestials"),
51-
address: address,
5252
celestials: celestials,
5353
states: state,
5454
tx: tx,
@@ -59,6 +59,7 @@ func New(
5959
databaseTimeout: time.Minute,
6060
limit: 100,
6161
celestialsDatasource: celestialsDatasource,
62+
addressHandler: addressHandler,
6263
}
6364

6465
for i := range opts {
@@ -76,6 +77,9 @@ func (m *Module) Close() error {
7677
}
7778

7879
func (m *Module) Start(ctx context.Context) {
80+
if m.addressHandler == nil {
81+
panic("nil address handler")
82+
}
7983
if err := m.getState(ctx); err != nil {
8084
m.Log.Err(err).Msg("state receiving")
8185
return
@@ -146,6 +150,10 @@ func (m *Module) sync(ctx context.Context) error {
146150
if err != nil {
147151
return errors.Wrap(err, "get changes")
148152
}
153+
log.Info().
154+
Int("changes_count", len(changes.Changes)).
155+
Int64("head", changes.Head).
156+
Msg("received changes")
149157

150158
cids := make(map[string]storage.Celestial)
151159
addressIds := make(map[uint64]struct{})
@@ -155,22 +163,9 @@ func (m *Module) sync(ctx context.Context) error {
155163
continue
156164
}
157165
m.state.ChangeId = changes.Changes[i].ChangeID
158-
159-
prefix, hash, err := bech32.DecodeAndConvert(changes.Changes[i].Address)
160-
if err != nil {
161-
return errors.Wrapf(err, "decoding address %s", changes.Changes[i].Address)
162-
}
163-
if m.prefix != "" && prefix != m.prefix {
164-
return errors.Errorf("invalid address prefix %s", changes.Changes[i].Address)
165-
}
166-
167-
addressId, err := m.address.IdByHash(ctx, hash)
166+
addressId, err := m.addressHandler(ctx, changes.Changes[i].Address)
168167
if err != nil {
169-
return errors.Wrap(err, "address by hash")
170-
}
171-
172-
if len(addressId) == 0 {
173-
return errors.Errorf("can't find address %s", changes.Changes[i].Address)
168+
return errors.Wrap(err, "address handler")
174169
}
175170

176171
status, err := storage.ParseStatus(changes.Changes[i].Status)
@@ -179,13 +174,13 @@ func (m *Module) sync(ctx context.Context) error {
179174
}
180175

181176
if status == storage.StatusPRIMARY {
182-
addressIds[addressId[0]] = struct{}{}
177+
addressIds[addressId] = struct{}{}
183178
}
184179

185180
cids[changes.Changes[i].CelestialID] = storage.Celestial{
186181
Id: changes.Changes[i].CelestialID,
187182
ImageUrl: changes.Changes[i].ImageURL,
188-
AddressId: addressId[0],
183+
AddressId: addressId,
189184
ChangeId: changes.Changes[i].ChangeID,
190185
Status: status,
191186
}
@@ -194,6 +189,11 @@ func (m *Module) sync(ctx context.Context) error {
194189
if err := m.save(ctx, cids, addressIds); err != nil {
195190
return errors.Wrap(err, "save")
196191
}
192+
log.Debug().
193+
Int("changes_count", len(cids)).
194+
Int64("head", m.state.ChangeId).
195+
Msg("saved changes")
196+
197197
end = len(changes.Changes) < int(m.limit)
198198
}
199199

pkg/module/module_test.go

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import (
88

99
celestials "github.com/celenium-io/celestial-module/pkg/api"
1010
celestialsMock "github.com/celenium-io/celestial-module/pkg/api/mock"
11-
"github.com/celenium-io/celestial-module/pkg/module/mock"
1211
"github.com/celenium-io/celestial-module/pkg/storage"
1312
pg "github.com/celenium-io/celestial-module/pkg/storage/postgres"
1413
"github.com/dipdup-net/go-lib/config"
@@ -32,7 +31,6 @@ type ModuleTestSuite struct {
3231
celestialState *pg.CelestialState
3332
ctrl *gomock.Controller
3433
api *celestialsMock.MockAPI
35-
address *mock.MockIdByHash
3634
}
3735

3836
// SetupSuite -
@@ -79,7 +77,6 @@ func (s *ModuleTestSuite) SetupSuite() {
7977

8078
s.ctrl = gomock.NewController(s.T())
8179
s.api = celestialsMock.NewMockAPI(s.ctrl)
82-
s.address = mock.NewMockIdByHash(s.ctrl)
8380
}
8481

8582
// TearDownSuite -
@@ -126,11 +123,6 @@ func (s *ModuleTestSuite) TestSync() {
126123
},
127124
}, nil)
128125

129-
s.address.EXPECT().
130-
IdByHash(gomock.Any(), gomock.Any()).
131-
Return([]uint64{1}, nil).
132-
Times(1)
133-
134126
cfgDs := config.DataSource{
135127
Kind: "celestials",
136128
URL: "base_url",
@@ -143,13 +135,14 @@ func (s *ModuleTestSuite) TestSync() {
143135

144136
m := New(
145137
cfgDs,
146-
s.address,
138+
func(ctx context.Context, address string) (uint64, error) {
139+
return 1, nil
140+
},
147141
s.celestials,
148142
s.celestialState,
149143
s.storage.Transactable,
150144
testIndexerName,
151145
network,
152-
WithAddressPrefix("celestia"),
153146
WithLimit(10),
154147
)
155148
m.celestialsApi = s.api

pkg/module/options.go

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,6 @@ import "time"
44

55
type ModuleOption func(*Module)
66

7-
func WithAddressPrefix(prefix string) ModuleOption {
8-
return func(m *Module) {
9-
m.prefix = prefix
10-
}
11-
}
12-
137
func WithIndexPeriod(period time.Duration) ModuleOption {
148
return func(m *Module) {
159
m.indexPeriod = period

0 commit comments

Comments
 (0)