Skip to content
This repository has been archived by the owner on Oct 31, 2024. It is now read-only.

Commit

Permalink
feat: add start block (#318)
Browse files Browse the repository at this point in the history
  • Loading branch information
atanmarko authored Oct 3, 2023
1 parent 21fdd0e commit 4ce1a60
Show file tree
Hide file tree
Showing 10 changed files with 219 additions and 83 deletions.
8 changes: 7 additions & 1 deletion crates/topos-sequencer-subnet-runtime/src/certification.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ pub struct Certification {
pub verifier: u32,
/// Key for signing certificates, currently secp256k1
signing_key: Vec<u8>,
/// Optional synchronization from particular block number
pub start_block: Option<u64>,
}

impl Debug for Certification {
Expand All @@ -33,13 +35,15 @@ impl Certification {
source_head_certificate_id: Option<CertificateId>,
verifier: u32,
signing_key: Vec<u8>,
start_block: Option<u64>,
) -> Result<Arc<Mutex<Certification>>, crate::Error> {
Ok(Arc::new(Mutex::from(Self {
last_certificate_id: source_head_certificate_id,
finalized_blocks: LinkedList::<BlockInfo>::new(),
subnet_id: *subnet_id,
verifier,
signing_key,
start_block,
})))
}

Expand Down Expand Up @@ -97,10 +101,12 @@ impl Certification {
let is_genesis_certificate: bool = self
.finalized_blocks
.front()
.map(|b| b.number == 0)
.map(|b| b.number == 0 || self.start_block.is_some())
.unwrap_or(false);
let last_known_certificate_id = if is_genesis_certificate {
// We are creating genesis certificate, there were no previous certificates
// In case where start block is present, we also consider start block as genesis certificate,
// so it has no history (prev cert id all 0)
CertificateId::default()
} else {
self.last_certificate_id
Expand Down
1 change: 1 addition & 0 deletions crates/topos-sequencer-subnet-runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ pub struct SubnetRuntimeProxyConfig {
pub subnet_contract_address: String,
pub source_head_certificate_id: Option<CertificateId>,
pub verifier: u32,
pub start_block: Option<u64>,
}

/// Thread safe client to the protocol aggregate
Expand Down
18 changes: 15 additions & 3 deletions crates/topos-sequencer-subnet-runtime/src/proxy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ impl SubnetRuntimeProxy {
None,
config.verifier,
signing_key.clone(),
config.start_block,
)?;

let runtime_proxy = Arc::new(Mutex::from(Self {
Expand All @@ -104,7 +105,14 @@ impl SubnetRuntimeProxy {
let runtime_proxy = runtime_proxy.clone();
let subnet_contract_address = subnet_contract_address.clone();
tokio::spawn(async move {
let mut latest_acquired_subnet_block_number: i128 = -1;
// If the `start_block` sequencer parameter is provided, first block retrieved from blockchain (for genesis certificate)
// will be `start_block`. `default_block_sync_start` is hence `start_block`-1
// as first block retrieved from subnet node is `latest_acquired_subnet_block_number` + 1
let default_block_sync_start: i128 = config
.start_block
.map(|block_number| (block_number - 1) as i128)
.unwrap_or(-1);
let mut latest_acquired_subnet_block_number: i128 = default_block_sync_start;

{
// To start producing certificates, we need to know latest delivered or pending certificate id from TCE
Expand All @@ -122,11 +130,15 @@ impl SubnetRuntimeProxy {
"Source head certificate id received {:?}",
certificate_and_position
);
// If the position is not provided, it should start form -1, so that first fetched is subnet genesis block
// If tce source head position is provided, continue synchronizing from it
// If the `start_block` sequencer parameter is provided and tce source head is missing,
// we should start synchronizing from that block instead of genesis
// If neither tce source head position nor start_block parameters are provided,
// sync should start form -1, so that first fetched is subnet genesis block
let cert_id = certificate_and_position.map(|(id, _position)| id);
let position: i128 = certificate_and_position
.map(|(_id, position)| position as i128)
.unwrap_or(-1);
.unwrap_or(default_block_sync_start);
// Certificate generation is now ready to run
certification.last_certificate_id = cert_id;
latest_acquired_subnet_block_number = position;
Expand Down
Loading

0 comments on commit 4ce1a60

Please sign in to comment.