44 "context"
55 "github.com/deso-protocol/core/lib"
66 "github.com/deso-protocol/state-consumer/consumer"
7+ lru "github.com/hashicorp/golang-lru/v2"
78 "github.com/pkg/errors"
89 "github.com/uptrace/bun"
910 "github.com/uptrace/bun/extra/bunbig"
@@ -98,15 +99,15 @@ func ValidatorEncoderToPGStruct(validatorEntry *lib.ValidatorEntry, keyBytes []b
9899
99100// ValidatorBatchOperation is the entry point for processing a batch of Validator entries.
100101// It determines the appropriate handler based on the operation type and executes it.
101- func ValidatorBatchOperation (entries []* lib.StateChangeEntry , db bun.IDB , params * lib.DeSoParams ) error {
102+ func ValidatorBatchOperation (entries []* lib.StateChangeEntry , db bun.IDB , params * lib.DeSoParams , cachedEntries * lru. Cache [ string , [] byte ] ) error {
102103 // We check before we call this function that there is at least one operation type.
103104 // We also ensure before this that all entries have the same operation type.
104105 operationType := entries [0 ].OperationType
105106 var err error
106107 if operationType == lib .DbOperationTypeDelete {
107- err = bulkDeleteValidatorEntry (entries , db , operationType )
108+ err = bulkDeleteValidatorEntry (entries , db , operationType , cachedEntries )
108109 } else {
109- err = bulkInsertValidatorEntry (entries , db , operationType , params )
110+ err = bulkInsertValidatorEntry (entries , db , operationType , params , cachedEntries )
110111 }
111112 if err != nil {
112113 return errors .Wrapf (err , "entries.ValidatorBatchOperation: Problem with operation type %v" , operationType )
@@ -115,9 +116,13 @@ func ValidatorBatchOperation(entries []*lib.StateChangeEntry, db bun.IDB, params
115116}
116117
117118// bulkInsertValidatorEntry inserts a batch of validator entries into the database.
118- func bulkInsertValidatorEntry (entries []* lib.StateChangeEntry , db bun.IDB , operationType lib.StateSyncerOperationType , params * lib.DeSoParams ) error {
119+ func bulkInsertValidatorEntry (entries []* lib.StateChangeEntry , db bun.IDB , operationType lib.StateSyncerOperationType , params * lib.DeSoParams , cachedEntries * lru. Cache [ string , [] byte ] ) error {
119120 // Track the unique entries we've inserted so we don't insert the same entry twice.
120121 uniqueEntries := consumer .UniqueEntries (entries )
122+
123+ // Filter out any entries that are already tracked in the cache.
124+ uniqueEntries = consumer .FilterCachedEntries (uniqueEntries , cachedEntries )
125+
121126 uniqueValidatorEntries := consumer .FilterEntriesByPrefix (uniqueEntries , lib .Prefixes .PrefixValidatorByPKID )
122127 uniqueSnapshotValidatorEntries := consumer .FilterEntriesByPrefix (uniqueEntries , lib .Prefixes .PrefixSnapshotValidatorSetByPKID )
123128 // Create a new array to hold the bun struct.
@@ -156,11 +161,17 @@ func bulkInsertValidatorEntry(entries []*lib.StateChangeEntry, db bun.IDB, opera
156161 return errors .Wrapf (err , "entries.bulkInsertValidatorEntry: Error inserting snapshot validator entries" )
157162 }
158163 }
164+
165+ // Add any new entries to the cache.
166+ for _ , entry := range uniqueEntries {
167+ cachedEntries .Add (string (entry .KeyBytes ), entry .EncoderBytes )
168+ }
169+
159170 return nil
160171}
161172
162173// bulkDeleteValidatorEntry deletes a batch of validator entries from the database.
163- func bulkDeleteValidatorEntry (entries []* lib.StateChangeEntry , db bun.IDB , operationType lib.StateSyncerOperationType ) error {
174+ func bulkDeleteValidatorEntry (entries []* lib.StateChangeEntry , db bun.IDB , operationType lib.StateSyncerOperationType , cachedEntries * lru. Cache [ string , [] byte ] ) error {
164175 // Track the unique entries we've inserted so we don't insert the same entry twice.
165176 uniqueEntries := consumer .UniqueEntries (entries )
166177 uniqueKeys := consumer .KeysToDelete (uniqueEntries )
@@ -195,6 +206,12 @@ func bulkDeleteValidatorEntry(entries []*lib.StateChangeEntry, db bun.IDB, opera
195206 }
196207 }
197208
209+ // Delete cached validator entries.
210+ for _ , key := range validatorKeysToDelete {
211+ keyStr := string (key )
212+ cachedEntries .Remove (keyStr )
213+ }
214+
198215 return nil
199216}
200217
0 commit comments