diff --git a/database/account.go b/database/account.go index e8a28238..669cb917 100644 --- a/database/account.go +++ b/database/account.go @@ -165,6 +165,19 @@ func (d *Database) SetStakeVoteRegistrationDelegation( ) } +// SetVoteDelegation saves a vote delegation certificate +func (d *Database) SetVoteDelegation( + cert *lcommon.VoteDelegationCertificate, + slot uint64, + txn *Txn, +) error { + return d.metadata.SetVoteDelegation( + cert, + slot, + txn.Metadata(), + ) +} + // SetVoteRegistrationDelegation saves a vote registration delegation certificate func (d *Database) SetVoteRegistrationDelegation( cert *lcommon.VoteRegistrationDelegationCertificate, diff --git a/database/plugin/metadata/sqlite/account.go b/database/plugin/metadata/sqlite/account.go index 0dd23116..92b98eb4 100644 --- a/database/plugin/metadata/sqlite/account.go +++ b/database/plugin/metadata/sqlite/account.go @@ -287,6 +287,45 @@ func (d *MetadataStoreSqlite) SetStakeVoteRegistrationDelegation( return nil } +// SetVoteDelegation saves a vote-only delegation certificate and updates the account. +func (d *MetadataStoreSqlite) SetVoteDelegation( + cert *lcommon.VoteDelegationCertificate, + slot uint64, + txn *gorm.DB, +) error { + stakeKey := cert.StakeCredential.Credential.Bytes() + tmpAccount, err := d.GetAccount(stakeKey, txn) + if err != nil { + return err + } + + tmpItem := models.VoteDelegation{ + StakingKey: stakeKey, + Drep: cert.Drep.Credential[:], + AddedSlot: slot, + } + + tmpAccount.Drep = tmpItem.Drep + + if txn != nil { + if accountErr := txn.Save(&tmpAccount); accountErr.Error != nil { + return accountErr.Error + } + if result := txn.Create(&tmpItem); result.Error != nil { + return result.Error + } + } else { + if accountErr := d.DB().Save(&tmpAccount); accountErr.Error != nil { + return accountErr.Error + } + if result := d.DB().Create(&tmpItem); result.Error != nil { + return result.Error + } + } + + return nil +} + // SetVoteRegistrationDelegation saves a vote registration delegation certificate and account func (d *MetadataStoreSqlite) SetVoteRegistrationDelegation( cert *lcommon.VoteRegistrationDelegationCertificate, diff --git a/database/plugin/metadata/sqlite/models/account.go b/database/plugin/metadata/sqlite/models/account.go index 629cb420..107a9f92 100644 --- a/database/plugin/metadata/sqlite/models/account.go +++ b/database/plugin/metadata/sqlite/models/account.go @@ -105,6 +105,17 @@ func (StakeVoteRegistrationDelegation) TableName() string { return "stake_vote_registration_delegation" } +type VoteDelegation struct { + ID uint `gorm:"primarykey"` + StakingKey []byte `gorm:"index"` + Drep []byte `gorm:"index"` + AddedSlot uint64 +} + +func (VoteDelegation) TableName() string { + return "vote_delegation" +} + type VoteRegistrationDelegation struct { ID uint `gorm:"primarykey"` StakingKey []byte `gorm:"index"` diff --git a/database/plugin/metadata/sqlite/models/vote_delegation.go b/database/plugin/metadata/sqlite/models/vote_delegation.go deleted file mode 100644 index 544ff1a6..00000000 --- a/database/plugin/metadata/sqlite/models/vote_delegation.go +++ /dev/null @@ -1,26 +0,0 @@ -// Copyright 2025 Blink Labs Software -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -package models - -type VoteDelegation struct { - ID uint `gorm:"primarykey"` - StakingKey []byte `gorm:"index"` - Drep []byte `gorm:"index"` - AddedSlot uint64 -} - -func (VoteDelegation) TableName() string { - return "vote_delegation" -} diff --git a/database/plugin/metadata/store.go b/database/plugin/metadata/store.go index a9eca990..9bdc08c0 100644 --- a/database/plugin/metadata/store.go +++ b/database/plugin/metadata/store.go @@ -172,6 +172,11 @@ type MetadataStore interface { []byte, // stake *gorm.DB, ) error + SetVoteDelegation( + *lcommon.VoteDelegationCertificate, + uint64, // slot + *gorm.DB, + ) error SetVoteRegistrationDelegation( *lcommon.VoteRegistrationDelegationCertificate, uint64, // slot diff --git a/ledger/certs.go b/ledger/certs.go index abc80d05..2bbeaa9c 100644 --- a/ledger/certs.go +++ b/ledger/certs.go @@ -144,6 +144,15 @@ func (ls *LedgerState) processTransactionCertificates( if err != nil { return err } + case *lcommon.VoteDelegationCertificate: + err := ls.db.SetVoteDelegation( + cert, + blockPoint.Slot, + txn, + ) + if err != nil { + return err + } case *lcommon.VoteRegistrationDelegationCertificate: err := ls.db.SetVoteRegistrationDelegation( cert,