Skip to content

Commit cf78aa0

Browse files
Lazy Ninatholonious
authored andcommitted
Add StakeEntry, LockedStakeEntry, and ValidatorEntry. Update Dockerfile for pos
1 parent 3e16391 commit cf78aa0

13 files changed

+960
-0
lines changed

entries/epoch.go

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
package entries
2+
3+
import (
4+
"context"
5+
"github.com/deso-protocol/core/lib"
6+
"github.com/deso-protocol/state-consumer/consumer"
7+
"github.com/pkg/errors"
8+
"github.com/uptrace/bun"
9+
)
10+
11+
// TODO: when to use nullzero vs use_zero?
12+
type EpochEntry struct {
13+
EpochNumber uint64
14+
InitialBlockHeight uint64
15+
InitialView uint64
16+
FinalBlockHeight uint64
17+
CreatedAtBlockTimestampNanoSecs uint64
18+
19+
BadgerKey []byte `pg:",pk,use_zero"`
20+
}
21+
22+
type PGEpochEntry struct {
23+
bun.BaseModel `bun:"table:epoch_entry"`
24+
EpochEntry
25+
}
26+
27+
// TODO: Do I need this?
28+
type PGEpochUtxoOps struct {
29+
bun.BaseModel `bun:"table:epoch_entry_utxo_ops"`
30+
EpochEntry
31+
UtxoOperation
32+
}
33+
34+
// Convert the EpochEntry DeSo encoder to the PGEpochEntry struct used by bun.
35+
func EpochEntryEncoderToPGStruct(epochEntry *lib.EpochEntry, keyBytes []byte, params *lib.DeSoParams) EpochEntry {
36+
return EpochEntry{
37+
EpochNumber: epochEntry.EpochNumber,
38+
InitialBlockHeight: epochEntry.InitialBlockHeight,
39+
InitialView: epochEntry.InitialView,
40+
FinalBlockHeight: epochEntry.FinalBlockHeight,
41+
BadgerKey: keyBytes,
42+
}
43+
}
44+
45+
// EpochEntryBatchOperation is the entry point for processing a batch of Epoch entries.
46+
// It determines the appropriate handler based on the operation type and executes it.
47+
func EpochEntryBatchOperation(entries []*lib.StateChangeEntry, db *bun.DB, params *lib.DeSoParams) error {
48+
// We check before we call this function that there is at least one operation type.
49+
// We also ensure before this that all entries have the same operation type.
50+
operationType := entries[0].OperationType
51+
var err error
52+
if operationType == lib.DbOperationTypeDelete {
53+
err = bulkDeleteEpochEntry(entries, db, operationType)
54+
} else {
55+
err = bulkInsertEpochEntry(entries, db, operationType, params)
56+
}
57+
if err != nil {
58+
return errors.Wrapf(err, "entries.EpochEntryBatchOperation: Problem with operation type %v", operationType)
59+
}
60+
return nil
61+
}
62+
63+
// bulkInsertEpochEntry inserts a batch of locked stake entries into the database.
64+
func bulkInsertEpochEntry(entries []*lib.StateChangeEntry, db *bun.DB, operationType lib.StateSyncerOperationType, params *lib.DeSoParams) error {
65+
// Track the unique entries we've inserted so we don't insert the same entry twice.
66+
uniqueEntries := consumer.UniqueEntries(entries)
67+
// Create a new array to hold the bun struct.
68+
pgEntrySlice := make([]*PGEpochEntry, len(uniqueEntries))
69+
70+
// Loop through the entries and convert them to PGEntry.
71+
for ii, entry := range uniqueEntries {
72+
pgEntrySlice[ii] = &PGEpochEntry{EpochEntry: EpochEntryEncoderToPGStruct(entry.Encoder.(*lib.EpochEntry), entry.KeyBytes, params)}
73+
}
74+
75+
// Execute the insert query.
76+
query := db.NewInsert().Model(&pgEntrySlice)
77+
78+
if operationType == lib.DbOperationTypeUpsert {
79+
query = query.On("CONFLICT (badger_key) DO UPDATE")
80+
}
81+
82+
if _, err := query.Returning("").Exec(context.Background()); err != nil {
83+
return errors.Wrapf(err, "entries.bulkInsertEpochEntry: Error inserting entries")
84+
}
85+
return nil
86+
}
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+
}

