Skip to content

Commit 7035719

Browse files
committed
Minimize number of slot advances for virtual accounts in memory
1 parent 790239a commit 7035719

File tree

1 file changed

+28
-14
lines changed

1 file changed

+28
-14
lines changed

geyser/handler_memory.go

Lines changed: 28 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,18 @@ import (
1818
"github.com/code-payments/code-vm-indexer/data/ram"
1919
)
2020

21+
const (
22+
confirmationsToFinalization = 32
23+
)
24+
2125
type cachedVirtualAccount struct {
22-
IsInitialized bool
23-
Index int
24-
Type cvm.VirtualAccountType
25-
Address string
26-
State []byte
27-
Slot uint64
26+
IsInitialized bool
27+
Index int
28+
Type cvm.VirtualAccountType
29+
Address string
30+
State []byte
31+
Slot uint64
32+
IsSlotAdvanced bool
2833
}
2934

3035
type MemoryAccountWithDataUpdateHandler struct {
@@ -301,19 +306,28 @@ func (h *MemoryAccountWithDataUpdateHandler) onStateObserved(ctx context.Context
301306

302307
var dbUpdate *cachedVirtualAccount
303308
if isInitialized {
309+
var isSlotAdvanced bool
304310
if cachedVirtualAccountState.IsInitialized &&
305311
cachedVirtualAccountState.Address == base58VirtualAccountAddress &&
306-
bytes.Equal(cachedVirtualAccountState.State, newVirtualAccountState) &&
307-
observedAtSlot-cachedVirtualAccountState.Slot < 100 { // todo: configurable?
308-
continue
312+
bytes.Equal(cachedVirtualAccountState.State, newVirtualAccountState) {
313+
314+
if cachedVirtualAccountState.IsSlotAdvanced || observedAtSlot-cachedVirtualAccountState.Slot < 2*confirmationsToFinalization {
315+
continue
316+
}
317+
318+
// Advance the slot sufficiently far past finalization if it hasn't
319+
// already been advanced. This is necessary for systems that rely on
320+
// finalized states via the RPC service.
321+
isSlotAdvanced = true
309322
}
310323

311324
dbUpdate = &cachedVirtualAccount{
312-
IsInitialized: true,
313-
Index: index,
314-
Type: virtualAccountType,
315-
Address: base58VirtualAccountAddress,
316-
State: newVirtualAccountState,
325+
IsInitialized: true,
326+
Index: index,
327+
Type: virtualAccountType,
328+
Address: base58VirtualAccountAddress,
329+
State: newVirtualAccountState,
330+
IsSlotAdvanced: isSlotAdvanced,
317331
}
318332
} else {
319333
if !cachedVirtualAccountState.IsInitialized {

0 commit comments

Comments
 (0)