Skip to content

Commit

Permalink
Merge pull request #937 from everx-labs/IgorKoval/bugfix
Browse files Browse the repository at this point in the history
Fix bug with state init
  • Loading branch information
IgorKoval authored Jul 16, 2024
2 parents b428fdf + 0d9dfd7 commit 24912a5
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 47 deletions.
70 changes: 35 additions & 35 deletions Cargo.lock

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

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ license = 'Apache-2.0'
name = 'ever-cli'
readme = 'README.md'
repository = 'https://github.com/everx-labs/ever-cli'
version = '0.37.1'
version = '0.38.0'
default-run = 'ever-cli'

[dependencies]
Expand Down
14 changes: 9 additions & 5 deletions src/genaddr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use crate::config::Config;
use crate::helpers::{create_client_local, read_keys, load_abi, calc_acc_address, load_abi_str};
use serde_json::json;
use std::fs::OpenOptions;

use std::ptr::eq;
use crate::crypto::{gen_seed_phrase, generate_keypair_from_mnemonic};
use ton_client::utils::{convert_address, ParamsOfConvertAddress, AddressStringFormat};

Expand Down Expand Up @@ -146,10 +146,9 @@ fn update_contract_state(tvc_file: &str, pubkey: &[u8], data: Option<String>, ab
let mut state_init = OpenOptions::new().read(true).write(true).open(tvc_file)
.map_err(|e| format!("unable to open contract file: {}", e))?;

let pubkey_object = pubkey.try_into()
.map_err(|e| format!("unable to load public key: {}", e))?;

let mut contract_image = if data_map_supported {
let pubkey_object = pubkey.try_into()
.map_err(|e| format!("unable to load public key: {}", e))?;
ContractImage::from_state_init_and_key(&mut state_init, &pubkey_object)
.map_err(|e| format!("unable to load contract image with key: {}", e))?
} else {
Expand All @@ -163,8 +162,13 @@ fn update_contract_state(tvc_file: &str, pubkey: &[u8], data: Option<String>, ab
.map_err(|e| format!("unable to update contract image data: {}", e))?;
}
} else {
let pk = if eq(pubkey, vec![0; 32].as_slice()) {
Some(hex::encode(pubkey))
} else {
None
};
let js_init_data = crate::helpers::insert_pubkey_to_init_data(
Some(hex::encode(pubkey)),
pk,
data.as_deref()
)?;
contract_image.update_data(false, js_init_data.as_str(), abi)
Expand Down
10 changes: 4 additions & 6 deletions src/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1028,17 +1028,15 @@ pub fn decode_data(data: &str, param_name: &str) -> Result<Vec<u8>, String> {
}

pub fn insert_pubkey_to_init_data(pubkey: Option<String>, opt_init_data: Option<&str>) -> Result<String, String> {
let init_data = match opt_init_data {
Some(json) => json,
None => "{}"
};
let init_data = opt_init_data.unwrap_or_else(|| "{}");

let mut js_init_data = serde_json::from_str(init_data)
.map_err(|e| format!("Failed to decode initial data as json: {}", e))?;
match &mut js_init_data {
Value::Object(obj) => {
if obj.contains_key(&"_pubkey".to_string()) {
return Err("Public key was set via init data. Please, use command line options --genkey/--setkey to set public key.".to_owned())
if obj.contains_key(&"_pubkey".to_string()) && pubkey.is_some() {
return Err("Public key was set via init data and via commad-line option --genkey/--setkey. \
Please, use one way to set public key.".to_owned())
}
if let Some(pk) = pubkey {
let pubkey_str = format!("0x{}", pk);
Expand Down

0 comments on commit 24912a5

Please sign in to comment.