entries/locked_stake.go

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
package entries
2+
3+
import (
4+
"context"
5+
"github.com/deso-protocol/core/lib"
6+
"github.com/deso-protocol/state-consumer/consumer"
7+
"github.com/pkg/errors"
8+
"github.com/uptrace/bun"
9+
"github.com/uptrace/bun/extra/bunbig"
10+
)
11+
12+
// TODO: when to use nullzero vs use_zero?
13+
type LockedStakeEntry struct {
14+
StakerPKID string `bun:",nullzero"`
15+
ValidatorPKID string `bun:",nullzero"`
16+
LockedAmountNanos *bunbig.Int `pg:",use_zero"`
17+
LockedAtEpochNumber uint64
18+
19+
ExtraData map[string]string `bun:"type:jsonb"`
20+
BadgerKey []byte `pg:",pk,use_zero"`
21+
}
22+
23+
type PGLockedStakeEntry struct {
24+
bun.BaseModel `bun:"table:locked_stake_entry"`
25+
LockedStakeEntry
26+
}
27+
28+
// TODO: Do I need this?
29+
type PGLockedStakeEntryUtxoOps struct {
30+
bun.BaseModel `bun:"table:locked_stake_entry_utxo_ops"`
31+
LockedStakeEntry
32+
UtxoOperation
33+
}
34+
35+
// Convert the LockedStakeEntry DeSo encoder to the PGLockedStakeEntry struct used by bun.
36+
func LockedStakeEncoderToPGStruct(lockedStakeEntry *lib.LockedStakeEntry, keyBytes []byte, params *lib.DeSoParams) LockedStakeEntry {
37+
pgLockedStakeEntry := LockedStakeEntry{
38+
ExtraData: consumer.ExtraDataBytesToString(lockedStakeEntry.ExtraData),
39+
BadgerKey: keyBytes,
40+
}
41+
42+
if lockedStakeEntry.StakerPKID != nil {
43+
pgLockedStakeEntry.StakerPKID = consumer.PublicKeyBytesToBase58Check((*lockedStakeEntry.StakerPKID)[:], params)
44+
}
45+
46+
if lockedStakeEntry.ValidatorPKID != nil {
47+
pgLockedStakeEntry.ValidatorPKID = consumer.PublicKeyBytesToBase58Check((*lockedStakeEntry.ValidatorPKID)[:], params)
48+
}
49+
50+
pgLockedStakeEntry.LockedAtEpochNumber = lockedStakeEntry.LockedAtEpochNumber
51+
pgLockedStakeEntry.LockedAmountNanos = bunbig.FromMathBig(lockedStakeEntry.LockedAmountNanos.ToBig())
52+
53+
return pgLockedStakeEntry
54+
}
55+
56+
// LockedStakeBatchOperation is the entry point for processing a batch of LockedStake entries.
57+
// It determines the appropriate handler based on the operation type and executes it.
58+
func LockedStakeBatchOperation(entries []*lib.StateChangeEntry, db *bun.DB, params *lib.DeSoParams) error {
59+
// We check before we call this function that there is at least one operation type.
60+
// We also ensure before this that all entries have the same operation type.
61+
operationType := entries[0].OperationType
62+
var err error
63+
if operationType == lib.DbOperationTypeDelete {
64+
err = bulkDeleteLockedStakeEntry(entries, db, operationType)
65+
} else {
66+
err = bulkInsertLockedStakeEntry(entries, db, operationType, params)
67+
}
68+
if err != nil {
69+
return errors.Wrapf(err, "entries.LockedStakeBatchOperation: Problem with operation type %v", operationType)
70+
}
71+
return nil
72+
}
73+
74+
// bulkInsertLockedStakeEntry inserts a batch of locked stake entries into the database.
75+
func bulkInsertLockedStakeEntry(entries []*lib.StateChangeEntry, db *bun.DB, operationType lib.StateSyncerOperationType, params *lib.DeSoParams) error {
76+
// Track the unique entries we've inserted so we don't insert the same entry twice.
77+
uniqueEntries := consumer.UniqueEntries(entries)
78+
// Create a new array to hold the bun struct.
79+
pgEntrySlice := make([]*PGLockedStakeEntry, len(uniqueEntries))
80+
81+
// Loop through the entries and convert them to PGEntry.
82+
for ii, entry := range uniqueEntries {
83+
pgEntrySlice[ii] = &PGLockedStakeEntry{LockedStakeEntry: LockedStakeEncoderToPGStruct(entry.Encoder.(*lib.LockedStakeEntry), entry.KeyBytes, params)}
84+
}
85+
86+
// Execute the insert query.
87+
query := db.NewInsert().Model(&pgEntrySlice)
88+
89+
if operationType == lib.DbOperationTypeUpsert {
90+
query = query.On("CONFLICT (badger_key) DO UPDATE")
91+
}
92+
93+
if _, err := query.Returning("").Exec(context.Background()); err != nil {
94+
return errors.Wrapf(err, "entries.bulkInsertLockedStakeEntry: Error inserting entries")
95+
}
96+
return nil
97+
}
98+
99+
// bulkDeleteLockedStakeEntry deletes a batch of locked stake entries from the database.
100+
func bulkDeleteLockedStakeEntry(entries []*lib.StateChangeEntry, db *bun.DB, operationType lib.StateSyncerOperationType) error {
101+
// Track the unique entries we've inserted so we don't insert the same entry twice.
102+
uniqueEntries := consumer.UniqueEntries(entries)
103+
104+
// Transform the entries into a list of keys to delete.
105+
keysToDelete := consumer.KeysToDelete(uniqueEntries)
106+
107+
// Execute the delete query.
108+
if _, err := db.NewDelete().
109+
Model(&PGLockedStakeEntry{}).
110+
Where("badger_key IN (?)", bun.In(keysToDelete)).
111+
Returning("").
112+
Exec(context.Background()); err != nil {
113+
return errors.Wrapf(err, "entries.bulkDeleteLockedStakeEntry: Error deleting entries")
114+
}
115+
116+
return nil
117+
}

