Skip to content

Commit dabfa2a

Browse files
authored
feat(database): track stake pools (#646)
Signed-off-by: Chris Gianelloni <[email protected]>
1 parent 28fc0a2 commit dabfa2a

File tree

5 files changed

+271
-188
lines changed

5 files changed

+271
-188
lines changed

database/plugin/metadata/sqlite/certs.go

Lines changed: 0 additions & 161 deletions
Original file line numberDiff line numberDiff line change
@@ -16,97 +16,10 @@ package sqlite
1616

1717
import (
1818
"github.com/blinklabs-io/dingo/database/plugin/metadata/sqlite/models"
19-
"github.com/blinklabs-io/dingo/database/types"
20-
"github.com/blinklabs-io/gouroboros/cbor"
2119
lcommon "github.com/blinklabs-io/gouroboros/ledger/common"
2220
"gorm.io/gorm"
2321
)
2422

25-
// GetPoolRegistrations returns pool registration certificates
26-
func (d *MetadataStoreSqlite) GetPoolRegistrations(
27-
pkh lcommon.PoolKeyHash,
28-
txn *gorm.DB,
29-
) ([]lcommon.PoolRegistrationCertificate, error) {
30-
ret := []lcommon.PoolRegistrationCertificate{}
31-
certs := []models.PoolRegistration{}
32-
if txn != nil {
33-
result := txn.Where("pool_key_hash = ?", lcommon.Blake2b224(pkh).Bytes()).
34-
Order("id DESC").
35-
Find(&certs)
36-
if result.Error != nil {
37-
return ret, result.Error
38-
}
39-
} else {
40-
result := d.DB().Where("pool_key_hash = ?", lcommon.Blake2b224(pkh).Bytes()).
41-
Order("id DESC").
42-
Find(&certs)
43-
if result.Error != nil {
44-
return ret, result.Error
45-
}
46-
}
47-
var addrKeyHash lcommon.AddrKeyHash
48-
var tmpCert lcommon.PoolRegistrationCertificate
49-
var tmpMargin cbor.Rat
50-
var tmpRelay lcommon.PoolRelay
51-
for _, cert := range certs {
52-
tmpMargin = cbor.Rat{Rat: cert.Margin.Rat}
53-
tmpCert = lcommon.PoolRegistrationCertificate{
54-
CertType: lcommon.CertificateTypePoolRegistration,
55-
Operator: lcommon.PoolKeyHash(
56-
lcommon.NewBlake2b224(cert.PoolKeyHash),
57-
),
58-
VrfKeyHash: lcommon.VrfKeyHash(
59-
lcommon.NewBlake2b256(cert.VrfKeyHash),
60-
),
61-
Pledge: uint64(cert.Pledge),
62-
Cost: uint64(cert.Cost),
63-
Margin: tmpMargin,
64-
RewardAccount: lcommon.AddrKeyHash(
65-
lcommon.NewBlake2b224(cert.RewardAccount),
66-
),
67-
}
68-
for _, owner := range cert.Owners {
69-
addrKeyHash = lcommon.AddrKeyHash(
70-
lcommon.NewBlake2b224(owner.KeyHash),
71-
)
72-
tmpCert.PoolOwners = append(tmpCert.PoolOwners, addrKeyHash)
73-
}
74-
for _, relay := range cert.Relays {
75-
tmpRelay = lcommon.PoolRelay{}
76-
// Determine type
77-
if relay.Port != 0 {
78-
port := uint32(relay.Port) // #nosec G115
79-
tmpRelay.Port = &port
80-
if relay.Hostname != "" {
81-
hostname := relay.Hostname
82-
tmpRelay.Type = lcommon.PoolRelayTypeSingleHostName
83-
tmpRelay.Hostname = &hostname
84-
} else {
85-
tmpRelay.Type = lcommon.PoolRelayTypeSingleHostAddress
86-
tmpRelay.Ipv4 = relay.Ipv4
87-
tmpRelay.Ipv6 = relay.Ipv6
88-
}
89-
} else {
90-
hostname := relay.Hostname
91-
tmpRelay.Type = lcommon.PoolRelayTypeMultiHostName
92-
tmpRelay.Hostname = &hostname
93-
}
94-
tmpCert.Relays = append(tmpCert.Relays, tmpRelay)
95-
}
96-
if cert.MetadataUrl != "" {
97-
poolMetadata := &lcommon.PoolMetadata{
98-
Url: cert.MetadataUrl,
99-
Hash: lcommon.PoolMetadataHash(
100-
lcommon.NewBlake2b256(cert.MetadataHash),
101-
),
102-
}
103-
tmpCert.PoolMetadata = poolMetadata
104-
}
105-
ret = append(ret, tmpCert)
106-
}
107-
return ret, nil
108-
}
109-
11023
// GetStakeRegistrations returns stake registration certificates
11124
func (d *MetadataStoreSqlite) GetStakeRegistrations(
11225
stakingKey []byte,
@@ -144,80 +57,6 @@ func (d *MetadataStoreSqlite) GetStakeRegistrations(
14457
return ret, nil
14558
}
14659

147-
// SetPoolRegistration saves a pool registration certificate
148-
func (d *MetadataStoreSqlite) SetPoolRegistration(
149-
cert *lcommon.PoolRegistrationCertificate,
150-
slot, deposit uint64,
151-
txn *gorm.DB,
152-
) error {
153-
tmpItem := models.PoolRegistration{
154-
PoolKeyHash: cert.Operator[:],
155-
VrfKeyHash: cert.VrfKeyHash[:],
156-
Pledge: types.Uint64(cert.Pledge),
157-
Cost: types.Uint64(cert.Cost),
158-
Margin: &types.Rat{Rat: cert.Margin.Rat},
159-
AddedSlot: slot,
160-
DepositAmount: deposit,
161-
}
162-
if cert.PoolMetadata != nil {
163-
tmpItem.MetadataUrl = cert.PoolMetadata.Url
164-
tmpItem.MetadataHash = cert.PoolMetadata.Hash[:]
165-
}
166-
for _, owner := range cert.PoolOwners {
167-
tmpItem.Owners = append(
168-
tmpItem.Owners,
169-
models.PoolRegistrationOwner{KeyHash: owner[:]},
170-
)
171-
}
172-
var tmpRelay models.PoolRegistrationRelay
173-
for _, relay := range cert.Relays {
174-
tmpRelay = models.PoolRegistrationRelay{
175-
Ipv4: relay.Ipv4,
176-
Ipv6: relay.Ipv6,
177-
}
178-
if relay.Port != nil {
179-
tmpRelay.Port = uint(*relay.Port)
180-
}
181-
if relay.Hostname != nil {
182-
tmpRelay.Hostname = *relay.Hostname
183-
}
184-
tmpItem.Relays = append(tmpItem.Relays, tmpRelay)
185-
}
186-
if txn != nil {
187-
if result := txn.Create(&tmpItem); result.Error != nil {
188-
return result.Error
189-
}
190-
} else {
191-
if result := d.DB().Create(&tmpItem); result.Error != nil {
192-
return result.Error
193-
}
194-
}
195-
return nil
196-
}
197-
198-
// SetPoolRetirement saves a pool retirement certificate
199-
func (d *MetadataStoreSqlite) SetPoolRetirement(
200-
cert *lcommon.PoolRetirementCertificate,
201-
slot uint64,
202-
txn *gorm.DB,
203-
) error {
204-
tmpItem := models.PoolRetirement{
205-
PoolKeyHash: cert.PoolKeyHash[:],
206-
Epoch: cert.Epoch,
207-
AddedSlot: slot,
208-
}
209-
if txn != nil {
210-
if result := txn.Create(&tmpItem); result.Error != nil {
211-
return result.Error
212-
}
213-
} else {
214-
if result := d.DB().Create(&tmpItem); result.Error != nil {
215-
return result.Error
216-
}
217-
}
218-
return nil
219-
}
220-
22160
// SetStakeDelegation saves a stake delegation certificate
22261
func (d *MetadataStoreSqlite) SetStakeDelegation(
22362
cert *lcommon.StakeDelegationCertificate,

database/plugin/metadata/sqlite/models/models.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ var MigrateModels = []any{
2121
&Deregistration{},
2222
&Drep{},
2323
&Epoch{},
24+
&Pool{},
2425
&PoolRegistration{},
2526
&PoolRegistrationOwner{},
2627
&PoolRegistrationRelay{},

database/plugin/metadata/sqlite/models/pool_registration.go renamed to database/plugin/metadata/sqlite/models/pool.go

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import (
2020
"github.com/blinklabs-io/dingo/database/types"
2121
)
2222

23-
type PoolRegistration struct {
23+
type Pool struct {
2424
ID uint `gorm:"primarykey"`
2525
PoolKeyHash []byte `gorm:"index"`
2626
VrfKeyHash []byte
@@ -30,6 +30,25 @@ type PoolRegistration struct {
3030
RewardAccount []byte
3131
Owners []PoolRegistrationOwner
3232
Relays []PoolRegistrationRelay
33+
Registration []PoolRegistration
34+
Retirement []PoolRetirement
35+
}
36+
37+
func (p *Pool) TableName() string {
38+
return "pool"
39+
}
40+
41+
type PoolRegistration struct {
42+
ID uint `gorm:"primarykey"`
43+
PoolID uint
44+
PoolKeyHash []byte `gorm:"index"`
45+
VrfKeyHash []byte
46+
Pledge types.Uint64
47+
Cost types.Uint64
48+
Margin *types.Rat
49+
RewardAccount []byte
50+
Owners []PoolRegistrationOwner
51+
Relays []PoolRegistrationRelay
3352
MetadataUrl string
3453
MetadataHash []byte
3554
AddedSlot uint64
@@ -43,6 +62,7 @@ func (PoolRegistration) TableName() string {
4362
type PoolRegistrationOwner struct {
4463
ID uint `gorm:"primarykey"`
4564
PoolRegistrationID uint
65+
PoolID uint
4666
KeyHash []byte
4767
}
4868

@@ -53,6 +73,7 @@ func (PoolRegistrationOwner) TableName() string {
5373
type PoolRegistrationRelay struct {
5474
ID uint `gorm:"primarykey"`
5575
PoolRegistrationID uint
76+
PoolID uint
5677
Port uint
5778
Ipv4 *net.IP
5879
Ipv6 *net.IP
@@ -62,3 +83,15 @@ type PoolRegistrationRelay struct {
6283
func (PoolRegistrationRelay) TableName() string {
6384
return "pool_registration_relay"
6485
}
86+
87+
type PoolRetirement struct {
88+
ID uint `gorm:"primarykey"`
89+
PoolID uint
90+
PoolKeyHash []byte `gorm:"index"`
91+
Epoch uint64
92+
AddedSlot uint64
93+
}
94+
95+
func (PoolRetirement) TableName() string {
96+
return "pool_retirement"
97+
}

database/plugin/metadata/sqlite/models/pool_retirement.go

Lines changed: 0 additions & 26 deletions
This file was deleted.

0 commit comments

Comments
 (0)