Skip to content

Commit

Permalink
feat: using base structs and adding serializer/deserializer to their …
Browse files Browse the repository at this point in the history
…types
  • Loading branch information
csgui committed Aug 8, 2023
1 parent f9cf83c commit fe3fe1f
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 65 deletions.
81 changes: 34 additions & 47 deletions components/clarinet-cli/src/devnet/package.rs
Original file line number Diff line number Diff line change
@@ -1,51 +1,18 @@
use std::fs::{read_to_string, File};
use std::fs::File;
use std::io::{self, Write};
use std::process;
use serde::{Serialize, Deserialize};

use clarinet_deployments::types::DeploymentSpecificationFile;
use clarinet_files::{FileLocation, NetworkManifestFile, ProjectManifestFile};
use clarinet_deployments::get_default_deployment_path;
use clarinet_deployments::types::DeploymentSpecification;
use clarinet_files::chainhook_types::StacksNetwork;
use clarinet_files::{NetworkManifest, ProjectManifest};

#[derive(Serialize, Deserialize, Debug)]
struct ConfigurationPackage {
deployment_plan: DeploymentSpecificationFile,
devnet_config: NetworkManifestFile,
clarinet_config: ProjectManifestFile,
}

fn get_devnet_config() -> Result<NetworkManifestFile, io::Error> {
let file_content = read_to_string("./settings/Devnet.toml")?;

let devnet_config: NetworkManifestFile = match toml::from_str(&file_content) {
Ok(data) => data,
Err(err) => {
println!("Unable to load data from Devnet.toml file: {}", err);
process::exit(1);
}
};

Ok(devnet_config)
}

fn get_clarinet_config(manifest_location: FileLocation) -> Result<ProjectManifestFile, io::Error> {
let clarinet_config_content = read_to_string(manifest_location.to_string())?;

let clarinet_config: ProjectManifestFile = match toml::from_str(&clarinet_config_content) {
Ok(data) => data,
Err(err) => {
println!("Unable to load data from Clarinet.toml file: {}", err);
process::exit(1);
}
};

Ok(clarinet_config)
}

fn get_deployment_plan() -> Result<DeploymentSpecificationFile, io::Error> {
let deployment_spec_file = File::open("./deployments/default.simnet-plan.yaml")?;
let deployment_plan: DeploymentSpecificationFile =
serde_yaml::from_reader(deployment_spec_file).unwrap();

Ok(deployment_plan)
deployment_plan: DeploymentSpecification,
devnet_config: NetworkManifest,
clarinet_config: ProjectManifest,
}

