network

package
v1.0.0-rc0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 20, 2026 License: MIT Imports: 7 Imported by: 0

Documentation

Overview

Package network provides the public SDK for developing devnet-builder network plugins.

Package network provides the public SDK for developing devnet-builder network plugins.

This package allows third-party developers to create custom network modules for their own Cosmos SDK-based blockchains.

Example usage:

package main

import (
    "github.com/altuslabsxyz/devnet-builder/pkg/network"
    "github.com/altuslabsxyz/devnet-builder/pkg/network/plugin"
)

type MyNetwork struct{}

func (m *MyNetwork) Name() string { return "mychain" }
// ... implement other methods

func main() {
    plugin.Serve(&MyNetwork{})
}

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type BinarySource

type BinarySource struct {
	// Type specifies the source type: "github", "local", "docker"
	Type string `json:"type"`

	// GitHub source configuration
	Owner     string `json:"owner,omitempty"`      // GitHub owner/org
	Repo      string `json:"repo,omitempty"`       // GitHub repository
	AssetName string `json:"asset_name,omitempty"` // Release asset name pattern

	// Local source configuration
	LocalPath string `json:"local_path,omitempty"` // Path to local binary

	// Build configuration
	BuildTags []string `json:"build_tags,omitempty"` // Go build tags (e.g., ["no_dynamic_precompiles"])
}

BinarySource defines how to acquire the network binary.

func (BinarySource) IsGitHub

func (s BinarySource) IsGitHub() bool

IsGitHub returns true if this is a GitHub source.

type BuildConfig

type BuildConfig struct {
	// Tags are Go build tags passed to the compiler.
	// These enable conditional compilation of code.
	// Examples: ["netgo", "ledger", "osusergo", "no_dynamic_precompiles"]
	Tags []string `json:"tags,omitempty"`

	// LDFlags are linker flags passed to the Go linker.
	// These are used to inject values at compile-time using -X flag.
	// Format: ["-X package.Variable=value", "-w", "-s"]
	// Examples:
	//   - "-X github.com/stablelabs/stable/app.EVMChainID=988" (set EVM chain ID)
	//   - "-w" (omit DWARF symbol table)
	//   - "-s" (omit symbol table and debug information)
	LDFlags []string `json:"ldflags,omitempty"`

	// Env contains environment variables for the build process.
	// These are set before running the build command.
	// Examples:
	//   - {"CGO_ENABLED": "0"} (disable CGO for static binary)
	//   - {"GOOS": "linux", "GOARCH": "amd64"} (cross-compilation)
	Env map[string]string `json:"env,omitempty"`

	// ExtraArgs are additional arguments passed to the build tool (goreleaser).
	// Examples: ["--skip-validate", "--debug", "--clean"]
	ExtraArgs []string `json:"extra_args,omitempty"`
}

BuildConfig contains network-specific build configuration for compiling binaries. This enables plugins to customize binary compilation for different deployment targets (mainnet, testnet, devnet) by providing custom build tags, linker flags, and environment variables.

Example usage:

config := &BuildConfig{
    Tags:    []string{"netgo", "ledger"},
    LDFlags: []string{"-X github.com/example/app.EVMChainID=988", "-w", "-s"},
    Env:     map[string]string{"CGO_ENABLED": "0"},
}

All fields are optional. If a plugin doesn't need custom build configuration, it can return an empty BuildConfig{}.

func (*BuildConfig) Clone

func (b *BuildConfig) Clone() *BuildConfig

Clone creates a deep copy of the BuildConfig. This is useful for creating independent configurations that can be modified without affecting the original.

func (*BuildConfig) Hash

func (b *BuildConfig) Hash() string

Hash computes a unique hash of the BuildConfig. This is used for cache key generation to ensure binaries built with different configurations are cached separately.

The hash includes all configuration fields:

  • Tags (sorted for deterministic hashing)
  • LDFlags (sorted for deterministic hashing)
  • Env (sorted by key for deterministic hashing)
  • ExtraArgs (sorted for deterministic hashing)

Returns: 16-character hex string (first 64 bits of SHA256 hash)

func (*BuildConfig) IsEmpty

