Skip to content

Commit b4053e5

Browse files
authored
feat(database): deregistration certificate (#663)
Signed-off-by: Chris Gianelloni <[email protected]>
1 parent 6659480 commit b4053e5

File tree

4 files changed

+61
-0
lines changed

4 files changed

+61
-0
lines changed

database/account.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,19 @@ func (d *Database) SetAccount(
7070
)
7171
}
7272

73+
// SetDeregistration saves a deregistration certificate
74+
func (d *Database) SetDeregistration(
75+
cert *lcommon.DeregistrationCertificate,
76+
slot uint64,
77+
txn *Txn,
78+
) error {
79+
return d.metadata.SetDeregistration(
80+
cert,
81+
slot,
82+
txn.Metadata(),
83+
)
84+
}
85+
7386
// SetRegistration saves a registration certificate
7487
func (d *Database) SetRegistration(
7588
cert *lcommon.RegistrationCertificate,

database/plugin/metadata/sqlite/account.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,40 @@ func (d *MetadataStoreSqlite) SetAccount(
6666
return nil
6767
}
6868

69+
// SetDeregistration saves a deregistration certificate
70+
func (d *MetadataStoreSqlite) SetDeregistration(
71+
cert *lcommon.DeregistrationCertificate,
72+
slot uint64,
73+
txn *gorm.DB,
74+
) error {
75+
stakeKey := cert.StakeCredential.Credential.Bytes()
76+
tmpAccount, err := d.GetAccount(stakeKey, txn)
77+
if err != nil {
78+
return err
79+
}
80+
tmpItem := models.Deregistration{
81+
StakingKey: stakeKey,
82+
AddedSlot: slot,
83+
}
84+
tmpAccount.Active = false
85+
if txn != nil {
86+
if accountErr := txn.Save(&tmpAccount); accountErr.Error != nil {
87+
return accountErr.Error
88+
}
89+
if result := txn.Create(&tmpItem); result.Error != nil {
90+
return result.Error
91+
}
92+
} else {
93+
if accountErr := d.DB().Save(&tmpAccount); accountErr.Error != nil {
94+
return accountErr.Error
95+
}
96+
if result := d.DB().Create(&tmpItem); result.Error != nil {
97+
return result.Error
98+
}
99+
}
100+
return nil
101+
}
102+
69103
// SetStakeRegistration saves a stake registration certificate and account
70104
func (d *MetadataStoreSqlite) SetStakeRegistration(
71105
cert *lcommon.StakeRegistrationCertificate,

database/plugin/metadata/store.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,11 @@ type MetadataStore interface {
7070
bool, // active
7171
*gorm.DB,
7272
) error
73+
SetDeregistration(
74+
*lcommon.DeregistrationCertificate,
75+
uint64, // slot
76+
*gorm.DB,
77+
) error
7378
SetEpoch(
7479
uint64, // slot
7580
uint64, // epoch

ledger/certs.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,15 @@ func (ls *LedgerState) processTransactionCertificates(
3737
return err
3838
}
3939
switch cert := tmpCert.(type) {
40+
case *lcommon.DeregistrationCertificate:
41+
err := ls.db.SetDeregistration(
42+
cert,
43+
blockPoint.Slot,
44+
txn,
45+
)
46+
if err != nil {
47+
return err
48+
}
4049
case *lcommon.PoolRegistrationCertificate:
4150
err := ls.db.SetPoolRegistration(
4251
cert,

0 commit comments

Comments
 (0)