@@ -38,6 +38,28 @@ type PGValidatorEntryUtxoOps struct {
3838 UtxoOperation
3939}
4040
41+ type SnapshotValidatorEntry struct {
42+ ValidatorPKID string `bun:",nullzero"`
43+ Domains []string `bun:",array"`
44+ DisableDelegatedStake bool
45+ DelegatedStakeCommissionBasisPoints uint64
46+ VotingPublicKey string `bun:",nullzero"`
47+ VotingAuthorization string `bun:",nullzero"`
48+ // Use bunbig.Int to store the balance as a numeric in the pg database.
49+ TotalStakeAmountNanos * bunbig.Int `pg:",use_zero"`
50+ LastActiveAtEpochNumber uint64
51+ JailedAtEpochNumber uint64
52+ SnapshotAtEpochNumber uint64 `pg:",use_zero"`
53+
54+ ExtraData map [string ]string `bun:"type:jsonb"`
55+ BadgerKey []byte `pg:",pk,use_zero"`
56+ }
57+
58+ type PGSnapshotValidatorEntry struct {
59+ bun.BaseModel `bun:"table:snapshot_validator_entry"`
60+ SnapshotValidatorEntry
61+ }
62+
4163// Convert the ValidatorEntry DeSo encoder to the PGValidatorEntry struct used by bun.
4264func ValidatorEncoderToPGStruct (validatorEntry * lib.ValidatorEntry , keyBytes []byte , params * lib.DeSoParams ) ValidatorEntry {
4365 pgValidatorEntry := ValidatorEntry {
@@ -96,23 +118,43 @@ func ValidatorBatchOperation(entries []*lib.StateChangeEntry, db *bun.DB, params
96118func bulkInsertValidatorEntry (entries []* lib.StateChangeEntry , db * bun.DB , operationType lib.StateSyncerOperationType , params * lib.DeSoParams ) error {
97119 // Track the unique entries we've inserted so we don't insert the same entry twice.
98120 uniqueEntries := consumer .UniqueEntries (entries )
121+ uniqueValidatorEntries := consumer .FilterEntriesByPrefix (uniqueEntries , lib .Prefixes .PrefixValidatorByPKID )
122+ uniqueSnapshotValidatorEntries := consumer .FilterEntriesByPrefix (uniqueEntries , lib .Prefixes .PrefixSnapshotValidatorSetByPKID )
99123 // Create a new array to hold the bun struct.
100- pgEntrySlice := make ([]* PGValidatorEntry , len (uniqueEntries ))
124+ pgEntrySlice := make ([]* PGValidatorEntry , len (uniqueValidatorEntries ))
125+ pgSnapshotEntrySlice := make ([]* PGSnapshotValidatorEntry , len (uniqueSnapshotValidatorEntries ))
101126
102127 // Loop through the entries and convert them to PGEntry.
103- for ii , entry := range uniqueEntries {
128+ for ii , entry := range uniqueValidatorEntries {
104129 pgEntrySlice [ii ] = & PGValidatorEntry {ValidatorEntry : ValidatorEncoderToPGStruct (entry .Encoder .(* lib.ValidatorEntry ), entry .KeyBytes , params )}
105130 }
131+ for ii , entry := range uniqueSnapshotValidatorEntries {
132+ pgSnapshotEntrySlice [ii ] = & PGSnapshotValidatorEntry {SnapshotValidatorEntry : SnapshotValidatorEncoderToPGStruct (entry .Encoder .(* lib.ValidatorEntry ), entry .KeyBytes , params )}
133+ }
106134
107135 // Execute the insert query.
108- query := db .NewInsert ().Model (& pgEntrySlice )
136+ if len (pgEntrySlice ) > 0 {
137+ query := db .NewInsert ().Model (& pgEntrySlice )
138+
139+ if operationType == lib .DbOperationTypeUpsert {
140+ query = query .On ("CONFLICT (badger_key) DO UPDATE" )
141+ }
109142
110- if operationType == lib .DbOperationTypeUpsert {
111- query = query .On ("CONFLICT (badger_key) DO UPDATE" )
143+ if _ , err := query .Returning ("" ).Exec (context .Background ()); err != nil {
144+ return errors .Wrapf (err , "entries.bulkInsertValidatorEntry: Error inserting validator entries" )
145+ }
112146 }
113147
114- if _ , err := query .Returning ("" ).Exec (context .Background ()); err != nil {
115- return errors .Wrapf (err , "entries.bulkInsertValidatorEntry: Error inserting entries" )
148+ if len (pgSnapshotEntrySlice ) > 0 {
149+ query := db .NewInsert ().Model (& pgSnapshotEntrySlice )
150+
151+ if operationType == lib .DbOperationTypeUpsert {
152+ query = query .On ("CONFLICT (badger_key) DO UPDATE" )
153+ }
154+
155+ if _ , err := query .Returning ("" ).Exec (context .Background ()); err != nil {
156+ return errors .Wrapf (err , "entries.bulkInsertValidatorEntry: Error inserting snapshot validator entries" )
157+ }
116158 }
117159 return nil
118160}
@@ -123,16 +165,64 @@ func bulkDeleteValidatorEntry(entries []*lib.StateChangeEntry, db *bun.DB, opera
123165 uniqueEntries := consumer .UniqueEntries (entries )
124166
125167 // Transform the entries into a list of keys to delete.
126- keysToDelete := consumer .KeysToDelete (uniqueEntries )
168+ validatorEntriesToDelete := consumer .FilterEntriesByPrefix (uniqueEntries , lib . Prefixes . PrefixValidatorByPKID )
127169
128- // Execute the delete query.
170+ snapshotValidatorEntriesToDelete := consumer .FilterEntriesByPrefix (uniqueEntries , lib .Prefixes .PrefixSnapshotValidatorSetByPKID )
171+
172+ // Execute the delete query for validator entries.
129173 if _ , err := db .NewDelete ().
130174 Model (& PGValidatorEntry {}).
131- Where ("badger_key IN (?)" , bun .In (keysToDelete )).
175+ Where ("badger_key IN (?)" , bun .In (validatorEntriesToDelete )).
132176 Returning ("" ).
133177 Exec (context .Background ()); err != nil {
134178 return errors .Wrapf (err , "entries.bulkDeleteValidatorEntry: Error deleting entries" )
135179 }
136180
181+ // Execute the delete query.
182+ if _ , err := db .NewDelete ().
183+ Model (& PGSnapshotValidatorEntry {}).
184+ Where ("badger_key IN (?)" , bun .In (snapshotValidatorEntriesToDelete )).
185+ Returning ("" ).
186+ Exec (context .Background ()); err != nil {
187+ return errors .Wrapf (err , "entries.bulkDeleteSnapshotValidatorEntry: Error deleting entries" )
188+ }
189+
137190 return nil
138191}
192+
193+ // Convert the SnapshotValidatorEntry DeSo encoder to the PGSnapshotValidatorEntry struct used by bun.
194+ func SnapshotValidatorEncoderToPGStruct (validatorEntry * lib.ValidatorEntry , keyBytes []byte , params * lib.DeSoParams ) SnapshotValidatorEntry {
195+ pgValidatorEntry := SnapshotValidatorEntry {
196+ ExtraData : consumer .ExtraDataBytesToString (validatorEntry .ExtraData ),
197+ BadgerKey : keyBytes ,
198+ }
199+
200+ if validatorEntry .ValidatorPKID != nil {
201+ pgValidatorEntry .ValidatorPKID = consumer .PublicKeyBytesToBase58Check ((* validatorEntry .ValidatorPKID )[:], params )
202+ }
203+
204+ if validatorEntry .Domains != nil {
205+ pgValidatorEntry .Domains = make ([]string , len (validatorEntry .Domains ))
206+ for ii , domain := range validatorEntry .Domains {
207+ pgValidatorEntry .Domains [ii ] = string (domain )
208+ }
209+ }
210+
211+ pgValidatorEntry .DisableDelegatedStake = validatorEntry .DisableDelegatedStake
212+ pgValidatorEntry .DelegatedStakeCommissionBasisPoints = validatorEntry .DelegatedStakeCommissionBasisPoints
213+
214+ if validatorEntry .VotingPublicKey != nil {
215+ pgValidatorEntry .VotingPublicKey = validatorEntry .VotingPublicKey .ToString ()
216+ }
217+
218+ if validatorEntry .VotingAuthorization != nil {
219+ pgValidatorEntry .VotingAuthorization = validatorEntry .VotingAuthorization .ToString ()
220+ }
221+
222+ pgValidatorEntry .TotalStakeAmountNanos = bunbig .FromMathBig (validatorEntry .TotalStakeAmountNanos .ToBig ())
223+ pgValidatorEntry .LastActiveAtEpochNumber = validatorEntry .LastActiveAtEpochNumber
224+ pgValidatorEntry .JailedAtEpochNumber = validatorEntry .JailedAtEpochNumber
225+ keyBytesWithoutPrefix := keyBytes [1 :]
226+ pgValidatorEntry .SnapshotAtEpochNumber = lib .DecodeUint64 (keyBytesWithoutPrefix [:8 ])
227+ return pgValidatorEntry
228+ }
0 commit comments