Skip to content

feat(shelley): created genesis pools from shelley genesis #1054

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
Open
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
159 changes: 142 additions & 17 deletions ledger/common/certs.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,11 @@
package common

import (
"encoding/hex"
"encoding/json"
"errors"
"fmt"
"math/big"
"net"

"github.com/blinklabs-io/gouroboros/cbor"
Expand Down Expand Up @@ -299,11 +302,11 @@
)

type PoolRelay struct {
Type int
Port *uint32
Ipv4 *net.IP
Ipv6 *net.IP
Hostname *string
Type int `json:"type"`
Port *uint32 `json:"port,omitempty"`
Ipv4 *net.IP `json:"ipv4,omitempty"`
Ipv6 *net.IP `json:"ipv6,omitempty"`
Hostname *string `json:"hostname,omitempty"`
}

func (p *PoolRelay) UnmarshalCBOR(data []byte) error {
Expand Down Expand Up @@ -373,18 +376,140 @@
}

type PoolRegistrationCertificate struct {
cbor.StructAsArray
cbor.DecodeStoreCbor
CertType uint
Operator PoolKeyHash
VrfKeyHash VrfKeyHash
Pledge uint64
Cost uint64
Margin cbor.Rat
RewardAccount AddrKeyHash
PoolOwners []AddrKeyHash
Relays []PoolRelay
PoolMetadata *PoolMetadata
cbor.StructAsArray `json:"-"`
cbor.DecodeStoreCbor `json:"-"`
CertType uint `json:"certType,omitempty"`
Operator PoolKeyHash `json:"operator"`
VrfKeyHash VrfKeyHash `json:"vrfKeyHash"`
Pledge uint64 `json:"pledge"`
Cost uint64 `json:"cost"`
Margin cbor.Rat `json:"margin"`
RewardAccount AddrKeyHash `json:"rewardAccount"`
PoolOwners []AddrKeyHash `json:"poolOwners"`
Relays []PoolRelay `json:"relays"`
PoolMetadata *PoolMetadata `json:"poolMetadata,omitempty"`
}

func (p *PoolRegistrationCertificate) UnmarshalJSON(data []byte) error {
//nolint:musttag
type tempPool struct {
Operator string `json:"operator"`
VrfKeyHash string `json:"vrfKeyHash"`
Pledge uint64 `json:"pledge"`
Cost uint64 `json:"cost"`
Margin json.RawMessage `json:"margin"`
RewardAccount json.RawMessage `json:"rewardAccount"`
PoolOwners []string `json:"poolOwners"`
Relays []struct {
Type int `json:"type"`
Port *uint32 `json:"port,omitempty"`
Ipv4 *net.IP `json:"ipv4,omitempty"`
Ipv6 *net.IP `json:"ipv6,omitempty"`
Hostname *string `json:"hostname,omitempty"`
} `json:"relays"`

PoolMetadata *PoolMetadata `json:"poolMetadata,omitempty"`
}

var tmp tempPool
if err := json.Unmarshal(data, &tmp); err != nil {

Check failure on line 415 in ledger/common/certs.go

View workflow job for this annotation

GitHub Actions / lint

the given struct should be annotated with the `json` tag (musttag)
return fmt.Errorf("failed to unmarshal pool registration: %w", err)
}

p.Pledge = tmp.Pledge
p.Cost = tmp.Cost
p.Relays = make([]PoolRelay, len(tmp.Relays))
for i, relay := range tmp.Relays {
p.Relays[i] = PoolRelay{
Type: relay.Type,
Port: relay.Port,
Ipv4: relay.Ipv4,
Ipv6: relay.Ipv6,
Hostname: relay.Hostname,
}
}
p.PoolMetadata = tmp.PoolMetadata

// Handle margin field
if len(tmp.Margin) > 0 {
var marginValue interface{}
if err := json.Unmarshal(tmp.Margin, &marginValue); err != nil {
return fmt.Errorf("failed to unmarshal margin: %w", err)
}

switch v := marginValue.(type) {
case float64:
p.Margin.Rat = new(big.Rat).SetFloat64(v)
case []interface{}:
if len(v) == 2 {
if num, ok := v[0].(float64); ok {
if den, ok := v[1].(float64); ok && den != 0 {
p.Margin.Rat = new(big.Rat).SetFrac64(int64(num), int64(den))
}
}
}
}
}

// Handle reward account
if len(tmp.RewardAccount) > 0 {
type credential struct {
KeyHash string `json:"key hash"`
}
type rewardAccount struct {
Credential credential `json:"credential"`
}

var ra rewardAccount
if err := json.Unmarshal(tmp.RewardAccount, &ra); err != nil {
return fmt.Errorf("failed to unmarshal reward account: %w", err)
}

if ra.Credential.KeyHash != "" {
hashBytes, err := hex.DecodeString(ra.Credential.KeyHash)
if err != nil {
return fmt.Errorf("failed to decode reward account key hash: %w", err)
}
if len(hashBytes) != 28 {
return fmt.Errorf("invalid key hash length: expected 28, got %d", len(hashBytes))
}
var hash Blake2b224
copy(hash[:], hashBytes)
p.RewardAccount = AddrKeyHash(hash)
}
}

// Convert string fields to binary types
if tmp.Operator != "" {
opBytes, err := hex.DecodeString(tmp.Operator)
if err != nil {
return fmt.Errorf("invalid operator key: %w", err)
}
p.Operator = PoolKeyHash(Blake2b224(opBytes))
}

if tmp.VrfKeyHash != "" {
vrfBytes, err := hex.DecodeString(tmp.VrfKeyHash)
if err != nil {
return fmt.Errorf("invalid VRF key hash: %w", err)
}
p.VrfKeyHash = VrfKeyHash(Blake2b256(vrfBytes))
}

// Convert pool owners
if len(tmp.PoolOwners) > 0 {
owners := make([]AddrKeyHash, len(tmp.PoolOwners))
for i, owner := range tmp.PoolOwners {
ownerBytes, err := hex.DecodeString(owner)
if err != nil {
return fmt.Errorf("invalid pool owner key: %w", err)
}
owners[i] = AddrKeyHash(Blake2b224(ownerBytes))
}
p.PoolOwners = owners
}

return nil
}

func (c PoolRegistrationCertificate) isCertificate() {}
Expand Down
Loading
Loading