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

feat: add configurable override capabilities to forc-deploy #6596

Draft
wants to merge 9 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 Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 13 additions & 0 deletions forc-pkg/src/pkg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use crate::{
use anyhow::{anyhow, bail, Context, Error, Result};
use byte_unit::{Byte, UnitType};
use forc_tracing::{println_action_green, println_warning};
use forc_util::configurables::ConfigurableDeclarations;
use forc_util::{
default_output_directory, find_file_name, kebab_to_snake_case, print_compiling,
print_on_failure, print_warnings,
Expand Down Expand Up @@ -266,6 +267,8 @@ pub struct PrintOpts {
pub ir: PrintIr,
/// Output build errors and warnings in reverse order.
pub reverse_order: bool,
/// Generate a JSON file at the given path to use for overriding configurable slots.
pub configurable_override_file: Option<String>,
}

#[derive(Default, Clone)]
Expand Down Expand Up @@ -2149,6 +2152,7 @@ pub fn build_with_options(build_options: &BuildOpts) -> Result<Built> {
build_target,
member_filter,
experimental,
print: print_opts,
..
} = &build_options;

Expand Down Expand Up @@ -2229,6 +2233,15 @@ pub fn build_with_options(build_options: &BuildOpts) -> Result<Built> {
if let Some(outfile) = &debug_outfile {
built_package.write_debug_info(outfile.as_ref())?;
}
if let Some(path) = &print_opts.configurable_override_file {
if let ProgramABI::Fuel(program_abi) = &built_package.program_abi {
let config_decls = ConfigurableDeclarations::try_from(program_abi.clone())?;
let config_decls_str = serde_json::to_string_pretty(&config_decls)?;
fs::write(path, config_decls_str)?;
} else {
bail!("Only Fuel ABI configurable generation is supported");
}
}
built_package.write_output(minify, &pkg_manifest.project.name, &output_dir)?;
built_workspace.push(Arc::new(built_package));
}
Expand Down
12 changes: 11 additions & 1 deletion forc-plugins/forc-client/src/cmd/deploy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,11 +84,21 @@ pub struct Command {
#[clap(long, verbatim_doc_comment, name = "JSON_FILE_PATH")]
pub override_storage_slots: Option<String>,

/// Override configurable slot initialization.
///
/// By default, configurable slots are initialized with the values defined in the configurable block in
/// the sway code. You can override the initialization by providing the file path to a JSON file
/// containing the overridden values.
///
/// Use forc build --generate_configurable_slots_file <OUTPUT_PATH> to generate the file.
#[clap(long, verbatim_doc_comment)]
pub override_configurable_slots: Option<String>,

/// Disable the "new encoding" feature
#[clap(long)]
pub no_encoding_v1: bool,

/// AWS KMS signer arn. If present forc-deploy will automatically use AWS KMS signer instead of forc-wallet.
#[clap(long)]
#[clap(long, name = "AWS_KMS_ARN")]
pub aws_kms_signer: Option<String>,
}
27 changes: 24 additions & 3 deletions forc-plugins/forc-client/src/op/deploy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use anyhow::{bail, Context, Result};
use forc_pkg::manifest::GenericManifestFile;
use forc_pkg::{self as pkg, PackageManifestFile};
use forc_tracing::{println_action_green, println_warning};
use forc_util::default_output_directory;
use forc_util::{configurables::ConfigurableDeclarations, default_output_directory};
use forc_wallet::utils::default_wallet_path;
use fuel_core_client::client::types::{ChainInfo, TransactionStatus};
use fuel_core_client::client::FuelClient;
Expand All @@ -29,7 +29,10 @@ use fuels::{
types::{bech32::Bech32ContractId, transaction_builders::Blob},
};
use fuels_accounts::{provider::Provider, Account, ViewOnlyAccount};
use fuels_core::types::{transaction::TxPolicies, transaction_builders::CreateTransactionBuilder};
use fuels_core::{
codec::ABIEncoder,
types::{transaction::TxPolicies, transaction_builders::CreateTransactionBuilder},
};
use futures::FutureExt;
use pkg::{manifest::build_profile::ExperimentalFlags, BuildProfile, BuiltPackage};
use serde::{Deserialize, Serialize};
Expand Down Expand Up @@ -513,9 +516,26 @@ pub async fn deploy_pkg(
let node_url = provider.url();
let client = FuelClient::new(node_url)?;

let bytecode = &compiled.bytecode.bytes;
let mut bytecode = compiled.bytecode.bytes.clone();

let storage_slots = resolve_storage_slots(command, compiled)?;
if let Some(config_slots) = &command.override_configurable_slots {
let mut config_values_with_offset: Vec<_> = vec![];
let config_decls = ConfigurableDeclarations::from_file(&PathBuf::from_str(config_slots)?)?;
for decl in config_decls.declarations.values() {
let config_type = crate::util::encode::Type::from_str(&decl.config_type)?;
let token = crate::util::encode::Token::from_type_and_value(&config_type, &decl.value)?;
let abi_encoder = ABIEncoder::new(Default::default());
let encoded_val = abi_encoder.encode(&[token.0])?;
let offset = decl.offset as usize;
config_values_with_offset.push((offset, encoded_val));
}

for (offset, val) in config_values_with_offset {
bytecode[offset..offset + val.len()].copy_from_slice(&val);
}
}

let contract = Contract::from(bytecode.clone());
let root = contract.root();
let state_root = Contract::initial_state_root(storage_slots.iter());
Expand Down Expand Up @@ -651,6 +671,7 @@ fn build_opts_from_cmd(cmd: &cmd::Deploy) -> pkg::BuildOpts {
bytecode_spans: false,
ir: cmd.print.ir(),
reverse_order: cmd.print.reverse_order,
configurable_override_file: cmd.print.configurable_override_file.clone(),
},
time_phases: cmd.print.time_phases,
metrics_outfile: cmd.print.metrics_outfile.clone(),
Expand Down
1 change: 1 addition & 0 deletions forc-plugins/forc-client/src/op/run/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,7 @@ fn build_opts_from_cmd(cmd: &cmd::Run) -> pkg::BuildOpts {
bytecode_spans: false,
ir: cmd.print.ir(),
reverse_order: cmd.print.reverse_order,
configurable_override_file: cmd.print.configurable_override_file.clone(),
},
minify: pkg::MinifyOpts {
json_abi: cmd.minify.json_abi,
Expand Down
Loading
Loading