Skip to content

Commit e1ae848

Browse files
committed
add function to fully add an alt block
1 parent d648871 commit e1ae848

File tree

4 files changed

+124
-7
lines changed

4 files changed

+124
-7
lines changed
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
use bytemuck::TransparentWrapper;
2+
3+
use cuprate_database::{DatabaseRw, RuntimeError, StorableVec, DatabaseRo};
4+
use cuprate_helper::map::split_u128_into_low_high_bits;
5+
use cuprate_types::{AltBlockInformation, Chain, VerifiedTransactionInformation};
6+
7+
use crate::{
8+
tables::TablesMut,
9+
types::{AltBlockHeight, AltChainInfo, AltTransactionInfo, BlockHash, CompactAltBlockInfo},
10+
};
11+
12+
pub fn add_alt_block(
13+
alt_block: &AltBlockInformation,
14+
tables: &mut impl TablesMut,
15+
) -> Result<(), RuntimeError> {
16+
let alt_block_height = AltBlockHeight {
17+
chain_id: alt_block.chain_id.into(),
18+
height: alt_block.height,
19+
};
20+
21+
tables
22+
.alt_block_heights_mut()
23+
.put(&alt_block.block_hash, &alt_block_height)?;
24+
25+
check_add_alt_chain_info(&alt_block_height, &alt_block.block.header.previous, tables)?;
26+
27+
let (cumulative_difficulty_low, cumulative_difficulty_high) =
28+
split_u128_into_low_high_bits(alt_block.cumulative_difficulty);
29+
30+
let alt_block_info = CompactAltBlockInfo {
31+
block_hash: alt_block.block_hash,
32+
pow_hash: alt_block.pow_hash,
33+
height: alt_block.height,
34+
weight: alt_block.weight,
35+
long_term_weight: alt_block.long_term_weight,
36+
cumulative_difficulty_low,
37+
cumulative_difficulty_high,
38+
};
39+
40+
tables
41+
.alt_blocks_info_mut()
42+
.put(&alt_block_height, &alt_block_info)?;
43+
44+
tables.alt_block_blobs_mut().put(
45+
&alt_block_height,
46+
StorableVec::wrap_ref(&alt_block.block_blob),
47+
)?;
48+
49+
for tx in &alt_block.txs {
50+
add_alt_transaction(&tx, tables)?;
51+
}
52+
53+
Ok(())
54+
}
55+
56+
pub fn add_alt_transaction(
57+
tx: &VerifiedTransactionInformation,
58+
tables: &mut impl TablesMut,
59+
) -> Result<(), RuntimeError> {
60+
if tables.tx_ids().get(&tx.tx_hash).is_ok()
61+
|| tables.alt_transaction_infos().get(&tx.tx_hash).is_ok()
62+
{
63+
return Ok(());
64+
}
65+
66+
tables.alt_transaction_infos_mut().put(
67+
&tx.tx_hash,
68+
&AltTransactionInfo {
69+
tx_weight: tx.tx_weight,
70+
fee: tx.fee,
71+
tx_hash: tx.tx_hash,
72+
},
73+
)?;
74+
75+
tables
76+
.alt_transaction_blobs_mut()
77+
.put(&tx.tx_hash, StorableVec::wrap_ref(&tx.tx_blob))
78+
}
79+
80+
pub fn check_add_alt_chain_info(
81+
alt_block_height: &AltBlockHeight,
82+
prev_hash: &BlockHash,
83+
tables: &mut impl TablesMut,
84+
) -> Result<(), RuntimeError> {
85+
match tables.alt_chain_infos().get(&alt_block_height.chain_id) {
86+
Ok(_) => return Ok(()),
87+
Err(RuntimeError::KeyNotFound) => (),
88+
Err(e) => return Err(e),
89+
}
90+
91+
let parent_chain = match tables.alt_block_heights().get(prev_hash) {
92+
Ok(alt_parent_height) => Chain::Alt(alt_parent_height.chain_id.into()),
93+
Err(RuntimeError::KeyNotFound) => Chain::Main,
94+
Err(e) => return Err(e),
95+
};
96+
97+
tables.alt_chain_infos_mut().put(
98+
&alt_block_height.chain_id,
99+
&AltChainInfo {
100+
parent_chain: parent_chain.into(),
101+
common_ancestor_height: alt_block_height.height - 1,
102+
},
103+
)
104+
}

