-
Notifications
You must be signed in to change notification settings - Fork 561
/
ingester.go
129 lines (100 loc) · 3.78 KB
/
ingester.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
package sqs
import (
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/osmosis-labs/sqs/sqsdomain/repository"
chaininforedisrepo "github.com/osmosis-labs/sqs/sqsdomain/repository/redis/chaininfo"
"github.com/osmosis-labs/osmosis/v23/ingest/sqs/domain"
)
var _ domain.Ingester = &sqsIngester{}
// sqsIngester is a sidecar query server (SQS) implementation of Ingester.
// It encapsulates all individual SQS ingesters.
type sqsIngester struct {
txManager repository.TxManager
poolsIngester domain.PoolIngester
chainInfoRepo chaininforedisrepo.ChainInfoRepository
keepers domain.SQSIngestKeepers
}
// NewSidecarQueryServerIngester creates a new sidecar query server ingester.
// poolsRepository is the storage for pools.
// gammKeeper is the keeper for Gamm pools.
func NewSidecarQueryServerIngester(poolsIngester domain.PoolIngester, chainInfoIngester chaininforedisrepo.ChainInfoRepository, txManager repository.TxManager, keepers domain.SQSIngestKeepers) domain.Ingester {
return &sqsIngester{
txManager: txManager,
chainInfoRepo: chainInfoIngester,
poolsIngester: poolsIngester,
keepers: keepers,
}
}
// ProcessAllBlockData implements ingest.Ingester.
func (i *sqsIngester) ProcessAllBlockData(ctx sdk.Context) error {
// Start atomic transaction
tx := i.txManager.StartTx()
// Concentrated pools
concentratedPools, err := i.keepers.ConcentratedKeeper.GetPools(ctx)
if err != nil {
return err
}
// CFMM pools
cfmmPools, err := i.keepers.GammKeeper.GetPools(ctx)
if err != nil {
return err
}
// CosmWasm pools
cosmWasmPools, err := i.keepers.CosmWasmPoolKeeper.GetPoolsWithWasmKeeper(ctx)
if err != nil {
return err
}
blockPools := domain.BlockPools{
ConcentratedPools: concentratedPools,
CosmWasmPools: cosmWasmPools,
CFMMPools: cfmmPools,
}
// Process block by reading and writing data and ingesting data into sinks
if err := i.poolsIngester.ProcessPoolState(ctx, tx, blockPools); err != nil {
return err
}
return i.storeLatestHeight(ctx, tx)
}
// ProcessChangedBlockData implements ingest.Ingester.
func (i *sqsIngester) ProcessChangedBlockData(ctx sdk.Context, changedPools domain.BlockPools) error {
// Start atomic transaction
tx := i.txManager.StartTx()
concentratedPoolIDTickChange := changedPools.ConcentratedPoolIDTickChange
// Copy over the pools that were changed in the block
for _, pool := range changedPools.ConcentratedPools {
changedPools.ConcentratedPoolIDTickChange[pool.GetId()] = struct{}{}
}
// Update concentrated pools
for poolID := range concentratedPoolIDTickChange {
// Skip if the pool if it is already tracked
if _, ok := changedPools.ConcentratedPoolIDTickChange[poolID]; ok {
continue
}
pool, err := i.keepers.ConcentratedKeeper.GetConcentratedPoolById(ctx, poolID)
if err != nil {
return err
}
changedPools.ConcentratedPools = append(changedPools.ConcentratedPools, pool)
changedPools.ConcentratedPoolIDTickChange[poolID] = struct{}{}
}
// Process block by reading and writing data and ingesting data into sinks
if err := i.poolsIngester.ProcessPoolState(ctx, tx, changedPools); err != nil {
return err
}
return i.storeLatestHeight(ctx, tx)
}
// storeLatestHeight stores the latest blockchain height on the tx.
// Additionally, it executes the transaction.
// Returns error if the store operation fails.
func (i *sqsIngester) storeLatestHeight(ctx sdk.Context, tx repository.Tx) error {
goCtx := sdk.WrapSDKContext(ctx)
height := ctx.BlockHeight()
ctx.Logger().Info("ingesting latest blockchain height", "height", height)
err := i.chainInfoRepo.StoreLatestHeight(sdk.WrapSDKContext(ctx), tx, uint64(height))
if err != nil {
ctx.Logger().Error("failed to ingest latest blockchain height", "error", err)
return err
}
// Flush all writes atomically
return tx.Exec(goCtx)
}