|
| 1 | +package provider |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "errors" |
| 6 | + "fmt" |
| 7 | + "sync" |
| 8 | + "testing" |
| 9 | + "time" |
| 10 | + |
| 11 | + "github.com/avast/retry-go/v4" |
| 12 | + sui_sdk "github.com/block-vision/sui-go-sdk/sui" |
| 13 | + chain_selectors "github.com/smartcontractkit/chain-selectors" |
| 14 | + "github.com/smartcontractkit/chainlink-testing-framework/framework" |
| 15 | + "github.com/smartcontractkit/chainlink-testing-framework/framework/components/blockchain" |
| 16 | + "github.com/stretchr/testify/require" |
| 17 | + "github.com/testcontainers/testcontainers-go" |
| 18 | + |
| 19 | + "github.com/smartcontractkit/chainlink-deployments-framework/chain" |
| 20 | + "github.com/smartcontractkit/chainlink-deployments-framework/chain/sui" |
| 21 | +) |
| 22 | + |
| 23 | +// CTFChainProviderConfig holds the configuration to initialize the CTFChainProvider. |
| 24 | +type CTFChainProviderConfig struct { |
| 25 | + // Required: A generator for the deployer signer account. Use AccountGenPrivateKey to |
| 26 | + // create a deployer signer from a hex private key. |
| 27 | + DeployerSignerGen AccountGenerator |
| 28 | + |
| 29 | + // Required: A sync.Once instance to ensure that the CTF framework only sets up the new |
| 30 | + // DefaultNetwork once |
| 31 | + Once *sync.Once |
| 32 | +} |
| 33 | + |
| 34 | +// validate checks if the CTFChainProviderConfig is valid. |
| 35 | +func (c CTFChainProviderConfig) validate() error { |
| 36 | + if c.DeployerSignerGen == nil { |
| 37 | + return errors.New("deployer signer generator is required") |
| 38 | + } |
| 39 | + |
| 40 | + if c.Once == nil { |
| 41 | + return errors.New("sync.Once instance is required") |
| 42 | + } |
| 43 | + |
| 44 | + return nil |
| 45 | +} |
| 46 | + |
| 47 | +var _ chain.Provider = (*CTFChainProvider)(nil) |
| 48 | + |
| 49 | +// CTFChainProvider manages a Sui chain instance running inside a Chainlink Testing Framework (CTF) Docker container. |
| 50 | +// |
| 51 | +// This provider requires Docker to be installed and operational. Spinning up a new container can be slow, |
| 52 | +// so it is recommended to initialize the provider only once per test suite or parent test to optimize performance. |
| 53 | +type CTFChainProvider struct { |
| 54 | + t *testing.T |
| 55 | + selector uint64 |
| 56 | + config CTFChainProviderConfig |
| 57 | + |
| 58 | + chain *sui.Chain |
| 59 | +} |
| 60 | + |
| 61 | +// NewCTFChainProvider creates a new CTFChainProvider with the given selector and configuration. |
| 62 | +func NewCTFChainProvider( |
| 63 | + t *testing.T, selector uint64, config CTFChainProviderConfig, |
| 64 | +) *CTFChainProvider { |
| 65 | + t.Helper() |
| 66 | + |
| 67 | + p := &CTFChainProvider{ |
| 68 | + t: t, |
| 69 | + selector: selector, |
| 70 | + config: config, |
| 71 | + } |
| 72 | + |
| 73 | + return p |
| 74 | +} |
| 75 | + |
| 76 | +// Initialize sets up the Sui chain by validating the configuration, starting a CTF container, |
| 77 | +// generating a deployer signer account, and constructing the chain instance. |
| 78 | +func (p *CTFChainProvider) Initialize(_ context.Context) (chain.BlockChain, error) { |
| 79 | + if p.chain != nil { |
| 80 | + return *p.chain, nil // Already initialized |
| 81 | + } |
| 82 | + |
| 83 | + if err := p.config.validate(); err != nil { |
| 84 | + return nil, fmt.Errorf("failed to validate provider config: %w", err) |
| 85 | + } |
| 86 | + |
| 87 | + // Generate the deployer account |
| 88 | + deployerSigner, err := p.config.DeployerSignerGen.Generate() |
| 89 | + if err != nil { |
| 90 | + return nil, fmt.Errorf("failed to generate deployer account: %w", err) |
| 91 | + } |
| 92 | + |
| 93 | + // Get the Sui Chain ID |
| 94 | + chainID, err := chain_selectors.GetChainIDFromSelector(p.selector) |
| 95 | + if err != nil { |
| 96 | + return nil, fmt.Errorf("failed to get chain ID from selector %d: %w", p.selector, err) |
| 97 | + } |
| 98 | + |
| 99 | + // Start the CTF Container |
| 100 | + url, client := p.startContainer(chainID, deployerSigner) |
| 101 | + |
| 102 | + // Construct the chain |
| 103 | + p.chain = &sui.Chain{ |
| 104 | + ChainMetadata: sui.ChainMetadata{ |
| 105 | + Selector: p.selector, |
| 106 | + }, |
| 107 | + Client: client, |
| 108 | + Signer: deployerSigner, |
| 109 | + URL: url, |
| 110 | + // TODO: Implement ConfirmTransaction when available |
| 111 | + } |
| 112 | + |
| 113 | + return *p.chain, nil |
| 114 | +} |
| 115 | + |
| 116 | +// Name returns the name of the CTFChainProvider. |
| 117 | +func (*CTFChainProvider) Name() string { |
| 118 | + return "Sui CTF Chain Provider" |
| 119 | +} |
| 120 | + |
| 121 | +// ChainSelector returns the chain selector of the Sui chain managed by this provider. |
| 122 | +func (p *CTFChainProvider) ChainSelector() uint64 { |
| 123 | + return p.selector |
| 124 | +} |
| 125 | + |
| 126 | +// BlockChain returns the Sui chain instance managed by this provider. You must call Initialize |
| 127 | +// before using this method to ensure the chain is properly set up. |
| 128 | +func (p *CTFChainProvider) BlockChain() chain.BlockChain { |
| 129 | + return *p.chain |
| 130 | +} |
| 131 | + |
| 132 | +// startContainer starts a CTF container for the Sui chain with the given chain ID and deployer account. |
| 133 | +// It returns the URL of the Sui node and the client to interact with it. |
| 134 | +func (p *CTFChainProvider) startContainer( |
| 135 | + chainID string, account sui.SuiSigner, |
| 136 | +) (string, sui_sdk.ISuiAPI) { |
| 137 | + var ( |
| 138 | + attempts = uint(10) |
| 139 | + url string |
| 140 | + containerName string |
| 141 | + ) |
| 142 | + |
| 143 | + // initialize the docker network used by CTF |
| 144 | + err := framework.DefaultNetwork(p.config.Once) |
| 145 | + require.NoError(p.t, err) |
| 146 | + |
| 147 | + // Get address from signer |
| 148 | + address, err := account.GetAddress() |
| 149 | + require.NoError(p.t, err) |
| 150 | + |
| 151 | + type containerResult struct { |
| 152 | + url string |
| 153 | + containerName string |
| 154 | + } |
| 155 | + |
| 156 | + result, err := retry.DoWithData(func() (containerResult, error) { |
| 157 | + // NOTE: Sui blockchain containers use hardcoded ports (9000/9123) and ignore the Port field |
| 158 | + input := &blockchain.Input{ |
| 159 | + Image: "", // filled out by defaultSui function |
| 160 | + Type: blockchain.TypeSui, |
| 161 | + ChainID: chainID, |
| 162 | + PublicKey: address, |
| 163 | + // Port field is ignored by Sui containers - they always use ports 9000/9123 |
| 164 | + } |
| 165 | + |
| 166 | + output, rerr := blockchain.NewBlockchainNetwork(input) |
| 167 | + if rerr != nil { |
| 168 | + return containerResult{}, rerr |
| 169 | + } |
| 170 | + |
| 171 | + testcontainers.CleanupContainer(p.t, output.Container) |
| 172 | + |
| 173 | + return containerResult{ |
| 174 | + url: output.Nodes[0].ExternalHTTPUrl + "/v1", |
| 175 | + containerName: output.ContainerName, |
| 176 | + }, nil |
| 177 | + }, |
| 178 | + retry.Context(p.t.Context()), |
| 179 | + retry.Attempts(attempts), |
| 180 | + retry.Delay(1*time.Second), |
| 181 | + retry.DelayType(retry.FixedDelay), |
| 182 | + ) |
| 183 | + require.NoError(p.t, err) |
| 184 | + |
| 185 | + url = result.url |
| 186 | + containerName = result.containerName |
| 187 | + |
| 188 | + client := sui_sdk.NewSuiClient(url) |
| 189 | + |
| 190 | + var ready bool |
| 191 | + for i := range 30 { |
| 192 | + time.Sleep(time.Second) |
| 193 | + // TODO: Add appropriate readiness check when available |
| 194 | + p.t.Logf("Sui client ready check (attempt %d)\n", i+1) |
| 195 | + ready = true |
| 196 | + |
| 197 | + break |
| 198 | + } |
| 199 | + require.True(p.t, ready, "Sui network not ready") |
| 200 | + |
| 201 | + dc, err := framework.NewDockerClient() |
| 202 | + require.NoError(p.t, err) |
| 203 | + |
| 204 | + _, err = dc.ExecContainer(containerName, []string{ |
| 205 | + "sui", "client", "faucet", |
| 206 | + "--address", address, |
| 207 | + }) |
| 208 | + require.NoError(p.t, err) |
| 209 | + |
| 210 | + return url, client |
| 211 | +} |
0 commit comments