func (b *BuildConfig) IsEmpty() bool

IsEmpty returns true if the BuildConfig has no configuration. This is useful for checking if a plugin provided any custom build configuration.

func (*BuildConfig) Merge

func (b *BuildConfig) Merge(other *BuildConfig) *BuildConfig

Merge combines two BuildConfig instances, with other taking precedence. This is useful for combining default configurations with plugin-specific overrides.

Merge behavior:

  • Tags: Appends other's tags to this config's tags (no deduplication)
  • LDFlags: Appends other's ldflags to this config's ldflags
  • Env: Merges environment variables, with other's values overriding conflicts
  • ExtraArgs: Appends other's args to this config's args

Example:

base := &BuildConfig{
    Tags:    []string{"netgo"},
    LDFlags: []string{"-w"},
    Env:     map[string]string{"CGO_ENABLED": "0"},
}
override := &BuildConfig{
    Tags:    []string{"ledger"},
    LDFlags: []string{"-X main.Version=1.0"},
    Env:     map[string]string{"GOOS": "linux"},
}
merged := base.Merge(override)
// Result:
// Tags:    ["netgo", "ledger"]
// LDFlags: ["-w", "-X main.Version=1.0"]
// Env:     {"CGO_ENABLED": "0", "GOOS": "linux"}

func (*BuildConfig) String

func (b *BuildConfig) String() string

String returns a human-readable representation of the BuildConfig. This is useful for logging and debugging.

func (*BuildConfig) Validate

func (b *BuildConfig) Validate() error

Validate checks if the BuildConfig is valid. It performs comprehensive validation of all fields to catch configuration errors early before attempting to build binaries.

Returns:

  • nil if valid
  • error describing the validation failure

type ExportOptions

type ExportOptions struct {
	// ForZeroHeight resets the chain height to 0 for the exported genesis.
	ForZeroHeight bool `json:"for_zero_height"`

	// JailWhitelist is a list of validator operator addresses that should
	// not be jailed in the exported genesis.
	JailWhitelist []string `json:"jail_whitelist,omitempty"`

	// ModulesToSkip is a list of module names to skip during export.
	ModulesToSkip []string `json:"modules_to_skip,omitempty"`

	// Height specifies a specific height to export from.
	// If 0, exports from the latest height.
	Height int64 `json:"height"`

	// OutputPath is the path to write the exported genesis.
	// If empty, genesis is written to stdout.
	OutputPath string `json:"output_path,omitempty"`
}

ExportOptions defines options for genesis state export.

type FileBasedGenesisModifier

type FileBasedGenesisModifier interface {
	// ModifyGenesisFile modifies a genesis file at inputPath and writes to outputPath.
	// This is the file-based equivalent of Module.ModifyGenesis() for large genesis files.
	//
	// Parameters:
	//   - inputPath: Path to the input genesis.json file
	//   - outputPath: Path where the modified genesis should be written
	//   - opts: Genesis modification options (chain_id, validators, etc.)
	//
	// Returns:
	//   - outputSize: Size of the output file in bytes
	//   - error: Any error that occurred during modification
	ModifyGenesisFile(inputPath, outputPath string, opts GenesisOptions) (outputSize int64, err error)
}

FileBasedGenesisModifier is an optional interface for handling large genesis files. When genesis files exceed gRPC message size limits (default 4MB), this interface allows passing file paths instead of raw bytes over gRPC.

Implementations should:

  1. Read genesis from inputPath
  2. Apply modifications based on opts
  3. Write modified genesis to outputPath

This is particularly useful for fork-based devnets where exported genesis can be 50-100+ MB in size.

type GeneratorConfig

type GeneratorConfig struct {
	NumValidators    int    `json:"num_validators"`
	NumAccounts      int    `json:"num_accounts"`
	AccountBalance   string `json:"account_balance"`   // JSON encoded coins
	ValidatorBalance string `json:"validator_balance"` // JSON encoded coins
	ValidatorStake   string `json:"validator_stake"`   // JSON encoded amount
	OutputDir        string `json:"output_dir"`
	ChainID          string `json:"chain_id"`
}

