Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: change pull-type canisters into custom-type #3680

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .github/workflows/e2e.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ on:
push:
branches:
- master
- ens/poc-pull-info-custom-canister
pull_request:

concurrency:
Expand Down
7 changes: 6 additions & 1 deletion src/dfx/src/lib/builders/custom.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use crate::lib::builders::{
BuildConfig, BuildOutput, CanisterBuilder, IdlBuildOutput, WasmBuildOutput,
get_pull_build_output, BuildConfig, BuildOutput, CanisterBuilder, IdlBuildOutput,
WasmBuildOutput,
};
use crate::lib::canister_info::custom::CustomCanisterInfo;
use crate::lib::canister_info::CanisterInfo;
Expand Down Expand Up @@ -101,6 +102,10 @@ impl CanisterBuilder for CustomBuilder {
info: &CanisterInfo,
config: &BuildConfig,
) -> DfxResult<BuildOutput> {
if let Some(pull_info) = info.get_pull_info() {
return get_pull_build_output(info, pull_info);
}

let CustomBuilderExtra {
input_candid_url: _,
candid,
Expand Down
9 changes: 7 additions & 2 deletions src/dfx/src/lib/builders/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ mod pull;
mod rust;

pub use custom::custom_download;
pub use pull::get_pull_build_output;

#[derive(Debug)]
pub enum WasmBuildOutput {
Expand Down Expand Up @@ -148,7 +149,11 @@ pub trait CanisterBuilder {
)
})?;

let did_from_build = self.get_candid_path(pool, info, config)?;
let did_from_build = match info.get_common_output_idl_path() {
Some(p) => p,
None => self.get_candid_path(pool, info, config)?,
};

if !did_from_build.exists() {
bail!(
"Candid file: {} doesn't exist.",
Expand Down Expand Up @@ -520,7 +525,7 @@ impl BuilderPool {
("custom", Arc::new(custom::CustomBuilder::new(env)?)),
("motoko", Arc::new(motoko::MotokoBuilder::new(env)?)),
("rust", Arc::new(rust::RustBuilder::new(env)?)),
("pull", Arc::new(pull::PullBuilder::new(env)?)),
// ("pull", Arc::new(pull::PullBuilder::new(env)?)),
]);

Ok(Self { builders })
Expand Down
78 changes: 17 additions & 61 deletions src/dfx/src/lib/builders/pull.rs
Original file line number Diff line number Diff line change
@@ -1,65 +1,21 @@
use crate::lib::builders::{
BuildConfig, BuildOutput, CanisterBuilder, IdlBuildOutput, WasmBuildOutput,
};
use crate::lib::canister_info::pull::PullCanisterInfo;
use crate::lib::canister_info::CanisterInfo;
use crate::lib::environment::Environment;
use crate::lib::builders::{BuildOutput, IdlBuildOutput, WasmBuildOutput};
use crate::lib::canister_info::{CanisterInfo, PullInfo};
use crate::lib::error::DfxResult;
use crate::lib::models::canister::CanisterPool;
use candid::Principal as CanisterId;
use fn_error_context::context;
use slog::o;
use std::path::PathBuf;
use anyhow::anyhow;

pub struct PullBuilder {
_logger: slog::Logger,
}

impl PullBuilder {
#[context("Failed to create PullBuilder.")]
pub fn new(env: &dyn Environment) -> DfxResult<Self> {
Ok(Self {
_logger: env.get_logger().new(o! {
"module" => "pull"
}),
})
}
}

impl CanisterBuilder for PullBuilder {
#[context("Failed to get dependencies for canister '{}'.", info.get_name())]
fn get_dependencies(
&self,
_pool: &CanisterPool,
info: &CanisterInfo,
) -> DfxResult<Vec<CanisterId>> {
Ok(vec![])
}

#[context("Failed to build Pull canister '{}'.", canister_info.get_name())]
fn build(
&self,
_pool: &CanisterPool,
canister_info: &CanisterInfo,
_config: &BuildConfig,
) -> DfxResult<BuildOutput> {
let pull_info = canister_info.as_info::<PullCanisterInfo>()?;
Ok(BuildOutput {
canister_id: *pull_info.get_canister_id(),
// It's impossible to know if the downloaded wasm is gzip or not with only the info in `dfx.json`.
wasm: WasmBuildOutput::None,
idl: IdlBuildOutput::File(pull_info.get_output_idl_path().to_path_buf()),
})
}
pub fn get_pull_build_output(
canister_info: &CanisterInfo,
pull_info: &PullInfo,
) -> DfxResult<BuildOutput> {
let canister_id = *pull_info.get_canister_id();
let output_idl_path = canister_info
.get_common_output_idl_path()
.ok_or_else(|| anyhow!("no common output idl path"))?;

fn get_candid_path(
&self,
_pool: &CanisterPool,
info: &CanisterInfo,
_config: &BuildConfig,
) -> DfxResult<PathBuf> {
let pull_info = info.as_info::<PullCanisterInfo>()?;
let output_idl_path = pull_info.get_output_idl_path();
Ok(output_idl_path.to_path_buf())
}
Ok(BuildOutput {
canister_id,
// It's impossible to know if the downloaded wasm is gzip or not with only the info in `dfx.json`.
wasm: WasmBuildOutput::None,
idl: IdlBuildOutput::File(output_idl_path),
})
}
51 changes: 45 additions & 6 deletions src/dfx/src/lib/canister_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use dfx_core::config::model::dfinity::{
CanisterDeclarationsConfig, CanisterMetadataSection, CanisterTypeProperties, Config, Pullable,
WasmOptLevel,
};
use dfx_core::json::structure::SerdeVec;
use dfx_core::network::provider::get_network_context;
use dfx_core::util;
use fn_error_context::context;
Expand All @@ -17,9 +18,8 @@ use std::path::{Path, PathBuf};
pub mod assets;
pub mod custom;
pub mod motoko;
pub mod pull;
pub mod rust;
use self::pull::PullCanisterInfo;
use crate::lib::deps::get_candid_path_in_project;
use assets::AssetsCanisterInfo;
use custom::CustomCanisterInfo;
use motoko::MotokoCanisterInfo;
Expand Down Expand Up @@ -59,6 +59,14 @@ pub struct CanisterInfo {
pull_dependencies: Vec<(String, CanisterId)>,
gzip: bool,
init_arg: Option<String>,

pull: Option<PullInfo>,
common_output_idl_path: Option<PathBuf>,
}

#[derive(Debug)]
pub struct PullInfo {
id: Principal,
}

impl CanisterInfo {
Expand Down Expand Up @@ -133,7 +141,20 @@ impl CanisterInfo {

let output_root = build_root.join(name);

let type_specific = canister_config.type_specific.clone();
let mut pull = None;
let mut common_output_idl_path = None;

let mut type_specific = canister_config.type_specific.clone();
if let CanisterTypeProperties::Pull { id } = type_specific {
common_output_idl_path = Some(get_candid_path_in_project(workspace_root, &id));

pull = Some(PullInfo { id });
type_specific = CanisterTypeProperties::Custom {
wasm: "".to_string(),
build: SerdeVec::Many(vec![]),
candid: "".to_string(),
}
}

let args = match &canister_config.args {
Some(args) if !args.is_empty() => canister_config.args.clone(),
Expand Down Expand Up @@ -167,6 +188,8 @@ impl CanisterInfo {
pull_dependencies,
gzip,
init_arg,
pull,
common_output_idl_path,
};

Ok(canister_info)
Expand Down Expand Up @@ -294,6 +317,10 @@ impl CanisterInfo {
///
/// To be separated into service.did and init_args.
pub fn get_output_idl_path(&self) -> Option<PathBuf> {
if let Some(common_output_idl_path) = &self.common_output_idl_path {
return Some(common_output_idl_path.clone());
}

match &self.type_specific {
CanisterTypeProperties::Motoko { .. } => self
.as_info::<MotokoCanisterInfo>()
Expand All @@ -307,9 +334,7 @@ impl CanisterInfo {
CanisterTypeProperties::Rust { .. } => self
.as_info::<RustCanisterInfo>()
.map(|x| x.get_output_idl_path().to_path_buf()),
CanisterTypeProperties::Pull { .. } => self
.as_info::<PullCanisterInfo>()
.map(|x| x.get_output_idl_path().to_path_buf()),
CanisterTypeProperties::Pull { .. } => unreachable!(),
}
.ok()
.or_else(|| self.remote_candid.clone())
Expand Down Expand Up @@ -367,4 +392,18 @@ impl CanisterInfo {
pub fn get_init_arg(&self) -> Option<&str> {
self.init_arg.as_deref()
}

pub fn get_pull_info(&self) -> Option<&PullInfo> {
self.pull.as_ref()
}

pub fn get_common_output_idl_path(&self) -> Option<PathBuf> {
self.common_output_idl_path.as_ref().cloned()
}
}

impl PullInfo {
pub fn get_canister_id(&self) -> &Principal {
&self.id
}
}
52 changes: 0 additions & 52 deletions src/dfx/src/lib/canister_info/pull.rs

This file was deleted.

Loading