Skip to content

Commit c874975

Browse files
author
Jenita
committed
feat: created genesis pools from shelly genesis
Signed-off-by: Jenita <[email protected]>
1 parent e5340d3 commit c874975

File tree

2 files changed

+76
-28
lines changed

2 files changed

+76
-28
lines changed

ledger/common/certs.go

Lines changed: 76 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -392,51 +392,100 @@ type PoolRegistrationCertificate struct {
392392

393393
func (p *PoolRegistrationCertificate) UnmarshalJSON(data []byte) error {
394394
type Alias PoolRegistrationCertificate
395+
396+
// Temporary struct for initial unmarshaling
395397
aux := &struct {
396-
Margin interface{} `json:"margin"`
397-
RewardAccount struct {
398-
Credential struct {
399-
KeyHash string `json:"key hash"`
400-
} `json:"credential"`
401-
Network string `json:"network"`
402-
} `json:"rewardAccount"`
398+
Operator string `json:"operator"`
399+
VrfKeyHash string `json:"vrfKeyHash"`
400+
Pledge uint64 `json:"pledge"`
401+
Cost uint64 `json:"cost"`
402+
Margin json.RawMessage `json:"margin"`
403+
RewardAccount json.RawMessage `json:"rewardAccount"`
404+
PoolOwners []string `json:"poolOwners"`
405+
Relays []PoolRelay `json:"relays"`
406+
PoolMetadata *PoolMetadata `json:"poolMetadata,omitempty"`
403407
*Alias
404408
}{
405409
Alias: (*Alias)(p),
406410
}
407411

408-
if err := json.Unmarshal(data, &aux); err != nil {
409-
return err
412+
if err := json.Unmarshal(data, aux); err != nil {
413+
return fmt.Errorf("failed to unmarshal pool registration: %w", err)
410414
}
411415

416+
p.Cost = aux.Cost
417+
412418
// Handle margin field
413-
switch v := aux.Margin.(type) {
414-
case float64:
415-
p.Margin.Rat = new(big.Rat).SetFloat64(v)
416-
case []interface{}:
417-
if len(v) == 2 {
418-
if num, ok := v[0].(float64); ok {
419-
if den, ok := v[1].(float64); ok {
420-
p.Margin.Rat = new(big.Rat).SetFrac64(int64(num), int64(den))
419+
if len(aux.Margin) > 0 {
420+
var marginValue interface{}
421+
if err := json.Unmarshal(aux.Margin, &marginValue); err != nil {
422+
return fmt.Errorf("failed to unmarshal margin: %w", err)
423+
}
424+
425+
switch v := marginValue.(type) {
426+
case float64:
427+
p.Margin.Rat = new(big.Rat).SetFloat64(v)
428+
case []interface{}:
429+
if len(v) == 2 {
430+
if num, ok := v[0].(float64); ok {
431+
if den, ok := v[1].(float64); ok && den != 0 {
432+
p.Margin.Rat = new(big.Rat).SetFrac64(int64(num), int64(den))
433+
}
421434
}
422435
}
423436
}
424-
default:
425-
p.Margin.Rat = new(big.Rat).SetInt64(0)
426437
}
427438

428439
// Handle reward account
429-
if aux.RewardAccount.Credential.KeyHash != "" {
430-
hashBytes, err := hex.DecodeString(aux.RewardAccount.Credential.KeyHash)
440+
if len(aux.RewardAccount) > 0 {
441+
var rewardAccount struct {
442+
Credential struct {
443+
KeyHash string `json:"key hash"`
444+
} `json:"credential"`
445+
}
446+
if err := json.Unmarshal(aux.RewardAccount, &rewardAccount); err != nil {
447+
return fmt.Errorf("failed to unmarshal reward account: %w", err)
448+
}
449+
450+
if rewardAccount.Credential.KeyHash != "" {
451+
hashBytes, err := hex.DecodeString(rewardAccount.Credential.KeyHash)
452+
if err != nil {
453+
return fmt.Errorf("failed to decode reward account key hash: %w", err)
454+
}
455+
var hash Blake2b224
456+
copy(hash[:], hashBytes)
457+
p.RewardAccount = AddrKeyHash(hash)
458+
}
459+
}
460+
461+
// Convert string fields to binary types
462+
if aux.Operator != "" {
463+
opBytes, err := hex.DecodeString(aux.Operator)
431464
if err != nil {
432-
return fmt.Errorf("failed to decode reward account key hash: %w", err)
465+
return fmt.Errorf("invalid operator key: %w", err)
433466
}
434-
if len(hashBytes) != 28 {
435-
return fmt.Errorf("invalid key hash length: expected 28, got %d", len(hashBytes))
467+
p.Operator = PoolKeyHash(Blake2b224(opBytes))
468+
}
469+
470+
if aux.VrfKeyHash != "" {
471+
vrfBytes, err := hex.DecodeString(aux.VrfKeyHash)
472+
if err != nil {
473+
return fmt.Errorf("invalid VRF key hash: %w", err)
474+
}
475+
p.VrfKeyHash = VrfKeyHash(Blake2b256(vrfBytes))
476+
}
477+
478+
// Convert pool owners
479+
if len(aux.PoolOwners) > 0 {
480+
owners := make([]AddrKeyHash, len(aux.PoolOwners))
481+
for i, owner := range aux.PoolOwners {
482+
ownerBytes, err := hex.DecodeString(owner)
483+
if err != nil {
484+
return fmt.Errorf("invalid pool owner key: %w", err)
485+
}
486+
owners[i] = AddrKeyHash(Blake2b224(ownerBytes))
436487
}
437-
var hash Blake2b224
438-
copy(hash[:], hashBytes)
439-
p.RewardAccount = hash
488+
p.PoolOwners = owners
440489
}
441490

442491
return nil

ledger/shelley/genesis_test.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
package shelley_test
1616

1717
import (
18-
//"bytes"
1918
"encoding/hex"
2019
"encoding/json"
2120
"math/big"

0 commit comments

Comments
 (0)