11
11
blockstore_options:: { AccessType , BlockstoreOptions , LedgerColumnOptions } ,
12
12
} ,
13
13
bincode:: { deserialize, Options as BincodeOptions } ,
14
- byteorder:: { BigEndian , ByteOrder } ,
15
14
log:: * ,
16
15
prost:: Message ,
17
16
rocksdb:: {
@@ -103,6 +102,21 @@ const OPTIMISTIC_SLOTS_CF: &str = "optimistic_slots";
103
102
/// Column family for merkle roots
104
103
const MERKLE_ROOT_META_CF : & str = "merkle_root_meta" ;
105
104
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
+
106
120
#[ derive( Error , Debug ) ]
107
121
pub enum BlockstoreError {
108
122
#[ error( "shred for index exists" ) ]
@@ -837,8 +851,8 @@ impl<T: SlotColumn> Column for T {
837
851
}
838
852
839
853
/// 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 )
842
856
}
843
857
844
858
fn slot ( index : Self :: Index ) -> Slot {
@@ -881,22 +895,13 @@ pub trait ColumnIndexDeprecation: Column {
881
895
}
882
896
}
883
897
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
-
893
898
impl Column for columns:: TransactionStatus {
894
899
type Index = ( Signature , Slot ) ;
895
900
type Key = [ u8 ; SIGNATURE_BYTES + std:: mem:: size_of :: < Slot > ( ) ] ;
896
901
897
902
#[ inline]
898
903
fn key ( ( signature, slot) : & Self :: Index ) -> Self :: Key {
899
- concat_key_bytes ! ( Key ,
904
+ convert_column_index_to_key_bytes ! ( Key ,
900
905
..64 => signature. as_ref( ) ,
901
906
64 .. => & slot. to_be_bytes( ) ,
902
907
)
@@ -929,7 +934,7 @@ impl ColumnIndexDeprecation for columns::TransactionStatus {
929
934
type DeprecatedKey = [ u8 ; 80 ] ;
930
935
931
936
fn deprecated_key ( ( index, signature, slot) : Self :: DeprecatedIndex ) -> Self :: DeprecatedKey {
932
- concat_key_bytes ! ( DeprecatedKey ,
937
+ convert_column_index_to_key_bytes ! ( DeprecatedKey ,
933
938
..8 => & index. to_be_bytes( ) ,
934
939
8 ..72 => signature. as_ref( ) ,
935
940
72 .. => & slot. to_be_bytes( ) ,
@@ -940,19 +945,21 @@ impl ColumnIndexDeprecation for columns::TransactionStatus {
940
945
if key. len ( ) != std:: mem:: size_of :: < Self :: DeprecatedKey > ( ) {
941
946
return Err ( IndexError :: UnpackError ) ;
942
947
}
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
+ ) )
947
953
}
948
954
949
955
fn try_current_index ( key : & [ u8 ] ) -> std:: result:: Result < Self :: Index , IndexError > {
950
956
if key. len ( ) != Self :: CURRENT_INDEX_LEN {
951
957
return Err ( IndexError :: UnpackError ) ;
952
958
}
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
+ ) )
956
963
}
957
964
958
965
fn convert_index ( deprecated_index : Self :: DeprecatedIndex ) -> Self :: Index {
@@ -970,7 +977,7 @@ impl Column for columns::AddressSignatures {
970
977
971
978
#[ inline]
972
979
fn key ( ( pubkey, slot, transaction_index, signature) : & Self :: Index ) -> Self :: Key {
973
- concat_key_bytes ! ( Key ,
980
+ convert_column_index_to_key_bytes ! ( Key ,
974
981
..32 => pubkey. as_ref( ) ,
975
982
32 ..40 => & slot. to_be_bytes( ) ,
976
983
40 ..44 => & transaction_index. to_be_bytes( ) ,
@@ -1004,7 +1011,7 @@ impl ColumnIndexDeprecation for columns::AddressSignatures {
1004
1011
fn deprecated_key (
1005
1012
( primary_index, pubkey, slot, signature) : Self :: DeprecatedIndex ,
1006
1013
) -> Self :: DeprecatedKey {
1007
- concat_key_bytes ! ( DeprecatedKey ,
1014
+ convert_column_index_to_key_bytes ! ( DeprecatedKey ,
1008
1015
..8 => & primary_index. to_be_bytes( ) ,
1009
1016
8 ..40 => pubkey. as_ref( ) ,
1010
1017
40 ..48 => & slot. to_be_bytes( ) ,
@@ -1016,22 +1023,24 @@ impl ColumnIndexDeprecation for columns::AddressSignatures {
1016
1023
if key. len ( ) != std:: mem:: size_of :: < Self :: DeprecatedKey > ( ) {
1017
1024
return Err ( IndexError :: UnpackError ) ;
1018
1025
}
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
+ ) )
1024
1032
}
1025
1033
1026
1034
fn try_current_index ( key : & [ u8 ] ) -> std:: result:: Result < Self :: Index , IndexError > {
1027
1035
if key. len ( ) != Self :: CURRENT_INDEX_LEN {
1028
1036
return Err ( IndexError :: UnpackError ) ;
1029
1037
}
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
+ ) )
1035
1044
}
1036
1045
1037
1046
fn convert_index ( deprecated_index : Self :: DeprecatedIndex ) -> Self :: Index {
@@ -1046,7 +1055,7 @@ impl Column for columns::TransactionMemos {
1046
1055
1047
1056
#[ inline]
1048
1057
fn key ( ( signature, slot) : & Self :: Index ) -> Self :: Key {
1049
- concat_key_bytes ! ( Key ,
1058
+ convert_column_index_to_key_bytes ! ( Key ,
1050
1059
..64 => signature. as_ref( ) ,
1051
1060
64 .. => & slot. to_be_bytes( ) ,
1052
1061
)
@@ -1085,9 +1094,10 @@ impl ColumnIndexDeprecation for columns::TransactionMemos {
1085
1094
if key. len ( ) != Self :: CURRENT_INDEX_LEN {
1086
1095
return Err ( IndexError :: UnpackError ) ;
1087
1096
}
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
+ ) )
1091
1101
}
1092
1102
1093
1103
fn convert_index ( deprecated_index : Self :: DeprecatedIndex ) -> Self :: Index {
@@ -1104,8 +1114,8 @@ impl Column for columns::TransactionStatusIndex {
1104
1114
index. to_be_bytes ( )
1105
1115
}
1106
1116
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 )
1109
1119
}
1110
1120
1111
1121
fn slot ( _index : Self :: Index ) -> Slot {
@@ -1165,7 +1175,7 @@ impl Column for columns::ProgramCosts {
1165
1175
}
1166
1176
1167
1177
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 )
1169
1179
}
1170
1180
1171
1181
fn slot ( _index : Self :: Index ) -> Slot {
@@ -1178,7 +1188,7 @@ impl Column for columns::ProgramCosts {
1178
1188
}
1179
1189
1180
1190
impl Column for columns:: ShredCode {
1181
- type Index = ( Slot , u64 ) ;
1191
+ type Index = ( Slot , /*shred index:*/ u64 ) ;
1182
1192
type Key = <columns:: ShredData as Column >:: Key ;
1183
1193
1184
1194
#[ inline]
@@ -1187,7 +1197,7 @@ impl Column for columns::ShredCode {
1187
1197
<columns:: ShredData as Column >:: key ( index)
1188
1198
}
1189
1199
1190
- fn index ( key : & [ u8 ] ) -> ( Slot , u64 ) {
1200
+ fn index ( key : & [ u8 ] ) -> Self :: Index {
1191
1201
columns:: ShredData :: index ( key)
1192
1202
}
1193
1203
@@ -1204,21 +1214,22 @@ impl ColumnName for columns::ShredCode {
1204
1214
}
1205
1215
1206
1216
impl Column for columns:: ShredData {
1207
- type Index = ( Slot , u64 ) ;
1217
+ type Index = ( Slot , /*shred index:*/ u64 ) ;
1208
1218
type Key = [ u8 ; std:: mem:: size_of :: < Slot > ( ) + std:: mem:: size_of :: < u64 > ( ) ] ;
1209
1219
1210
1220
#[ inline]
1211
1221
fn key ( ( slot, index) : & Self :: Index ) -> Self :: Key {
1212
- concat_key_bytes ! ( Key ,
1222
+ convert_column_index_to_key_bytes ! ( Key ,
1213
1223
..8 => & slot. to_be_bytes( ) ,
1214
1224
8 .. => & index. to_be_bytes( ) ,
1215
1225
)
1216
1226
}
1217
1227
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
+ )
1222
1233
}
1223
1234
1224
1235
fn slot ( index : Self :: Index ) -> Slot {
@@ -1312,22 +1323,22 @@ impl TypedColumn for columns::SlotMeta {
1312
1323
}
1313
1324
1314
1325
impl Column for columns:: ErasureMeta {
1315
- type Index = ( Slot , u64 ) ;
1326
+ type Index = ( Slot , /*fec_set_index:*/ u64 ) ;
1316
1327
type Key = [ u8 ; std:: mem:: size_of :: < Slot > ( ) + std:: mem:: size_of :: < u64 > ( ) ] ;
1317
1328
1318
1329
#[ inline]
1319
1330
fn key ( ( slot, fec_set_index) : & Self :: Index ) -> Self :: Key {
1320
- concat_key_bytes ! ( Key ,
1331
+ convert_column_index_to_key_bytes ! ( Key ,
1321
1332
..8 => & slot. to_be_bytes( ) ,
1322
1333
8 .. => & fec_set_index. to_be_bytes( ) ,
1323
1334
)
1324
1335
}
1325
1336
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
+ )
1331
1342
}
1332
1343
1333
1344
fn slot ( index : Self :: Index ) -> Slot {
@@ -1359,17 +1370,17 @@ impl Column for columns::MerkleRootMeta {
1359
1370
1360
1371
#[ inline]
1361
1372
fn key ( ( slot, fec_set_index) : & Self :: Index ) -> Self :: Key {
1362
- concat_key_bytes ! ( Key ,
1373
+ convert_column_index_to_key_bytes ! ( Key ,
1363
1374
..8 => & slot. to_be_bytes( ) ,
1364
1375
8 .. => & fec_set_index. to_be_bytes( ) ,
1365
1376
)
1366
1377
}
1367
1378
1368
1379
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
+ )
1373
1384
}
1374
1385
1375
1386
fn slot ( ( slot, _fec_set_index) : Self :: Index ) -> Slot {
0 commit comments