entries/lockup.go

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
package entries
2+
3+
import (
4+
"context"
5+
"github.com/deso-protocol/core/lib"
6+
"github.com/deso-protocol/state-consumer/consumer"
7+
"github.com/pkg/errors"
8+
"github.com/uptrace/bun"
9+
"github.com/uptrace/bun/extra/bunbig"
10+
)
11+
12+
// TODO: when to use nullzero vs use_zero?
13+
type LockedBalanceEntry struct {
14+
HODLerPKID string `bun:",nullzero"`
15+
ProfilePKID string `bun:",nullzero"`
16+
UnlockTimestampNanoSecs int64
17+
VestingEndTimestampNanoSecs int64
18+
BalanceBaseUnits *bunbig.Int `pg:",use_zero"`
19+
20+
BadgerKey []byte `pg:",pk,use_zero"`
21+
}
22+
23+
type PGLockedBalanceEntry struct {
24+
bun.BaseModel `bun:"table:locked_balance_entry"`
25+
LockedBalanceEntry
26+
}
27+
28+
// TODO: Do I need this?
29+
type PGLockedBalanceEntryUtxoOps struct {
30+
bun.BaseModel `bun:"table:locked_balance_entry_utxo_ops"`
31+
LockedBalanceEntry
32+
UtxoOperation
33+
}
34+
35+
// Convert the LockedBalanceEntry DeSo encoder to the PGLockedBalnceEntry struct used by bun.
36+
func LockedBalanceEntryEncoderToPGStruct(lockedBalanceEntry *lib.LockedBalanceEntry, keyBytes []byte, params *lib.DeSoParams) LockedBalanceEntry {
37+
pgLockedBalanceEntry := LockedBalanceEntry{
38+
BadgerKey: keyBytes,
39+
}
40+
41+
if lockedBalanceEntry.HODLerPKID != nil {
42+
pgLockedBalanceEntry.HODLerPKID = consumer.PublicKeyBytesToBase58Check((*lockedBalanceEntry.HODLerPKID)[:], params)
43+
}
44+
45+
if lockedBalanceEntry.ProfilePKID != nil {
46+
pgLockedBalanceEntry.ProfilePKID = consumer.PublicKeyBytesToBase58Check((*lockedBalanceEntry.ProfilePKID)[:], params)
47+
}
48+
49+
pgLockedBalanceEntry.UnlockTimestampNanoSecs = lockedBalanceEntry.UnlockTimestampNanoSecs
50+
pgLockedBalanceEntry.VestingEndTimestampNanoSecs = lockedBalanceEntry.VestingEndTimestampNanoSecs
51+
pgLockedBalanceEntry.BalanceBaseUnits = bunbig.FromMathBig(lockedBalanceEntry.BalanceBaseUnits.ToBig())
52+
53+
return pgLockedBalanceEntry
54+
}
55+
56+
// LockedBalanceEntryBatchOperation is the entry point for processing a batch of LockedBalance entries.
57+
// It determines the appropriate handler based on the operation type and executes it.
58+
func LockedBalanceEntryBatchOperation(entries []*lib.StateChangeEntry, db *bun.DB, params *lib.DeSoParams) error {
59+
// We check before we call this function that there is at least one operation type.
60+
// We also ensure before this that all entries have the same operation type.
61+
operationType := entries[0].OperationType
62+
var err error
63+
if operationType == lib.DbOperationTypeDelete {
64+
err = bulkDeleteLockedBalanceEntry(entries, db, operationType)
65+
} else {
66+
err = bulkInsertLockedBalanceEntry(entries, db, operationType, params)
67+
}
68+
if err != nil {
69+
return errors.Wrapf(err, "entries.LockedBalanceEntryBatchOperation: Problem with operation type %v", operationType)
70+
}
71+
return nil
72+
}
73+
74+
// bulkInsertLockedBalanceEntry inserts a batch of locked stake entries into the database.
75+
func bulkInsertLockedBalanceEntry(entries []*lib.StateChangeEntry, db *bun.DB, operationType lib.StateSyncerOperationType, params *lib.DeSoParams) error {
76+
// Track the unique entries we've inserted so we don't insert the same entry twice.
77+
uniqueEntries := consumer.UniqueEntries(entries)
78+
// Create a new array to hold the bun struct.
79+
pgEntrySlice := make([]*PGLockedBalanceEntry, len(uniqueEntries))
80+
81+
// Loop through the entries and convert them to PGEntry.
82+
for ii, entry := range uniqueEntries {
83+
pgEntrySlice[ii] = &PGLockedBalanceEntry{LockedBalanceEntry: LockedBalanceEntryEncoderToPGStruct(entry.Encoder.(*lib.LockedBalanceEntry), entry.KeyBytes, params)}
84+
}
85+
86+
// Execute the insert query.
87+
query := db.NewInsert().Model(&pgEntrySlice)
88+
89+
if operationType == lib.DbOperationTypeUpsert {
90+
query = query.On("CONFLICT (badger_key) DO UPDATE")
91+
}
92+
93+
if _, err := query.Returning("").Exec(context.Background()); err != nil {
94+
return errors.Wrapf(err, "entries.bulkInsertLockedBalanceEntry: Error inserting entries")
95+
}
96+
return nil
97+
}
98+
99+
// bulkDeleteLockedBalanceEntry deletes a batch of locked stake entries from the database.
100+
func bulkDeleteLockedBalanceEntry(entries []*lib.StateChangeEntry, db *bun.DB, operationType lib.StateSyncerOperationType) error {
101+
// Track the unique entries we've inserted so we don't insert the same entry twice.
102+
uniqueEntries := consumer.UniqueEntries(entries)
103+
104+
// Transform the entries into a list of keys to delete.
105+
keysToDelete := consumer.KeysToDelete(uniqueEntries)
106+
107+
// Execute the delete query.
108+
if _, err := db.NewDelete().
109+
Model(&PGLockedBalanceEntry{}).
110+
Where("badger_key IN (?)", bun.In(keysToDelete)).
111+
Returning("").
112+
Exec(context.Background()); err != nil {
113+
return errors.Wrapf(err, "entries.bulkDeleteLockedBalanceEntry: Error deleting entries")
114+
}
115+
116+
return nil
117+
}

0 commit comments

Comments
 (0)