Skip to content

Commit 845443b

Browse files
Merge pull request #357 from mintlayer/fix/remove_tx_size_from_tx_index
Remove transaction size from transaction index
2 parents 0bf9dbf + 393b237 commit 845443b

File tree

4 files changed

+9
-26
lines changed

4 files changed

+9
-26
lines changed

chainstate-storage/src/internal/mod.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -261,10 +261,8 @@ impl<Tx: for<'a> traits::GetMapRef<'a, Schema>> BlockchainStorageRead for StoreT
261261
Ok(None) => Ok(None),
262262
Ok(Some(block)) => {
263263
let begin = tx_index.byte_offset_in_block() as usize;
264-
let end = begin + tx_index.serialized_size() as usize;
265-
let encoded_tx = block.get(begin..end).expect("Transaction outside of block range");
266-
let tx =
267-
Transaction::decode_all(&mut &*encoded_tx).expect("Invalid tx encoding in DB");
264+
let encoded_tx = block.get(begin..).expect("Transaction outside of block range");
265+
let tx = Transaction::decode(&mut &*encoded_tx).expect("Invalid tx encoding in DB");
268266
Ok(Some(tx))
269267
}
270268
}

chainstate-storage/src/internal/test.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,8 +103,7 @@ fn test_storage_manipulation() {
103103
&enc_block0[offset_tx0..].starts_with(&enc_tx0),
104104
"Transaction format has changed, adjust the offset in this test",
105105
);
106-
let pos_tx0 =
107-
TxMainChainPosition::new(block0.get_id(), offset_tx0 as u32, enc_tx0.len() as u32);
106+
let pos_tx0 = TxMainChainPosition::new(block0.get_id(), offset_tx0 as u32);
108107
assert_eq!(
109108
&store.get_mainchain_tx_by_position(&pos_tx0).unwrap().unwrap(),
110109
&tx0

common/src/chain/transaction/transaction_index/mod.rs

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -64,15 +64,13 @@ pub enum OutputSpentState {
6464
pub struct TxMainChainPosition {
6565
block_id: Id<Block>,
6666
byte_offset_in_block: u32,
67-
serialized_size: u32,
6867
}
6968

7069
impl TxMainChainPosition {
71-
pub fn new(block_id: Id<Block>, byte_offset_in_block: u32, serialized_size: u32) -> Self {
70+
pub fn new(block_id: Id<Block>, byte_offset_in_block: u32) -> Self {
7271
TxMainChainPosition {
7372
block_id,
7473
byte_offset_in_block,
75-
serialized_size,
7674
}
7775
}
7876

@@ -83,10 +81,6 @@ impl TxMainChainPosition {
8381
pub fn byte_offset_in_block(&self) -> u32 {
8482
self.byte_offset_in_block
8583
}
86-
87-
pub fn serialized_size(&self) -> u32 {
88-
self.serialized_size
89-
}
9084
}
9185

9286
#[derive(Clone, Debug, PartialEq, Eq)]
@@ -175,14 +169,7 @@ pub fn calculate_tx_index_from_block(
175169
.try_into()
176170
.expect("Number conversion from usize to u32 should not fail here (1)");
177171

178-
let tx_position = TxMainChainPosition::new(
179-
block.get_id(),
180-
offset_tx,
181-
enc_tx
182-
.len()
183-
.try_into()
184-
.expect("Number conversion from usize to u32 should not fail here (2)"),
185-
);
172+
let tx_position = TxMainChainPosition::new(block.get_id(), offset_tx);
186173

187174
TxMainChainIndex::new(
188175
SpendablePosition::from(tx_position),

common/src/chain/transaction/transaction_index/tests.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ use std::str::FromStr;
3737
fn invalid_output_count_for_transaction() {
3838
let block_id =
3939
H256::from_str("000000000000000000000000000000000000000000000000000000000000007b").unwrap();
40-
let pos = TxMainChainPosition::new(block_id.into(), 1, 2).into();
40+
let pos = TxMainChainPosition::new(block_id.into(), 1).into();
4141
let tx_index = TxMainChainIndex::new(pos, 0);
4242
assert_eq!(
4343
tx_index.unwrap_err(),
@@ -60,7 +60,7 @@ fn basic_spending() {
6060
H256::from_str("000000000000000000000000000000000000000000000000000000000000007b")
6161
.unwrap()
6262
.into();
63-
let pos = TxMainChainPosition::new(block_id, 1, 2).into();
63+
let pos = TxMainChainPosition::new(block_id, 1).into();
6464
let mut tx_index = TxMainChainIndex::new(pos, 3).unwrap();
6565

6666
// ensure index accesses are correct
@@ -311,10 +311,9 @@ fn test_indices_calculations() {
311311
SpendablePosition::BlockReward(_) => unreachable!(),
312312
};
313313
let tx_start_pos = pos.byte_offset_in_block() as usize;
314-
let tx_end_pos = pos.byte_offset_in_block() as usize + pos.serialized_size() as usize;
315-
let tx_serialized_in_block = &serialized_block[tx_start_pos..tx_end_pos];
314+
let tx_serialized_in_block = &serialized_block[tx_start_pos..];
316315
let tx_serialized = tx.encode();
317-
assert_eq!(tx_serialized_in_block, tx_serialized);
316+
assert_eq!(tx_serialized_in_block[..tx_serialized.len()], tx_serialized);
318317

319318
// to ensure Vec comparison is correct since I'm a paranoid C++ dude, let's mess things up
320319
let tx_messed = tx_serialized.iter().map(|c| c.wrapping_add(1)).collect::<Vec<u8>>();

0 commit comments

Comments
 (0)