Disable trieDB ref counting for now.

This commit is contained in:
Gav Wood 2016-01-18 00:51:55 +01:00
parent 589ecf10af
commit fab99e8538
5 changed files with 63 additions and 17 deletions

1
.gitignore vendored
View File

@ -11,6 +11,7 @@ Cargo.lock
# Generated by Cargo # Generated by Cargo
/target/ /target/
/util/target
# vim stuff # vim stuff
*.swp *.swp

View File

@ -61,6 +61,44 @@ impl<'a> Hashable for TransactionView<'a> {
} }
} }
/// View onto transaction rlp.
pub struct AccountView<'a> {
rlp: Rlp<'a>
}
impl<'a> AccountView<'a> {
/// Creates new view onto block from raw bytes.
pub fn new(bytes: &'a [u8]) -> AccountView<'a> {
AccountView {
rlp: Rlp::new(bytes)
}
}
/// Creates new view onto block from rlp.
pub fn new_from_rlp(rlp: Rlp<'a>) -> AccountView<'a> {
AccountView {
rlp: rlp
}
}
/// Return reference to underlaying rlp.
pub fn rlp(&self) -> &Rlp<'a> {
&self.rlp
}
/// Get the nonce field of the transaction.
pub fn nonce(&self) -> U256 { self.rlp.val_at(0) }
/// Get the gas_price field of the transaction.
pub fn balance(&self) -> U256 { self.rlp.val_at(1) }
/// Get the gas field of the transaction.
pub fn storage_root(&self) -> H256 { self.rlp.val_at(2) }
/// Get the value field of the transaction.
pub fn code_hash(&self) -> H256 { self.rlp.val_at(3) }
}
/// View onto block rlp. /// View onto block rlp.
pub struct BlockView<'a> { pub struct BlockView<'a> {
rlp: Rlp<'a> rlp: Rlp<'a>
@ -97,13 +135,13 @@ impl<'a> BlockView<'a> {
} }
/// Return List of transactions in given block. /// Return List of transactions in given block.
pub fn transaction_views(&self) -> Vec<TransactionView> { pub fn transactions(&self) -> Vec<Transaction> {
self.rlp.at(1).iter().map(|rlp| TransactionView::new_from_rlp(rlp)).collect() self.rlp.val_at(1)
} }
/// Return List of transactions in given block. /// Return List of transactions in given block.
pub fn transactions(&self) -> Vec<Transaction> { pub fn transaction_views(&self) -> Vec<TransactionView> {
self.rlp.val_at(1) self.rlp.at(1).iter().map(|rlp| TransactionView::new_from_rlp(rlp)).collect()
} }
/// Return transaction hashes. /// Return transaction hashes.
@ -116,6 +154,11 @@ impl<'a> BlockView<'a> {
self.rlp.val_at(2) self.rlp.val_at(2)
} }
/// Return List of transactions in given block.
pub fn uncle_views(&self) -> Vec<HeaderView> {
self.rlp.at(2).iter().map(|rlp| HeaderView::new_from_rlp(rlp)).collect()
}
/// Return list of uncle hashes of given block. /// Return list of uncle hashes of given block.
pub fn uncle_hashes(&self) -> Vec<H256> { pub fn uncle_hashes(&self) -> Vec<H256> {
self.rlp.at(2).iter().map(|rlp| rlp.as_raw().sha3()).collect() self.rlp.at(2).iter().map(|rlp| rlp.as_raw().sha3()).collect()

View File

@ -215,10 +215,14 @@ macro_rules! impl_hash {
} }
impl fmt::Display for $from { impl fmt::Display for $from {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
for i in self.0[0..3].iter() { for i in self.0[0..2].iter() {
try!(write!(f, "{:02x}", i)); try!(write!(f, "{:02x}", i));
} }
write!(f, "…{:02x}", self.0.last().unwrap()) try!(write!(f, ""));
for i in self.0[$size - 4..$size].iter() {
try!(write!(f, "{:02x}", i));
}
Ok(())
} }
} }
@ -544,7 +548,7 @@ mod tests {
fn hash() { fn hash() {
let h = H64([0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef]); let h = H64([0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef]);
assert_eq!(H64::from_str("0123456789abcdef").unwrap(), h); assert_eq!(H64::from_str("0123456789abcdef").unwrap(), h);
assert_eq!(format!("{}", h), "012345…ef"); assert_eq!(format!("{}", h), "012389abcdef");
assert_eq!(format!("{:?}", h), "0123456789abcdef"); assert_eq!(format!("{:?}", h), "0123456789abcdef");
assert_eq!(h.hex(), "0123456789abcdef"); assert_eq!(h.hex(), "0123456789abcdef");
assert!(h == h); assert!(h == h);

View File

@ -70,7 +70,9 @@ impl OverlayDB {
let mut ret = 0u32; let mut ret = 0u32;
for i in self.overlay.drain().into_iter() { for i in self.overlay.drain().into_iter() {
let (key, (value, rc)) = i; let (key, (value, rc)) = i;
if rc != 0 { // until we figure out state trie pruning, only commit stuff when it has a strictly positive delkta of RCs -
// this prevents RCs being reduced to 0 where the DB would pretent that the node had been removed.
if rc > 0 {
match self.payload(&key) { match self.payload(&key) {
Some(x) => { Some(x) => {
let (back_value, back_rc) = x; let (back_value, back_rc) = x;

View File

@ -42,7 +42,7 @@ impl<'db> TrieDB<'db> {
/// Panics, if `root` does not exist /// Panics, if `root` does not exist
pub fn new(db: &'db HashDB, root: &'db H256) -> Self { pub fn new(db: &'db HashDB, root: &'db H256) -> Self {
if !db.exists(root) { if !db.exists(root) {
flush(format!("Trie root not found {}", root)); flushln!("TrieDB::new({}): Trie root not found!", root);
panic!("Trie root not found!"); panic!("Trie root not found!");
} }
TrieDB { TrieDB {
@ -258,10 +258,7 @@ impl<'a> TrieDBIterator<'a> {
node: self.db.get_node(d) node: self.db.get_node(d)
}); });
match self.trail.last().unwrap().node { match self.trail.last().unwrap().node {
Node::Leaf(n, _) | Node::Extension(n, _) => { Node::Leaf(n, _) | Node::Extension(n, _) => { self.key_nibbles.extend(n.iter()); },
println!("Entering. Extend key {:?}, {:?}", self.key_nibbles, n.iter().collect::<Vec<_>>());
self.key_nibbles.extend(n.iter());
},
_ => {} _ => {}
} }
} }
@ -288,11 +285,10 @@ impl<'a> Iterator for TrieDBIterator<'a> {
(Status::Exiting, n) => { (Status::Exiting, n) => {
match n { match n {
Node::Leaf(n, _) | Node::Extension(n, _) => { Node::Leaf(n, _) | Node::Extension(n, _) => {
println!("Exiting. Truncate key {:?}, {:?}", self.key_nibbles, n.iter().collect::<Vec<_>>());
let l = self.key_nibbles.len(); let l = self.key_nibbles.len();
self.key_nibbles.truncate(l - n.len()); self.key_nibbles.truncate(l - n.len());
}, },
Node::Branch(_, _) => { println!("Exit branch. Pop {:?}.", self.key_nibbles); self.key_nibbles.pop(); }, Node::Branch(_, _) => { self.key_nibbles.pop(); },
_ => {} _ => {}
} }
self.trail.pop(); self.trail.pop();
@ -304,13 +300,13 @@ impl<'a> Iterator for TrieDBIterator<'a> {
(Status::At, Node::Branch(_, _)) => self.next(), (Status::At, Node::Branch(_, _)) => self.next(),
(Status::AtChild(i), Node::Branch(children, _)) if children[i].len() > 0 => { (Status::AtChild(i), Node::Branch(children, _)) if children[i].len() > 0 => {
match i { match i {
0 => { println!("Enter first child of branch. Push {:?}.", self.key_nibbles); self.key_nibbles.push(0); }, 0 => self.key_nibbles.push(0),
i => *self.key_nibbles.last_mut().unwrap() = i as u8, i => *self.key_nibbles.last_mut().unwrap() = i as u8,
} }
self.descend_next(children[i]) self.descend_next(children[i])
}, },
(Status::AtChild(i), Node::Branch(_, _)) => { (Status::AtChild(i), Node::Branch(_, _)) => {
if i == 0 { println!("Enter first child of branch. Push {:?}.", self.key_nibbles); self.key_nibbles.push(0); } if i == 0 { self.key_nibbles.push(0); }
self.next() self.next()
}, },
_ => panic!() // Should never see Entering or AtChild without a Branch here. _ => panic!() // Should never see Entering or AtChild without a Branch here.