Skip to content

Commit

Permalink
[TON]: Eliminate nondeterminism in TON BoC serialization (#4005)
Browse files Browse the repository at this point in the history
* Eliminate nondeterminism in TON boc serialization

Fix issue #4004

* Replace all instances of HashMap with BTreeMap
  • Loading branch information
10gic authored Sep 2, 2024
1 parent af67cae commit f703596
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 16 deletions.
28 changes: 12 additions & 16 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,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<IndexedCell>;
type CellsByHash = BTreeMap<H256, IndexedCellRef>;

#[derive(Debug, Clone)]
struct IndexedCell {
Expand Down Expand Up @@ -47,10 +48,14 @@ pub(crate) fn convert_to_raw_boc(boc: &BagOfCells) -> CellResult<RawBagOfCells>
})
}

fn build_and_verify_index(roots: &[CellArc]) -> HashMap<H256, IndexedCellRef> {
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() {
Expand Down Expand Up @@ -99,10 +104,7 @@ fn build_and_verify_index(roots: &[CellArc]) -> HashMap<H256, IndexedCellRef> {
cells_by_hash
}

fn root_indices(
roots: &[CellArc],
cells_dict: &HashMap<H256, IndexedCellRef>,
) -> CellResult<Vec<usize>> {
fn root_indices(roots: &[CellArc], cells_dict: &CellsByHash) -> CellResult<Vec<usize>> {
roots
.iter()
.map(|root_cell| root_cell.cell_hash())
Expand All @@ -122,17 +124,14 @@ fn root_indices(

fn raw_cells_from_cells(
cells: impl Iterator<Item = CellArc>,
cells_by_hash: &HashMap<H256, IndexedCellRef>,
cells_by_hash: &CellsByHash,
) -> CellResult<Vec<RawCell>> {
cells
.map(|cell| raw_cell_from_cell(&cell, cells_by_hash))
.collect()
}

fn raw_cell_from_cell(
cell: &Cell,
cells_by_hash: &HashMap<H256, IndexedCellRef>,
) -> CellResult<RawCell> {
fn raw_cell_from_cell(cell: &Cell, cells_by_hash: &CellsByHash) -> CellResult<RawCell> {
raw_cell_reference_indices(cell, cells_by_hash).map(|reference_indices| {
RawCell::new(
cell.data().to_vec(),
Expand All @@ -144,10 +143,7 @@ fn raw_cell_from_cell(
})
}

fn raw_cell_reference_indices(
cell: &Cell,
cells_by_hash: &HashMap<H256, IndexedCellRef>,
) -> CellResult<Vec<usize>> {
fn raw_cell_reference_indices(cell: &Cell, cells_by_hash: &CellsByHash) -> CellResult<Vec<usize>> {
cell.references()
.iter()
.map(|cell| {
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 f703596

Please sign in to comment.