fix triedb deal with null rlp key
@@ -2,9 +2,13 @@ use std::sync::Arc;
use cita_database::error::DatabaseError;
use cita_database::{DataCategory, Database};
+use cita_types::H256;
+use hashable::HASH_NULL_RLP;
use parking_lot::RwLock;
use std::collections::HashMap;
+static NULL_RLP_STATIC: [u8; 1] = [0x80; 1];
+
#[derive(Debug)]
pub struct TrieDB<DB>
where
@@ -38,6 +42,9 @@ where
type Error = DatabaseError;
fn get(&self, key: &[u8]) -> Result<Option<Vec<u8>>, Self::Error> {
+ if H256::from(key) == HASH_NULL_RLP {
+ return Ok(Some(NULL_RLP_STATIC.to_vec()));
+ }
match self.cache.read().get(key) {
Some(v) => Ok(Some(v.to_vec())),
None => self.db.get(Some(DataCategory::State), key),
@@ -45,11 +52,17 @@ where
}
fn insert(&self, key: Vec<u8>, value: Vec<u8>) -> Result<(), Self::Error> {
+ if H256::from(key.as_slice()) == HASH_NULL_RLP {
+ return Ok(());
+ }
self.cache.write().insert(key, value);
Ok(())
}
fn contains(&self, key: &[u8]) -> Result<bool, Self::Error> {
+ if H256::from(key) == HASH_NULL_RLP {
+ return Ok(true);
+ }
if self.cache.read().contains_key(key) {
Ok(true)
} else {
@@ -58,6 +71,9 @@ where
}
fn remove(&self, key: &[u8]) -> Result<(), Self::Error> {
+ if H256::from(key) == HASH_NULL_RLP {
+ return Ok(());
+ }
self.cache.write().remove(key);
self.db.remove(Some(DataCategory::State), key)
}
@@ -66,6 +82,9 @@ where
let mut cache = self.cache.write();
for i in 0..keys.len() {
let key = keys[i].clone();
+ if H256::from(key.as_slice()) == HASH_NULL_RLP {
+ continue;
+ }
let value = values[i].clone();
cache.insert(key, value);
}
@@ -73,13 +92,18 @@ where
}
fn remove_batch(&self, keys: &[Vec<u8>]) -> Result<(), Self::Error> {
+ let keys: Vec<Vec<u8>> = keys
+ .iter()
+ .cloned()
+ .filter(|key| H256::from(key.as_slice()) != HASH_NULL_RLP)
+ .collect();
{
let mut cache = self.cache.write();
- for key in keys {
+ for key in &keys {
cache.remove(key);
}
}
- self.db.remove_batch(Some(DataCategory::State), keys)
+ self.db.remove_batch(Some(DataCategory::State), &keys)
}
fn flush(&self) -> Result<(), Self::Error> {
