|
5 | 5 | "errors" |
6 | 6 | "fmt" |
7 | 7 |
|
8 | | - storetypes "github.com/cosmos/cosmos-sdk/store/types" |
9 | 8 | sdk "github.com/cosmos/cosmos-sdk/types" |
10 | 9 | "github.com/ethereum/go-ethereum/rpc" |
11 | 10 | sstypes "github.com/sei-protocol/sei-db/ss/types" |
@@ -48,44 +47,64 @@ func (m *WatermarkManager) Watermarks(ctx context.Context) (int64, int64, error) |
48 | 47 | } |
49 | 48 |
|
50 | 49 | var ( |
51 | | - earliestCandidates []int64 |
52 | | - latestCandidates []int64 |
| 50 | + latest int64 |
| 51 | + latestSet bool |
| 52 | + earliest int64 |
| 53 | + earliestSet bool |
53 | 54 | ) |
54 | 55 |
|
| 56 | + setLatest := func(candidate int64) { |
| 57 | + if candidate < 0 { |
| 58 | + return |
| 59 | + } |
| 60 | + if !latestSet || candidate < latest { |
| 61 | + latest = candidate |
| 62 | + latestSet = true |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + setEarliest := func(candidate int64) { |
| 67 | + if candidate < 0 { |
| 68 | + return |
| 69 | + } |
| 70 | + if !earliestSet || candidate > earliest { |
| 71 | + earliest = candidate |
| 72 | + earliestSet = true |
| 73 | + } |
| 74 | + } |
| 75 | + |
55 | 76 | // Tendermint heights |
56 | | - if latest, earliest, err := m.fetchTendermintWatermarks(ctx); err == nil { |
57 | | - latestCandidates = append(latestCandidates, latest) |
58 | | - earliestCandidates = append(earliestCandidates, earliest) |
| 77 | + if tmLatest, tmEarliest, err := m.fetchTendermintWatermarks(ctx); err == nil { |
| 78 | + setLatest(tmLatest) |
| 79 | + setEarliest(tmEarliest) |
59 | 80 | } else if !errors.Is(err, errNoHeightSource) { |
60 | 81 | return 0, 0, err |
61 | 82 | } |
62 | 83 |
|
| 84 | + if m.ctxProvider != nil { |
| 85 | + if ctxHeight := m.ctxProvider(LatestCtxHeight).BlockHeight(); ctxHeight > 0 { |
| 86 | + setLatest(ctxHeight) |
| 87 | + } |
| 88 | + } |
| 89 | + |
63 | 90 | // State store heights (historical state DB) |
64 | 91 | if ssLatest, ssEarliest, ok := m.fetchStateStoreWatermarks(); ok { |
65 | | - latestCandidates = append(latestCandidates, ssLatest) |
66 | | - earliestCandidates = append(earliestCandidates, ssEarliest) |
67 | | - } else if msLatest, msEarliest, ok := m.fetchMultiStoreWatermarks(); ok { |
68 | | - // Fall back to application multistore heights if state store unavailable |
69 | | - latestCandidates = append(latestCandidates, msLatest) |
70 | | - earliestCandidates = append(earliestCandidates, msEarliest) |
| 92 | + setLatest(ssLatest) |
| 93 | + setEarliest(ssEarliest) |
71 | 94 | } |
72 | 95 |
|
73 | 96 | // Receipt store height participates only in the latest watermark, since |
74 | 97 | // pruning guarantees the earliest watermark is bounded by TM+SS. |
75 | 98 | if m.receiptStore != nil { |
76 | | - if latest := m.receiptStore.GetLatestVersion(); latest > 0 { |
77 | | - latestCandidates = append(latestCandidates, latest) |
78 | | - } |
| 99 | + setLatest(m.receiptStore.GetLatestVersion()) |
79 | 100 | } |
80 | 101 |
|
81 | | - if len(latestCandidates) == 0 { |
| 102 | + if !latestSet { |
82 | 103 | return 0, 0, errNoHeightSource |
83 | 104 | } |
84 | 105 |
|
85 | | - latest := minInt64(latestCandidates) |
86 | | - earliest := int64(0) |
87 | | - if len(earliestCandidates) > 0 { |
88 | | - earliest = maxInt64(earliestCandidates) |
| 106 | + if !earliestSet { |
| 107 | + earliest = 0 |
89 | 108 | } |
90 | 109 |
|
91 | 110 | if latest < earliest { |
@@ -224,50 +243,3 @@ func (m *WatermarkManager) fetchStateStoreWatermarks() (int64, int64, bool) { |
224 | 243 | } |
225 | 244 | return m.stateStore.GetLatestVersion(), m.stateStore.GetEarliestVersion(), true |
226 | 245 | } |
227 | | - |
228 | | -func (m *WatermarkManager) fetchMultiStoreWatermarks() (latest int64, earliest int64, ok bool) { |
229 | | - if m.ctxProvider == nil { |
230 | | - return 0, 0, false |
231 | | - } |
232 | | - ctx := m.ctxProvider(LatestCtxHeight) |
233 | | - ms := ctx.MultiStore() |
234 | | - if ms == nil { |
235 | | - return 0, 0, false |
236 | | - } |
237 | | - defer func() { |
238 | | - if r := recover(); r != nil { |
239 | | - latest, earliest, ok = 0, 0, false |
240 | | - } |
241 | | - }() |
242 | | - earliest = ms.GetEarliestVersion() |
243 | | - if earliest == 0 { |
244 | | - earliest = ctx.BlockHeight() |
245 | | - } |
246 | | - if commitStore, implements := ms.(interface{ LastCommitID() storetypes.CommitID }); implements { |
247 | | - latest = commitStore.LastCommitID().Version |
248 | | - } |
249 | | - if latest == 0 { |
250 | | - latest = ctx.BlockHeight() |
251 | | - } |
252 | | - return latest, earliest, true |
253 | | -} |
254 | | - |
255 | | -func minInt64(values []int64) int64 { |
256 | | - min := values[0] |
257 | | - for _, v := range values[1:] { |
258 | | - if v < min { |
259 | | - min = v |
260 | | - } |
261 | | - } |
262 | | - return min |
263 | | -} |
264 | | - |
265 | | -func maxInt64(values []int64) int64 { |
266 | | - max := values[0] |
267 | | - for _, v := range values[1:] { |
268 | | - if v > max { |
269 | | - max = v |
270 | | - } |
271 | | - } |
272 | | - return max |
273 | | -} |
0 commit comments