Skip to content

Improve createSnowChain readability #4195

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 8 commits into
base: master
Choose a base branch
from
Open

Improve createSnowChain readability #4195

wants to merge 8 commits into from

Conversation

samliok
Copy link
Contributor

@samliok samliok commented Aug 21, 2025

Why this should be merged

Only stylistic changes to createSnowmanChain of the chain manager. createSnowmanChain initializes the VM, Handler and sets other components required to create a snowman chain. It was overly dense so I just separated it out into many helper functions to make it easier to digest. Very open to addition style suggestions, to make it even more understandable!

How this works

How this was tested

Need to be documented in RELEASES.md?

@Copilot Copilot AI review requested due to automatic review settings August 21, 2025 18:13
@samliok samliok marked this pull request as draft August 21, 2025 18:13
Copy link
Contributor

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR refactors the createSnowmanChain method in the chain manager to improve code readability by extracting its dense implementation into multiple focused helper functions. The refactoring breaks down the monolithic chain creation process into logical components like database creation, message sender setup, peer tracking, VM initialization, and engine creation.

Key Changes

  • Extracted database creation logic into createSnowmanDBs
  • Separated message sender initialization into createSnowmanMessageSender
  • Split peer tracking setup into createSnowmanTrackedPeers and createSnowmanPeerTracker
  • Isolated VM creation logic into createSnowmanVMs
  • Created dedicated engine setup in createSnowmanEngine
  • Added P-Chain state setup helper maybeSetupPChainState

Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.

// createSnowmanEngine creates a Snowman engine with the provided context, VM, and other parameters.
// It sets up the consensus engine, bootstrapper, and state syncer.
// It also handles setting up the P-Chain if necessary.
func (m *manager) createSnowmanEngine(ctx *snow.ConsensusContext, vm block.ChainVM, nonVerifyingParseFunc block.ParseFunc, sb subnets.Subnet, beacons validators.Manager, messageSender common.Sender, bootstrappingDB *prefixdb.Database, vdrs validators.Manager, connectedValidators tracker.Peers, peerTracker *p2p.PeerTracker, halter common.Halter) (*handler.Engine, error) {
Copy link
Preview

Copilot AI Aug 21, 2025

Choose a reason for hiding this comment

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

This function has 11 parameters, which makes it difficult to read and maintain. Consider grouping related parameters into a struct (e.g., EngineConfig) to improve readability and reduce the chance of parameter ordering errors.

Copilot uses AI. Check for mistakes.


func (m *manager) createSnowmanVMs(ctx *snow.ConsensusContext, vm block.ChainVM, db *prefixdb.Database, primaryAlias string, genesisData []byte, messageSender common.Sender, fxs []*common.Fx) (*block.ChangeNotifier, *proposervm.VM, error) {
Copy link
Preview

Copilot AI Aug 21, 2025

Choose a reason for hiding this comment

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

This function has 7 parameters, which is approaching the threshold for maintainability concerns. Consider grouping related parameters into a configuration struct to improve readability.

Copilot uses AI. Check for mistakes.

@samliok samliok marked this pull request as ready for review August 21, 2025 18:48
Comment on lines +1131 to +1142
func (m *manager) IsBootstrapped(id ids.ID) bool {
m.chainsLock.Lock()
chain, exists := m.chains[id]
m.chainsLock.Unlock()
if !exists {
return false
}

return chain.Context().State.Get().State == snow.NormalOp
}

func (m *manager) registerBootstrappedHealthChecks() error {
Copy link
Contributor

@joshua-kim joshua-kim Aug 22, 2025

Choose a reason for hiding this comment

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

This diff contains untouched but moved code hunks - which makes this diff harder to review. I'm open to us having helper functions but these code moves look unrelated to me - could we unstage these?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: No status
Development

Successfully merging this pull request may close these issues.

2 participants