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
161 changes: 0 additions & 161 deletions database/plugin/metadata/sqlite/certs.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,97 +16,10 @@ package sqlite

import (
"github.com/blinklabs-io/dingo/database/plugin/metadata/sqlite/models"
"github.com/blinklabs-io/dingo/database/types"
"github.com/blinklabs-io/gouroboros/cbor"
lcommon "github.com/blinklabs-io/gouroboros/ledger/common"
"gorm.io/gorm"
)

// GetPoolRegistrations returns pool registration certificates
func (d *MetadataStoreSqlite) GetPoolRegistrations(
pkh lcommon.PoolKeyHash,
txn *gorm.DB,
) ([]lcommon.PoolRegistrationCertificate, error) {
ret := []lcommon.PoolRegistrationCertificate{}
certs := []models.PoolRegistration{}
if txn != nil {
result := txn.Where("pool_key_hash = ?", lcommon.Blake2b224(pkh).Bytes()).
Order("id DESC").
Find(&certs)
if result.Error != nil {
return ret, result.Error
}
} else {
result := d.DB().Where("pool_key_hash = ?", lcommon.Blake2b224(pkh).Bytes()).
Order("id DESC").
Find(&certs)
if result.Error != nil {
return ret, result.Error
}
}
var addrKeyHash lcommon.AddrKeyHash
var tmpCert lcommon.PoolRegistrationCertificate
var tmpMargin cbor.Rat
var tmpRelay lcommon.PoolRelay
for _, cert := range certs {
tmpMargin = cbor.Rat{Rat: cert.Margin.Rat}
tmpCert = lcommon.PoolRegistrationCertificate{
CertType: lcommon.CertificateTypePoolRegistration,
Operator: lcommon.PoolKeyHash(
lcommon.NewBlake2b224(cert.PoolKeyHash),
),
VrfKeyHash: lcommon.VrfKeyHash(
lcommon.NewBlake2b256(cert.VrfKeyHash),
),
Pledge: uint64(cert.Pledge),
Cost: uint64(cert.Cost),
Margin: tmpMargin,
RewardAccount: lcommon.AddrKeyHash(
lcommon.NewBlake2b224(cert.RewardAccount),
),
}
for _, owner := range cert.Owners {
addrKeyHash = lcommon.AddrKeyHash(
lcommon.NewBlake2b224(owner.KeyHash),
)
tmpCert.PoolOwners = append(tmpCert.PoolOwners, addrKeyHash)
}
for _, relay := range cert.Relays {
tmpRelay = lcommon.PoolRelay{}
// Determine type
if relay.Port != 0 {
port := uint32(relay.Port) // #nosec G115
tmpRelay.Port = &port
if relay.Hostname != "" {
hostname := relay.Hostname
tmpRelay.Type = lcommon.PoolRelayTypeSingleHostName
tmpRelay.Hostname = &hostname
} else {
tmpRelay.Type = lcommon.PoolRelayTypeSingleHostAddress
tmpRelay.Ipv4 = relay.Ipv4
tmpRelay.Ipv6 = relay.Ipv6
}
} else {
hostname := relay.Hostname
tmpRelay.Type = lcommon.PoolRelayTypeMultiHostName
tmpRelay.Hostname = &hostname
}
tmpCert.Relays = append(tmpCert.Relays, tmpRelay)
}
if cert.MetadataUrl != "" {
poolMetadata := &lcommon.PoolMetadata{
Url: cert.MetadataUrl,
Hash: lcommon.PoolMetadataHash(
lcommon.NewBlake2b256(cert.MetadataHash),
),
}
tmpCert.PoolMetadata = poolMetadata
}
ret = append(ret, tmpCert)
}
return ret, nil
}