GeneratorConfig contains configuration for devnet generation.

type GenesisConfig

type GenesisConfig struct {
	// Chain identity
	ChainIDPattern string `json:"chain_id_pattern"` // e.g., "stable_{evmid}-1"
	EVMChainID     int64  `json:"evm_chain_id"`     // EVM chain ID

	// Token configuration
	BaseDenom     string `json:"base_denom"`     // e.g., "ustable"
	DenomExponent int    `json:"denom_exponent"` // Decimal places (typically 18)
	DisplayDenom  string `json:"display_denom"`  // Human-readable (e.g., "STABLE")

	// Staking parameters
	BondDenom         string        `json:"bond_denom"`
	MinSelfDelegation string        `json:"min_self_delegation"`
	UnbondingTime     time.Duration `json:"unbonding_time"`
	MaxValidators     uint32        `json:"max_validators"`

	// Governance parameters
	MinDeposit       string        `json:"min_deposit"`
	VotingPeriod     time.Duration `json:"voting_period"`
	MaxDepositPeriod time.Duration `json:"max_deposit_period"`

	// Distribution
	CommunityTax string `json:"community_tax"`
}

GenesisConfig contains genesis parameters for a network.

type GenesisOptions

type GenesisOptions struct {
	ChainID       string          `json:"chain_id,omitempty"`
	NumValidators int             `json:"num_validators,omitempty"`
	Validators    []ValidatorInfo `json:"validators,omitempty"`
}

GenesisOptions contains user-provided overrides for genesis modification.

type Module

