Skip to content

Commit

Permalink
add replace code
Browse files Browse the repository at this point in the history
  • Loading branch information
SilkovAlexander committed Jan 22, 2025
1 parent 4c6a233 commit d794437
Showing 1 changed file with 32 additions and 1 deletion.
33 changes: 32 additions & 1 deletion tvm_debugger/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@ use std::path::PathBuf;

use clap::ArgAction;
use clap::Parser;

use tvm_block::StateInit;
use tvm_types::{base64_decode, read_single_root_boc};
use tvm_block::Deserializable;
use tvm_block::Serializable;
use crate::execute::execute;
use crate::result::ExecutionResult;

Expand Down Expand Up @@ -82,16 +85,43 @@ struct Args {
/// Trace VM execution
#[arg(long, action=ArgAction::SetTrue, default_value = "false")]
trace: bool,

/// Update code in tvc without executing anything
#[arg(long)]
replace_code: Option<String>,
}

fn main() -> anyhow::Result<()> {
let args: Args = Args::parse();
if let Some( new_code) = args.replace_code {
replace_code(args.input_file, new_code)?;
return Ok(());
}
let mut res: ExecutionResult = ExecutionResult::new(args.json);
execute(&args, &mut res)?;
println!("{}", res.output());
Ok(())
}

fn replace_code(input_file: PathBuf, code: String) -> anyhow::Result<()> {
let mut contract_state_init =
StateInit::construct_from_file(&input_file).map_err(|e| {
anyhow::format_err!(
"Failed to load state init from input file {:?}: {e}",
input_file
)
})?;
let bytes = base64_decode(&code).map_err(|e| anyhow::format_err!("Failed to decode code as base64: {e}"))?;
let code_cell = read_single_root_boc(bytes).map_err(|e| anyhow::format_err!(
"Failed to construct code cell from base64 decoded code cell: {e}",
))?;
contract_state_init.set_code(code_cell);
contract_state_init
.write_to_file(&input_file)
.map_err(|e| anyhow::format_err!("Failed to save state init after execution: {e}"))?;
Ok(())
}

#[cfg(test)]
mod tests {
use std::fs;
Expand Down Expand Up @@ -127,6 +157,7 @@ mod tests {
decode_out_messages: false,
json: true,
trace: false,
replace_code: None,
}
}

Expand Down

0 comments on commit d794437

Please sign in to comment.