Skip to content

[refactor] Reduce Complexity in Upgrade Resumable, Provision Pipeline, and Server Init #103

@kangeunchan

Description

@kangeunchan

Enhancement Description

Refactor key application/daemon hotspots into testable state-machine/pipeline/builder patterns to reduce complexity and improve reliability.

Background

Several core functions mix control flow with business logic and initialization concerns, with high complexity and low/zero test coverage. Refactoring them into explicit patterns (table-driven state machine, pipeline steps, builder) improves reviewability and enables unit testing.

Scope

Refactor upgrade resumable execution to table-driven state machine

Files: internal/application/upgrade/resumable_execute.go

  • Replace imperative switch/fallthrough with handler map
  • Make state transitions explicit and testable

Decompose provisioning use case into pipeline steps

Files: internal/application/devnet/provision.go

  • Extract: prepareMetadata
  • Extract: fetchGenesis
  • Extract: initializeKeysAndNodes
  • Extract: patchGenesis
  • Extract: persistState

Refactor daemon server initialization to builder pattern

Files: internal/daemon/server/server.go

  • Introduce ServerBuilder with With* methods
  • Centralize cleanup in LIFO order using multierr

Non-Goals

  • Changing external behavior of upgrade/provision workflows
  • Large-scale architecture refactor beyond the affected hotspots
  • Redesign of persistence/storage models

Risks and Open Questions

  • State machine refactor must preserve resume semantics and persistence behavior
  • Pipeline step boundaries may change error propagation; ensure consistent wrapping and context
  • Builder pattern must preserve initialization order and lifecycle management

Validation Plan

Unit and Integration Checks

  • go test ./... for modified packages
  • New unit tests per state handler / pipeline step / builder component
  • Verify state transition map covers all prior states and paths

End-to-End Checks

  • Run upgrade resume scenarios (resume from each stage if feasible)
  • Run provisioning workflow end-to-end for representative networks
  • Start daemon and verify core services are available (smoke)

Evidence Required in Issue Updates

  • Before/after LOC/complexity summary for each refactored function
  • Example state transition diagram or table derived from handler map
  • Test output snippets showing coverage for new handlers/steps

Acceptance Criteria

  • executeWithGovResumable < 120 LOC and complexity < 15
  • ProvisionUseCase.Execute < 100 LOC and complexity < 12
  • server.New < 120 LOC and complexity < 10 (or new entrypoint meets target)
  • Added unit tests make steps independently testable

Deliverables

  • PR implementing table-driven state machine + tests
  • PR implementing provisioning pipeline + tests
  • PR implementing server builder + lifecycle tests

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions