diff --git a/rust/frameworks/tw_ton_sdk/src/boc/boc_to_raw_boc.rs b/rust/frameworks/tw_ton_sdk/src/boc/boc_to_raw_boc.rs index efcbddd4001..c3f4e3d60e0 100644 --- a/rust/frameworks/tw_ton_sdk/src/boc/boc_to_raw_boc.rs +++ b/rust/frameworks/tw_ton_sdk/src/boc/boc_to_raw_boc.rs @@ -9,12 +9,13 @@ use crate::boc::BagOfCells; use crate::cell::{Cell, CellArc}; use crate::error::{CellErrorType, CellResult}; use std::cell::RefCell; -use std::collections::HashMap; +use std::collections::BTreeMap; use std::sync::Arc; use tw_coin_entry::error::prelude::{OrTWError, ResultContext}; use tw_hash::H256; type IndexedCellRef = RefCell; +type CellsByHash = BTreeMap; #[derive(Debug, Clone)] struct IndexedCell { @@ -47,10 +48,14 @@ pub(crate) fn convert_to_raw_boc(boc: &BagOfCells) -> CellResult }) } -fn build_and_verify_index(roots: &[CellArc]) -> HashMap { +fn build_and_verify_index(roots: &[CellArc]) -> CellsByHash { let mut current_cells: Vec<_> = roots.iter().map(Arc::clone).collect(); let mut new_hash_index = 0; - let mut cells_by_hash = HashMap::new(); + + // The Bag of Cells serialization process is not deterministic, + // and these uncertainties make it difficult to write test cases. + // Therefore, we use a BTreeMap instead of a HashMap to remove the uncertainty. + let mut cells_by_hash = BTreeMap::new(); // Process cells to build the initial index. while !current_cells.is_empty() { @@ -99,10 +104,7 @@ fn build_and_verify_index(roots: &[CellArc]) -> HashMap { cells_by_hash } -fn root_indices( - roots: &[CellArc], - cells_dict: &HashMap, -) -> CellResult> { +fn root_indices(roots: &[CellArc], cells_dict: &CellsByHash) -> CellResult> { roots .iter() .map(|root_cell| root_cell.cell_hash()) @@ -122,17 +124,14 @@ fn root_indices( fn raw_cells_from_cells( cells: impl Iterator, - cells_by_hash: &HashMap, + cells_by_hash: &CellsByHash, ) -> CellResult> { cells .map(|cell| raw_cell_from_cell(&cell, cells_by_hash)) .collect() } -fn raw_cell_from_cell( - cell: &Cell, - cells_by_hash: &HashMap, -) -> CellResult { +fn raw_cell_from_cell(cell: &Cell, cells_by_hash: &CellsByHash) -> CellResult { raw_cell_reference_indices(cell, cells_by_hash).map(|reference_indices| { RawCell::new( cell.data().to_vec(), @@ -144,10 +143,7 @@ fn raw_cell_from_cell( }) } -fn raw_cell_reference_indices( - cell: &Cell, - cells_by_hash: &HashMap, -) -> CellResult> { +fn raw_cell_reference_indices(cell: &Cell, cells_by_hash: &CellsByHash) -> CellResult> { cell.references() .iter() .map(|cell| { diff --git a/tests/chains/TheOpenNetwork/TWAnySignerTests.cpp b/tests/chains/TheOpenNetwork/TWAnySignerTests.cpp index 8bdb62fd2bb..ffd5d276a78 100644 --- a/tests/chains/TheOpenNetwork/TWAnySignerTests.cpp +++ b/tests/chains/TheOpenNetwork/TWAnySignerTests.cpp @@ -57,6 +57,7 @@ TEST(TWAnySignerTheOpenNetwork, SignMessageToTransferAndDeployWalletV5R1) { ANY_SIGN(input, TWCoinTypeTON); // Successfully broadcasted: https://tonviewer.com/transaction/Q32uRBqVprzNzc6iVgwxPeJPE92Fx21dfsqrHnCh5Ss= + ASSERT_EQ(hex(output.hash()), "437dae441a95a6bccdcdcea2560c313de24f13dd85c76d5d7ecaab1e70a1e52b"); ASSERT_EQ(output.encoded(), "te6cckECGwEAA2sAAkWIACm9HPyVOpjCNOG6nbf+EwCONRHHpeMQsIlCoWNKhUaaHgECAgE0AwQBoXNpZ25///8R/////wAAAACACOfqY7L3l3aKc58eNxuJTaeH/fgBw2aG0coM+hjDpjWhJKbYKmsAD8v054HYSuO6vN3bQnV5U19BhsGfe1MDoAUBFP8A9KQT9LzyyAsGAFGAAAAAP///iKrcG+d35KaMMtuxik4jqNofFL51Mu1f8Qf19onqsDlyIAIKDsPIbQMWBwIBIAgJAWJiAC90HaFSSy94Zxb85WYu97uTKIxAlLvolycpcYkWc+8giFAAAAAAAAAAAAAAAAABFgIBSAoLAQLyDALc0CDXScEgkVuPYyDXCx8gghBleHRuvSGCEHNpbnS9sJJfA+CCEG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