Skip to content

Conversation

jkawan
Copy link
Contributor

@jkawan jkawan commented Jul 6, 2025

  1. Added function to generate initial pools
  2. Added changes to convert pools to cbor format
  3. Added function to return a specific pool by its ID
  4. Added unit tests for the above changes

Closes #1042

@jkawan jkawan requested a review from a team as a code owner July 6, 2025 04:30
}

// GetInitialPools returns all initial stake pools with their delegators
func (g *ShelleyGenesis) GetInitialPools() (map[string]GenesisPool, map[string][]string, error) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Drop the Get prefix on the function. It's not idiomatic Go

}

// GetPoolById returns a specific pool by its ID along with its delegators
func (g *ShelleyGenesis) GetPoolById(poolId string) (*GenesisPool, []string, error) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Drop the Get prefix on the function. It's not idiomatic Go

}

// GetInitialPools returns all initial stake pools with their delegators
func (g *ShelleyGenesis) GetInitialPools() (map[string]GenesisPool, map[string][]string, error) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should return native ledger types here like we do above for GenesisUtxos(). At minimum, we should use common.Address where appropriate.

}

// GetPoolById returns a specific pool by its ID along with its delegators
func (g *ShelleyGenesis) GetPoolById(poolId string) (*GenesisPool, []string, error) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should return []common.Address instead of []string

switch v := marginValue.(type) {
case float64:
p.Margin.Rat = new(big.Rat).SetFloat64(v)
case []interface{}:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Have you actually seen this in example JSON? If so, we should add support for decoding that format to GenesisRat.

if err != nil {
return fmt.Errorf("failed to decode reward account key hash: %w", err)
}
if len(hashBytes) != 28 {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use the AddressHashSize constant instead of a magic number

if len(tmp.RewardAccount) > 0 {
type credential struct {
KeyHash string `json:"key hash"`
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This type can be nested inline in the next one, since it's never used separately, or outside of this code block

return fmt.Errorf("invalid key hash length: expected 28, got %d", len(hashBytes))
}
var hash Blake2b224
copy(hash[:], hashBytes)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can use common.NewBlake2b224(hashBytes) here, which does the copy() for you.


switch v := marginValue.(type) {
case float64:
p.Margin.Rat = new(big.Rat).SetFloat64(v)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We want to avoid using a float to represent any of our ratios anywhere along the way, since a loss of precision could result in different outcomes from calculations using those values.

return nil, nil, errors.New("failed to decode stake key")
}

stakeAddrBytes := append([]byte{0xE1}, stakeKeyBytes...)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The 0xE1 here probably isn't correct. This represents a script address on mainnet, which is not going to be correct everywhere

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

have addressed the comments, could you please review again ?

return headers.Script, nil
}
return headers.Base, nil
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can replace all of this with something like:

newAddr := common.NewAddressFromParts(
  common.AddressTypeNoneKey,
  common.AddressNetworkMainnet,
  nil,
  stakeKeyHashBytes,
)

For a script stake address, use common.AddressTypeNoneScript as the first arg.

You will probably need to also modify NewAddressFromParts() to allow a zero-length payment address part to do this.

addrBytes = append(addrBytes, stakingAddr...)
}

return NewAddressFromBytes(addrBytes)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's no need to do the round-trip through byte. Use the method that we already had to build the new Address and return it.

}

if addrType == AddressTypeNoneScript && networkId == AddressNetworkTestnet {
header := byte(0xF1)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't want to hard-code magic values like this


header := addrType<<4 | networkId
addrBytes := append([]byte{header}, stakingAddr...)
return NewAddressFromBytes(addrBytes)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use the method that we already had for building a new Address rather than round-tripping it through bytes


if a.addressType == AddressTypeNoneScript && a.networkId == AddressNetworkTestnet {
header = (AddressTypeNoneScript << 4) | 1
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure what's happening here. This seems like a very specific circumstance that we could probably account for in other ways

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

eliminated this , it was for a test only.

@agaffney agaffney merged commit c90352f into main Aug 2, 2025
8 checks passed
@agaffney agaffney deleted the feat/shelley-genesis-pool branch August 2, 2025 15:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Create genesis pools from Shelley Genesis
3 participants