Skip to content

Commit 2c86c5a

Browse files
committed
Add LeaderScheduleEntry
1 parent b64ca72 commit 2c86c5a

5 files changed

Lines changed: 25 additions & 37 deletions

File tree

entries/epoch.go

Lines changed: 17 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -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

2221
type PGEpochEntry struct {
@@ -33,12 +32,17 @@ type PGEpochUtxoOps struct {
3332

3433
// Convert the EpochEntry DeSo encoder to the PGEpochEntry struct used by bun.
3534
func 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-
}

migrations/initial_migrations/20240129000003_create_epoch_entry_table.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,18 @@ import (
1212
func createEpochEntryTable(db *bun.DB, tableName string) error {
1313
_, err := db.Exec(strings.Replace(`
1414
CREATE TABLE {tableName} (
15-
epoch_number BIGINT NOT NULL,
15+
epoch_number BIGINT PRIMARY KEY NOT NULL,
1616
initial_block_height BIGINT NOT NULL,
1717
initial_view BIGINT NOT NULL,
1818
final_block_height BIGINT NOT NULL,
1919
created_at_block_timestamp_nano_secs BIGINT NOT NULL,
20-
21-
badger_key BYTEA PRIMARY KEY
20+
snapshot_at_epoch_number BIGINT NOT NULL
2221
);
22+
23+
CREATE INDEX {tableName}_epoch_number_idx ON {tableName} (epoch_number);
24+
CREATE INDEX {tableName}_initial_block_height_idx ON {tableName} (initial_block_height);
25+
CREATE INDEX {tableName}_final_block_height_idx ON {tableName} (final_block_height);
26+
CREATE INDEX {tableName}_snapshot_at_epoch_number_idx ON {tableName} (snapshot_at_epoch_number);
2327
`, "{tableName}", tableName, -1))
2428
// TODO: What other fields do we need indexed?
2529
return err

migrations/initial_migrations/20240215000001_create_leader_schedule.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ func createLeaderScheduleTable(db *bun.DB, tableName string) error {
1919
CREATE INDEX {tableName}_snapshot_at_epoch_number_idx ON {tableName} (snapshot_at_epoch_number);
2020
CREATE INDEX {tableName}_snapshot_at_epoch_number_leader_index_idx ON {tableName} (snapshot_at_epoch_number, leader_index);
2121
`, "{tableName}", tableName, -1))
22-
// TODO: What other fields do we need indexed?
2322
return err
2423
}
2524

migrations/post_sync_migrations/20240213000002_create_pos_fk_comments.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ func init() {
2222
comment on column locked_stake_entry.badger_key is E'@omit';
2323
comment on column yield_curve_point.badger_key is E'@omit';
2424
comment on column locked_balance_entry.badger_key is E'@omit';
25-
comment on column epoch_entry.badger_key is E'@omit';
2625
comment on table transaction_partition_34 is E'@omit';
2726
comment on table transaction_partition_35 is E'@omit';
2827
comment on table transaction_partition_36 is E'@omit';

migrations/post_sync_migrations/20240215000002_create_leader_schedule_fk_comments.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,10 @@ import (
66
"github.com/uptrace/bun"
77
)
88

9-
// TODO: revisit access group relationships when we refactor the messaging app to use the graphql API.
109
func init() {
1110
Migrations.MustRegister(func(ctx context.Context, db *bun.DB) error {
1211
_, err := db.Exec(`
13-
comment on table leader_schedule_entry is E'@foreignKey (validator_pkid) references account (pkid)|@foreignFieldName leaderScheduleEntries|@fieldName leaderAccount\n@foreignKey (validator_pkid) references validator_entry (validator_pkid)|@foreignFieldName leaderScheduleEntries|@fieldName validatorEntry';
12+
comment on table leader_schedule_entry is E'@foreignKey (validator_pkid) references account (pkid)|@foreignFieldName leaderScheduleEntries|@fieldName leaderAccount\n@foreignKey (validator_pkid) references validator_entry (validator_pkid)|@foreignFieldName leaderScheduleEntries|@fieldName validatorEntry\n@foreignKey (snapshot_at_epoch_number) references epoch_entry (snapshot_at_epoch_number)|@foreignFieldName leaderScheduleEntries|@fieldName epochEntryBySnapshot';
1413
comment on column leader_schedule_entry.badger_key is E'@omit';
1514
`)
1615
if err != nil {

0 commit comments

Comments
 (0)