Przeglądaj źródła

feat: Upload IDL by default with option to skip (#3863)

* feat: Upload IDL by default with option to skip

* fix: cargo clippy

* chore: Update CHANGELOG.md

* fix: prevent IDL upload attempt with non-existent file

* refactor: rename skip_idl to no_idl

* chore: Fix CHANGELOG.md
Swaroop M 1 miesiąc temu
rodzic
commit
d94d9c3d55
3 zmienionych plików z 33 dodań i 12 usunięć
  1. 1 0
      CHANGELOG.md
  2. 2 4
      cli/src/config.rs
  3. 30 8
      cli/src/lib.rs

+ 1 - 0
CHANGELOG.md

@@ -15,6 +15,7 @@ The minor version will be incremented upon a breaking change and the patch versi
 - lang: Add `#[error]` attribute to `declare_program!` ([#3757](https://github.com/coral-xyz/anchor/pull/3757)).
 - lang: Add `#[error]` attribute to `declare_program!` ([#3757](https://github.com/coral-xyz/anchor/pull/3757)).
 - lang: Replace `solana-program` crate with smaller crates ([#3819](https://github.com/solana-foundation/anchor/pull/3819)).
 - lang: Replace `solana-program` crate with smaller crates ([#3819](https://github.com/solana-foundation/anchor/pull/3819)).
 - cli: Replace `anchor verify` to use `solana-verify` under the hood, adding automatic installation via AVM, local path support, and future-proof argument passing ([#3768](https://github.com/solana-foundation/anchor/pull/3768)).
 - cli: Replace `anchor verify` to use `solana-verify` under the hood, adding automatic installation via AVM, local path support, and future-proof argument passing ([#3768](https://github.com/solana-foundation/anchor/pull/3768)).
+- cli: Make `anchor deploy` to upload the IDL to the cluster by default unless `--no-idl` is passed ([#3863](https://github.com/solana-foundation/anchor/pull/3863)).
 
 
 ### Fixes
 ### Fixes
 
 

+ 2 - 4
cli/src/config.rs

@@ -1,4 +1,4 @@
-use crate::{get_keypair, is_hidden, keys_sync};
+use crate::{get_keypair, is_hidden, keys_sync, DEFAULT_RPC_PORT};
 use anchor_client::Cluster;
 use anchor_client::Cluster;
 use anchor_lang_idl::types::Idl;
 use anchor_lang_idl::types::Idl;
 use anyhow::{anyhow, Context, Error, Result};
 use anyhow::{anyhow, Context, Error, Result};
@@ -1107,9 +1107,7 @@ impl From<_Validator> for Validator {
                 .ledger
                 .ledger
                 .unwrap_or_else(|| get_default_ledger_path().display().to_string()),
                 .unwrap_or_else(|| get_default_ledger_path().display().to_string()),
             limit_ledger_size: _validator.limit_ledger_size,
             limit_ledger_size: _validator.limit_ledger_size,
-            rpc_port: _validator
-                .rpc_port
-                .unwrap_or(solana_sdk::rpc_port::DEFAULT_RPC_PORT),
+            rpc_port: _validator.rpc_port.unwrap_or(DEFAULT_RPC_PORT),
             slots_per_epoch: _validator.slots_per_epoch,
             slots_per_epoch: _validator.slots_per_epoch,
             ticks_per_slot: _validator.ticks_per_slot,
             ticks_per_slot: _validator.ticks_per_slot,
             warp_slot: _validator.warp_slot,
             warp_slot: _validator.warp_slot,

+ 30 - 8
cli/src/lib.rs

@@ -5,6 +5,8 @@ use crate::config::{
 };
 };
 use anchor_client::Cluster;
 use anchor_client::Cluster;
 use anchor_lang::idl::{IdlAccount, IdlInstruction, ERASED_AUTHORITY};
 use anchor_lang::idl::{IdlAccount, IdlInstruction, ERASED_AUTHORITY};
+use anchor_lang::prelude::UpgradeableLoaderState;
+use anchor_lang::solana_program::bpf_loader_upgradeable;
 use anchor_lang::{AccountDeserialize, AnchorDeserialize, AnchorSerialize, Discriminator};
 use anchor_lang::{AccountDeserialize, AnchorDeserialize, AnchorSerialize, Discriminator};
 use anchor_lang_idl::convert::convert_idl;
 use anchor_lang_idl::convert::convert_idl;
 use anchor_lang_idl::types::{Idl, IdlArrayLen, IdlDefinedFields, IdlType, IdlTypeDefTy};
 use anchor_lang_idl::types::{Idl, IdlArrayLen, IdlDefinedFields, IdlType, IdlTypeDefTy};
@@ -21,7 +23,6 @@ use rust_template::{ProgramTemplate, TestTemplate};
 use semver::{Version, VersionReq};
 use semver::{Version, VersionReq};
 use serde_json::{json, Map, Value as JsonValue};
 use serde_json::{json, Map, Value as JsonValue};
 use solana_client::rpc_client::RpcClient;
 use solana_client::rpc_client::RpcClient;
-use solana_sdk::bpf_loader_upgradeable::{self, UpgradeableLoaderState};
 use solana_sdk::commitment_config::CommitmentConfig;
 use solana_sdk::commitment_config::CommitmentConfig;
 use solana_sdk::compute_budget::ComputeBudgetInstruction;
 use solana_sdk::compute_budget::ComputeBudgetInstruction;
 use solana_sdk::instruction::{AccountMeta, Instruction};
 use solana_sdk::instruction::{AccountMeta, Instruction};
@@ -49,6 +50,9 @@ pub mod rust_template;
 pub const VERSION: &str = env!("CARGO_PKG_VERSION");
 pub const VERSION: &str = env!("CARGO_PKG_VERSION");
 pub const DOCKER_BUILDER_VERSION: &str = VERSION;
 pub const DOCKER_BUILDER_VERSION: &str = VERSION;
 
 
+/// Default RPC port
+pub const DEFAULT_RPC_PORT: u16 = 8899;
+
 #[derive(Debug, Parser)]
 #[derive(Debug, Parser)]
 #[clap(version = VERSION)]
 #[clap(version = VERSION)]
 pub struct Opts {
 pub struct Opts {
@@ -239,6 +243,9 @@ pub enum Command {
         /// If true, deploy from path target/verifiable
         /// If true, deploy from path target/verifiable
         #[clap(short, long)]
         #[clap(short, long)]
         verifiable: bool,
         verifiable: bool,
+        /// Don't upload IDL during deployment (IDL is uploaded by default)
+        #[clap(long)]
+        no_idl: bool,
         /// Arguments to pass to the underlying `solana program deploy` command.
         /// Arguments to pass to the underlying `solana program deploy` command.
         #[clap(required = false, last = true)]
         #[clap(required = false, last = true)]
         solana_args: Vec<String>,
         solana_args: Vec<String>,
@@ -791,12 +798,14 @@ fn process_command(opts: Opts) -> Result<()> {
             program_name,
             program_name,
             program_keypair,
             program_keypair,
             verifiable,
             verifiable,
+            no_idl,
             solana_args,
             solana_args,
         } => deploy(
         } => deploy(
             &opts.cfg_override,
             &opts.cfg_override,
             program_name,
             program_name,
             program_keypair,
             program_keypair,
             verifiable,
             verifiable,
+            no_idl,
             solana_args,
             solana_args,
         ),
         ),
         Command::Expand {
         Command::Expand {
@@ -2953,7 +2962,7 @@ fn test(
         // In either case, skip the deploy if the user specifies.
         // In either case, skip the deploy if the user specifies.
         let is_localnet = cfg.provider.cluster == Cluster::Localnet;
         let is_localnet = cfg.provider.cluster == Cluster::Localnet;
         if (!is_localnet || skip_local_validator) && !skip_deploy {
         if (!is_localnet || skip_local_validator) && !skip_deploy {
-            deploy(cfg_override, None, None, false, vec![])?;
+            deploy(cfg_override, None, None, false, true, vec![])?;
         }
         }
         let mut is_first_suite = true;
         let mut is_first_suite = true;
         if let Some(test_script) = cfg.scripts.get_mut("test") {
         if let Some(test_script) = cfg.scripts.get_mut("test") {
@@ -3370,7 +3379,7 @@ fn start_test_validator(
         .test_validator
         .test_validator
         .as_ref()
         .as_ref()
         .and_then(|test| test.validator.as_ref().map(|v| v.rpc_port))
         .and_then(|test| test.validator.as_ref().map(|v| v.rpc_port))
-        .unwrap_or(solana_sdk::rpc_port::DEFAULT_RPC_PORT);
+        .unwrap_or(DEFAULT_RPC_PORT);
     if !portpicker::is_free(rpc_port) {
     if !portpicker::is_free(rpc_port) {
         return Err(anyhow!(
         return Err(anyhow!(
             "Your configured rpc port: {rpc_port} is already in use"
             "Your configured rpc port: {rpc_port} is already in use"
@@ -3520,6 +3529,7 @@ fn deploy(
     program_name: Option<String>,
     program_name: Option<String>,
     program_keypair: Option<String>,
     program_keypair: Option<String>,
     verifiable: bool,
     verifiable: bool,
+    no_idl: bool,
     solana_args: Vec<String>,
     solana_args: Vec<String>,
 ) -> Result<()> {
 ) -> Result<()> {
     // Execute the code within the workspace
     // Execute the code within the workspace
@@ -3572,16 +3582,28 @@ fn deploy(
                 std::process::exit(exit.status.code().unwrap_or(1));
                 std::process::exit(exit.status.code().unwrap_or(1));
             }
             }
 
 
+            // Get the IDL filepath
+            let idl_filepath = Path::new("target")
+                .join("idl")
+                .join(&program.lib_name)
+                .with_extension("json");
+
             if let Some(idl) = program.idl.as_mut() {
             if let Some(idl) = program.idl.as_mut() {
                 // Add program address to the IDL.
                 // Add program address to the IDL.
                 idl.address = program_id.to_string();
                 idl.address = program_id.to_string();
 
 
                 // Persist it.
                 // Persist it.
-                let idl_out = Path::new("target")
-                    .join("idl")
-                    .join(&idl.metadata.name)
-                    .with_extension("json");
-                write_idl(idl, OutFile::File(idl_out))?;
+                write_idl(idl, OutFile::File(idl_filepath.clone()))?;
+
+                // Upload the IDL to the cluster by default (unless no_idl is set)
+                if !no_idl {
+                    idl_init(
+                        cfg_override,
+                        program_id,
+                        idl_filepath.display().to_string(),
+                        None,
+                    )?;
+                }
             }
             }
         }
         }