Skip to content

Commit

Permalink
Eliminate nondeterminism in TON boc serialization
Browse files Browse the repository at this point in the history
Fix issue #4004
  • Loading branch information
10gic committed Aug 31, 2024
1 parent af67cae commit 9f0e5cb
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 2 deletions.
9 changes: 7 additions & 2 deletions rust/frameworks/tw_ton_sdk/src/boc/boc_to_raw_boc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use crate::boc::BagOfCells;
use crate::cell::{Cell, CellArc};
use crate::error::{CellErrorType, CellResult};
use std::cell::RefCell;
use std::collections::BTreeMap;
use std::collections::HashMap;
use std::sync::Arc;
use tw_coin_entry::error::prelude::{OrTWError, ResultContext};
Expand Down Expand Up @@ -50,7 +51,11 @@ pub(crate) fn convert_to_raw_boc(boc: &BagOfCells) -> CellResult<RawBagOfCells>
fn build_and_verify_index(roots: &[CellArc]) -> HashMap<H256, IndexedCellRef> {
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() {
Expand Down Expand Up @@ -96,7 +101,7 @@ fn build_and_verify_index(roots: &[CellArc]) -> HashMap<H256, IndexedCellRef> {
}
}

cells_by_hash
cells_by_hash.into_iter().collect()
}

fn root_indices(
Expand Down
1 change: 1 addition & 0 deletions tests/chains/TheOpenNetwork/TWAnySignerTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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+CCEGV4dG66jrSAINchAdB01yH6QDD6RPgo+kQwWL2RW+DtRNCBAUHXIfQFgwf0Dm+hMZEw4YBA1yFwf9s84DEg10mBAoC5kTDgcOIXDQIBIA4PAR4g1wsfghBzaWduuvLgin8NAeaO8O2i7fshgwjXIgKDCNcjIIAg1yHTH9Mf0x/tRNDSANMfINMf0//XCgAK+QFAzPkQmiiUXwrbMeHywIffArNQB7Dy0IRRJbry4IVQNrry4Ib4I7vy0IgikvgA3gGkf8jKAMsfAc8Wye1UIJL4D95w2zzYFwIBIBARABm+Xw9qJoQICg65D6AsAgFuEhMCAUgUFQAZrc52omhAIOuQ64X/wAAZrx32omhAEOuQ64WPwAAXsyX7UTQcdch1wsfgABGyYvtRNDXCgCAAAAP27aLt+wL0BCFukmwhjkwCIdc5MHCUIccAs44tAdcoIHYeQ2wg10nACPLgkyDXSsAC8uCTINcdBscSwgBSMLDy0InXTNc5MAGk6GwShAe78uCT10rAAPLgk+1V4tIAAcAAkVvg69csCBQgkXCWAdcsCBwS4lIQseMPINdKGBkaAJYB+kAB+kT4KPpEMFi68uCR7UTQgQFB1xj0BQSdf8jKAEAEgwf0U/Lgi44UA4MH9Fvy4Iwi1woAIW4Bs7Dy0JDiyFADzxYS9ADJ7VQAcjDXLAgkji0h8uCS0gDtRNDSAFETuvLQj1RQMJExnAGBAUDXIdcKAPLgjuLIygBYzxbJ7VST8sCN4gAQk1vbMeHXTNB8Ui06");
}

Expand Down

0 comments on commit 9f0e5cb

Please sign in to comment.