@@ -63,7 +63,7 @@ td::Status AccountStorageStat::replace_roots(std::vector<Ref<vm::Cell>> new_root
6363 for (const Ref<vm::Cell>& root : to_add) {
6464 TRY_RESULT (info, add_cell (root));
6565 if (check_merkle_depth && info.max_merkle_depth > MAX_MERKLE_DEPTH) {
66- return td::Status::Error (" too big Merkle depth" );
66+ return td::Status::Error (errorcode_limits_exceeded, " too big Merkle depth" );
6767 }
6868 }
6969 for (const Ref<vm::Cell>& root : to_del) {
@@ -92,10 +92,10 @@ void AccountStorageStat::add_hint(const td::HashSet<vm::CellHash>& hint) {
9292 return ;
9393 }
9494 }
95+ e.max_merkle_depth = 0 ;
9596 if (hint.contains (cell->get_hash ())) {
9697 bool spec;
9798 vm::CellSlice cs = vm::load_cell_slice_special (cell, spec);
98- e.size_bits = cs.size ();
9999 for (unsigned i = 0 ; i < cs.size_refs (); ++i) {
100100 dfs (cs.prefetch_ref (i), false );
101101 }
@@ -108,11 +108,11 @@ void AccountStorageStat::add_hint(const td::HashSet<vm::CellHash>& hint) {
108108
109109td::Result<AccountStorageStat::CellInfo> AccountStorageStat::add_cell (const Ref<vm::Cell>& cell) {
110110 Entry& e = get_entry (cell);
111- if (!e.exists_known || e. refcnt_diff < 0 ) {
111+ if (!e.exists_known ) {
112112 TRY_STATUS (fetch_from_dict (e));
113113 }
114114 ++e.refcnt_diff ;
115- if (e.exists || e.refcnt_diff > 1 || (e. refcnt && e. refcnt . value () + e. refcnt_diff != 1 ) ) {
115+ if (e.exists || e.refcnt_diff > 1 ) {
116116 if (!e.max_merkle_depth ) {
117117 TRY_STATUS (fetch_from_dict (e));
118118 if (!e.max_merkle_depth ) {
@@ -125,7 +125,6 @@ td::Result<AccountStorageStat::CellInfo> AccountStorageStat::add_cell(const Ref<
125125 td::uint32 max_merkle_depth = 0 ;
126126 bool spec;
127127 vm::CellSlice cs = vm::load_cell_slice_special (cell, spec);
128- e.size_bits = cs.size ();
129128 for (unsigned i = 0 ; i < cs.size_refs (); ++i) {
130129 TRY_RESULT (info, add_cell (cs.prefetch_ref (i)));
131130 max_merkle_depth = std::max (max_merkle_depth, info.max_merkle_depth );
@@ -137,6 +136,8 @@ td::Result<AccountStorageStat::CellInfo> AccountStorageStat::add_cell(const Ref<
137136 max_merkle_depth = std::min (max_merkle_depth, MERKLE_DEPTH_LIMIT);
138137 Entry& e2 = get_entry (cell);
139138 e2 .max_merkle_depth = max_merkle_depth;
139+ ++total_cells_;
140+ total_bits_ += cs.size ();
140141 return CellInfo{max_merkle_depth};
141142}
142143
@@ -158,16 +159,28 @@ td::Status AccountStorageStat::remove_cell(const Ref<vm::Cell>& cell) {
158159 }
159160 bool spec;
160161 vm::CellSlice cs = vm::load_cell_slice_special (cell, spec);
161- e.size_bits = cs.size ();
162162 for (unsigned i = 0 ; i < cs.size_refs (); ++i) {
163163 TRY_STATUS (remove_cell (cs.prefetch_ref (i)));
164164 }
165+ --total_cells_;
166+ total_bits_ -= cs.size ();
165167 return td::Status::OK ();
166168}
167169
168170td::Result<Ref<vm::Cell>> AccountStorageStat::get_dict_root () {
169171 if (!dict_up_to_date_) {
170172 std::vector<std::pair<td::ConstBitPtr, Ref<vm::CellBuilder>>> values;
173+ if (parent_ && !parent_->dict_up_to_date_ ) {
174+ for (auto & [_, ep] : parent_->cache_ ) {
175+ if (ep.dict_refcnt_diff == 0 ) {
176+ continue ;
177+ }
178+ Entry& e = cache_[ep.hash ];
179+ if (!e.inited ) {
180+ e = ep;
181+ }
182+ }
183+ }
171184 for (auto & [_, e] : cache_) {
172185 if (e.dict_refcnt_diff == 0 ) {
173186 continue ;
@@ -259,25 +272,9 @@ td::Status AccountStorageStat::finalize_entry(Entry& e) {
259272 e.refcnt .value () += e.refcnt_diff ;
260273 e.dict_refcnt_diff += e.refcnt_diff ;
261274 e.refcnt_diff = 0 ;
262- if (e.refcnt .value () == 0 ) {
263- if (!e.size_bits ) {
264- return td::Status::Error (PSTRING () << " Failed to store entry " << e.hash .to_hex () << " : unknown cell bits" );
265- }
266- --total_cells_;
267- total_bits_ -= e.size_bits .value ();
268- e.exists = false ;
269- } else {
270- if (!e.exists ) {
271- if (!e.size_bits ) {
272- return td::Status::Error (PSTRING () << " Failed to store entry " << e.hash .to_hex () << " : unknown cell bits" );
273- }
274- ++total_cells_;
275- total_bits_ += e.size_bits .value ();
276- }
277- e.exists = true ;
278- if (!e.max_merkle_depth ) {
279- return td::Status::Error (PSTRING () << " Failed to store entry " << e.hash .to_hex () << " : unknown merkle depth" );
280- }
275+ e.exists = (e.refcnt .value () != 0 );
276+ if (e.exists && !e.max_merkle_depth ) {
277+ return td::Status::Error (PSTRING () << " Error on entry " << e.hash .to_hex () << " : unknown merkle depth" );
281278 }
282279 return td::Status::OK ();
283280}
0 commit comments