|
| 1 | +package ccip_attestation |
| 2 | + |
| 3 | +import ( |
| 4 | + "archive/zip" |
| 5 | + "bytes" |
| 6 | + "context" |
| 7 | + "fmt" |
| 8 | + "io" |
| 9 | + "net/http" |
| 10 | + "os" |
| 11 | + "path/filepath" |
| 12 | + |
| 13 | + "github.com/Masterminds/semver/v3" |
| 14 | + "github.com/gagliardetto/solana-go" |
| 15 | + signer_registry "github.com/smartcontractkit/ccip-base/chains/solana/go_bindings" |
| 16 | + chainsel "github.com/smartcontractkit/chain-selectors" |
| 17 | + |
| 18 | + cldf_solana "github.com/smartcontractkit/chainlink-deployments-framework/chain/solana" |
| 19 | + |
| 20 | + cldf "github.com/smartcontractkit/chainlink-deployments-framework/deployment" |
| 21 | + "github.com/smartcontractkit/chainlink/deployment" |
| 22 | + "github.com/smartcontractkit/chainlink/deployment/ccip/shared" |
| 23 | +) |
| 24 | + |
| 25 | +// use this changeset to deploy the base signer registry contract |
| 26 | +var _ cldf.ChangeSet[DeployBaseSignerRegistryContractConfig] = DeployBaseSignerRegistryContractChangeset |
| 27 | + |
| 28 | +// use this changeset to initialize the base signer registry contract and set an initial owner |
| 29 | +var _ cldf.ChangeSet[InitalizeBaseSignerRegistryContractConfig] = InitializeBaseSignerRegistryContractChangeset |
| 30 | + |
| 31 | +type DeployBaseSignerRegistryContractConfig struct { |
| 32 | + ChainSelector uint64 |
| 33 | + Version semver.Version |
| 34 | + WorkflowRun string |
| 35 | + ArtifactId string |
| 36 | + IsUpgrade bool |
| 37 | +} |
| 38 | + |
| 39 | +type InitalizeBaseSignerRegistryContractConfig struct { |
| 40 | + ChainSelector uint64 |
| 41 | + Owner solana.PublicKey |
| 42 | +} |
| 43 | + |
| 44 | +func DeployBaseSignerRegistryContractChangeset(e cldf.Environment, c DeployBaseSignerRegistryContractConfig) (cldf.ChangesetOutput, error) { |
| 45 | + e.Logger.Infow("Deploying base signer registry", "chain_selector", c.ChainSelector) |
| 46 | + c.Validate(e) |
| 47 | + chainSel := c.ChainSelector |
| 48 | + chain := e.BlockChains.SolanaChains()[chainSel] |
| 49 | + |
| 50 | + newAddresses := cldf.NewMemoryAddressBook() |
| 51 | + if err := DownloadReleaseArtifactsFromGithubWorkflowRun(context.Background(), c.WorkflowRun, c.ArtifactId, chain.ProgramsPath); err != nil { |
| 52 | + return cldf.ChangesetOutput{}, fmt.Errorf("failed to download release artifacts: %w", err) |
| 53 | + } |
| 54 | + _, err := deployBaseSignerRegistryContract(e, chain, newAddresses, c) |
| 55 | + if err != nil { |
| 56 | + return cldf.ChangesetOutput{}, fmt.Errorf("failed to deploy base signer registry contract: %w", err) |
| 57 | + } |
| 58 | + |
| 59 | + return cldf.ChangesetOutput{ |
| 60 | + AddressBook: newAddresses, |
| 61 | + }, nil |
| 62 | +} |
| 63 | + |
| 64 | +func InitializeBaseSignerRegistryContractChangeset(e cldf.Environment, c InitalizeBaseSignerRegistryContractConfig) (cldf.ChangesetOutput, error) { |
| 65 | + e.Logger.Infow("Initializing base signer registry", "chain_selector", c.ChainSelector) |
| 66 | + c.Validate(e) |
| 67 | + chainSel := c.ChainSelector |
| 68 | + chain := e.BlockChains.SolanaChains()[chainSel] |
| 69 | + authority := chain.DeployerKey.PublicKey() |
| 70 | + |
| 71 | + configPda, _, _ := solana.FindProgramAddress([][]byte{[]byte("config")}, signer_registry.ProgramID) |
| 72 | + signersPda, _, _ := solana.FindProgramAddress([][]byte{[]byte("signers")}, signer_registry.ProgramID) |
| 73 | + eventAuthorityPda, _, _ := solana.FindProgramAddress([][]byte{[]byte("__event_authority")}, signer_registry.ProgramID) |
| 74 | + ix, err := signer_registry.NewInitializeInstruction(authority, solana.SystemProgramID, configPda, signersPda, eventAuthorityPda, signer_registry.ProgramID) |
| 75 | + if err != nil { |
| 76 | + return cldf.ChangesetOutput{}, fmt.Errorf("Failed to initialize base signer registry contract: %w", err) |
| 77 | + } |
| 78 | + |
| 79 | + if err := chain.Confirm([]solana.Instruction{ix}); err != nil { |
| 80 | + return cldf.ChangesetOutput{}, fmt.Errorf("Failed to initialize base signer registry contract: %w", err) |
| 81 | + } |
| 82 | + |
| 83 | + return cldf.ChangesetOutput{}, nil |
| 84 | +} |
| 85 | + |
| 86 | +func deployBaseSignerRegistryContract(e cldf.Environment, chain cldf_solana.Chain, ab cldf.AddressBook, config DeployBaseSignerRegistryContractConfig, |
| 87 | +) (solana.PublicKey, error) { |
| 88 | + contractType := shared.BaseSignerRegistry |
| 89 | + programName := deployment.BaseSignerRegistryProgramName |
| 90 | + |
| 91 | + programID, err := chain.DeployProgram(e.Logger, cldf_solana.ProgramInfo{ |
| 92 | + Name: programName, |
| 93 | + Bytes: deployment.SolanaProgramBytes[programName], |
| 94 | + }, config.IsUpgrade, true) |
| 95 | + |
| 96 | + if err != nil { |
| 97 | + return solana.PublicKey{}, fmt.Errorf("failed to deploy program: %w", err) |
| 98 | + } |
| 99 | + address := solana.MustPublicKeyFromBase58(programID) |
| 100 | + |
| 101 | + e.Logger.Infow("Deployed program", "Program", contractType, "addr", programID, "chain", chain.String()) |
| 102 | + tv := cldf.NewTypeAndVersion(contractType, config.Version) |
| 103 | + err = ab.Save(chain.Selector, programID, tv) |
| 104 | + if err != nil { |
| 105 | + return solana.PublicKey{}, fmt.Errorf("failed to save address: %w", err) |
| 106 | + } |
| 107 | + |
| 108 | + return address, nil |
| 109 | + |
| 110 | +} |
| 111 | + |
| 112 | +func (c DeployBaseSignerRegistryContractConfig) Validate(e cldf.Environment) error { |
| 113 | + if err := cldf.IsValidChainSelector(c.ChainSelector); err != nil { |
| 114 | + return fmt.Errorf("invalid chain selector: %d - %w", c.ChainSelector, err) |
| 115 | + } |
| 116 | + family, _ := chainsel.GetSelectorFamily(c.ChainSelector) |
| 117 | + if family != chainsel.FamilySolana { |
| 118 | + return fmt.Errorf("chain %d is not a solana chain", c.ChainSelector) |
| 119 | + } |
| 120 | + |
| 121 | + return nil |
| 122 | +} |
| 123 | + |
| 124 | +func (c InitalizeBaseSignerRegistryContractConfig) Validate(e cldf.Environment) error { |
| 125 | + if err := cldf.IsValidChainSelector(c.ChainSelector); err != nil { |
| 126 | + return fmt.Errorf("invalid chain selector: %d - %w", c.ChainSelector, err) |
| 127 | + } |
| 128 | + family, _ := chainsel.GetSelectorFamily(c.ChainSelector) |
| 129 | + if family != chainsel.FamilySolana { |
| 130 | + return fmt.Errorf("chain %d is not a solana chain", c.ChainSelector) |
| 131 | + } |
| 132 | + |
| 133 | + return nil |
| 134 | +} |
| 135 | + |
| 136 | +func DownloadReleaseArtifactsFromGithubWorkflowRun( |
| 137 | + ctx context.Context, |
| 138 | + run string, |
| 139 | + artifactId string, |
| 140 | + targetPath string, |
| 141 | +) error { |
| 142 | + url := fmt.Sprintf( |
| 143 | + "https://github.com/smartcontractkit/ccip-base/actions/runs/%s/artifacts/%s", |
| 144 | + run, |
| 145 | + artifactId, |
| 146 | + ) |
| 147 | + |
| 148 | + req, err := http.NewRequestWithContext(ctx, http.MethodGet, url, nil) |
| 149 | + if err != nil { |
| 150 | + return fmt.Errorf("failed to create request: %w", err) |
| 151 | + } |
| 152 | + |
| 153 | + client := &http.Client{} |
| 154 | + resp, err := client.Do(req) |
| 155 | + if err != nil { |
| 156 | + return fmt.Errorf("failed to download release asset: %w", err) |
| 157 | + } |
| 158 | + defer resp.Body.Close() |
| 159 | + |
| 160 | + if resp.StatusCode != http.StatusOK { |
| 161 | + return fmt.Errorf("failed to download release asset: HTTP %d", resp.StatusCode) |
| 162 | + } |
| 163 | + |
| 164 | + // Read the entire zip file into memory |
| 165 | + body, err := io.ReadAll(resp.Body) |
| 166 | + if err != nil { |
| 167 | + return fmt.Errorf("failed to read response body: %w", err) |
| 168 | + } |
| 169 | + |
| 170 | + zipReader, err := zip.NewReader(bytes.NewReader(body), int64(len(body))) |
| 171 | + if err != nil { |
| 172 | + return fmt.Errorf("failed to create zip reader: %w", err) |
| 173 | + } |
| 174 | + |
| 175 | + // Extract each file from the zip archive |
| 176 | + for _, file := range zipReader.File { |
| 177 | + targetPath := filepath.Join(targetPath, file.Name) |
| 178 | + |
| 179 | + if file.FileInfo().IsDir() { |
| 180 | + if err := os.MkdirAll(targetPath, file.Mode()); err != nil { |
| 181 | + return fmt.Errorf("failed to create directory %s: %w", targetPath, err) |
| 182 | + } |
| 183 | + continue |
| 184 | + } |
| 185 | + |
| 186 | + if err := os.MkdirAll(filepath.Dir(targetPath), 0755); err != nil { |
| 187 | + return fmt.Errorf("failed to create parent directory for %s: %w", targetPath, err) |
| 188 | + } |
| 189 | + |
| 190 | + fileReader, err := file.Open() |
| 191 | + if err != nil { |
| 192 | + return fmt.Errorf("failed to open file in zip %s: %w", file.Name, err) |
| 193 | + } |
| 194 | + defer fileReader.Close() |
| 195 | + |
| 196 | + targetFile, err := os.OpenFile(targetPath, os.O_CREATE|os.O_RDWR|os.O_TRUNC, file.Mode()) |
| 197 | + if err != nil { |
| 198 | + return fmt.Errorf("failed to create file %s: %w", targetPath, err) |
| 199 | + } |
| 200 | + |
| 201 | + if _, err := io.Copy(targetFile, fileReader); err != nil { |
| 202 | + targetFile.Close() |
| 203 | + return fmt.Errorf("failed to write file %s: %w", targetPath, err) |
| 204 | + } |
| 205 | + |
| 206 | + } |
| 207 | + |
| 208 | + return nil |
| 209 | +} |
0 commit comments