@@ -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,19 @@ 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+
36+ var snapshotAtEpochNumber uint64
37+ // Epochs use data snapshotted from two epochs ago. Epochs 0 and 1 use data from epoch 0.
38+ if epochEntry .EpochNumber >= 2 {
39+ snapshotAtEpochNumber = epochEntry .EpochNumber - 2
40+ }
3641 return EpochEntry {
37- EpochNumber : epochEntry .EpochNumber ,
38- InitialBlockHeight : epochEntry .InitialBlockHeight ,
39- InitialView : epochEntry .InitialView ,
40- FinalBlockHeight : epochEntry .FinalBlockHeight ,
41- BadgerKey : keyBytes ,
42+ EpochNumber : epochEntry .EpochNumber ,
43+ InitialBlockHeight : epochEntry .InitialBlockHeight ,
44+ InitialView : epochEntry .InitialView ,
45+ FinalBlockHeight : epochEntry .FinalBlockHeight ,
46+ CreatedAtBlockTimestampNanoSecs : epochEntry .CreatedAtBlockTimestampNanoSecs ,
47+ SnapshotAtEpochNumber : snapshotAtEpochNumber ,
4248 }
4349}
4450
@@ -49,8 +55,11 @@ func EpochEntryBatchOperation(entries []*lib.StateChangeEntry, db *bun.DB, param
4955 // We also ensure before this that all entries have the same operation type.
5056 operationType := entries [0 ].OperationType
5157 var err error
58+ // Core only tracks the current epoch entry and never deletes them.
59+ // In order to track all historical epoch entries, we don't use the badger
60+ // key to uniquely identify them, but rather the epoch number.
5261 if operationType == lib .DbOperationTypeDelete {
53- err = bulkDeleteEpochEntry ( entries , db , operationType )
62+ return errors . Wrapf ( err , "entries.EpochEntryBatchOperation: Delete operation type not supported" )
5463 } else {
5564 err = bulkInsertEpochEntry (entries , db , operationType , params )
5665 }
@@ -76,31 +85,11 @@ func bulkInsertEpochEntry(entries []*lib.StateChangeEntry, db *bun.DB, operation
7685 query := db .NewInsert ().Model (& pgEntrySlice )
7786
7887 if operationType == lib .DbOperationTypeUpsert {
79- query = query .On ("CONFLICT (badger_key ) DO UPDATE" )
88+ query = query .On ("CONFLICT (epoch_number ) DO UPDATE" )
8089 }
8190
8291 if _ , err := query .Returning ("" ).Exec (context .Background ()); err != nil {
8392 return errors .Wrapf (err , "entries.bulkInsertEpochEntry: Error inserting entries" )
8493 }
8594 return nil
8695}
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