Skip to content

Commit

Permalink
Merge pull request #253 from tonlabs/1.0.0-get-boc-hash
Browse files Browse the repository at this point in the history
get_boc_hash
  • Loading branch information
melsomino authored Nov 9, 2020
2 parents cfe8f5c + 3b1ebe9 commit e6eea9f
Show file tree
Hide file tree
Showing 6 changed files with 79 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ All notable changes to this project will be documented in this file.
### New
- ChaCha20 encryption support `crypto.chacha20`.
- `boc.parse_shardstate` function for shardstates parsing.
- `boc.get_boc_hash` function for calculating BOC root hash
- `client.build_info` fully defined and documented.
- `processing.wait_for_transaction` and `processing.process_message` functions execute contract
locally in case if transaction waiting fails in order to resolve the contract execution error
Expand Down
41 changes: 41 additions & 0 deletions ton_client/client/src/boc/hash.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* Copyright 2018-2020 TON DEV SOLUTIONS LTD.
*
* Licensed under the SOFTWARE EVALUATION License (the "License"); you may not use
* this file except in compliance with the License.
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific TON DEV software governing permissions and
* limitations under the License.
*/

use crate::boc::internal::deserialize_cell_from_base64;
use crate::client::ClientContext;
use crate::error::ClientResult;

#[derive(Serialize, Deserialize, Clone, ApiType)]
pub struct ParamsOfGetBocHash {
/// BOC encoded as base64
pub boc: String,
}

#[derive(Serialize, Deserialize, Clone, ApiType)]
pub struct ResultOfGetBocHash {
/// BOC root hash encoded with hex
pub hash: String,
}

/// Calculates BOC root hash
#[api_function]
pub fn get_boc_hash(
_context: std::sync::Arc<ClientContext>,
params: ParamsOfGetBocHash,
) -> ClientResult<ResultOfGetBocHash> {
let (_, cell) = deserialize_cell_from_base64(&params.boc, "")?;

Ok(ResultOfGetBocHash {
hash: cell.repr_hash().to_hex_string()
})
}
13 changes: 13 additions & 0 deletions ton_client/client/src/boc/internal.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,16 @@
/*
* Copyright 2018-2020 TON DEV SOLUTIONS LTD.
*
* Licensed under the SOFTWARE EVALUATION License (the "License"); you may not use
* this file except in compliance with the License.
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific TON DEV software governing permissions and
* limitations under the License.
*/

use crate::boc::Error;
use crate::error::ClientResult;
use ton_block::{Deserializable, Serializable};
Expand Down
7 changes: 6 additions & 1 deletion ton_client/client/src/boc/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,11 @@
pub(crate) mod blockchain_config;
mod errors;
pub(crate) mod parse;
pub(crate) mod internal;
pub(crate) mod hash;

#[cfg(test)]
mod tests;
pub(crate) mod internal;

pub use crate::boc::parse::{
source_boc, required_boc,
Expand All @@ -26,4 +28,7 @@ pub use crate::boc::parse::{
pub use blockchain_config::{
get_blockchain_config, ParamsOfGetBlockchainConfig, ResultOfGetBlockchainConfig,
};
pub use hash::{
get_boc_hash, ParamsOfGetBocHash, ResultOfGetBocHash
};
pub use errors::{Error, ErrorCode};
17 changes: 17 additions & 0 deletions ton_client/client/src/boc/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,23 @@ use crate::boc::{ParamsOfParse, ParamsOfParseShardstate, ResultOfParse};
use crate::tests::TestClient;
use pretty_assertions::assert_eq;

#[test]
fn get_boc_hash() {
let client = TestClient::new();

let result: super::ResultOfGetBocHash = client.request(
"boc.get_boc_hash",
super::ParamsOfGetBocHash {
boc: String::from("te6ccgEBAQEAWAAAq2n+AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAE/zMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzSsG8DgAAAAAjuOu9NAL7BxYpA")
}
).unwrap();

assert_eq!(
result.hash,
"dfd47194f3058ee058bfbfad3ea40cbbd9ad17ca77cd0904d4d9f18a48c2fbca"
);
}

#[test]
fn parse_message() {
let client = TestClient::new();
Expand Down
1 change: 1 addition & 0 deletions ton_client/client/src/json_interface/modules.rs
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,7 @@ fn register_boc(handlers: &mut RuntimeHandlers) {
crate::boc::get_blockchain_config,
crate::boc::blockchain_config::get_blockchain_config_api,
);
module.register_sync_fn(crate::boc::get_boc_hash, crate::boc::hash::get_boc_hash_api);
module.register();
}

Expand Down

0 comments on commit e6eea9f

Please sign in to comment.