storage/blockchain/src/ops/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,5 +108,6 @@ pub mod key_image;
108108
pub mod output;
109109
pub mod property;
110110
pub mod tx;
111+
pub mod alt_block;
111112

112113
mod macros;

storage/blockchain/src/tables.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,12 @@
1616
//! accessing _all_ tables defined here at once.
1717
1818
//---------------------------------------------------------------------------------------------------- Import
19-
use crate::types::{Amount, AmountIndex, AmountIndices, BlockBlob, BlockHash, BlockHeight, BlockInfo, KeyImage, Output, PreRctOutputId, PrunableBlob, PrunableHash, PrunedBlob, RctOutput, TxBlob, TxHash, TxId, UnlockTime, RawChainId, AltChainInfo, AltBlockHeight, CompactAltBlockInfo, AltTransactionInfo};
19+
use crate::types::{
20+
AltBlockHeight, AltChainInfo, AltTransactionInfo, Amount, AmountIndex, AmountIndices,
21+
BlockBlob, BlockHash, BlockHeight, BlockInfo, CompactAltBlockInfo, KeyImage, Output,
22+
PreRctOutputId, PrunableBlob, PrunableHash, PrunedBlob, RawChainId, RctOutput, TxBlob, TxHash,
23+
TxId, UnlockTime,
24+
};
2025

2126
//---------------------------------------------------------------------------------------------------- Tables
2227
// Notes:

storage/blockchain/src/types.rs

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -348,6 +348,14 @@ impl From<RawChain> for Chain {
348348
}
349349
}
350350

351+
impl From<RawChainId> for RawChain {
352+
fn from(value: RawChainId) -> Self {
353+
assert_ne!(value.0, 0);
354+
355+
RawChain(value.0)
356+
}
357+
}
358+
351359
//---------------------------------------------------------------------------------------------------- RawChainId
352360
#[derive(Copy, Clone, Debug, PartialEq, PartialOrd, Eq, Ord, Hash, Pod, Zeroable)]
353361
#[repr(transparent)]
@@ -371,16 +379,16 @@ impl Key for RawChainId {}
371379
#[derive(Copy, Clone, Debug, PartialEq, PartialOrd, Eq, Ord, Hash, Pod, Zeroable)]
372380
#[repr(C)]
373381
pub struct AltChainInfo {
374-
parent_chain: RawChain,
375-
common_ancestor_height: u64
382+
pub parent_chain: RawChain,
383+
pub common_ancestor_height: usize,
376384
}
377385

378386
//---------------------------------------------------------------------------------------------------- AltBlockHeight
379387
#[derive(Copy, Clone, Debug, PartialEq, PartialOrd, Eq, Ord, Hash, Pod, Zeroable)]
380388
#[repr(C)]
381389
pub struct AltBlockHeight {
382-
chain_id: u64,
383-
height: u64,
390+
pub chain_id: RawChainId,
391+
pub height: usize,
384392
}
385393

386394
impl Key for AltBlockHeight {}
@@ -396,15 +404,14 @@ pub struct CompactAltBlockInfo {
396404
/// The block's proof-of-work hash.
397405
pub pow_hash: [u8; 32],
398406
/// The block's height.
399-
pub height: u64,
407+
pub height: usize,
400408
/// The adjusted block size, in bytes.
401409
pub weight: usize,
402410
/// The long term block weight, which is the weight factored in with previous block weights.
403411
pub long_term_weight: usize,
404412
/// The cumulative difficulty of all blocks up until and including this block.
405413
pub cumulative_difficulty_low: u64,
406414
pub cumulative_difficulty_high: u64,
407-
408415
}
409416

410417
//---------------------------------------------------------------------------------------------------- AltTransactionInfo

0 commit comments

Comments
 (0)