Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 0 additions & 7 deletions entries/block.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ type BlockEntry struct {
ExtraNonce uint64
BlockVersion uint32
TxnConnectStatusByIndexHash string `pg:",use_zero"`
ProposerPublicKey string `pg:",use_zero"`
ProposerVotingPublicKey string `pg:",use_zero"`
ProposerRandomSeedSignature string `pg:",use_zero"`
ProposedInView uint64
Expand All @@ -45,11 +44,6 @@ func BlockEncoderToPGStruct(block *lib.MsgDeSoBlock, keyBytes []byte, params *li
if block.Header.TxnConnectStatusByIndexHash != nil {
txnConnectStatusByIndexHash = hex.EncodeToString(block.Header.TxnConnectStatusByIndexHash.ToBytes())
}
var proposerPublicKey string
if block.Header.ProposerPublicKey != nil {
proposerPublicKey = consumer.PublicKeyBytesToBase58Check(
block.Header.ProposerPublicKey.ToBytes(), params)
}
return &PGBlockEntry{
BlockEntry: BlockEntry{
BlockHash: hex.EncodeToString(blockHash[:]),
Expand All @@ -61,7 +55,6 @@ func BlockEncoderToPGStruct(block *lib.MsgDeSoBlock, keyBytes []byte, params *li
ExtraNonce: block.Header.ExtraNonce,
BlockVersion: block.Header.Version,
TxnConnectStatusByIndexHash: txnConnectStatusByIndexHash,
ProposerPublicKey: proposerPublicKey,
ProposerVotingPublicKey: block.Header.ProposerVotingPublicKey.ToString(),
ProposerRandomSeedSignature: block.Header.ProposerRandomSeedSignature.ToString(),
ProposedInView: block.Header.ProposedInView,
Expand Down
105 changes: 105 additions & 0 deletions entries/bls_pkid_pair.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
package entries

import (
"context"
"github.com/deso-protocol/core/lib"
"github.com/deso-protocol/state-consumer/consumer"
"github.com/pkg/errors"
"github.com/uptrace/bun"
)

// TODO: when to use nullzero vs use_zero?
type BLSPublicKeyPKIDPairEntry struct {
PKID string `bun:",nullzero"`
BLSPublicKey string `bun:",nullzero"`

BadgerKey []byte `pg:",pk,use_zero"`
}

type PGBLSPkidPairEntry struct {
bun.BaseModel `bun:"table:bls_public_key_pkid_pair_entry"`
BLSPublicKeyPKIDPairEntry
}

// Convert the BLSPublicKeyPKIDPairEntry DeSo encoder to the PGBLSPkidPairEntry struct used by bun.
func BLSPublicKeyPKIDPairEncoderToPGStruct(blsPublicKeyPKIDPairEntry *lib.BLSPublicKeyPKIDPairEntry, keyBytes []byte, params *lib.DeSoParams) BLSPublicKeyPKIDPairEntry {
pgBLSPkidPairEntry := BLSPublicKeyPKIDPairEntry{
BadgerKey: keyBytes,
}

if blsPublicKeyPKIDPairEntry.PKID != nil {
pgBLSPkidPairEntry.PKID = consumer.PublicKeyBytesToBase58Check((*blsPublicKeyPKIDPairEntry.PKID)[:], params)
}

if !blsPublicKeyPKIDPairEntry.BLSPublicKey.IsEmpty() {
pgBLSPkidPairEntry.BLSPublicKey = blsPublicKeyPKIDPairEntry.BLSPublicKey.ToString()
}

return pgBLSPkidPairEntry
}

// BLSPublicKeyPKIDPairBatchOperation is the entry point for processing a batch of BLSPublicKeyPKIDPair entries.
// It determines the appropriate handler based on the operation type and executes it.
func BLSPublicKeyPKIDPairBatchOperation(entries []*lib.StateChangeEntry, db *bun.DB, params *lib.DeSoParams) error {
// We check before we call this function that there is at least one operation type.
// We also ensure before this that all entries have the same operation type.
operationType := entries[0].OperationType
var err error
if operationType == lib.DbOperationTypeDelete {
err = bulkDeleteBLSPkidPairEntry(entries, db, operationType)
} else {
err = bulkInsertBLSPkidPairEntry(entries, db, operationType, params)
}
if err != nil {
return errors.Wrapf(err, "entries.StakeBatchOperation: Problem with operation type %v", operationType)
}
return nil
}

// bulkInsertBLSPkidPairEntry inserts a batch of stake entries into the database.
func bulkInsertBLSPkidPairEntry(
entries []*lib.StateChangeEntry, db *bun.DB, operationType lib.StateSyncerOperationType, params *lib.DeSoParams,
) error {
// Track the unique entries we've inserted so we don't insert the same entry twice.
uniqueEntries := consumer.UniqueEntries(entries)
// Create a new array to hold the bun struct.
pgEntrySlice := make([]*PGBLSPkidPairEntry, len(uniqueEntries))

// Loop through the entries and convert them to PGEntry.
for ii, entry := range uniqueEntries {
pgEntrySlice[ii] = &PGBLSPkidPairEntry{BLSPublicKeyPKIDPairEntry: BLSPublicKeyPKIDPairEncoderToPGStruct(
entry.Encoder.(*lib.BLSPublicKeyPKIDPairEntry), entry.KeyBytes, params)}
}

// Execute the insert query.
query := db.NewInsert().Model(&pgEntrySlice)

if operationType == lib.DbOperationTypeUpsert {
query = query.On("CONFLICT (badger_key) DO UPDATE")
}

if _, err := query.Returning("").Exec(context.Background()); err != nil {
return errors.Wrapf(err, "entries.bulkInsertBLSPkidPairEntry: Error inserting entries")
}
return nil
}

// bulkDeleteBLSPkidPairEntry deletes a batch of stake entries from the database.
func bulkDeleteBLSPkidPairEntry(entries []*lib.StateChangeEntry, db *bun.DB, operationType lib.StateSyncerOperationType) error {
// Track the unique entries we've inserted so we don't insert the same entry twice.
uniqueEntries := consumer.UniqueEntries(entries)

// Transform the entries into a list of keys to delete.
keysToDelete := consumer.KeysToDelete(uniqueEntries)

// Execute the delete query.
if _, err := db.NewDelete().
Model(&PGBLSPkidPairEntry{}).
Where("badger_key IN (?)", bun.In(keysToDelete)).
Returning("").
Exec(context.Background()); err != nil {
return errors.Wrapf(err, "entries.bulkDeleteBLSPkidPairEntry: Error deleting entries")
}

return nil
}
2 changes: 1 addition & 1 deletion entries/pkid.go
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ func bulkDeletePkid(entries []*lib.StateChangeEntry, db *bun.DB, operationType l

// Execute the delete query.
if _, err := db.NewDelete().
Model(&LeaderScheduleEntry{}).
Model(&PGLeaderScheduleEntry{}).
Where("badger_key IN (?)", bun.In(leaderSchedKeysToDelete)).
Returning("").
Exec(context.Background()); err != nil {
Expand Down
2 changes: 1 addition & 1 deletion entries/utxo_operation.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ func bulkInsertUtxoOperationsEntry(entries []*lib.StateChangeEntry, db *bun.DB,
blockEntries = append(blockEntries, blockEntry)
for ii, txn := range block.Txns {
// Check if the transaction connects or not.
txnConnects := blockEntry.Height < uint64(params.ForkHeights.ProofOfStake2ConsensusCutoverBlockHeight) ||
txnConnects := params.IsPoWBlockHeight(blockEntry.Height) ||
ii == 0 || block.TxnConnectStatusByIndex.Get(ii-1)
pgTxn, err := TransactionEncoderToPGStruct(
txn, uint64(ii), blockEntry.BlockHash, blockEntry.Height, blockEntry.Timestamp, txnConnects, params)
Expand Down
2 changes: 2 additions & 0 deletions handler/data_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,8 @@ func (postgresDataHandler *PostgresDataHandler) HandleEntryBatch(batchedEntries
err = entries.PkidBatchOperation(batchedEntries, postgresDataHandler.DB, postgresDataHandler.Params)
case lib.EncoderTypeGlobalParamsEntry:
err = entries.GlobalParamsBatchOperation(batchedEntries, postgresDataHandler.DB, postgresDataHandler.Params)
case lib.EncoderTypeBLSPublicKeyPKIDPairEntry:
err = entries.BLSPublicKeyPKIDPairBatchOperation(batchedEntries, postgresDataHandler.DB, postgresDataHandler.Params)
}

if err != nil {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ func updateBlockTableWithPoSFields(db *bun.DB, tableName string) error {
ALTER TABLE block
ADD COLUMN block_version BIGINT,
ADD COLUMN txn_connect_status_by_index_hash VARCHAR,
ADD COLUMN proposer_public_key VARCHAR,
ADD COLUMN proposer_voting_public_key VARCHAR,
ADD COLUMN proposer_random_seed_signature VARCHAR,
ADD COLUMN proposed_in_view BIGINT,
Expand All @@ -29,7 +28,6 @@ func init() {
ALTER TABLE block
DROP COLUMN block_version,
DROP COLUMN txn_connect_status_by_index_hash,
DROP COLUMN proposer_public_key,
DROP COLUMN proposer_voting_public_key,
DROP COLUMN proposer_random_seed_signature,
DROP COLUMN proposed_in_view,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package initial_migrations

import (
"context"
"strings"

"github.com/uptrace/bun"
)

// TODO: Not nullable fields
func createBLSPublicKeyPKIDPairEntryTable(db *bun.DB, tableName string) error {
_, err := db.Exec(strings.Replace(`
CREATE TABLE {tableName} (
pkid VARCHAR NOT NULL,
bls_public_key VARCHAR NOT NULL,

badger_key BYTEA PRIMARY KEY
);
CREATE INDEX {tableName}_pkid_idx ON {tableName} (pkid);
CREATE INDEX {tableName}_bls_public_key_idx ON {tableName} (bls_public_key);
`, "{tableName}", tableName, -1))
return err
}

func init() {
Migrations.MustRegister(func(ctx context.Context, db *bun.DB) error {
return createBLSPublicKeyPKIDPairEntryTable(db, "bls_public_key_pkid_pair_entry")
}, func(ctx context.Context, db *bun.DB) error {
_, err := db.Exec(`
DROP TABLE IF EXISTS bls_public_key_pkid_pair_entry;
`)
if err != nil {
return err
}
return nil
})
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ func init() {
comment on table locked_stake_entry is E'@foreignKey (staker_pkid) references account (pkid)|@foreignFieldName lockedStakeEntries|@fieldName staker\n@foreignKey (validator_pkid) references account (pkid)|@foreignFieldName validatorLockedStakeEntries|@fieldName validatorAccount\n@foreignKey (validator_pkid) references validator_entry(validator_pkid)|@foreignFieldName validatorLockedStakeEntries|@fieldName validatorEntry';
comment on table yield_curve_point is E'@foreignKey (profile_pkid) references account (pkid)|@foreignFieldName yieldCurvePoints|@fieldName account';
comment on table locked_balance_entry is E'@foreignKey (profile_pkid) references account (pkid)|@foreignFieldName profileLockedBalanceEntries|@fieldName profileAccount\n@foreignKey (hodler_pkid) references account (pkid)|@foreignFieldName hodlerLockedBalanceEntries|@fieldName hodlerAccount';
comment on table block is E'@unique block_hash\n@unique height\n@foreignKey (proposer_public_key) references account (public_key)|@foreignFieldName blocksProposed|@fieldName blockProposer';
comment on column stake_entry.badger_key is E'@omit';
comment on column validator_entry.badger_key is E'@omit';
comment on column locked_stake_entry.badger_key is E'@omit';
Expand Down