@@ -14,9 +14,8 @@ type EpochEntry struct {
1414 InitialBlockHeight uint64
1515 InitialView uint64
1616 FinalBlockHeight uint64
17- CreatedAtBlockTimestampNanoSecs uint64
18-
19- BadgerKey []byte `pg:",pk,use_zero"`
17+ CreatedAtBlockTimestampNanoSecs int64
18+ SnapshotAtEpochNumber uint64
2019}
2120
2221type PGEpochEntry struct {
@@ -33,12 +32,17 @@ type PGEpochUtxoOps struct {
3332
3433// Convert the EpochEntry DeSo encoder to the PGEpochEntry struct used by bun.
3534func EpochEntryEncoderToPGStruct (epochEntry * lib.EpochEntry , keyBytes []byte , params * lib.DeSoParams ) EpochEntry {
35+ var snapshotAtEpochNumber uint64
36+ if epochEntry .EpochNumber >= 2 {
37+ snapshotAtEpochNumber = epochEntry .EpochNumber - 2
38+ }
3639 return EpochEntry {
37- EpochNumber : epochEntry .EpochNumber ,
38- InitialBlockHeight : epochEntry .InitialBlockHeight ,
39- InitialView : epochEntry .InitialView ,
40- FinalBlockHeight : epochEntry .FinalBlockHeight ,
41- BadgerKey : keyBytes ,
40+ EpochNumber : epochEntry .EpochNumber ,
41+ InitialBlockHeight : epochEntry .InitialBlockHeight ,
42+ InitialView : epochEntry .InitialView ,
43+ FinalBlockHeight : epochEntry .FinalBlockHeight ,
44+ CreatedAtBlockTimestampNanoSecs : epochEntry .CreatedAtBlockTimestampNanoSecs ,
45+ SnapshotAtEpochNumber : snapshotAtEpochNumber ,
4246 }
4347}
4448
@@ -49,8 +53,11 @@ func EpochEntryBatchOperation(entries []*lib.StateChangeEntry, db *bun.DB, param
4953 // We also ensure before this that all entries have the same operation type.
5054 operationType := entries [0 ].OperationType
5155 var err error
56+ // Core only tracks the current epoch entry and never deletes them.
57+ // In order to track all historical epoch entries, we don't use the badger
58+ // key to uniquely identify them, but rather the epoch number.
5259 if operationType == lib .DbOperationTypeDelete {
53- err = bulkDeleteEpochEntry ( entries , db , operationType )
60+ return errors . Wrapf ( err , "entries.EpochEntryBatchOperation: Delete operation type not supported" )
5461 } else {
5562 err = bulkInsertEpochEntry (entries , db , operationType , params )
5663 }
@@ -76,31 +83,11 @@ func bulkInsertEpochEntry(entries []*lib.StateChangeEntry, db *bun.DB, operation
7683 query := db .NewInsert ().Model (& pgEntrySlice )
7784
7885 if operationType == lib .DbOperationTypeUpsert {
79- query = query .On ("CONFLICT (badger_key ) DO UPDATE" )
86+ query = query .On ("CONFLICT (epoch_number ) DO UPDATE" )
8087 }
8188
8289 if _ , err := query .Returning ("" ).Exec (context .Background ()); err != nil {
8390 return errors .Wrapf (err , "entries.bulkInsertEpochEntry: Error inserting entries" )
8491 }
8592 return nil
8693}
87-
88- // bulkDeleteEpochEntry deletes a batch of locked stake entries from the database.
89- func bulkDeleteEpochEntry (entries []* lib.StateChangeEntry , db * bun.DB , operationType lib.StateSyncerOperationType ) error {
90- // Track the unique entries we've inserted so we don't insert the same entry twice.
91- uniqueEntries := consumer .UniqueEntries (entries )
92-
93- // Transform the entries into a list of keys to delete.
94- keysToDelete := consumer .KeysToDelete (uniqueEntries )
95-
96- // Execute the delete query.
97- if _ , err := db .NewDelete ().
98- Model (& PGEpochEntry {}).
99- Where ("badger_key IN (?)" , bun .In (keysToDelete )).
100- Returning ("" ).
101- Exec (context .Background ()); err != nil {
102- return errors .Wrapf (err , "entries.bulkDeleteEpochEntry: Error deleting entries" )
103- }
104-
105- return nil
106- }
0 commit comments