config.rs 52 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539
  1. use crate::{get_keypair, is_hidden, keys_sync};
  2. use anchor_client::Cluster;
  3. use anchor_lang_idl::types::Idl;
  4. use anyhow::{anyhow, bail, Context, Error, Result};
  5. use clap::{Parser, ValueEnum};
  6. use dirs::home_dir;
  7. use heck::ToSnakeCase;
  8. use reqwest::Url;
  9. use serde::de::{self, MapAccess, Visitor};
  10. use serde::ser::SerializeMap;
  11. use serde::{Deserialize, Deserializer, Serialize, Serializer};
  12. use solana_cli_config::{Config as SolanaConfig, CONFIG_FILE};
  13. use solana_sdk::clock::Slot;
  14. use solana_sdk::pubkey::Pubkey;
  15. use solana_sdk::signature::{Keypair, Signer};
  16. use solang_parser::pt::{ContractTy, SourceUnitPart};
  17. use std::collections::{BTreeMap, HashMap};
  18. use std::convert::TryFrom;
  19. use std::ffi::OsStr;
  20. use std::fs::{self, File};
  21. use std::io::prelude::*;
  22. use std::marker::PhantomData;
  23. use std::ops::Deref;
  24. use std::path::Path;
  25. use std::path::PathBuf;
  26. use std::str::FromStr;
  27. use std::{fmt, io};
  28. use walkdir::WalkDir;
  29. pub trait Merge: Sized {
  30. fn merge(&mut self, _other: Self) {}
  31. }
  32. #[derive(Default, Debug, Parser)]
  33. pub struct ConfigOverride {
  34. /// Cluster override.
  35. #[clap(global = true, long = "provider.cluster")]
  36. pub cluster: Option<Cluster>,
  37. /// Wallet override.
  38. #[clap(global = true, long = "provider.wallet")]
  39. pub wallet: Option<WalletPath>,
  40. }
  41. #[derive(Debug)]
  42. pub struct WithPath<T> {
  43. inner: T,
  44. path: PathBuf,
  45. }
  46. impl<T> WithPath<T> {
  47. pub fn new(inner: T, path: PathBuf) -> Self {
  48. Self { inner, path }
  49. }
  50. pub fn path(&self) -> &PathBuf {
  51. &self.path
  52. }
  53. pub fn into_inner(self) -> T {
  54. self.inner
  55. }
  56. }
  57. impl<T> std::convert::AsRef<T> for WithPath<T> {
  58. fn as_ref(&self) -> &T {
  59. &self.inner
  60. }
  61. }
  62. #[derive(Debug, Clone, PartialEq)]
  63. pub struct Manifest(cargo_toml::Manifest);
  64. impl Manifest {
  65. pub fn from_path(p: impl AsRef<Path>) -> Result<Self> {
  66. cargo_toml::Manifest::from_path(&p)
  67. .map(Manifest)
  68. .map_err(anyhow::Error::from)
  69. .with_context(|| format!("Error reading manifest from path: {}", p.as_ref().display()))
  70. }
  71. pub fn lib_name(&self) -> Result<String> {
  72. match &self.lib {
  73. Some(cargo_toml::Product {
  74. name: Some(name), ..
  75. }) => Ok(name.to_owned()),
  76. _ => self
  77. .package
  78. .as_ref()
  79. .ok_or_else(|| anyhow!("package section not provided"))
  80. .map(|pkg| pkg.name.to_snake_case()),
  81. }
  82. }
  83. pub fn version(&self) -> String {
  84. match &self.package {
  85. Some(package) => package.version().to_string(),
  86. _ => "0.0.0".to_string(),
  87. }
  88. }
  89. // Climbs each parent directory from the current dir until we find a Cargo.toml
  90. pub fn discover() -> Result<Option<WithPath<Manifest>>> {
  91. Manifest::discover_from_path(std::env::current_dir()?)
  92. }
  93. // Climbs each parent directory from a given starting directory until we find a Cargo.toml.
  94. pub fn discover_from_path(start_from: PathBuf) -> Result<Option<WithPath<Manifest>>> {
  95. let mut cwd_opt = Some(start_from.as_path());
  96. while let Some(cwd) = cwd_opt {
  97. let mut anchor_toml = false;
  98. for f in fs::read_dir(cwd).with_context(|| {
  99. format!("Error reading the directory with path: {}", cwd.display())
  100. })? {
  101. let p = f
  102. .with_context(|| {
  103. format!("Error reading the directory with path: {}", cwd.display())
  104. })?
  105. .path();
  106. if let Some(filename) = p.file_name().and_then(|name| name.to_str()) {
  107. if filename == "Cargo.toml" {
  108. return Ok(Some(WithPath::new(Manifest::from_path(&p)?, p)));
  109. }
  110. if filename == "Anchor.toml" {
  111. anchor_toml = true;
  112. }
  113. }
  114. }
  115. // Not found. Go up a directory level, but don't go up from Anchor.toml
  116. if anchor_toml {
  117. break;
  118. }
  119. cwd_opt = cwd.parent();
  120. }
  121. Ok(None)
  122. }
  123. }
  124. impl Deref for Manifest {
  125. type Target = cargo_toml::Manifest;
  126. fn deref(&self) -> &Self::Target {
  127. &self.0
  128. }
  129. }
  130. impl WithPath<Config> {
  131. pub fn get_rust_program_list(&self) -> Result<Vec<PathBuf>> {
  132. // Canonicalize the workspace filepaths to compare with relative paths.
  133. let (members, exclude) = self.canonicalize_workspace()?;
  134. // Get all candidate programs.
  135. //
  136. // If [workspace.members] exists, then use that.
  137. // Otherwise, default to `programs/*`.
  138. let program_paths: Vec<PathBuf> = {
  139. if members.is_empty() {
  140. let path = self.path().parent().unwrap().join("programs");
  141. if let Ok(entries) = fs::read_dir(path) {
  142. entries
  143. .filter(|entry| entry.as_ref().map(|e| e.path().is_dir()).unwrap_or(false))
  144. .map(|dir| dir.map(|d| d.path().canonicalize().unwrap()))
  145. .collect::<Vec<Result<PathBuf, std::io::Error>>>()
  146. .into_iter()
  147. .collect::<Result<Vec<PathBuf>, std::io::Error>>()?
  148. } else {
  149. Vec::new()
  150. }
  151. } else {
  152. members
  153. }
  154. };
  155. // Filter out everything part of the exclude array.
  156. Ok(program_paths
  157. .into_iter()
  158. .filter(|m| !exclude.contains(m))
  159. .collect())
  160. }
  161. /// Parse all the files with the .sol extension, and get a list of the all
  162. /// contracts defined in them along with their path. One Solidity file may
  163. /// define multiple contracts.
  164. pub fn get_solidity_program_list(&self) -> Result<Vec<(String, PathBuf)>> {
  165. let path = self.path().parent().unwrap().join("solidity");
  166. let mut res = Vec::new();
  167. if let Ok(entries) = fs::read_dir(path) {
  168. for entry in entries {
  169. let path = entry?.path();
  170. if !path.is_file() || path.extension() != Some(OsStr::new("sol")) {
  171. continue;
  172. }
  173. let source = fs::read_to_string(&path)?;
  174. let tree = match solang_parser::parse(&source, 0) {
  175. Ok((tree, _)) => tree,
  176. Err(diag) => {
  177. // The parser can return multiple errors, however this is exceedingly rare.
  178. // Just use the first one, else the formatting will be a mess.
  179. bail!(
  180. "{}: {}: {}",
  181. path.display(),
  182. diag[0].level.to_string(),
  183. diag[0].message
  184. );
  185. }
  186. };
  187. tree.0.iter().for_each(|part| {
  188. if let SourceUnitPart::ContractDefinition(contract) = part {
  189. // Must be a contract, not library/interface/abstract contract
  190. if matches!(&contract.ty, ContractTy::Contract(..)) {
  191. if let Some(name) = &contract.name {
  192. res.push((name.name.clone(), path.clone()));
  193. }
  194. }
  195. }
  196. });
  197. }
  198. }
  199. Ok(res)
  200. }
  201. pub fn read_all_programs(&self) -> Result<Vec<Program>> {
  202. let mut r = vec![];
  203. for path in self.get_rust_program_list()? {
  204. let cargo = Manifest::from_path(path.join("Cargo.toml"))?;
  205. let lib_name = cargo.lib_name()?;
  206. let idl_filepath = Path::new("target")
  207. .join("idl")
  208. .join(&lib_name)
  209. .with_extension("json");
  210. let idl = fs::read(idl_filepath)
  211. .ok()
  212. .map(|bytes| serde_json::from_reader(&*bytes))
  213. .transpose()?;
  214. r.push(Program {
  215. lib_name,
  216. solidity: false,
  217. path,
  218. idl,
  219. });
  220. }
  221. for (lib_name, path) in self.get_solidity_program_list()? {
  222. let idl_filepath = Path::new("target")
  223. .join("idl")
  224. .join(&lib_name)
  225. .with_extension("json");
  226. let idl = fs::read(idl_filepath)
  227. .ok()
  228. .map(|bytes| serde_json::from_reader(&*bytes))
  229. .transpose()?;
  230. r.push(Program {
  231. lib_name,
  232. solidity: true,
  233. path,
  234. idl,
  235. });
  236. }
  237. Ok(r)
  238. }
  239. /// Read and get all the programs from the workspace.
  240. ///
  241. /// This method will only return the given program if `name` exists.
  242. pub fn get_programs(&self, name: Option<String>) -> Result<Vec<Program>> {
  243. let programs = self.read_all_programs()?;
  244. let programs = match name {
  245. Some(name) => vec![programs
  246. .into_iter()
  247. .find(|program| {
  248. name == program.lib_name
  249. || name == program.path.file_name().unwrap().to_str().unwrap()
  250. })
  251. .ok_or_else(|| anyhow!("Program {name} not found"))?],
  252. None => programs,
  253. };
  254. Ok(programs)
  255. }
  256. /// Get the specified program from the workspace.
  257. pub fn get_program(&self, name: &str) -> Result<Program> {
  258. self.get_programs(Some(name.to_owned()))?
  259. .into_iter()
  260. .next()
  261. .ok_or_else(|| anyhow!("Expected a program"))
  262. }
  263. pub fn canonicalize_workspace(&self) -> Result<(Vec<PathBuf>, Vec<PathBuf>)> {
  264. let members = self.process_paths(&self.workspace.members)?;
  265. let exclude = self.process_paths(&self.workspace.exclude)?;
  266. Ok((members, exclude))
  267. }
  268. fn process_paths(&self, paths: &[String]) -> Result<Vec<PathBuf>, Error> {
  269. let base_path = self.path().parent().unwrap();
  270. paths
  271. .iter()
  272. .flat_map(|m| {
  273. let path = base_path.join(m);
  274. if m.ends_with("/*") {
  275. let dir = path.parent().unwrap();
  276. match fs::read_dir(dir) {
  277. Ok(entries) => entries
  278. .filter_map(|entry| entry.ok())
  279. .map(|entry| self.process_single_path(&entry.path()))
  280. .collect(),
  281. Err(e) => vec![Err(Error::new(io::Error::other(format!(
  282. "Error reading directory {:?}: {}",
  283. dir, e
  284. ))))],
  285. }
  286. } else {
  287. vec![self.process_single_path(&path)]
  288. }
  289. })
  290. .collect()
  291. }
  292. fn process_single_path(&self, path: &PathBuf) -> Result<PathBuf, Error> {
  293. path.canonicalize().map_err(|e| {
  294. Error::new(io::Error::other(format!(
  295. "Error canonicalizing path {:?}: {}",
  296. path, e
  297. )))
  298. })
  299. }
  300. }
  301. impl<T> std::ops::Deref for WithPath<T> {
  302. type Target = T;
  303. fn deref(&self) -> &Self::Target {
  304. &self.inner
  305. }
  306. }
  307. impl<T> std::ops::DerefMut for WithPath<T> {
  308. fn deref_mut(&mut self) -> &mut Self::Target {
  309. &mut self.inner
  310. }
  311. }
  312. #[derive(Debug, Default)]
  313. pub struct Config {
  314. pub toolchain: ToolchainConfig,
  315. pub features: FeaturesConfig,
  316. pub registry: RegistryConfig,
  317. pub provider: ProviderConfig,
  318. pub programs: ProgramsConfig,
  319. pub scripts: ScriptsConfig,
  320. pub workspace: WorkspaceConfig,
  321. // Separate entry next to test_config because
  322. // "anchor localnet" only has access to the Anchor.toml,
  323. // not the Test.toml files
  324. pub test_validator: Option<TestValidator>,
  325. pub test_config: Option<TestConfig>,
  326. }
  327. #[derive(Default, Clone, Debug, Serialize, Deserialize)]
  328. pub struct ToolchainConfig {
  329. pub anchor_version: Option<String>,
  330. pub solana_version: Option<String>,
  331. pub package_manager: Option<PackageManager>,
  332. }
  333. /// Package manager to use for the project.
  334. #[derive(Clone, Debug, Default, Eq, PartialEq, Parser, ValueEnum, Serialize, Deserialize)]
  335. #[serde(rename_all = "lowercase")]
  336. pub enum PackageManager {
  337. /// Use npm as the package manager.
  338. NPM,
  339. /// Use yarn as the package manager.
  340. #[default]
  341. Yarn,
  342. /// Use pnpm as the package manager.
  343. PNPM,
  344. /// Use bun as the package manager.
  345. Bun,
  346. }
  347. impl std::fmt::Display for PackageManager {
  348. fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
  349. let pkg_manager_str = match self {
  350. PackageManager::NPM => "npm",
  351. PackageManager::Yarn => "yarn",
  352. PackageManager::PNPM => "pnpm",
  353. PackageManager::Bun => "bun",
  354. };
  355. write!(f, "{pkg_manager_str}")
  356. }
  357. }
  358. #[derive(Clone, Debug, Serialize, Deserialize)]
  359. pub struct FeaturesConfig {
  360. /// Enable account resolution.
  361. ///
  362. /// Not able to specify default bool value: https://github.com/serde-rs/serde/issues/368
  363. #[serde(default = "FeaturesConfig::get_default_resolution")]
  364. pub resolution: bool,
  365. /// Disable safety comment checks
  366. #[serde(default, rename = "skip-lint")]
  367. pub skip_lint: bool,
  368. }
  369. impl FeaturesConfig {
  370. fn get_default_resolution() -> bool {
  371. true
  372. }
  373. }
  374. impl Default for FeaturesConfig {
  375. fn default() -> Self {
  376. Self {
  377. resolution: Self::get_default_resolution(),
  378. skip_lint: false,
  379. }
  380. }
  381. }
  382. #[derive(Clone, Debug, Serialize, Deserialize)]
  383. pub struct RegistryConfig {
  384. pub url: String,
  385. }
  386. impl Default for RegistryConfig {
  387. fn default() -> Self {
  388. Self {
  389. url: "https://api.apr.dev".to_string(),
  390. }
  391. }
  392. }
  393. #[derive(Debug, Default)]
  394. pub struct ProviderConfig {
  395. pub cluster: Cluster,
  396. pub wallet: WalletPath,
  397. }
  398. pub type ScriptsConfig = BTreeMap<String, String>;
  399. pub type ProgramsConfig = BTreeMap<Cluster, BTreeMap<String, ProgramDeployment>>;
  400. #[derive(Debug, Default, Clone, Serialize, Deserialize)]
  401. pub struct WorkspaceConfig {
  402. #[serde(default, skip_serializing_if = "Vec::is_empty")]
  403. pub members: Vec<String>,
  404. #[serde(default, skip_serializing_if = "Vec::is_empty")]
  405. pub exclude: Vec<String>,
  406. #[serde(default, skip_serializing_if = "String::is_empty")]
  407. pub types: String,
  408. }
  409. #[derive(ValueEnum, Parser, Clone, PartialEq, Eq, Debug)]
  410. pub enum BootstrapMode {
  411. None,
  412. Debian,
  413. }
  414. #[derive(ValueEnum, Parser, Clone, PartialEq, Eq, Debug)]
  415. pub enum ProgramArch {
  416. Bpf,
  417. Sbf,
  418. }
  419. impl ProgramArch {
  420. pub fn build_subcommand(&self) -> &str {
  421. match self {
  422. Self::Bpf => "build-bpf",
  423. Self::Sbf => "build-sbf",
  424. }
  425. }
  426. }
  427. #[derive(Debug, Clone)]
  428. pub struct BuildConfig {
  429. pub verifiable: bool,
  430. pub solana_version: Option<String>,
  431. pub docker_image: String,
  432. pub bootstrap: BootstrapMode,
  433. }
  434. impl Config {
  435. pub fn add_test_config(
  436. &mut self,
  437. root: impl AsRef<Path>,
  438. test_paths: Vec<PathBuf>,
  439. ) -> Result<()> {
  440. self.test_config = TestConfig::discover(root, test_paths)?;
  441. Ok(())
  442. }
  443. pub fn docker(&self) -> String {
  444. let version = self
  445. .toolchain
  446. .anchor_version
  447. .as_deref()
  448. .unwrap_or(crate::DOCKER_BUILDER_VERSION);
  449. format!("solanafoundation/anchor:v{version}")
  450. }
  451. pub fn discover(cfg_override: &ConfigOverride) -> Result<Option<WithPath<Config>>> {
  452. Config::_discover().map(|opt| {
  453. opt.map(|mut cfg| {
  454. if let Some(cluster) = cfg_override.cluster.clone() {
  455. cfg.provider.cluster = cluster;
  456. }
  457. if let Some(wallet) = cfg_override.wallet.clone() {
  458. cfg.provider.wallet = wallet;
  459. }
  460. cfg
  461. })
  462. })
  463. }
  464. // Climbs each parent directory until we find an Anchor.toml.
  465. fn _discover() -> Result<Option<WithPath<Config>>> {
  466. let _cwd = std::env::current_dir()?;
  467. let mut cwd_opt = Some(_cwd.as_path());
  468. while let Some(cwd) = cwd_opt {
  469. for f in fs::read_dir(cwd).with_context(|| {
  470. format!("Error reading the directory with path: {}", cwd.display())
  471. })? {
  472. let p = f
  473. .with_context(|| {
  474. format!("Error reading the directory with path: {}", cwd.display())
  475. })?
  476. .path();
  477. if let Some(filename) = p.file_name() {
  478. if filename.to_str() == Some("Anchor.toml") {
  479. // Make sure the program id is correct (only on the initial build)
  480. let mut cfg = Config::from_path(&p)?;
  481. let deploy_dir = p.parent().unwrap().join("target").join("deploy");
  482. if !deploy_dir.exists() && !cfg.programs.contains_key(&Cluster::Localnet) {
  483. println!("Updating program ids...");
  484. fs::create_dir_all(deploy_dir)?;
  485. keys_sync(&ConfigOverride::default(), None)?;
  486. cfg = Config::from_path(&p)?;
  487. }
  488. return Ok(Some(WithPath::new(cfg, p)));
  489. }
  490. }
  491. }
  492. cwd_opt = cwd.parent();
  493. }
  494. Ok(None)
  495. }
  496. fn from_path(p: impl AsRef<Path>) -> Result<Self> {
  497. fs::read_to_string(&p)
  498. .with_context(|| format!("Error reading the file with path: {}", p.as_ref().display()))?
  499. .parse::<Self>()
  500. }
  501. pub fn wallet_kp(&self) -> Result<Keypair> {
  502. get_keypair(&self.provider.wallet.to_string())
  503. }
  504. }
  505. #[derive(Debug, Serialize, Deserialize)]
  506. struct _Config {
  507. toolchain: Option<ToolchainConfig>,
  508. features: Option<FeaturesConfig>,
  509. programs: Option<BTreeMap<String, BTreeMap<String, serde_json::Value>>>,
  510. registry: Option<RegistryConfig>,
  511. provider: Provider,
  512. workspace: Option<WorkspaceConfig>,
  513. scripts: Option<ScriptsConfig>,
  514. test: Option<_TestValidator>,
  515. }
  516. #[derive(Debug, Serialize, Deserialize)]
  517. struct Provider {
  518. #[serde(serialize_with = "ser_cluster", deserialize_with = "des_cluster")]
  519. cluster: Cluster,
  520. wallet: String,
  521. }
  522. fn ser_cluster<S: Serializer>(cluster: &Cluster, s: S) -> Result<S::Ok, S::Error> {
  523. match cluster {
  524. Cluster::Custom(http, ws) => {
  525. match (Url::parse(http), Url::parse(ws)) {
  526. // If `ws` was derived from `http`, serialize `http` as string
  527. (Ok(h), Ok(w)) if h.domain() == w.domain() => s.serialize_str(http),
  528. _ => {
  529. let mut map = s.serialize_map(Some(2))?;
  530. map.serialize_entry("http", http)?;
  531. map.serialize_entry("ws", ws)?;
  532. map.end()
  533. }
  534. }
  535. }
  536. _ => s.serialize_str(&cluster.to_string()),
  537. }
  538. }
  539. fn des_cluster<'de, D>(deserializer: D) -> Result<Cluster, D::Error>
  540. where
  541. D: Deserializer<'de>,
  542. {
  543. struct StringOrCustomCluster(PhantomData<fn() -> Cluster>);
  544. impl<'de> Visitor<'de> for StringOrCustomCluster {
  545. type Value = Cluster;
  546. fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
  547. formatter.write_str("string or map")
  548. }
  549. fn visit_str<E>(self, value: &str) -> Result<Cluster, E>
  550. where
  551. E: de::Error,
  552. {
  553. value.parse().map_err(de::Error::custom)
  554. }
  555. fn visit_map<M>(self, mut map: M) -> Result<Cluster, M::Error>
  556. where
  557. M: MapAccess<'de>,
  558. {
  559. // Gets keys
  560. if let (Some((http_key, http_value)), Some((ws_key, ws_value))) = (
  561. map.next_entry::<String, String>()?,
  562. map.next_entry::<String, String>()?,
  563. ) {
  564. // Checks keys
  565. if http_key != "http" || ws_key != "ws" {
  566. return Err(de::Error::custom("Invalid key"));
  567. }
  568. // Checks urls
  569. Url::parse(&http_value).map_err(de::Error::custom)?;
  570. Url::parse(&ws_value).map_err(de::Error::custom)?;
  571. Ok(Cluster::Custom(http_value, ws_value))
  572. } else {
  573. Err(de::Error::custom("Invalid entry"))
  574. }
  575. }
  576. }
  577. deserializer.deserialize_any(StringOrCustomCluster(PhantomData))
  578. }
  579. impl fmt::Display for Config {
  580. fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
  581. let programs = {
  582. let c = ser_programs(&self.programs);
  583. if c.is_empty() {
  584. None
  585. } else {
  586. Some(c)
  587. }
  588. };
  589. let cfg = _Config {
  590. toolchain: Some(self.toolchain.clone()),
  591. features: Some(self.features.clone()),
  592. registry: Some(self.registry.clone()),
  593. provider: Provider {
  594. cluster: self.provider.cluster.clone(),
  595. wallet: self.provider.wallet.stringify_with_tilde(),
  596. },
  597. test: self.test_validator.clone().map(Into::into),
  598. scripts: match self.scripts.is_empty() {
  599. true => None,
  600. false => Some(self.scripts.clone()),
  601. },
  602. programs,
  603. workspace: (!self.workspace.members.is_empty() || !self.workspace.exclude.is_empty())
  604. .then(|| self.workspace.clone()),
  605. };
  606. let cfg = toml::to_string(&cfg).expect("Must be well formed");
  607. write!(f, "{}", cfg)
  608. }
  609. }
  610. impl FromStr for Config {
  611. type Err = Error;
  612. fn from_str(s: &str) -> Result<Self, Self::Err> {
  613. let cfg: _Config =
  614. toml::from_str(s).map_err(|e| anyhow!("Unable to deserialize config: {e}"))?;
  615. Ok(Config {
  616. toolchain: cfg.toolchain.unwrap_or_default(),
  617. features: cfg.features.unwrap_or_default(),
  618. registry: cfg.registry.unwrap_or_default(),
  619. provider: ProviderConfig {
  620. cluster: cfg.provider.cluster,
  621. wallet: shellexpand::tilde(&cfg.provider.wallet).parse()?,
  622. },
  623. scripts: cfg.scripts.unwrap_or_default(),
  624. test_validator: cfg.test.map(Into::into),
  625. test_config: None,
  626. programs: cfg.programs.map_or(Ok(BTreeMap::new()), deser_programs)?,
  627. workspace: cfg.workspace.unwrap_or_default(),
  628. })
  629. }
  630. }
  631. pub fn get_solana_cfg_url() -> Result<String, io::Error> {
  632. let config_file = CONFIG_FILE.as_ref().ok_or_else(|| {
  633. io::Error::new(
  634. io::ErrorKind::NotFound,
  635. "Default Solana config was not found",
  636. )
  637. })?;
  638. SolanaConfig::load(config_file).map(|config| config.json_rpc_url)
  639. }
  640. fn ser_programs(
  641. programs: &BTreeMap<Cluster, BTreeMap<String, ProgramDeployment>>,
  642. ) -> BTreeMap<String, BTreeMap<String, serde_json::Value>> {
  643. programs
  644. .iter()
  645. .map(|(cluster, programs)| {
  646. let cluster = cluster.to_string();
  647. let programs = programs
  648. .iter()
  649. .map(|(name, deployment)| {
  650. (
  651. name.clone(),
  652. to_value(&_ProgramDeployment::from(deployment)),
  653. )
  654. })
  655. .collect::<BTreeMap<String, serde_json::Value>>();
  656. (cluster, programs)
  657. })
  658. .collect::<BTreeMap<String, BTreeMap<String, serde_json::Value>>>()
  659. }
  660. fn to_value(dep: &_ProgramDeployment) -> serde_json::Value {
  661. if dep.path.is_none() && dep.idl.is_none() {
  662. return serde_json::Value::String(dep.address.to_string());
  663. }
  664. serde_json::to_value(dep).unwrap()
  665. }
  666. fn deser_programs(
  667. programs: BTreeMap<String, BTreeMap<String, serde_json::Value>>,
  668. ) -> Result<BTreeMap<Cluster, BTreeMap<String, ProgramDeployment>>> {
  669. programs
  670. .iter()
  671. .map(|(cluster, programs)| {
  672. let cluster: Cluster = cluster.parse()?;
  673. let programs = programs
  674. .iter()
  675. .map(|(name, program_id)| {
  676. Ok((
  677. name.clone(),
  678. ProgramDeployment::try_from(match &program_id {
  679. serde_json::Value::String(address) => _ProgramDeployment {
  680. address: address.parse()?,
  681. path: None,
  682. idl: None,
  683. },
  684. serde_json::Value::Object(_) => {
  685. serde_json::from_value(program_id.clone())
  686. .map_err(|_| anyhow!("Unable to read toml"))?
  687. }
  688. _ => return Err(anyhow!("Invalid toml type")),
  689. })?,
  690. ))
  691. })
  692. .collect::<Result<BTreeMap<String, ProgramDeployment>>>()?;
  693. Ok((cluster, programs))
  694. })
  695. .collect::<Result<BTreeMap<Cluster, BTreeMap<String, ProgramDeployment>>>>()
  696. }
  697. #[derive(Default, Debug, Clone, Serialize, Deserialize)]
  698. pub struct TestValidator {
  699. pub genesis: Option<Vec<GenesisEntry>>,
  700. pub validator: Option<Validator>,
  701. pub startup_wait: i32,
  702. pub shutdown_wait: i32,
  703. pub upgradeable: bool,
  704. }
  705. #[derive(Default, Debug, Clone, Serialize, Deserialize)]
  706. pub struct _TestValidator {
  707. #[serde(skip_serializing_if = "Option::is_none")]
  708. pub genesis: Option<Vec<GenesisEntry>>,
  709. #[serde(skip_serializing_if = "Option::is_none")]
  710. pub validator: Option<_Validator>,
  711. #[serde(skip_serializing_if = "Option::is_none")]
  712. pub startup_wait: Option<i32>,
  713. #[serde(skip_serializing_if = "Option::is_none")]
  714. pub shutdown_wait: Option<i32>,
  715. #[serde(skip_serializing_if = "Option::is_none")]
  716. pub upgradeable: Option<bool>,
  717. }
  718. pub const STARTUP_WAIT: i32 = 5000;
  719. pub const SHUTDOWN_WAIT: i32 = 2000;
  720. impl From<_TestValidator> for TestValidator {
  721. fn from(_test_validator: _TestValidator) -> Self {
  722. Self {
  723. shutdown_wait: _test_validator.shutdown_wait.unwrap_or(SHUTDOWN_WAIT),
  724. startup_wait: _test_validator.startup_wait.unwrap_or(STARTUP_WAIT),
  725. genesis: _test_validator.genesis,
  726. validator: _test_validator.validator.map(Into::into),
  727. upgradeable: _test_validator.upgradeable.unwrap_or(false),
  728. }
  729. }
  730. }
  731. impl From<TestValidator> for _TestValidator {
  732. fn from(test_validator: TestValidator) -> Self {
  733. Self {
  734. shutdown_wait: Some(test_validator.shutdown_wait),
  735. startup_wait: Some(test_validator.startup_wait),
  736. genesis: test_validator.genesis,
  737. validator: test_validator.validator.map(Into::into),
  738. upgradeable: Some(test_validator.upgradeable),
  739. }
  740. }
  741. }
  742. #[derive(Debug, Clone)]
  743. pub struct TestConfig {
  744. pub test_suite_configs: HashMap<PathBuf, TestToml>,
  745. }
  746. impl Deref for TestConfig {
  747. type Target = HashMap<PathBuf, TestToml>;
  748. fn deref(&self) -> &Self::Target {
  749. &self.test_suite_configs
  750. }
  751. }
  752. impl TestConfig {
  753. pub fn discover(root: impl AsRef<Path>, test_paths: Vec<PathBuf>) -> Result<Option<Self>> {
  754. let walker = WalkDir::new(root).into_iter();
  755. let mut test_suite_configs = HashMap::new();
  756. for entry in walker.filter_entry(|e| !is_hidden(e)) {
  757. let entry = entry?;
  758. if entry.file_name() == "Test.toml" {
  759. let entry_path = entry.path();
  760. let test_toml = TestToml::from_path(entry_path)?;
  761. if test_paths.is_empty() || test_paths.iter().any(|p| entry_path.starts_with(p)) {
  762. test_suite_configs.insert(entry.path().into(), test_toml);
  763. }
  764. }
  765. }
  766. Ok(match test_suite_configs.is_empty() {
  767. true => None,
  768. false => Some(Self { test_suite_configs }),
  769. })
  770. }
  771. }
  772. // This file needs to have the same (sub)structure as Anchor.toml
  773. // so it can be parsed as a base test file from an Anchor.toml
  774. #[derive(Debug, Clone, Serialize, Deserialize)]
  775. pub struct _TestToml {
  776. pub extends: Option<Vec<String>>,
  777. pub test: Option<_TestValidator>,
  778. pub scripts: Option<ScriptsConfig>,
  779. }
  780. impl _TestToml {
  781. fn from_path(path: impl AsRef<Path>) -> Result<Self, Error> {
  782. let s = fs::read_to_string(&path)?;
  783. let parsed_toml: Self = toml::from_str(&s)?;
  784. let mut current_toml = _TestToml {
  785. extends: None,
  786. test: None,
  787. scripts: None,
  788. };
  789. if let Some(bases) = &parsed_toml.extends {
  790. for base in bases {
  791. let mut canonical_base = base.clone();
  792. canonical_base = canonicalize_filepath_from_origin(&canonical_base, &path)?;
  793. current_toml.merge(_TestToml::from_path(&canonical_base)?);
  794. }
  795. }
  796. current_toml.merge(parsed_toml);
  797. if let Some(test) = &mut current_toml.test {
  798. if let Some(genesis_programs) = &mut test.genesis {
  799. for entry in genesis_programs {
  800. entry.program = canonicalize_filepath_from_origin(&entry.program, &path)?;
  801. }
  802. }
  803. if let Some(validator) = &mut test.validator {
  804. if let Some(ledger_dir) = &mut validator.ledger {
  805. *ledger_dir = canonicalize_filepath_from_origin(&ledger_dir, &path)?;
  806. }
  807. if let Some(accounts) = &mut validator.account {
  808. for entry in accounts {
  809. entry.filename = canonicalize_filepath_from_origin(&entry.filename, &path)?;
  810. }
  811. }
  812. }
  813. }
  814. Ok(current_toml)
  815. }
  816. }
  817. /// canonicalizes the `file_path` arg.
  818. /// uses the `path` arg as the current dir
  819. /// from which to turn the relative path
  820. /// into a canonical one
  821. fn canonicalize_filepath_from_origin(
  822. file_path: impl AsRef<Path>,
  823. origin: impl AsRef<Path>,
  824. ) -> Result<String> {
  825. let previous_dir = std::env::current_dir()?;
  826. std::env::set_current_dir(origin.as_ref().parent().unwrap())?;
  827. let result = fs::canonicalize(&file_path)
  828. .with_context(|| {
  829. format!(
  830. "Error reading (possibly relative) path: {}. If relative, this is the path that was used as the current path: {}",
  831. &file_path.as_ref().display(),
  832. &origin.as_ref().display()
  833. )
  834. })?
  835. .display()
  836. .to_string();
  837. std::env::set_current_dir(previous_dir)?;
  838. Ok(result)
  839. }
  840. #[derive(Debug, Clone, Serialize, Deserialize)]
  841. pub struct TestToml {
  842. #[serde(skip_serializing_if = "Option::is_none")]
  843. pub test: Option<TestValidator>,
  844. pub scripts: ScriptsConfig,
  845. }
  846. impl TestToml {
  847. pub fn from_path(p: impl AsRef<Path>) -> Result<Self> {
  848. WithPath::new(_TestToml::from_path(&p)?, p.as_ref().into()).try_into()
  849. }
  850. }
  851. impl Merge for _TestToml {
  852. fn merge(&mut self, other: Self) {
  853. let mut my_scripts = self.scripts.take();
  854. match &mut my_scripts {
  855. None => my_scripts = other.scripts,
  856. Some(my_scripts) => {
  857. if let Some(other_scripts) = other.scripts {
  858. for (name, script) in other_scripts {
  859. my_scripts.insert(name, script);
  860. }
  861. }
  862. }
  863. }
  864. let mut my_test = self.test.take();
  865. match &mut my_test {
  866. Some(my_test) => {
  867. if let Some(other_test) = other.test {
  868. if let Some(startup_wait) = other_test.startup_wait {
  869. my_test.startup_wait = Some(startup_wait);
  870. }
  871. if let Some(other_genesis) = other_test.genesis {
  872. match &mut my_test.genesis {
  873. Some(my_genesis) => {
  874. for other_entry in other_genesis {
  875. match my_genesis
  876. .iter()
  877. .position(|g| *g.address == other_entry.address)
  878. {
  879. None => my_genesis.push(other_entry),
  880. Some(i) => my_genesis[i] = other_entry,
  881. }
  882. }
  883. }
  884. None => my_test.genesis = Some(other_genesis),
  885. }
  886. }
  887. let mut my_validator = my_test.validator.take();
  888. match &mut my_validator {
  889. None => my_validator = other_test.validator,
  890. Some(my_validator) => {
  891. if let Some(other_validator) = other_test.validator {
  892. my_validator.merge(other_validator)
  893. }
  894. }
  895. }
  896. my_test.validator = my_validator;
  897. }
  898. }
  899. None => my_test = other.test,
  900. };
  901. // Instantiating a new Self object here ensures that
  902. // this function will fail to compile if new fields get added
  903. // to Self. This is useful as a reminder if they also require merging
  904. *self = Self {
  905. test: my_test,
  906. scripts: my_scripts,
  907. extends: self.extends.take(),
  908. };
  909. }
  910. }
  911. impl TryFrom<WithPath<_TestToml>> for TestToml {
  912. type Error = Error;
  913. fn try_from(mut value: WithPath<_TestToml>) -> Result<Self, Self::Error> {
  914. Ok(Self {
  915. test: value.test.take().map(Into::into),
  916. scripts: value
  917. .scripts
  918. .take()
  919. .ok_or_else(|| anyhow!("Missing 'scripts' section in Test.toml file."))?,
  920. })
  921. }
  922. }
  923. #[derive(Debug, Clone, Serialize, Deserialize)]
  924. pub struct GenesisEntry {
  925. // Base58 pubkey string.
  926. pub address: String,
  927. // Filepath to the compiled program to embed into the genesis.
  928. pub program: String,
  929. // Whether the genesis program is upgradeable.
  930. pub upgradeable: Option<bool>,
  931. }
  932. #[derive(Debug, Clone, Serialize, Deserialize)]
  933. pub struct CloneEntry {
  934. // Base58 pubkey string.
  935. pub address: String,
  936. }
  937. #[derive(Debug, Clone, Serialize, Deserialize)]
  938. pub struct AccountEntry {
  939. // Base58 pubkey string.
  940. pub address: String,
  941. // Name of JSON file containing the account data.
  942. pub filename: String,
  943. }
  944. #[derive(Debug, Clone, Serialize, Deserialize)]
  945. pub struct AccountDirEntry {
  946. // Directory containing account JSON files
  947. pub directory: String,
  948. }
  949. #[derive(Debug, Default, Clone, Serialize, Deserialize)]
  950. pub struct _Validator {
  951. // Load an account from the provided JSON file
  952. #[serde(skip_serializing_if = "Option::is_none")]
  953. pub account: Option<Vec<AccountEntry>>,
  954. // Load all the accounts from the JSON files found in the specified DIRECTORY
  955. #[serde(skip_serializing_if = "Option::is_none")]
  956. pub account_dir: Option<Vec<AccountDirEntry>>,
  957. // IP address to bind the validator ports. [default: 0.0.0.0]
  958. #[serde(skip_serializing_if = "Option::is_none")]
  959. pub bind_address: Option<String>,
  960. // Copy an account from the cluster referenced by the url argument.
  961. #[serde(skip_serializing_if = "Option::is_none")]
  962. pub clone: Option<Vec<CloneEntry>>,
  963. // Range to use for dynamically assigned ports. [default: 1024-65535]
  964. #[serde(skip_serializing_if = "Option::is_none")]
  965. pub dynamic_port_range: Option<String>,
  966. // Enable the faucet on this port [default: 9900].
  967. #[serde(skip_serializing_if = "Option::is_none")]
  968. pub faucet_port: Option<u16>,
  969. // Give the faucet address this much SOL in genesis. [default: 1000000]
  970. #[serde(skip_serializing_if = "Option::is_none")]
  971. pub faucet_sol: Option<String>,
  972. // Geyser plugin config location
  973. #[serde(skip_serializing_if = "Option::is_none")]
  974. pub geyser_plugin_config: Option<String>,
  975. // Gossip DNS name or IP address for the validator to advertise in gossip. [default: 127.0.0.1]
  976. #[serde(skip_serializing_if = "Option::is_none")]
  977. pub gossip_host: Option<String>,
  978. // Gossip port number for the validator
  979. #[serde(skip_serializing_if = "Option::is_none")]
  980. pub gossip_port: Option<u16>,
  981. // URL for Solana's JSON RPC or moniker.
  982. #[serde(skip_serializing_if = "Option::is_none")]
  983. pub url: Option<String>,
  984. // Use DIR as ledger location
  985. #[serde(skip_serializing_if = "Option::is_none")]
  986. pub ledger: Option<String>,
  987. // Keep this amount of shreds in root slots. [default: 10000]
  988. #[serde(skip_serializing_if = "Option::is_none")]
  989. pub limit_ledger_size: Option<String>,
  990. // Enable JSON RPC on this port, and the next port for the RPC websocket. [default: 8899]
  991. #[serde(skip_serializing_if = "Option::is_none")]
  992. pub rpc_port: Option<u16>,
  993. // Override the number of slots in an epoch.
  994. #[serde(skip_serializing_if = "Option::is_none")]
  995. pub slots_per_epoch: Option<String>,
  996. // The number of ticks in a slot
  997. #[serde(skip_serializing_if = "Option::is_none")]
  998. pub ticks_per_slot: Option<u16>,
  999. // Warp the ledger to WARP_SLOT after starting the validator.
  1000. #[serde(skip_serializing_if = "Option::is_none")]
  1001. pub warp_slot: Option<Slot>,
  1002. // Deactivate one or more features.
  1003. #[serde(skip_serializing_if = "Option::is_none")]
  1004. pub deactivate_feature: Option<Vec<String>>,
  1005. }
  1006. #[derive(Debug, Default, Clone, Serialize, Deserialize)]
  1007. pub struct Validator {
  1008. #[serde(skip_serializing_if = "Option::is_none")]
  1009. pub account: Option<Vec<AccountEntry>>,
  1010. #[serde(skip_serializing_if = "Option::is_none")]
  1011. pub account_dir: Option<Vec<AccountDirEntry>>,
  1012. pub bind_address: String,
  1013. #[serde(skip_serializing_if = "Option::is_none")]
  1014. pub clone: Option<Vec<CloneEntry>>,
  1015. #[serde(skip_serializing_if = "Option::is_none")]
  1016. pub dynamic_port_range: Option<String>,
  1017. #[serde(skip_serializing_if = "Option::is_none")]
  1018. pub faucet_port: Option<u16>,
  1019. #[serde(skip_serializing_if = "Option::is_none")]
  1020. pub faucet_sol: Option<String>,
  1021. #[serde(skip_serializing_if = "Option::is_none")]
  1022. pub geyser_plugin_config: Option<String>,
  1023. #[serde(skip_serializing_if = "Option::is_none")]
  1024. pub gossip_host: Option<String>,
  1025. #[serde(skip_serializing_if = "Option::is_none")]
  1026. pub gossip_port: Option<u16>,
  1027. #[serde(skip_serializing_if = "Option::is_none")]
  1028. pub url: Option<String>,
  1029. pub ledger: String,
  1030. #[serde(skip_serializing_if = "Option::is_none")]
  1031. pub limit_ledger_size: Option<String>,
  1032. pub rpc_port: u16,
  1033. #[serde(skip_serializing_if = "Option::is_none")]
  1034. pub slots_per_epoch: Option<String>,
  1035. #[serde(skip_serializing_if = "Option::is_none")]
  1036. pub ticks_per_slot: Option<u16>,
  1037. #[serde(skip_serializing_if = "Option::is_none")]
  1038. pub warp_slot: Option<Slot>,
  1039. #[serde(skip_serializing_if = "Option::is_none")]
  1040. pub deactivate_feature: Option<Vec<String>>,
  1041. }
  1042. impl From<_Validator> for Validator {
  1043. fn from(_validator: _Validator) -> Self {
  1044. Self {
  1045. account: _validator.account,
  1046. account_dir: _validator.account_dir,
  1047. bind_address: _validator
  1048. .bind_address
  1049. .unwrap_or_else(|| DEFAULT_BIND_ADDRESS.to_string()),
  1050. clone: _validator.clone,
  1051. dynamic_port_range: _validator.dynamic_port_range,
  1052. faucet_port: _validator.faucet_port,
  1053. faucet_sol: _validator.faucet_sol,
  1054. geyser_plugin_config: _validator.geyser_plugin_config,
  1055. gossip_host: _validator.gossip_host,
  1056. gossip_port: _validator.gossip_port,
  1057. url: _validator.url,
  1058. ledger: _validator
  1059. .ledger
  1060. .unwrap_or_else(|| get_default_ledger_path().display().to_string()),
  1061. limit_ledger_size: _validator.limit_ledger_size,
  1062. rpc_port: _validator
  1063. .rpc_port
  1064. .unwrap_or(solana_sdk::rpc_port::DEFAULT_RPC_PORT),
  1065. slots_per_epoch: _validator.slots_per_epoch,
  1066. ticks_per_slot: _validator.ticks_per_slot,
  1067. warp_slot: _validator.warp_slot,
  1068. deactivate_feature: _validator.deactivate_feature,
  1069. }
  1070. }
  1071. }
  1072. impl From<Validator> for _Validator {
  1073. fn from(validator: Validator) -> Self {
  1074. Self {
  1075. account: validator.account,
  1076. account_dir: validator.account_dir,
  1077. bind_address: Some(validator.bind_address),
  1078. clone: validator.clone,
  1079. dynamic_port_range: validator.dynamic_port_range,
  1080. faucet_port: validator.faucet_port,
  1081. faucet_sol: validator.faucet_sol,
  1082. geyser_plugin_config: validator.geyser_plugin_config,
  1083. gossip_host: validator.gossip_host,
  1084. gossip_port: validator.gossip_port,
  1085. url: validator.url,
  1086. ledger: Some(validator.ledger),
  1087. limit_ledger_size: validator.limit_ledger_size,
  1088. rpc_port: Some(validator.rpc_port),
  1089. slots_per_epoch: validator.slots_per_epoch,
  1090. ticks_per_slot: validator.ticks_per_slot,
  1091. warp_slot: validator.warp_slot,
  1092. deactivate_feature: validator.deactivate_feature,
  1093. }
  1094. }
  1095. }
  1096. pub fn get_default_ledger_path() -> PathBuf {
  1097. Path::new(".anchor").join("test-ledger")
  1098. }
  1099. const DEFAULT_BIND_ADDRESS: &str = "0.0.0.0";
  1100. impl Merge for _Validator {
  1101. fn merge(&mut self, other: Self) {
  1102. // Instantiating a new Self object here ensures that
  1103. // this function will fail to compile if new fields get added
  1104. // to Self. This is useful as a reminder if they also require merging
  1105. *self = Self {
  1106. account: match self.account.take() {
  1107. None => other.account,
  1108. Some(mut entries) => match other.account {
  1109. None => Some(entries),
  1110. Some(other_entries) => {
  1111. for other_entry in other_entries {
  1112. match entries
  1113. .iter()
  1114. .position(|my_entry| *my_entry.address == other_entry.address)
  1115. {
  1116. None => entries.push(other_entry),
  1117. Some(i) => entries[i] = other_entry,
  1118. };
  1119. }
  1120. Some(entries)
  1121. }
  1122. },
  1123. },
  1124. account_dir: match self.account_dir.take() {
  1125. None => other.account_dir,
  1126. Some(mut entries) => match other.account_dir {
  1127. None => Some(entries),
  1128. Some(other_entries) => {
  1129. for other_entry in other_entries {
  1130. match entries
  1131. .iter()
  1132. .position(|my_entry| *my_entry.directory == other_entry.directory)
  1133. {
  1134. None => entries.push(other_entry),
  1135. Some(i) => entries[i] = other_entry,
  1136. };
  1137. }
  1138. Some(entries)
  1139. }
  1140. },
  1141. },
  1142. bind_address: other.bind_address.or_else(|| self.bind_address.take()),
  1143. clone: match self.clone.take() {
  1144. None => other.clone,
  1145. Some(mut entries) => match other.clone {
  1146. None => Some(entries),
  1147. Some(other_entries) => {
  1148. for other_entry in other_entries {
  1149. match entries
  1150. .iter()
  1151. .position(|my_entry| *my_entry.address == other_entry.address)
  1152. {
  1153. None => entries.push(other_entry),
  1154. Some(i) => entries[i] = other_entry,
  1155. };
  1156. }
  1157. Some(entries)
  1158. }
  1159. },
  1160. },
  1161. dynamic_port_range: other
  1162. .dynamic_port_range
  1163. .or_else(|| self.dynamic_port_range.take()),
  1164. faucet_port: other.faucet_port.or_else(|| self.faucet_port.take()),
  1165. faucet_sol: other.faucet_sol.or_else(|| self.faucet_sol.take()),
  1166. geyser_plugin_config: other
  1167. .geyser_plugin_config
  1168. .or_else(|| self.geyser_plugin_config.take()),
  1169. gossip_host: other.gossip_host.or_else(|| self.gossip_host.take()),
  1170. gossip_port: other.gossip_port.or_else(|| self.gossip_port.take()),
  1171. url: other.url.or_else(|| self.url.take()),
  1172. ledger: other.ledger.or_else(|| self.ledger.take()),
  1173. limit_ledger_size: other
  1174. .limit_ledger_size
  1175. .or_else(|| self.limit_ledger_size.take()),
  1176. rpc_port: other.rpc_port.or_else(|| self.rpc_port.take()),
  1177. slots_per_epoch: other
  1178. .slots_per_epoch
  1179. .or_else(|| self.slots_per_epoch.take()),
  1180. ticks_per_slot: other.ticks_per_slot.or_else(|| self.ticks_per_slot.take()),
  1181. warp_slot: other.warp_slot.or_else(|| self.warp_slot.take()),
  1182. deactivate_feature: other
  1183. .deactivate_feature
  1184. .or_else(|| self.deactivate_feature.take()),
  1185. };
  1186. }
  1187. }
  1188. #[derive(Debug, Clone)]
  1189. pub struct Program {
  1190. pub lib_name: String,
  1191. pub solidity: bool,
  1192. // Canonicalized path to the program directory or Solidity source file
  1193. pub path: PathBuf,
  1194. pub idl: Option<Idl>,
  1195. }
  1196. impl Program {
  1197. pub fn pubkey(&self) -> Result<Pubkey> {
  1198. self.keypair().map(|kp| kp.pubkey())
  1199. }
  1200. pub fn keypair(&self) -> Result<Keypair> {
  1201. let file = self.keypair_file()?;
  1202. get_keypair(file.path().to_str().unwrap())
  1203. }
  1204. // Lazily initializes the keypair file with a new key if it doesn't exist.
  1205. pub fn keypair_file(&self) -> Result<WithPath<File>> {
  1206. let deploy_dir_path = Path::new("target").join("deploy");
  1207. fs::create_dir_all(&deploy_dir_path)
  1208. .with_context(|| format!("Error creating directory with path: {deploy_dir_path:?}"))?;
  1209. let path = std::env::current_dir()
  1210. .expect("Must have current dir")
  1211. .join(deploy_dir_path.join(format!("{}-keypair.json", self.lib_name)));
  1212. if path.exists() {
  1213. return Ok(WithPath::new(
  1214. File::open(&path)
  1215. .with_context(|| format!("Error opening file with path: {}", path.display()))?,
  1216. path,
  1217. ));
  1218. }
  1219. let program_kp = Keypair::new();
  1220. let mut file = File::create(&path)
  1221. .with_context(|| format!("Error creating file with path: {}", path.display()))?;
  1222. file.write_all(format!("{:?}", &program_kp.to_bytes()).as_bytes())?;
  1223. Ok(WithPath::new(file, path))
  1224. }
  1225. pub fn binary_path(&self, verifiable: bool) -> PathBuf {
  1226. let path = Path::new("target")
  1227. .join(if verifiable { "verifiable" } else { "deploy" })
  1228. .join(&self.lib_name)
  1229. .with_extension("so");
  1230. std::env::current_dir()
  1231. .expect("Must have current dir")
  1232. .join(path)
  1233. }
  1234. }
  1235. #[derive(Debug, Default)]
  1236. pub struct ProgramDeployment {
  1237. pub address: Pubkey,
  1238. pub path: Option<String>,
  1239. pub idl: Option<String>,
  1240. }
  1241. impl TryFrom<_ProgramDeployment> for ProgramDeployment {
  1242. type Error = anyhow::Error;
  1243. fn try_from(pd: _ProgramDeployment) -> Result<Self, Self::Error> {
  1244. Ok(ProgramDeployment {
  1245. address: pd.address.parse()?,
  1246. path: pd.path,
  1247. idl: pd.idl,
  1248. })
  1249. }
  1250. }
  1251. #[derive(Debug, Default, Serialize, Deserialize)]
  1252. pub struct _ProgramDeployment {
  1253. pub address: String,
  1254. pub path: Option<String>,
  1255. pub idl: Option<String>,
  1256. }
  1257. impl From<&ProgramDeployment> for _ProgramDeployment {
  1258. fn from(pd: &ProgramDeployment) -> Self {
  1259. Self {
  1260. address: pd.address.to_string(),
  1261. path: pd.path.clone(),
  1262. idl: pd.idl.clone(),
  1263. }
  1264. }
  1265. }
  1266. pub struct ProgramWorkspace {
  1267. pub name: String,
  1268. pub program_id: Pubkey,
  1269. pub idl: Idl,
  1270. }
  1271. #[derive(Debug, Serialize, Deserialize)]
  1272. pub struct AnchorPackage {
  1273. pub name: String,
  1274. pub address: String,
  1275. pub idl: Option<String>,
  1276. }
  1277. impl AnchorPackage {
  1278. pub fn from(name: String, cfg: &WithPath<Config>) -> Result<Self> {
  1279. let cluster = &cfg.provider.cluster;
  1280. if cluster != &Cluster::Mainnet {
  1281. return Err(anyhow!("Publishing requires the mainnet cluster"));
  1282. }
  1283. let program_details = cfg
  1284. .programs
  1285. .get(cluster)
  1286. .ok_or_else(|| anyhow!("Program not provided in Anchor.toml"))?
  1287. .get(&name)
  1288. .ok_or_else(|| anyhow!("Program not provided in Anchor.toml"))?;
  1289. let idl = program_details.idl.clone();
  1290. let address = program_details.address.to_string();
  1291. Ok(Self { name, address, idl })
  1292. }
  1293. }
  1294. #[macro_export]
  1295. macro_rules! home_path {
  1296. ($my_struct:ident, $path:literal) => {
  1297. #[derive(Clone, Debug)]
  1298. pub struct $my_struct(String);
  1299. impl Default for $my_struct {
  1300. fn default() -> Self {
  1301. $my_struct(
  1302. home_dir()
  1303. .unwrap()
  1304. .join($path.replace('/', std::path::MAIN_SEPARATOR_STR))
  1305. .display()
  1306. .to_string(),
  1307. )
  1308. }
  1309. }
  1310. impl $my_struct {
  1311. fn stringify_with_tilde(&self) -> String {
  1312. self.0
  1313. .replacen(home_dir().unwrap().to_str().unwrap(), "~", 1)
  1314. }
  1315. }
  1316. impl FromStr for $my_struct {
  1317. type Err = anyhow::Error;
  1318. fn from_str(s: &str) -> Result<Self, Self::Err> {
  1319. Ok(Self(s.to_owned()))
  1320. }
  1321. }
  1322. impl fmt::Display for $my_struct {
  1323. fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
  1324. write!(f, "{}", self.0)
  1325. }
  1326. }
  1327. };
  1328. }
  1329. home_path!(WalletPath, ".config/solana/id.json");
  1330. #[cfg(test)]
  1331. mod tests {
  1332. use super::*;
  1333. const BASE_CONFIG: &str = "
  1334. [provider]
  1335. cluster = \"localnet\"
  1336. wallet = \"id.json\"
  1337. ";
  1338. #[test]
  1339. fn parse_custom_cluster_str() {
  1340. let config = Config::from_str(
  1341. "
  1342. [provider]
  1343. cluster = \"http://my-url.com\"
  1344. wallet = \"id.json\"
  1345. ",
  1346. )
  1347. .unwrap();
  1348. assert!(!config.features.skip_lint);
  1349. // Make sure the layout of `provider.cluster` stays the same after serialization
  1350. assert!(config
  1351. .to_string()
  1352. .contains(r#"cluster = "http://my-url.com""#));
  1353. }
  1354. #[test]
  1355. fn parse_custom_cluster_map() {
  1356. let config = Config::from_str(
  1357. "
  1358. [provider]
  1359. cluster = { http = \"http://my-url.com\", ws = \"ws://my-url.com\" }
  1360. wallet = \"id.json\"
  1361. ",
  1362. )
  1363. .unwrap();
  1364. assert!(!config.features.skip_lint);
  1365. }
  1366. #[test]
  1367. fn parse_skip_lint_no_section() {
  1368. let config = Config::from_str(BASE_CONFIG).unwrap();
  1369. assert!(!config.features.skip_lint);
  1370. }
  1371. #[test]
  1372. fn parse_skip_lint_no_value() {
  1373. let string = BASE_CONFIG.to_owned() + "[features]";
  1374. let config = Config::from_str(&string).unwrap();
  1375. assert!(!config.features.skip_lint);
  1376. }
  1377. #[test]
  1378. fn parse_skip_lint_true() {
  1379. let string = BASE_CONFIG.to_owned() + "[features]\nskip-lint = true";
  1380. let config = Config::from_str(&string).unwrap();
  1381. assert!(config.features.skip_lint);
  1382. }
  1383. #[test]
  1384. fn parse_skip_lint_false() {
  1385. let string = BASE_CONFIG.to_owned() + "[features]\nskip-lint = false";
  1386. let config = Config::from_str(&string).unwrap();
  1387. assert!(!config.features.skip_lint);
  1388. }
  1389. }