diff --git a/src/db_ledger.rs b/src/db_ledger.rs index 15d8183d45a7b9..7012880506d57e 100644 --- a/src/db_ledger.rs +++ b/src/db_ledger.rs @@ -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}; @@ -103,7 +103,7 @@ pub struct MetaCf {} impl MetaCf { pub fn key(slot_height: u64) -> Vec { 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 } } @@ -144,20 +144,20 @@ impl DataCf { pub fn key(slot_height: u64, index: u64) -> Vec { 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 { let mut rdr = io::Cursor::new(&key[0..8]); - let height = rdr.read_u64::()?; + let height = rdr.read_u64::()?; Ok(height) } pub fn index_from_key(key: &[u8]) -> Result { let mut rdr = io::Cursor::new(&key[8..16]); - let index = rdr.read_u64::()?; + let index = rdr.read_u64::()?; Ok(index) } } @@ -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(¤t_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"); + } }