-
Notifications
You must be signed in to change notification settings - Fork 110
feat(l1): download all headers from new to old as first step of full sync #4812
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 33 commits
57810ee
81866ce
c02da60
678d29b
67da9ca
e2a93d6
931de19
b5ce864
524055c
acca858
181bc04
26d5066
41475ab
f8c2936
e0845fe
116ec2c
c9be0e6
dda6c75
4f2eb9d
648970e
a3cc994
8733ab2
8a70cc5
90ddd87
12d7ba4
1739c43
24f4f61
119c5cf
9d8128d
339c369
88f6513
a23be6e
593e273
7b2575b
b82100b
5678b09
5a736d5
211bedd
2b9bcb2
b7acb61
21e3b13
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -47,6 +47,8 @@ pub struct StoreInner { | |
invalid_ancestors: HashMap<BlockHash, BlockHash>, | ||
// Stores current Snap State | ||
snap_state: SnapState, | ||
// Stores fetched headers during a fullsync | ||
fullsync_headers: HashMap<BlockNumber, BlockHeader>, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. if this is expected to have less than < 10000~ it might be better to use a btree too here |
||
} | ||
|
||
#[derive(Default, Debug)] | ||
|
@@ -695,6 +697,37 @@ impl StoreEngine for Store { | |
|
||
Ok(()) | ||
} | ||
|
||
async fn add_fullsync_batch(&self, headers: Vec<BlockHeader>) -> Result<(), StoreError> { | ||
self.inner()? | ||
.fullsync_headers | ||
.extend(headers.into_iter().map(|h| (h.number, h))); | ||
Ok(()) | ||
} | ||
|
||
async fn read_fullsync_batch( | ||
&self, | ||
start: BlockNumber, | ||
limit: u64, | ||
) -> Result<Vec<BlockHeader>, StoreError> { | ||
let store = self.inner()?; | ||
(start..start + limit) | ||
.map(|ref n| { | ||
store | ||
.fullsync_headers | ||
.get(n) | ||
.cloned() | ||
.ok_or(StoreError::Custom(format!( | ||
"Missing fullsync header for block {n}" | ||
))) | ||
}) | ||
.collect::<Result<Vec<_>, _>>() | ||
} | ||
|
||
async fn clear_fullsync_headers(&self) -> Result<(), StoreError> { | ||
self.inner()?.fullsync_headers.clear(); | ||
Ok(()) | ||
} | ||
} | ||
|
||
impl Debug for Store { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -104,6 +104,11 @@ const CF_PENDING_BLOCKS: &str = "pending_blocks"; | |
/// - [`Vec<u8>`] = `BlockHashRLP::from(latest_valid).bytes().clone()` | ||
const CF_INVALID_ANCESTORS: &str = "invalid_ancestors"; | ||
|
||
/// Block headers downloaded during fullsync column family: [`u8;_`] => [`Vec<u8>`] | ||
/// - [`u8;_`] = `block_number.to_le_bytes()` | ||
/// - [`Vec<u8>`] = `BlockHeaderRLP::from(block.header.clone()).bytes().clone()` | ||
const CF_FULLSYNC_HEADERS: &str = "fullsync_headers"; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this could be merged with the main headers table. I opened #4903 to check that. |
||
|
||
#[derive(Debug)] | ||
pub struct Store { | ||
db: Arc<OptimisticTransactionDB<MultiThreaded>>, | ||
|
@@ -166,6 +171,7 @@ impl Store { | |
CF_TRIE_NODES, | ||
CF_PENDING_BLOCKS, | ||
CF_INVALID_ANCESTORS, | ||
CF_FULLSYNC_HEADERS, | ||
]; | ||
|
||
// Get existing column families to know which ones to drop later | ||
|
@@ -1479,6 +1485,46 @@ impl StoreEngine for Store { | |
|
||
self.write_batch_async(batch_ops).await | ||
} | ||
|
||
async fn add_fullsync_batch(&self, headers: Vec<BlockHeader>) -> Result<(), StoreError> { | ||
let mut batch_ops = Vec::new(); | ||
|
||
for header in headers { | ||
let number_value = header.number.to_le_bytes().to_vec(); | ||
let header_value = BlockHeaderRLP::from(header).bytes().clone(); | ||
|
||
batch_ops.push((CF_FULLSYNC_HEADERS.to_string(), number_value, header_value)); | ||
} | ||
|
||
self.write_batch_async(batch_ops).await | ||
} | ||
|
||
async fn read_fullsync_batch( | ||
&self, | ||
start: BlockNumber, | ||
limit: u64, | ||
) -> Result<Vec<BlockHeader>, StoreError> { | ||
self.read_bulk_async( | ||
CF_FULLSYNC_HEADERS, | ||
(start..start + limit).map(|n| n.to_le_bytes()).collect(), | ||
|bytes| { | ||
BlockHeaderRLP::from_bytes(bytes) | ||
.to() | ||
.map_err(StoreError::from) | ||
}, | ||
) | ||
.await | ||
} | ||
|
||
async fn clear_fullsync_headers(&self) -> Result<(), StoreError> { | ||
let db = self.db.clone(); | ||
|
||
tokio::task::spawn_blocking(move || { | ||
db.drop_cf(CF_FULLSYNC_HEADERS).map_err(StoreError::from) | ||
}) | ||
.await | ||
.map_err(|e| StoreError::Custom(format!("Task panicked: {}", e)))? | ||
} | ||
} | ||
|
||
/// Open column families | ||
|
Uh oh!
There was an error while loading. Please reload this page.