type Module interface {

	// Name returns the unique identifier for this network.
	// This should be lowercase, alphanumeric with hyphens only.
	// Examples: "stable", "ault", "osmosis", "cosmos-hub"
	Name() string

	// DisplayName returns a human-readable name for the network.
	// Examples: "Stable Network", "Osmosis DEX", "Cosmos Hub"
	DisplayName() string

	// Version returns the module version (semantic versioning recommended).
	// Example: "1.0.0"
	Version() string

	// BinaryName returns the name of the network's CLI binary.
	// Examples: "stabled", "osmosisd", "gaiad"
	BinaryName() string

	// BinarySource returns configuration for acquiring the binary.
	BinarySource() BinarySource

	// DefaultBinaryVersion returns the default version to build/use.
	// Examples: "v1.1.3", "latest", "main"
	DefaultBinaryVersion() string

	// GetBuildConfig returns network-specific build configuration for binary compilation.
	// This method enables plugins to customize build tags, linker flags, and environment
	// variables for different network types (mainnet, testnet, devnet).
	//
	// Parameters:
	//   - networkType: Target network type ("mainnet", "testnet", "devnet", etc.)
	//
	// Returns:
	//   - BuildConfig with custom build configuration
	//   - error if network type is not supported
	//
	// Example:
	//   cfg, err := module.GetBuildConfig("testnet")
	//   if err != nil {
	//       return err
	//   }
	//   // cfg.LDFlags might contain: ["-X github.com/example/app.EVMChainID=2201"]
	//
	// If the plugin doesn't need network-specific build configuration, it should
	// return an empty BuildConfig{} (not nil) to indicate no custom configuration.
	//
	// This is called during binary building to inject network-specific values
	// (like chain IDs) at compile-time. The returned config is merged with
	// default build configurations before being passed to the build tool.
	GetBuildConfig(networkType string) (*BuildConfig, error)

	// DefaultChainID returns the default chain ID for the devnet.
	//
	// Deprecated: Chain ID is extracted from genesis files and stored in metadata.
	// This method will be removed in v2.0.0. Return empty string for new plugins.
	// Examples: "stabledevnet_2200-1", "osmosis-devnet-1"
	DefaultChainID() string

	// Bech32Prefix returns the address prefix for this network.
	// Examples: "stable", "osmo", "cosmos"
	Bech32Prefix() string

	// BaseDenom returns the base token denomination.
	// Examples: "ustable", "uosmo", "uatom"
	BaseDenom() string

	// GenesisConfig returns default genesis parameters.
	GenesisConfig() GenesisConfig

	// DefaultPorts returns the default port configuration.
	DefaultPorts() PortConfig

	// DockerImage returns the Docker image name for this network.
	// Example: "ghcr.io/stablelabs/stable"
	DockerImage() string

	// DockerImageTag returns the Docker tag for a given version.
	// Allows networks to customize version-to-tag mapping.
	DockerImageTag(version string) string

	// DockerHomeDir returns the home directory inside Docker containers.
	// Example: "/home/stabled"
	DockerHomeDir() string

	// DefaultNodeHome returns the default node home directory path.
	// Example: "/root/.stabled"
	DefaultNodeHome() string

	// PIDFileName returns the process ID file name.
	// Example: "stabled.pid"
	PIDFileName() string

	// LogFileName returns the log file name.
	// Example: "stabled.log"
	LogFileName() string

	// ProcessPattern returns the regex pattern to match running processes.
	// Example: "stabled.*start"
	ProcessPattern() string

	// InitCommand returns arguments for initializing a node.
	// Parameters: homeDir, chainID, moniker
	// Returns: e.g., ["init", "node0", "--chain-id", "mychain-1", "--home", "/path"]
	InitCommand(homeDir, chainID, moniker string) []string

	// StartCommand returns arguments for starting a node.
	// Parameters: homeDir
	// Returns: e.g., ["start", "--home", "/path"]
	StartCommand(homeDir string) []string

	// ExportCommand returns arguments for exporting genesis/state.
	// Parameters: homeDir
	// Returns: e.g., ["export", "--home", "/path"]
	ExportCommand(homeDir string) []string

	// ModifyGenesis applies network-specific modifications to a genesis file.
	// Parameters:
	//   - genesis: Raw genesis JSON bytes
	//   - opts: User-provided customization options
	// Returns: Modified genesis JSON bytes, or error
	ModifyGenesis(genesis []byte, opts GenesisOptions) ([]byte, error)

	// GenerateDevnet generates validators, accounts, and modifies genesis.
	// This is the main entry point for devnet creation.
	GenerateDevnet(ctx context.Context, config GeneratorConfig, genesisFile string) error

	// DefaultGeneratorConfig returns the default configuration for devnet generation.
	DefaultGeneratorConfig() GeneratorConfig

	// GetCodec returns the network-specific codec configuration.
	// This is used for serialization/deserialization of chain-specific types.
	// Returns encoded codec configuration or error.
	GetCodec() ([]byte, error)

	// Validate checks if the module configuration is valid.
	// Called during registration and before use.
	Validate() error

	// SnapshotURL returns the snapshot download URL for the given network type.
	// Parameters:
	//   - networkType: Type of network ("mainnet", "testnet", etc.)
	// Returns: Full URL to the snapshot archive, or empty string if not available
	// Example: "https://stable-mainnet-data.s3.amazonaws.com/snapshots/stable_pruned.tar.zst"
	SnapshotURL(networkType string) string

	// RPCEndpoint returns the RPC endpoint for the given network type.
	// Parameters:
	//   - networkType: Type of network ("mainnet", "testnet", etc.)
	// Returns: Full URL to the RPC endpoint, or empty string if not available
	// Example: "https://cosmos-rpc.stable.xyz"
	RPCEndpoint(networkType string) string

	// AvailableNetworks returns a list of supported network types.
	// Returns: Slice of network type strings (e.g., ["mainnet", "testnet"])
	AvailableNetworks() []string

	// GetConfigOverrides returns TOML configuration overrides for a node.
	// The returned bytes are partial TOML that will be merged with the
	// default configs generated by node init.
	// Parameters:
	//   - nodeIndex: Index of the node (0, 1, 2, ...)
	//   - opts: Configuration options including ports and peers
	// Returns:
	//   - configToml: Partial config.toml overrides (can be nil if no overrides)
	//   - appToml: Partial app.toml overrides (can be nil if no overrides)
	//   - error: Error if generation fails
	GetConfigOverrides(nodeIndex int, opts NodeConfigOptions) (configToml []byte, appToml []byte, err error)
}

Module is the interface that all network plugins must implement. This is the core abstraction for supporting multiple Cosmos SDK-based networks.