// GetStakeRegistrations returns stake registration certificates
func (d *MetadataStoreSqlite) GetStakeRegistrations(
stakingKey []byte,
Expand Down Expand Up @@ -144,80 +57,6 @@ func (d *MetadataStoreSqlite) GetStakeRegistrations(
return ret, nil
}

// SetPoolRegistration saves a pool registration certificate
func (d *MetadataStoreSqlite) SetPoolRegistration(
cert *lcommon.PoolRegistrationCertificate,
slot, deposit uint64,
txn *gorm.DB,
) error {
tmpItem := models.PoolRegistration{
PoolKeyHash: cert.Operator[:],
VrfKeyHash: cert.VrfKeyHash[:],
Pledge: types.Uint64(cert.Pledge),
Cost: types.Uint64(cert.Cost),
Margin: &types.Rat{Rat: cert.Margin.Rat},
AddedSlot: slot,
DepositAmount: deposit,
}
if cert.PoolMetadata != nil {
tmpItem.MetadataUrl = cert.PoolMetadata.Url
tmpItem.MetadataHash = cert.PoolMetadata.Hash[:]
}
for _, owner := range cert.PoolOwners {
tmpItem.Owners = append(
tmpItem.Owners,
models.PoolRegistrationOwner{KeyHash: owner[:]},
)
}
var tmpRelay models.PoolRegistrationRelay
for _, relay := range cert.Relays {
tmpRelay = models.PoolRegistrationRelay{
Ipv4: relay.Ipv4,
Ipv6: relay.Ipv6,
}
if relay.Port != nil {
tmpRelay.Port = uint(*relay.Port)
}
if relay.Hostname != nil {
tmpRelay.Hostname = *relay.Hostname
}
tmpItem.Relays = append(tmpItem.Relays, tmpRelay)
}
if txn != nil {
if result := txn.Create(&tmpItem); result.Error != nil {
return result.Error
}
} else {
if result := d.DB().Create(&tmpItem); result.Error != nil {
return result.Error
}
}
return nil
}

// SetPoolRetirement saves a pool retirement certificate
func (d *MetadataStoreSqlite) SetPoolRetirement(
cert *lcommon.PoolRetirementCertificate,
slot uint64,
txn *gorm.DB,
) error {
tmpItem := models.PoolRetirement{
PoolKeyHash: cert.PoolKeyHash[:],
Epoch: cert.Epoch,
AddedSlot: slot,
}
if txn != nil {
if result := txn.Create(&tmpItem); result.Error != nil {
return result.Error
}
} else {
if result := d.DB().Create(&tmpItem); result.Error != nil {
return result.Error
}
}
return nil
}

// SetStakeDelegation saves a stake delegation certificate
func (d *MetadataStoreSqlite) SetStakeDelegation(
cert *lcommon.StakeDelegationCertificate,
Expand Down
1 change: 1 addition & 0 deletions database/plugin/metadata/sqlite/models/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ var MigrateModels = []any{
&Deregistration{},
&Drep{},
&Epoch{},
&Pool{},
&PoolRegistration{},
&PoolRegistrationOwner{},
&PoolRegistrationRelay{},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import (
"github.com/blinklabs-io/dingo/database/types"
)

type PoolRegistration struct {
type Pool struct {
ID uint `gorm:"primarykey"`
PoolKeyHash []byte `gorm:"index"`
VrfKeyHash []byte
Expand All @@ -30,6 +30,25 @@ type PoolRegistration struct {
RewardAccount []byte
Owners []PoolRegistrationOwner
Relays []PoolRegistrationRelay
Registration []PoolRegistration
Retirement []PoolRetirement
}

func (p *Pool) TableName() string {
return "pool"
}

type PoolRegistration struct {
ID uint `gorm:"primarykey"`
PoolID uint
PoolKeyHash []byte `gorm:"index"`
VrfKeyHash []byte
Pledge types.Uint64
Cost types.Uint64
Margin *types.Rat
RewardAccount []byte
Owners []PoolRegistrationOwner
Relays []PoolRegistrationRelay
MetadataUrl string
MetadataHash []byte
AddedSlot uint64
Expand All @@ -43,6 +62,7 @@ func (PoolRegistration) TableName() string {
type PoolRegistrationOwner struct {
ID uint `gorm:"primarykey"`
PoolRegistrationID uint
PoolID uint
KeyHash []byte
}

Expand All @@ -53,6 +73,7 @@ func (PoolRegistrationOwner) TableName() string {
type PoolRegistrationRelay struct {
ID uint `gorm:"primarykey"`
PoolRegistrationID uint
PoolID uint
Port uint
Ipv4 *net.IP
Ipv6 *net.IP
Expand All @@ -62,3 +83,15 @@ type PoolRegistrationRelay struct {
func (PoolRegistrationRelay) TableName() string {
return "pool_registration_relay"
}

type PoolRetirement struct {
ID uint `gorm:"primarykey"`
PoolID uint
PoolKeyHash []byte `gorm:"index"`
Epoch uint64
AddedSlot uint64
}

func (PoolRetirement) TableName() string {
return "pool_retirement"
}
26 changes: 0 additions & 26 deletions database/plugin/metadata/sqlite/models/pool_retirement.go

This file was deleted.

Loading
Loading