Skip to content

Commit

Permalink
Fix endianess in db_ledger to account for the default byte-comparator…
Browse files Browse the repository at this point in the history
… used by Rocksdb (#1885)
  • Loading branch information
carllin authored Nov 22, 2018
1 parent 521de13 commit a3321a5
Showing 1 changed file with 46 additions and 6 deletions.
52 changes: 46 additions & 6 deletions src/db_ledger.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
//! access read to a persistent file-based ledger.
use bincode::{deserialize, serialize};
use byteorder::{ByteOrder, LittleEndian, ReadBytesExt};
use byteorder::{BigEndian, ByteOrder, ReadBytesExt};
use entry::Entry;
use ledger::Block;
use packet::{Blob, SharedBlob, BLOB_HEADER_SIZE};
Expand Down Expand Up @@ -103,7 +103,7 @@ pub struct MetaCf {}
impl MetaCf {
pub fn key(slot_height: u64) -> Vec<u8> {
let mut key = vec![0u8; 8];
LittleEndian::write_u64(&mut key[0..8], slot_height);
BigEndian::write_u64(&mut key[0..8], slot_height);
key
}
}
Expand Down Expand Up @@ -144,20 +144,20 @@ impl DataCf {

pub fn key(slot_height: u64, index: u64) -> Vec<u8> {
let mut key = vec![0u8; 16];
LittleEndian::write_u64(&mut key[0..8], slot_height);
LittleEndian::write_u64(&mut key[8..16], index);
BigEndian::write_u64(&mut key[0..8], slot_height);
BigEndian::write_u64(&mut key[8..16], index);
key
}

pub fn slot_height_from_key(key: &[u8]) -> Result<u64> {
let mut rdr = io::Cursor::new(&key[0..8]);
let height = rdr.read_u64::<LittleEndian>()?;
let height = rdr.read_u64::<BigEndian>()?;
Ok(height)
}

pub fn index_from_key(key: &[u8]) -> Result<u64> {
let mut rdr = io::Cursor::new(&key[8..16]);
let index = rdr.read_u64::<LittleEndian>()?;
let index = rdr.read_u64::<BigEndian>()?;
Ok(index)
}
}
Expand Down Expand Up @@ -631,4 +631,44 @@ mod tests {
DB::destroy(&Options::default(), &ledger_path)
.expect("Expected successful database destruction");
}

#[test]
pub fn test_iteration_order() {
let slot = 0;
// Create RocksDb ledger
let db_ledger_path = get_tmp_ledger_path("test_iteration_order");
{
let mut db_ledger = DbLedger::open(&db_ledger_path).unwrap();

// Write entries
let num_entries = 8;
let shared_blobs = make_tiny_test_entries(num_entries).to_blobs();

for (b, i) in shared_blobs.iter().zip(0..num_entries) {
b.write().unwrap().set_index(1 << (i * 8)).unwrap();
}

db_ledger
.write_shared_blobs(DEFAULT_SLOT_HEIGHT, &shared_blobs)
.expect("Expected successful write of blobs");
let mut db_iterator = db_ledger
.db
.raw_iterator_cf(db_ledger.data_cf.handle(&db_ledger.db))
.expect("Expected to be able to open database iterator");

db_iterator.seek(&DataCf::key(slot, 1));

// Iterate through ledger
for i in 0..num_entries {
assert!(db_iterator.valid());
let current_key = db_iterator.key().expect("Expected a valid key");
let current_index = DataCf::index_from_key(&current_key)
.expect("Expect to be able to parse index from valid key");
assert_eq!(current_index, (1 as u64) << (i * 8));
db_iterator.next();
}
}
DB::destroy(&Options::default(), &db_ledger_path)
.expect("Expected successful database destruction");
}
}

0 comments on commit a3321a5

Please sign in to comment.