type NodeConfigOptions

type NodeConfigOptions struct {
	// ChainID is the chain identifier
	ChainID string `json:"chain_id"`

	// Ports is the port configuration for this node
	Ports PortConfig `json:"ports"`

	// PersistentPeers is the persistent peers string (node_id@host:port,...)
	PersistentPeers string `json:"persistent_peers"`

	// NumValidators is the total number of validators
	NumValidators int `json:"num_validators"`

	// IsValidator indicates if this node is a validator
	IsValidator bool `json:"is_validator"`

	// Moniker is the node's moniker/name
	Moniker string `json:"moniker"`
}

NodeConfigOptions contains options for node configuration. This is passed to ConfigureNode to customize config.toml and app.toml.

type PortConfig

type PortConfig struct {
	RPC       int `json:"rpc"`        // Tendermint RPC (default: 26657)
	P2P       int `json:"p2p"`        // P2P networking (default: 26656)
	GRPC      int `json:"grpc"`       // gRPC server (default: 9090)
	GRPCWeb   int `json:"grpc_web"`   // gRPC-Web (default: 9091)
	API       int `json:"api"`        // REST API (default: 1317)
	EVMRPC    int `json:"evm_rpc"`    // EVM JSON-RPC (default: 8545)
	EVMSocket int `json:"evm_socket"` // EVM WebSocket (default: 8546)
}

PortConfig contains network port configuration.

type SnapshotFormat

type SnapshotFormat string

SnapshotFormat defines the archive format for snapshots.

const (
	SnapshotFormatTarLz4 SnapshotFormat = "tar.lz4"
	SnapshotFormatTarZst SnapshotFormat = "tar.zst"
	SnapshotFormatTarGz  SnapshotFormat = "tar.gz"
)

func (SnapshotFormat) Extension

func (f SnapshotFormat) Extension() string

Extension returns the file extension for the snapshot format.

type SnapshotInfo

type SnapshotInfo struct {
	// URL is the download URL for the snapshot archive.
	URL string `json:"url"`

	// Format is the archive format.
	Format SnapshotFormat `json:"format"`

	// Height is the block height of the snapshot.
	Height int64 `json:"height"`

	// Hash is the hash of the snapshot archive for verification.
	Hash string `json:"hash,omitempty"`

	// Size is the size of the snapshot in bytes.
	Size int64 `json:"size"`
}

SnapshotInfo contains information about a snapshot.

type StateExporter

type StateExporter interface {
	// ExportCommandWithOptions returns arguments for exporting genesis/state
	// with the given export options.
	// Parameters: homeDir, opts
	// Returns: e.g., ["export", "--home", "/path", "--for-zero-height"]
	ExportCommandWithOptions(homeDir string, opts ExportOptions) []string

	// ValidateExportedGenesis validates the exported genesis for this network.
	// This performs network-specific validation (e.g., checking for EVM modules).
	ValidateExportedGenesis(genesis []byte) error

	// RequiredModules returns the list of modules that must be present
	// in an exported genesis for it to be valid.
	RequiredModules() []string

	// SnapshotFormat returns the expected snapshot archive format for this network.
	// Parameters: networkType ("mainnet", "testnet")
	// Returns: SnapshotFormat (e.g., SnapshotFormatTarLz4)
	SnapshotFormat(networkType string) SnapshotFormat
}

StateExporter is an optional interface that network modules can implement to provide state export functionality for snapshot-based genesis creation. If a module implements this interface, the devnet provisioning can use snapshots to create devnets with real on-chain state.

type ValidatorInfo

type ValidatorInfo struct {
	Moniker         string `json:"moniker"`
	ConsPubKey      string `json:"cons_pub_key"`     // Base64 encoded Ed25519 pubkey
	OperatorAddress string `json:"operator_address"` // Bech32 valoper address
	SelfDelegation  string `json:"self_delegation"`  // Amount of tokens delegated
}

ValidatorInfo represents validator information for genesis injection.

Directories

Path Synopsis
Package plugin provides helpers for developing devnet-builder network plugins.
Package plugin provides helpers for developing devnet-builder network plugins.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL