Skip to content

Commit e0e6553

Browse files
mikhailOKBowen Wang
authored andcommitted
fix(client): fix trie_node_touched bug (#3025)
Change some code so that Trie instances are no longer persisted. Test plan --------- existing tests, new regression test
1 parent 31db559 commit e0e6553

15 files changed

Lines changed: 229 additions & 138 deletions

File tree

chain/chain/src/test_utils.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -266,7 +266,7 @@ impl RuntimeAdapter for KeyValueRuntime {
266266
self.tries.clone()
267267
}
268268

269-
fn get_trie_for_shard(&self, shard_id: ShardId) -> Arc<Trie> {
269+
fn get_trie_for_shard(&self, shard_id: ShardId) -> Trie {
270270
self.tries.get_trie_for_shard(shard_id)
271271
}
272272

chain/chain/src/types.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ pub trait RuntimeAdapter: Send + Sync {
137137
fn get_tries(&self) -> ShardTries;
138138

139139
/// Returns trie.
140-
fn get_trie_for_shard(&self, shard_id: ShardId) -> Arc<Trie>;
140+
fn get_trie_for_shard(&self, shard_id: ShardId) -> Trie;
141141

142142
/// Verify block producer validity
143143
fn verify_block_signature(&self, header: &BlockHeader) -> Result<(), Error>;

core/store/src/db.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ use rocksdb::{
1212
use strum_macros::EnumIter;
1313

1414
use near_primitives::version::DbVersion;
15+
use std::marker::PhantomPinned;
1516

1617
#[derive(Debug, Clone, PartialEq)]
1718
pub struct DBError(rocksdb::Error);
@@ -226,6 +227,7 @@ impl DBTransaction {
226227
pub struct RocksDB {
227228
db: DB,
228229
cfs: Vec<*const ColumnFamily>,
230+
_pin: PhantomPinned,
229231
}
230232

231233
// DB was already Send+Sync. cf and read_options are const pointers using only functions in
@@ -404,7 +406,7 @@ impl RocksDB {
404406
let db = DB::open_cf_for_read_only(&options, path, cf_names.iter(), false)?;
405407
let cfs =
406408
cf_names.iter().map(|n| db.cf_handle(n).unwrap() as *const ColumnFamily).collect();
407-
Ok(Self { db, cfs })
409+
Ok(Self { db, cfs, _pin: PhantomPinned })
408410
}
409411

410412
pub fn new<P: AsRef<std::path::Path>>(path: P) -> Result<Self, DBError> {
@@ -416,7 +418,7 @@ impl RocksDB {
416418
let db = DB::open_cf_descriptors(&options, path, cf_descriptors)?;
417419
let cfs =
418420
cf_names.iter().map(|n| db.cf_handle(n).unwrap() as *const ColumnFamily).collect();
419-
Ok(Self { db, cfs })
421+
Ok(Self { db, cfs, _pin: PhantomPinned })
420422
}
421423
}
422424

core/store/src/lib.rs

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -32,18 +32,20 @@ pub use crate::trie::{
3232
update::TrieUpdateValuePtr, KeyForStateChanges, PartialStorage, ShardTries, Trie, TrieChanges,
3333
WrappedTrieChanges,
3434
};
35+
use std::ops::Deref;
36+
use std::pin::Pin;
3537

3638
mod db;
3739
pub mod migrations;
3840
pub mod test_utils;
3941
mod trie;
4042

4143
pub struct Store {
42-
storage: Arc<dyn Database>,
44+
storage: Pin<Arc<dyn Database>>,
4345
}
4446

4547
impl Store {
46-
pub fn new(storage: Arc<dyn Database>) -> Store {
48+
pub fn new(storage: Pin<Arc<dyn Database>>) -> Store {
4749
Store { storage }
4850
}
4951

@@ -134,14 +136,14 @@ impl Store {
134136

135137
/// Keeps track of current changes to the database and can commit all of them to the database.
136138
pub struct StoreUpdate {
137-
storage: Arc<dyn Database>,
139+
storage: Pin<Arc<dyn Database>>,
138140
transaction: DBTransaction,
139141
/// Optionally has reference to the trie to clear cache on the commit.
140142
tries: Option<ShardTries>,
141143
}
142144

143145
impl StoreUpdate {
144-
pub fn new(storage: Arc<dyn Database>) -> Self {
146+
pub fn new(storage: Pin<Arc<dyn Database>>) -> Self {
145147
let transaction = storage.transaction();
146148
StoreUpdate { storage, transaction, tries: None }
147149
}
@@ -178,8 +180,8 @@ impl StoreUpdate {
178180
self.tries = Some(tries);
179181
} else {
180182
debug_assert_eq!(
181-
self.tries.as_ref().unwrap().tries.as_ref() as *const _,
182-
tries.tries.as_ref() as *const _
183+
self.tries.as_ref().unwrap().caches.as_ref() as *const _,
184+
tries.caches.as_ref() as *const _
183185
);
184186
}
185187
}
@@ -215,8 +217,8 @@ impl StoreUpdate {
215217
);
216218
if let Some(tries) = self.tries {
217219
assert_eq!(
218-
tries.get_store().storage.as_ref() as *const _,
219-
self.storage.as_ref() as *const _
220+
tries.get_store().storage.deref() as *const _,
221+
self.storage.deref() as *const _
220222
);
221223
tries.update_cache(&self.transaction)?;
222224
}
@@ -255,7 +257,7 @@ pub fn read_with_cache<'a, T: BorshDeserialize + 'a>(
255257
}
256258

257259
pub fn create_store(path: &str) -> Arc<Store> {
258-
let db = Arc::new(RocksDB::new(path).expect("Failed to open the database"));
260+
let db = Arc::pin(RocksDB::new(path).expect("Failed to open the database"));
259261
Arc::new(Store::new(db))
260262
}
261263

core/store/src/test_utils.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,13 @@ use rand::seq::SliceRandom;
55
use rand::Rng;
66

77
use crate::db::TestDB;
8-
use crate::{ShardTries, Store, Trie};
8+
use crate::{ShardTries, Store};
99
use near_primitives::hash::CryptoHash;
10+
use near_primitives::types::ShardId;
1011

1112
/// Creates an in-memory database.
1213
pub fn create_test_store() -> Arc<Store> {
13-
let db = Arc::new(TestDB::new());
14+
let db = Arc::pin(TestDB::new());
1415
Arc::new(Store::new(db))
1516
}
1617

@@ -21,12 +22,13 @@ pub fn create_tries() -> ShardTries {
2122
}
2223

2324
pub fn test_populate_trie(
24-
trie: Arc<Trie>,
25+
tries: &ShardTries,
2526
root: &CryptoHash,
27+
shard_id: ShardId,
2628
changes: Vec<(Vec<u8>, Option<Vec<u8>>)>,
2729
) -> CryptoHash {
30+
let trie = tries.get_trie_for_shard(shard_id);
2831
assert_eq!(trie.storage.as_caching_storage().unwrap().shard_id, 0);
29-
let tries = Arc::new(ShardTries { tries: Arc::new(vec![trie.clone()]) });
3032
let trie_changes = trie.update(root, changes.iter().cloned()).unwrap();
3133
let (store_update, root) = tries.apply_all(&trie_changes, 0).unwrap();
3234
store_update.commit().unwrap();

0 commit comments

Comments
 (0)