|
| 1 | +// Package chainfamily defines the adapter interface for multi-chain simulation support. |
| 2 | +// Each chain family (EVM, Aptos, Solana, etc.) implements the Adapter interface and |
| 3 | +// self-registers via init(). The core simulate flow iterates registered adapters |
| 4 | +// instead of hardcoding chain-specific logic. |
| 5 | +package chainfamily |
| 6 | + |
| 7 | +import ( |
| 8 | + "context" |
| 9 | + "io" |
| 10 | + |
| 11 | + "github.com/spf13/cobra" |
| 12 | + |
| 13 | + "github.com/smartcontractkit/chainlink-common/pkg/logger" |
| 14 | + "github.com/smartcontractkit/chainlink-common/pkg/services" |
| 15 | + "github.com/smartcontractkit/chainlink/v2/core/capabilities" |
| 16 | +) |
| 17 | + |
| 18 | +// ChainSelector is a uint64 chain selector from the chain-selectors package. |
| 19 | +type ChainSelector = uint64 |
| 20 | + |
| 21 | +// SupportedChain describes a chain known to the simulator with its default config. |
| 22 | +type SupportedChain struct { |
| 23 | + Selector ChainSelector |
| 24 | + Forwarder string // hex address, family-specific format |
| 25 | +} |
| 26 | + |
| 27 | +// ExperimentalChainConfig holds per-chain overrides from experimental config, |
| 28 | +// already filtered to a specific adapter's family by the core. |
| 29 | +type ExperimentalChainConfig struct { |
| 30 | + ChainSelector ChainSelector |
| 31 | + RPCURL string |
| 32 | + Forwarder string |
| 33 | +} |
| 34 | + |
| 35 | +// SetupConfig contains everything an adapter needs to set itself up. |
| 36 | +// The core populates this once; no viper dependency leaks into adapters. |
| 37 | +// Capability registration happens separately via ChainRuntime.RegisterCapabilities. |
| 38 | +type SetupConfig struct { |
| 39 | + Logger logger.Logger |
| 40 | + |
| 41 | + // RPCURLs maps chain name -> RPC URL, scoped to this adapter's family. |
| 42 | + RPCURLs map[string]string |
| 43 | + |
| 44 | + // ExperimentalChains filtered to THIS adapter's family by the core. |
| 45 | + ExperimentalChains []ExperimentalChainConfig |
| 46 | + |
| 47 | + // DryRun is true when chain writes should be simulated (default behavior). |
| 48 | + DryRun bool |
| 49 | + |
| 50 | + // SecretsPath is the path to the secrets file, if any. |
| 51 | + SecretsPath string |
| 52 | + |
| 53 | + // FlagValues provides access to family-specific flag values registered via AddFlags. |
| 54 | + FlagValues func(name string) string |
| 55 | + |
| 56 | + // ChainWriteReportSizeLimit is the max report size in bytes (0 = no limit). |
| 57 | + ChainWriteReportSizeLimit int |
| 58 | + |
| 59 | + // ChainWriteGasLimit is the max gas limit for chain writes (0 = no limit). |
| 60 | + ChainWriteGasLimit uint64 |
| 61 | +} |
| 62 | + |
| 63 | +// TriggerRequest contains everything an adapter needs to build a trigger function. |
| 64 | +type TriggerRequest struct { |
| 65 | + TriggerID string // e.g. "evm:ChainSelector:123@1.0.0 LogTrigger" |
| 66 | + TriggerRegistrationID string |
| 67 | + ChainSelector ChainSelector |
| 68 | + PromptUser bool // true = interactive mode |
| 69 | + FlagValues func(name string) string |
| 70 | +} |
| 71 | + |
| 72 | +// ChainRuntime is the live handle returned by Setup(). The adapter owns its |
| 73 | +// internal state (clients, fake chains) behind this interface. The core code |
| 74 | +// never inspects chain internals - it just calls BuildTriggerFunc and Close. |
| 75 | +type ChainRuntime interface { |
| 76 | + // RegisterCapabilities registers chain-specific capabilities with the |
| 77 | + // capability registry. Called during simulator initialization when the |
| 78 | + // registry is available, not during Setup(). |
| 79 | + RegisterCapabilities(ctx context.Context, registry *capabilities.Registry) error |
| 80 | + |
| 81 | + // BuildTriggerFunc creates a function that fires the given chain trigger. |
| 82 | + BuildTriggerFunc(ctx context.Context, req TriggerRequest) (func() error, error) |
| 83 | + |
| 84 | + // OwnedSelectors returns all chain selectors this runtime manages. |
| 85 | + // Used for trigger routing without relying on GetSelectorFamily. |
| 86 | + OwnedSelectors() []ChainSelector |
| 87 | + |
| 88 | + // Services returns all services managed by this runtime for lifecycle management. |
| 89 | + Services() []services.Service |
| 90 | + |
| 91 | + // Close releases all resources (RPC clients, fake chains, etc.) |
| 92 | + io.Closer |
| 93 | +} |
| 94 | + |
| 95 | +// Adapter is the contract each chain family implements. Adapters self-register |
| 96 | +// via init() + Register(). |
| 97 | +type Adapter interface { |
| 98 | + // Family returns the chain family identifier (e.g. "evm", "aptos", "solana"). |
| 99 | + Family() string |
| 100 | + |
| 101 | + // SupportedChains returns default chain configs for this family. |
| 102 | + SupportedChains() []SupportedChain |
| 103 | + |
| 104 | + // AddFlags registers family-specific CLI flags on the simulate command. |
| 105 | + AddFlags(cmd *cobra.Command) |
| 106 | + |
| 107 | + // Setup dials RPCs, health-checks, and returns a ChainRuntime. |
| 108 | + // Returns (nil, nil) if no chains of this family are configured. |
| 109 | + Setup(ctx context.Context, cfg SetupConfig) (ChainRuntime, error) |
| 110 | +} |
0 commit comments