Skip to content

Commit 5f69daa

Browse files
committed
Revert "Simplify utxoindex key parsing"
This reverts commit 16641dc.
1 parent 9550f04 commit 5f69daa

1 file changed

Lines changed: 11 additions & 20 deletions

File tree

indexes/utxoindex/src/stores/indexed_utxos.rs

Lines changed: 11 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,6 @@ use std::fmt::Display;
1414
use std::sync::Arc;
1515

1616
pub const VERSION_TYPE_SIZE: usize = size_of::<ScriptPublicKeyVersion>(); // Const since we need to re-use this a few times.
17-
const SCRIPT_LENGTH_SIZE: usize = size_of::<u64>();
18-
const SCRIPT_PUBLIC_KEY_BUCKET_HEADER_SIZE: usize = VERSION_TYPE_SIZE + SCRIPT_LENGTH_SIZE;
1917

2018
/// [`ScriptPublicKeyBucket`].
2119
/// Consists of 2 bytes of little endian [VersionType] bytes, followed by a variable size of [ScriptVec].
@@ -24,7 +22,7 @@ struct ScriptPublicKeyBucket(Vec<u8>);
2422

2523
impl From<&ScriptPublicKey> for ScriptPublicKeyBucket {
2624
fn from(script_public_key: &ScriptPublicKey) -> Self {
27-
let mut bytes: Vec<u8> = Vec::with_capacity(SCRIPT_PUBLIC_KEY_BUCKET_HEADER_SIZE + script_public_key.script().len());
25+
let mut bytes: Vec<u8> = Vec::with_capacity(VERSION_TYPE_SIZE + script_public_key.script().len());
2826
bytes.extend_from_slice(&script_public_key.version().to_le_bytes());
2927
bytes.extend_from_slice(&(script_public_key.script().len() as u64).to_le_bytes()); // TODO: Consider using a smaller integer
3028
bytes.extend_from_slice(script_public_key.script());
@@ -39,9 +37,9 @@ impl From<ScriptPublicKeyBucket> for ScriptPublicKey {
3937
);
4038

4139
let script_size =
42-
u64::from_le_bytes(bucket.0[VERSION_TYPE_SIZE..SCRIPT_PUBLIC_KEY_BUCKET_HEADER_SIZE].try_into().unwrap()) as usize;
40+
u64::from_le_bytes(bucket.0[VERSION_TYPE_SIZE..VERSION_TYPE_SIZE + size_of::<u64>()].try_into().unwrap()) as usize;
4341
let script =
44-
ScriptVec::from_slice(&bucket.0[SCRIPT_PUBLIC_KEY_BUCKET_HEADER_SIZE..SCRIPT_PUBLIC_KEY_BUCKET_HEADER_SIZE + script_size]);
42+
ScriptVec::from_slice(&bucket.0[VERSION_TYPE_SIZE + size_of::<u64>()..VERSION_TYPE_SIZE + size_of::<u64>() + script_size]);
4543

4644
Self::new(version, script)
4745
}
@@ -64,16 +62,6 @@ pub const TRANSACTION_OUTPOINT_KEY_SIZE: usize = kaspa_hashes::HASH_SIZE + size_
6462
#[derive(Eq, Hash, PartialEq, Debug, Copy, Clone)]
6563
struct TransactionOutpointKey([u8; TRANSACTION_OUTPOINT_KEY_SIZE]);
6664

67-
impl TransactionOutpointKey {
68-
fn from_slice(bytes: &[u8]) -> Self {
69-
Self(bytes.try_into().expect("expected transaction outpoint key size"))
70-
}
71-
72-
fn from_suffix(bytes: &[u8]) -> Self {
73-
Self::from_slice(&bytes[bytes.len() - TRANSACTION_OUTPOINT_KEY_SIZE..])
74-
}
75-
}
76-
7765
impl From<TransactionOutpointKey> for TransactionOutpoint {
7866
fn from(key: TransactionOutpointKey) -> Self {
7967
let transaction_id = Hash::from_slice(&key.0[..kaspa_hashes::HASH_SIZE]);
@@ -118,6 +106,10 @@ impl UtxoEntryFullAccessKey {
118106
bytes.extend_from_slice(transaction_outpoint_key.as_ref());
119107
Self(Arc::new(bytes))
120108
}
109+
110+
pub fn extract_outpoint(&self) -> TransactionOutpoint {
111+
TransactionOutpoint::from(TransactionOutpointKey(self.0[(self.0.len() - TRANSACTION_OUTPOINT_KEY_SIZE)..].try_into().unwrap()))
112+
}
121113
}
122114

123115
impl AsRef<[u8]> for UtxoEntryFullAccessKey {
@@ -173,7 +165,7 @@ impl UtxoSetByScriptPublicKeyStoreReader for DbUtxoSetByScriptPublicKeyStore {
173165
let utxos_by_script_public_keys_inner = CompactUtxoCollection::from_iter(
174166
self.access.seek_iterator(Some(script_public_key_bucket.as_ref()), None, usize::MAX, false).map(|res| {
175167
let (key, entry) = res.unwrap();
176-
(TransactionOutpointKey::from_slice(key.as_ref()).into(), entry)
168+
(TransactionOutpointKey(<[u8; TRANSACTION_OUTPOINT_KEY_SIZE]>::try_from(&key[..]).unwrap()).into(), entry)
177169
}),
178170
);
179171
entries_count += utxos_by_script_public_keys_inner.len();
@@ -206,10 +198,9 @@ impl UtxoSetByScriptPublicKeyStoreReader for DbUtxoSetByScriptPublicKeyStore {
206198

207199
// This can have a big memory footprint, so it should be used only for tests.
208200
fn get_all_outpoints(&self) -> StoreResult<HashSet<TransactionOutpoint>> {
209-
Ok(HashSet::from_iter(self.access.iterator().map(|res| {
210-
let (key, _) = res.unwrap();
211-
TransactionOutpoint::from(TransactionOutpointKey::from_suffix(key.as_ref()))
212-
})))
201+
Ok(HashSet::from_iter(
202+
self.access.iterator().map(|res| UtxoEntryFullAccessKey(Arc::new(res.unwrap().0.to_vec())).extract_outpoint()),
203+
))
213204
}
214205
}
215206

0 commit comments

Comments
 (0)