Skip to content

Commit 0f56076

Browse files
authored
simplifies blockstore Column::index implementations (#4464)
The commits adds a small macro to simplify pub trait Column { fn index(key: &[u8]) -> Self::Index; // ... } implementations.
1 parent b441da9 commit 0f56076

File tree

5 files changed

+71
-64
lines changed

5 files changed

+71
-64
lines changed

Cargo.lock

Lines changed: 0 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

ledger/Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ edition = { workspace = true }
1313
assert_matches = { workspace = true }
1414
bincode = { workspace = true }
1515
bitflags = { workspace = true, features = ["serde"] }
16-
byteorder = { workspace = true }
1716
bzip2 = { workspace = true }
1817
chrono = { workspace = true, features = ["default", "serde"] }
1918
chrono-humanize = { workspace = true }

ledger/src/blockstore_db.rs

Lines changed: 71 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ use {
1111
blockstore_options::{AccessType, BlockstoreOptions, LedgerColumnOptions},
1212
},
1313
bincode::{deserialize, Options as BincodeOptions},
14-
byteorder::{BigEndian, ByteOrder},
1514
log::*,
1615
prost::Message,
1716
rocksdb::{
@@ -103,6 +102,21 @@ const OPTIMISTIC_SLOTS_CF: &str = "optimistic_slots";
103102
/// Column family for merkle roots
104103
const MERKLE_ROOT_META_CF: &str = "merkle_root_meta";
105104

105+
macro_rules! convert_column_index_to_key_bytes {
106+
($key:ident, $($range:expr => $bytes:expr),* $(,)?) => {{
107+
let mut key = [0u8; std::mem::size_of::<Self::$key>()];
108+
debug_assert_eq!(0 $(+$bytes.len())*, key.len());
109+
$(key[$range].copy_from_slice($bytes);)*
110+
key
111+
}};
112+
}
113+
114+
macro_rules! convert_column_key_bytes_to_index {
115+
($k:ident, $($a:literal..$b:literal => $f:expr),* $(,)?) => {{
116+
($($f(<[u8; $b-$a]>::try_from(&$k[$a..$b]).unwrap())),*)
117+
}};
118+
}
119+
106120
#[derive(Error, Debug)]
107121
pub enum BlockstoreError {
108122
#[error("shred for index exists")]
@@ -837,8 +851,8 @@ impl<T: SlotColumn> Column for T {
837851
}
838852

839853
/// Converts a RocksDB key to its u64 Index.
840-
fn index(key: &[u8]) -> u64 {
841-
BigEndian::read_u64(&key[..8])
854+
fn index(key: &[u8]) -> Self::Index {
855+
convert_column_key_bytes_to_index!(key, 0..8 => Slot::from_be_bytes)
842856
}
843857

844858
fn slot(index: Self::Index) -> Slot {
@@ -881,22 +895,13 @@ pub trait ColumnIndexDeprecation: Column {
881895
}
882896
}
883897

884-
macro_rules! concat_key_bytes {
885-
($key:ident, $($range:expr => $bytes:expr),* $(,)?) => {{
886-
let mut key = [0u8; std::mem::size_of::<Self::$key>()];
887-
debug_assert_eq!(0 $(+$bytes.len())*, key.len());
888-
$(key[$range].copy_from_slice($bytes);)*
889-
key
890-
}};
891-
}
892-
893898
impl Column for columns::TransactionStatus {
894899
type Index = (Signature, Slot);
895900
type Key = [u8; SIGNATURE_BYTES + std::mem::size_of::<Slot>()];
896901

897902
#[inline]
898903
fn key((signature, slot): &Self::Index) -> Self::Key {
899-
concat_key_bytes!(Key,
904+
convert_column_index_to_key_bytes!(Key,
900905
..64 => signature.as_ref(),
901906
64.. => &slot.to_be_bytes(),
902907
)
@@ -929,7 +934,7 @@ impl ColumnIndexDeprecation for columns::TransactionStatus {
929934
type DeprecatedKey = [u8; 80];
930935

931936
fn deprecated_key((index, signature, slot): Self::DeprecatedIndex) -> Self::DeprecatedKey {
932-
concat_key_bytes!(DeprecatedKey,
937+
convert_column_index_to_key_bytes!(DeprecatedKey,
933938
..8 => &index.to_be_bytes(),
934939
8..72 => signature.as_ref(),
935940
72.. => &slot.to_be_bytes(),
@@ -940,19 +945,21 @@ impl ColumnIndexDeprecation for columns::TransactionStatus {
940945
if key.len() != std::mem::size_of::<Self::DeprecatedKey>() {
941946
return Err(IndexError::UnpackError);
942947
}
943-
let primary_index = BigEndian::read_u64(&key[0..8]);
944-
let signature = Signature::try_from(&key[8..72]).unwrap();
945-
let slot = BigEndian::read_u64(&key[72..80]);
946-
Ok((primary_index, signature, slot))
948+
Ok(convert_column_key_bytes_to_index!(key,
949+
0..8 => u64::from_be_bytes, // primary index
950+
8..72 => Signature::from,
951+
72..80 => Slot::from_be_bytes,
952+
))
947953
}
948954

949955
fn try_current_index(key: &[u8]) -> std::result::Result<Self::Index, IndexError> {
950956
if key.len() != Self::CURRENT_INDEX_LEN {
951957
return Err(IndexError::UnpackError);
952958
}
953-
let signature = Signature::try_from(&key[0..64]).unwrap();
954-
let slot = BigEndian::read_u64(&key[64..72]);
955-
Ok((signature, slot))
959+
Ok(convert_column_key_bytes_to_index!(key,
960+
0..64 => Signature::from,
961+
64..72 => Slot::from_be_bytes,
962+
))
956963
}
957964

958965
fn convert_index(deprecated_index: Self::DeprecatedIndex) -> Self::Index {
@@ -970,7 +977,7 @@ impl Column for columns::AddressSignatures {
970977

971978
#[inline]
972979
fn key((pubkey, slot, transaction_index, signature): &Self::Index) -> Self::Key {
973-
concat_key_bytes!(Key,
980+
convert_column_index_to_key_bytes!(Key,
974981
..32 => pubkey.as_ref(),
975982
32..40 => &slot.to_be_bytes(),
976983
40..44 => &transaction_index.to_be_bytes(),
@@ -1004,7 +1011,7 @@ impl ColumnIndexDeprecation for columns::AddressSignatures {
10041011
fn deprecated_key(
10051012
(primary_index, pubkey, slot, signature): Self::DeprecatedIndex,
10061013
) -> Self::DeprecatedKey {
1007-
concat_key_bytes!(DeprecatedKey,
1014+
convert_column_index_to_key_bytes!(DeprecatedKey,
10081015
..8 => &primary_index.to_be_bytes(),
10091016
8..40 => pubkey.as_ref(),
10101017
40..48 => &slot.to_be_bytes(),
@@ -1016,22 +1023,24 @@ impl ColumnIndexDeprecation for columns::AddressSignatures {
10161023
if key.len() != std::mem::size_of::<Self::DeprecatedKey>() {
10171024
return Err(IndexError::UnpackError);
10181025
}
1019-
let primary_index = BigEndian::read_u64(&key[0..8]);
1020-
let pubkey = Pubkey::try_from(&key[8..40]).unwrap();
1021-
let slot = BigEndian::read_u64(&key[40..48]);
1022-
let signature = Signature::try_from(&key[48..112]).unwrap();
1023-
Ok((primary_index, pubkey, slot, signature))
1026+
Ok(convert_column_key_bytes_to_index!(key,
1027+
0..8 => u64::from_be_bytes, // primary index
1028+
8..40 => Pubkey::from,
1029+
40..48 => Slot::from_be_bytes,
1030+
48..112 => Signature::from,
1031+
))
10241032
}
10251033

10261034
fn try_current_index(key: &[u8]) -> std::result::Result<Self::Index, IndexError> {
10271035
if key.len() != Self::CURRENT_INDEX_LEN {
10281036
return Err(IndexError::UnpackError);
10291037
}
1030-
let pubkey = Pubkey::try_from(&key[0..32]).unwrap();
1031-
let slot = BigEndian::read_u64(&key[32..40]);
1032-
let transaction_index = BigEndian::read_u32(&key[40..44]);
1033-
let signature = Signature::try_from(&key[44..108]).unwrap();
1034-
Ok((pubkey, slot, transaction_index, signature))
1038+
Ok(convert_column_key_bytes_to_index!(key,
1039+
0..32 => Pubkey::from,
1040+
32..40 => Slot::from_be_bytes,
1041+
40..44 => u32::from_be_bytes, // transaction index
1042+
44..108 => Signature::from,
1043+
))
10351044
}
10361045

10371046
fn convert_index(deprecated_index: Self::DeprecatedIndex) -> Self::Index {
@@ -1046,7 +1055,7 @@ impl Column for columns::TransactionMemos {
10461055

10471056
#[inline]
10481057
fn key((signature, slot): &Self::Index) -> Self::Key {
1049-
concat_key_bytes!(Key,
1058+
convert_column_index_to_key_bytes!(Key,
10501059
..64 => signature.as_ref(),
10511060
64.. => &slot.to_be_bytes(),
10521061
)
@@ -1085,9 +1094,10 @@ impl ColumnIndexDeprecation for columns::TransactionMemos {
10851094
if key.len() != Self::CURRENT_INDEX_LEN {
10861095
return Err(IndexError::UnpackError);
10871096
}
1088-
let signature = Signature::try_from(&key[0..64]).unwrap();
1089-
let slot = BigEndian::read_u64(&key[64..72]);
1090-
Ok((signature, slot))
1097+
Ok(convert_column_key_bytes_to_index!(key,
1098+
0..64 => Signature::from,
1099+
64..72 => Slot::from_be_bytes,
1100+
))
10911101
}
10921102

10931103
fn convert_index(deprecated_index: Self::DeprecatedIndex) -> Self::Index {
@@ -1104,8 +1114,8 @@ impl Column for columns::TransactionStatusIndex {
11041114
index.to_be_bytes()
11051115
}
11061116

1107-
fn index(key: &[u8]) -> u64 {
1108-
BigEndian::read_u64(&key[..8])
1117+
fn index(key: &[u8]) -> Self::Index {
1118+
convert_column_key_bytes_to_index!(key, 0..8 => u64::from_be_bytes)
11091119
}
11101120

11111121
fn slot(_index: Self::Index) -> Slot {
@@ -1165,7 +1175,7 @@ impl Column for columns::ProgramCosts {
11651175
}
11661176

11671177
fn index(key: &[u8]) -> Self::Index {
1168-
Pubkey::try_from(&key[..32]).unwrap()
1178+
convert_column_key_bytes_to_index!(key, 0..32 => Pubkey::from)
11691179
}
11701180

11711181
fn slot(_index: Self::Index) -> Slot {
@@ -1178,7 +1188,7 @@ impl Column for columns::ProgramCosts {
11781188
}
11791189

11801190
impl Column for columns::ShredCode {
1181-
type Index = (Slot, u64);
1191+
type Index = (Slot, /*shred index:*/ u64);
11821192
type Key = <columns::ShredData as Column>::Key;
11831193

11841194
#[inline]
@@ -1187,7 +1197,7 @@ impl Column for columns::ShredCode {
11871197
<columns::ShredData as Column>::key(index)
11881198
}
11891199

1190-
fn index(key: &[u8]) -> (Slot, u64) {
1200+
fn index(key: &[u8]) -> Self::Index {
11911201
columns::ShredData::index(key)
11921202
}
11931203

@@ -1204,21 +1214,22 @@ impl ColumnName for columns::ShredCode {
12041214
}
12051215

12061216
impl Column for columns::ShredData {
1207-
type Index = (Slot, u64);
1217+
type Index = (Slot, /*shred index:*/ u64);
12081218
type Key = [u8; std::mem::size_of::<Slot>() + std::mem::size_of::<u64>()];
12091219

12101220
#[inline]
12111221
fn key((slot, index): &Self::Index) -> Self::Key {
1212-
concat_key_bytes!(Key,
1222+
convert_column_index_to_key_bytes!(Key,
12131223
..8 => &slot.to_be_bytes(),
12141224
8.. => &index.to_be_bytes(),
12151225
)
12161226
}
12171227

1218-
fn index(key: &[u8]) -> (Slot, u64) {
1219-
let slot = BigEndian::read_u64(&key[..8]);
1220-
let index = BigEndian::read_u64(&key[8..16]);
1221-
(slot, index)
1228+
fn index(key: &[u8]) -> Self::Index {
1229+
convert_column_key_bytes_to_index!(key,
1230+
0..8 => Slot::from_be_bytes,
1231+
8..16 => u64::from_be_bytes, // shred index
1232+
)
12221233
}
12231234

12241235
fn slot(index: Self::Index) -> Slot {
@@ -1312,22 +1323,22 @@ impl TypedColumn for columns::SlotMeta {
13121323
}
13131324

13141325
impl Column for columns::ErasureMeta {
1315-
type Index = (Slot, u64);
1326+
type Index = (Slot, /*fec_set_index:*/ u64);
13161327
type Key = [u8; std::mem::size_of::<Slot>() + std::mem::size_of::<u64>()];
13171328

13181329
#[inline]
13191330
fn key((slot, fec_set_index): &Self::Index) -> Self::Key {
1320-
concat_key_bytes!(Key,
1331+
convert_column_index_to_key_bytes!(Key,
13211332
..8 => &slot.to_be_bytes(),
13221333
8.. => &fec_set_index.to_be_bytes(),
13231334
)
13241335
}
13251336

1326-
fn index(key: &[u8]) -> (Slot, u64) {
1327-
let slot = BigEndian::read_u64(&key[..8]);
1328-
let set_index = BigEndian::read_u64(&key[8..]);
1329-
1330-
(slot, set_index)
1337+
fn index(key: &[u8]) -> Self::Index {
1338+
convert_column_key_bytes_to_index!(key,
1339+
0..8 => Slot::from_be_bytes,
1340+
8..16 => u64::from_be_bytes, // fec_set_index
1341+
)
13311342
}
13321343

13331344
fn slot(index: Self::Index) -> Slot {
@@ -1359,17 +1370,17 @@ impl Column for columns::MerkleRootMeta {
13591370

13601371
#[inline]
13611372
fn key((slot, fec_set_index): &Self::Index) -> Self::Key {
1362-
concat_key_bytes!(Key,
1373+
convert_column_index_to_key_bytes!(Key,
13631374
..8 => &slot.to_be_bytes(),
13641375
8.. => &fec_set_index.to_be_bytes(),
13651376
)
13661377
}
13671378

13681379
fn index(key: &[u8]) -> Self::Index {
1369-
let slot = BigEndian::read_u64(&key[..8]);
1370-
let fec_set_index = BigEndian::read_u32(&key[8..]);
1371-
1372-
(slot, fec_set_index)
1380+
convert_column_key_bytes_to_index!(key,
1381+
0..8 => Slot::from_be_bytes,
1382+
8..12 => u32::from_be_bytes, // fec_set_index
1383+
)
13731384
}
13741385

13751386
fn slot((slot, _fec_set_index): Self::Index) -> Slot {

programs/sbf/Cargo.lock

Lines changed: 0 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

svm/examples/Cargo.lock

Lines changed: 0 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)