Explorar o código

cli: Expose --account flag for solana-test-validator in Anchor.toml (#1366)

Tom Linton %!s(int64=3) %!d(string=hai) anos
pai
achega
5e2821fd85
Modificáronse 3 ficheiros con 39 adicións e 11 borrados
  1. 3 0
      CHANGELOG.md
  2. 14 1
      cli/src/config.rs
  3. 22 10
      cli/src/lib.rs

+ 3 - 0
CHANGELOG.md

@@ -22,6 +22,7 @@ incremented for features.
 * ts: Remove error logging in the event parser when log websocket encounters a program error ([#1313](https://github.com/project-serum/anchor/pull/1313)).
 * ts: Add new `methods` namespace to the program client, introducing a more ergonomic builder API ([#1324](https://github.com/project-serum/anchor/pull/1324)).
 * ts: Add registry utility for fetching the latest verified build ([#1371](https://github.com/project-serum/anchor/pull/1371)).
+* cli: Expose the solana-test-validator --account flag in Anchor.toml via [[test.validator.account]] ([#1366](https://github.com/project-serum/anchor/pull/1366)).
 
 ### Breaking
 
@@ -29,6 +30,8 @@ incremented for features.
 * lang: rename `loader_account` module to `account_loader` module ([#1279](https://github.com/project-serum/anchor/pull/1279))
 * lang: The `Accounts` trait's `try_accounts` method now has an additional `bumps: &mut BTreeMap<String, u8>` argument, which accumulates bump seeds ([#1367](https://github.com/project-serum/anchor/pull/1367)).
 * ts: `Coder` is now an interface and the existing class has been renamed to `BorshCoder`. This change allows the generation of Anchor clients for non anchor programs  ([#1259](https://github.com/project-serum/anchor/pull/1259/files)).
+* cli: [[test.clone]] key in Anchor.toml is renamed to [[test.validator.clone]] ([#1366](https://github.com/project-serum/anchor/pull/1366)).
+
 
 ## [0.20.1] - 2022-01-09
 

+ 14 - 1
cli/src/config.rs

@@ -506,7 +506,6 @@ fn deser_programs(
 #[derive(Debug, Clone, Serialize, Deserialize)]
 pub struct Test {
     pub genesis: Option<Vec<GenesisEntry>>,
-    pub clone: Option<Vec<CloneEntry>>,
     pub validator: Option<Validator>,
     pub startup_wait: Option<i32>,
 }
@@ -525,11 +524,25 @@ pub struct CloneEntry {
     pub address: String,
 }
 
+#[derive(Debug, Clone, Serialize, Deserialize)]
+pub struct AccountEntry {
+    // Base58 pubkey string.
+    pub address: String,
+    // Name of JSON file containing the account data.
+    pub filename: String,
+}
+
 #[derive(Debug, Default, Clone, Serialize, Deserialize)]
 pub struct Validator {
+    // Load an account from the provided JSON file
+    #[serde(skip_serializing_if = "Option::is_none")]
+    pub account: Option<Vec<AccountEntry>>,
     // IP address to bind the validator ports. [default: 0.0.0.0]
     #[serde(default = "default_bind_address")]
     pub bind_address: String,
+    // Copy an account from the cluster referenced by the url argument.
+    #[serde(skip_serializing_if = "Option::is_none")]
+    pub clone: Option<Vec<CloneEntry>>,
     // Range to use for dynamically assigned ports. [default: 1024-65535]
     #[serde(skip_serializing_if = "Option::is_none")]
     pub dynamic_port_range: Option<String>,

+ 22 - 10
cli/src/lib.rs

@@ -1891,22 +1891,34 @@ fn validator_flags(cfg: &WithPath<Config>) -> Result<Vec<String>> {
                 flags.push(entry.program.clone());
             }
         }
-        if let Some(clone) = &test.clone {
-            for entry in clone {
-                flags.push("--clone".to_string());
-                flags.push(entry.address.clone());
-            }
-        }
         if let Some(validator) = &test.validator {
             for (key, value) in serde_json::to_value(validator)?.as_object().unwrap() {
                 if key == "ledger" {
+                    // Ledger flag is a special case as it is passed separately to the rest of
+                    // these validator flags.
                     continue;
                 };
-                flags.push(format!("--{}", key.replace('_', "-")));
-                if let serde_json::Value::String(v) = value {
-                    flags.push(v.to_string());
+                if key == "account" {
+                    for entry in value.as_array().unwrap() {
+                        // Push the account flag for each array entry
+                        flags.push("--account".to_string());
+                        flags.push(entry["address"].as_str().unwrap().to_string());
+                        flags.push(entry["filename"].as_str().unwrap().to_string());
+                    }
+                } else if key == "clone" {
+                    for entry in value.as_array().unwrap() {
+                        // Push the clone flag for each array entry
+                        flags.push("--clone".to_string());
+                        flags.push(entry["address"].as_str().unwrap().to_string());
+                    }
                 } else {
-                    flags.push(value.to_string());
+                    // Remaining validator flags are non-array types
+                    flags.push(format!("--{}", key.replace('_', "-")));
+                    if let serde_json::Value::String(v) = value {
+                        flags.push(v.to_string());
+                    } else {
+                        flags.push(value.to_string());
+                    }
                 }
             }
         }