fn pack_to_file(file_name: &str, package: ConfigurationPackage) -> Result<(), io::Error> {
Expand Down Expand Up @@ -75,11 +42,31 @@ fn pack_to_stdout(package: ConfigurationPackage) {
io::stdout().write(s.as_bytes()).ok();
}

pub fn pack(file_name: Option<String>, manifest_location: FileLocation) -> Result<(), io::Error> {
pub fn pack(file_name: Option<String>, project_manifest: ProjectManifest) -> Result<(), io::Error> {
let deployment_path =
get_default_deployment_path(&project_manifest, &StacksNetwork::Devnet).unwrap();

let deployment_manifest = DeploymentSpecification::from_config_file(
&deployment_path,
&project_manifest
.location
.get_project_root_location()
.unwrap(),
)
.unwrap();

let network_manifest = NetworkManifest::from_project_manifest_location(
&project_manifest.location,
&StacksNetwork::Devnet.get_networks(),
None,
None,
)
.unwrap();

let package = ConfigurationPackage {
deployment_plan: get_deployment_plan()?,
devnet_config: get_devnet_config()?,
clarinet_config: get_clarinet_config(manifest_location)?,
deployment_plan: deployment_manifest,
devnet_config: network_manifest,
clarinet_config: project_manifest,
};

match file_name {
Expand All @@ -88,4 +75,4 @@ pub fn pack(file_name: Option<String>, manifest_location: FileLocation) -> Resul
}

Ok(())
}
}
3 changes: 1 addition & 2 deletions components/clarinet-cli/src/frontend/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1443,8 +1443,7 @@ pub fn main() {
Command::Devnet(subcommand) => match subcommand {
Devnet::Package(cmd) => {
let manifest = load_manifest_or_exit(cmd.manifest_path);

Package::pack(cmd.package_file_name, manifest.location)
Package::pack(cmd.package_file_name, manifest)
.expect("Could not execute the package command.");
}
},
Expand Down
32 changes: 19 additions & 13 deletions components/clarinet-deployments/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ use clarity_repl::clarity::{ClarityName, ClarityVersion, ContractName};

use clarinet_files::chainhook_types::StacksNetwork;
use serde::{Deserialize, Serialize};
use serde_with::{serde_as, Bytes};
use serde_yaml;
use std::collections::BTreeMap;

Expand Down Expand Up @@ -71,7 +72,7 @@ pub struct DeploymentGenerationArtifacts {
pub success: bool,
}

#[derive(Debug, PartialEq, Clone)]
#[derive(Serialize, Deserialize, Debug, PartialEq, Clone)]
pub struct TransactionPlanSpecification {
pub batches: Vec<TransactionsBatchSpecification>,
}
Expand Down Expand Up @@ -196,14 +197,14 @@ pub struct EmulatedContractPublishSpecificationFile {
pub clarity_version: Option<u8>,
}

#[derive(Debug, PartialEq, Clone)]
#[derive(Serialize, Deserialize, Debug, PartialEq, Clone)]
pub struct TransactionsBatchSpecification {
pub id: usize,
pub transactions: Vec<TransactionSpecification>,
pub epoch: Option<EpochSpec>,
}

#[derive(Debug, PartialEq, Clone)]
#[derive(Serialize, Deserialize, Debug, PartialEq, Clone)]
pub enum TransactionSpecification {
ContractCall(ContractCallSpecification),
ContractPublish(ContractPublishSpecification),
Expand All @@ -214,12 +215,17 @@ pub enum TransactionSpecification {
StxTransfer(StxTransferSpecification),
}

#[derive(Debug, PartialEq, Clone)]
const MEMO_BYTE_COUNT: usize = 34;
type MemoArr = [u8; MEMO_BYTE_COUNT];

#[serde_as]
#[derive(Serialize, Deserialize, Debug, PartialEq, Clone)]
pub struct StxTransferSpecification {
pub expected_sender: StandardPrincipalData,
pub recipient: PrincipalData,
pub mstx_amount: u64,
pub memo: [u8; 34],
#[serde_as(as = "Bytes")]
pub memo: MemoArr,
pub cost: u64,
pub anchor_block_only: bool,
}
Expand Down Expand Up @@ -276,7 +282,7 @@ impl StxTransferSpecification {
}
}

#[derive(Debug, PartialEq, Clone)]
#[derive(Serialize, Deserialize, Debug, PartialEq, Clone)]
pub struct BtcTransferSpecification {
pub expected_sender: String,
pub recipient: String,
Expand All @@ -298,7 +304,7 @@ impl BtcTransferSpecification {
}
}

#[derive(Debug, PartialEq, Clone)]
#[derive(Serialize, Deserialize, Debug, PartialEq, Clone)]
pub struct ContractCallSpecification {
pub contract_id: QualifiedContractIdentifier,
pub expected_sender: StandardPrincipalData,
Expand Down Expand Up @@ -654,7 +660,7 @@ impl EmulatedContractPublishSpecification {
}
}

#[derive(Debug, Clone)]
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct DeploymentSpecification {
pub id: u32,
pub name: String,
Expand Down Expand Up @@ -768,6 +774,7 @@ impl DeploymentSpecification {
}
TransactionSpecificationFile::ContractPublish(spec) => {
let spec = ContractPublishSpecification::from_specifications(spec, project_root_location)?;

let contract_id = QualifiedContractIdentifier::new(spec.expected_sender.clone(), spec.contract_name.clone());
contracts.insert(contract_id, (spec.source.clone(), spec.location.clone()));
TransactionSpecification::ContractPublish(spec)
Expand Down Expand Up @@ -841,7 +848,6 @@ impl DeploymentSpecification {

#[derive(Debug, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "kebab-case")]

pub struct DeploymentSpecificationFile {
pub id: Option<u32>,
pub name: String,
Expand All @@ -864,15 +870,15 @@ pub struct GenesisSpecificationFile {
pub contracts: Vec<String>,
}

#[derive(Debug, PartialEq, Serialize, Deserialize)]
#[derive(Debug, PartialEq, Deserialize, Serialize)]
#[serde(rename_all = "kebab-case")]
pub struct WalletSpecificationFile {
pub name: String,
pub address: String,
pub balance: String,
}

#[derive(Debug, PartialEq, Clone)]
#[derive(Serialize, Deserialize, Debug, PartialEq, Clone)]
pub struct GenesisSpecification {
pub wallets: Vec<WalletSpecification>,
pub contracts: Vec<String>,
Expand Down Expand Up @@ -910,7 +916,7 @@ impl GenesisSpecification {
}
}

#[derive(Debug, PartialEq, Clone)]
#[derive(Serialize, Deserialize, Debug, PartialEq, Clone)]
pub struct WalletSpecification {
pub name: String,
pub address: StandardPrincipalData,
Expand Down Expand Up @@ -1064,4 +1070,4 @@ impl TransactionPlanSpecification {

TransactionPlanSpecificationFile { batches }
}
}
}
6 changes: 3 additions & 3 deletions components/clarinet-files/src/project_manifest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ pub struct ProjectConfigFile {
cache_dir: Option<String>,
}

#[derive(Serialize, Debug, Clone)]
#[derive(Deserialize, Serialize, Debug, Clone)]
pub struct ProjectManifest {
pub project: ProjectConfig,
#[serde(serialize_with = "toml::ser::tables_last")]
Expand All @@ -58,7 +58,7 @@ pub struct ProjectManifest {
pub contracts_settings: HashMap<FileLocation, ClarityContractMetadata>,
}

#[derive(Debug, Clone)]
#[derive(Deserialize, Debug, Clone)]
pub struct ProjectConfig {
pub name: String,
pub authors: Vec<String>,
Expand Down Expand Up @@ -405,4 +405,4 @@ fn test_get_epoch_and_clarity_version() {
Some(&Value::Integer(2)),
);
assert_eq!(result, Ok((Epoch21, Clarity2)));
}
}

0 comments on commit fe3fe1f

Please sign in to comment.