pub(crate) fn create_data_payload(
    subnet_id: SubnetId,
    registry_client: &dyn RegistryClient,
    crypto: &dyn ConsensusCrypto,
    pool_reader: &PoolReader<'_>,
    ecdsa_pool: Arc<RwLock<dyn EcdsaPool>>,
    state_manager: &dyn StateManager<State = ReplicatedState>,
    context: &ValidationContext,
    parent_block: &Block,
    ecdsa_payload_metrics: &EcdsaPayloadMetrics,
    log: ReplicaLogger,
) -> Result<ecdsa::Payload, EcdsaPayloadError> {
    let height = parent_block.height().increment();
    if !ecdsa_feature_is_enabled(subnet_id, registry_client, pool_reader, height)? {
        return Ok(None);
    }
    let block_payload = &parent_block.payload.as_ref();
    if block_payload.is_summary() {
        let summary = block_payload.as_summary();
        match &summary.ecdsa {
            None => Ok(None),
            Some(ecdsa_summary) => {
                let (summary_registry_version, node_ids) =
                    get_registry_version_and_subnet_nodes_from_summary(
                        summary,
                        registry_client,
                        subnet_id,
                    )?;
                let ecdsa_config = registry_client
                    .get_ecdsa_config(subnet_id, summary_registry_version)?
                    .ok_or(EcdsaPayloadError::EcdsaConfigNotFound(
                        summary_registry_version,
                    ))?;
                let mut next_unused_transcript_id = ecdsa_summary.next_unused_transcript_id;
                let quadruples_in_creation = next_quadruples_in_creation(
                    &node_ids,
                    summary_registry_version,
                    ecdsa_summary,
                    ecdsa_config.as_ref(),
                    &mut next_unused_transcript_id,
                )?;
                let payload = ecdsa::EcdsaDataPayload {
                    signature_agreements: BTreeMap::new(),
                    ongoing_signatures: ecdsa_summary.ongoing_signatures.clone(),
                    available_quadruples: ecdsa_summary.available_quadruples.clone(),
                    quadruples_in_creation,
                    next_unused_transcript_id,
                };
                Ok(Some(payload))
            }
        }
    } else {
        match &block_payload.as_data().ecdsa {
            None => Ok(None),
            Some(prev_payload) => {
                let summary_block =
                    pool_reader
                        .dkg_summary_block(parent_block)
                        .unwrap_or_else(|| {
                            panic!(
                                "Impossible: fail to the summary block that governs height {}",
                                parent_block.height()
                            )
                        });
                let summary = summary_block.payload.as_ref().as_summary();
                let ecdsa_summary = summary.ecdsa.as_ref().unwrap_or_else(|| {
                    panic!("ecdsa payload exists but previous summary is not found")
                });
                let (summary_registry_version, node_ids) =
                    get_registry_version_and_subnet_nodes_from_summary(
                        summary,
                        registry_client,
                        subnet_id,
                    )?;
                let mut payload = prev_payload.clone();
                update_signature_agreements(
                    pool_reader.as_cache(),
                    ecdsa_pool.clone(),
                    crypto,
                    &mut payload,
                    ecdsa_payload_metrics,
                    log.clone(),
                );
                let count = update_signing_requests(
                    state_manager,
                    context,
                    &mut payload,
                    &ecdsa_summary.current_ecdsa_transcript,
                    log.clone(),
                )?;
                // quadruples are consumed, need to produce more
                let next_available_quadruple_id = payload
                    .available_quadruples
                    .keys()
                    .last()
                    .cloned()
                    .map(|x| x.increment())
                    .unwrap_or_default();
                start_making_new_quadruples(
                    count,
                    &node_ids,
                    summary_registry_version,
                    &mut payload.next_unused_transcript_id,
                    &mut payload.quadruples_in_creation,
                    next_available_quadruple_id,
                )?;
                let mut completed_transcripts = BTreeMap::new();
                let transcript_builder = EcdsaTranscriptBuilderImpl::new(
                    pool_reader.as_cache(),
                    crypto,
                    ecdsa_payload_metrics,
                    log.clone(),
                );
                let ecdsa_pool = ecdsa_pool.read().unwrap();
                for transcript in transcript_builder
                    .get_completed_transcripts(ecdsa_pool.deref())
                    .into_iter()
                {
                    completed_transcripts.insert(transcript.transcript_id, transcript);
                }
                update_quadruples_in_creation(None, &mut payload, &mut completed_transcripts, log)?;
                ecdsa_payload_metrics.payload_metrics_set(
                    "available_quadruples",
                    payload.available_quadruples.len() as i64,
                );
                ecdsa_payload_metrics.payload_metrics_set(
                    "ongoing_signatures",
                    payload.ongoing_signatures.len() as i64,
                );
                ecdsa_payload_metrics.payload_metrics_set(
                    "quaruples_in_creation",
                    payload.quadruples_in_creation.len() as i64,
                );
                Ok(Some(payload))
            }
        }
    }
}
/// Creates a threshold ECDSA batch payload.
Creates a threshold ECDSA batch payload.
Creates a threshold ECDSA batch payload .
pub(crate) fn create_data_payload(
    subnet_id: SubnetId,
    registry_client: &dyn RegistryClient,
    crypto: &dyn ConsensusCrypto,
    pool_reader: &PoolReader<'_>,
    ecdsa_pool: Arc<RwLock<dyn EcdsaPool>>,
    state_manager: &dyn StateManager<State = ReplicatedState>,
    context: &ValidationContext,
    parent_block: &Block,
    ecdsa_payload_metrics: &EcdsaPayloadMetrics,
    log: ReplicaLogger,
) -> Result<ecdsa::Payload, EcdsaPayloadError> {
    let height = parent_block.height().increment();
    if !ecdsa_feature_is_enabled(subnet_id, registry_client, pool_reader, height)? {
        return Ok(None);
    }
    let block_payload = &parent_block.payload.as_ref();
    if block_payload.is_summary() {
        let summary = block_payload.as_summary();
        match &summary.ecdsa {
            None => Ok(None),
            Some(ecdsa_summary) => {
                let (summary_registry_version, node_ids) =
                    get_registry_version_and_subnet_nodes_from_summary(
                        summary,
                        registry_client,
                        subnet_id,
                    )?;
                let ecdsa_config = registry_client
                    .get_ecdsa_config(subnet_id, summary_registry_version)?
                    .ok_or(EcdsaPayloadError::EcdsaConfigNotFound(
                        summary_registry_version,
                    ))?;
                let mut next_unused_transcript_id = ecdsa_summary.next_unused_transcript_id;
                let quadruples_in_creation = next_quadruples_in_creation(
                    &node_ids,
                    summary_registry_version,
                    ecdsa_summary,
                    ecdsa_config.as_ref(),
                    &mut next_unused_transcript_id,
                )?;
                let payload = ecdsa::EcdsaDataPayload {
                    signature_agreements: BTreeMap::new(),
                    ongoing_signatures: ecdsa_summary.ongoing_signatures.clone(),
                    available_quadruples: ecdsa_summary.available_quadruples.clone(),
                    quadruples_in_creation,
                    next_unused_transcript_id,
                };
                Ok(Some(payload))
            }
        }
    } else {
        match &block_payload.as_data().ecdsa {
            None => Ok(None),
            Some(prev_payload) => {
                let summary_block =
                    pool_reader
                        .dkg_summary_block(parent_block)
                        .unwrap_or_else(|| {
                            panic!(
                                "Impossible: fail to the summary block that governs height {}",
                                parent_block.height()
                            )
                        });
                let summary = summary_block.payload.as_ref().as_summary();
                let ecdsa_summary = summary.ecdsa.as_ref().unwrap_or_else(|| {
                    panic!("ecdsa payload exists but previous summary is not found")
                });
                let (summary_registry_version, node_ids) =
                    get_registry_version_and_subnet_nodes_from_summary(
                        summary,
                        registry_client,
                        subnet_id,
                    )?;
                let mut payload = prev_payload.clone();
                update_signature_agreements(
                    pool_reader.as_cache(),
                    ecdsa_pool.clone(),
                    crypto,
                    &mut payload,
                    ecdsa_payload_metrics,
                    log.clone(),
                );
                let count = update_signing_requests(
                    state_manager,
                    context,
                    &mut payload,
                    &ecdsa_summary.current_ecdsa_transcript,
                    log.clone(),
                )?;
                // quadruples are consumed, need to produce more
                let next_available_quadruple_id = payload
                    .available_quadruples
                    .keys()
                    .last()
                    .cloned()
                    .map(|x| x.increment())
                    .unwrap_or_default();
                start_making_new_quadruples(
                    count,
                    &node_ids,
                    summary_registry_version,
                    &mut payload.next_unused_transcript_id,
                    &mut payload.quadruples_in_creation,
                    next_available_quadruple_id,
                )?;
                let mut completed_transcripts = BTreeMap::new();
                let transcript_builder = EcdsaTranscriptBuilderImpl::new(
                    pool_reader.as_cache(),
                    crypto,
                    ecdsa_payload_metrics,
                    log.clone(),
                );
                let ecdsa_pool = ecdsa_pool.read().unwrap();
                for transcript in transcript_builder
                    .get_completed_transcripts(ecdsa_pool.deref())
                    .into_iter()
                {
                    completed_transcripts.insert(transcript.transcript_id, transcript);
                }
                update_quadruples_in_creation(None, &mut payload, &mut completed_transcripts, log)?;
                ecdsa_payload_metrics.payload_metrics_set(
                    "available_quadruples",
                    payload.available_quadruples.len() as i64,
                );
                ecdsa_payload_metrics.payload_metrics_set(
                    "ongoing_signatures",
                    payload.ongoing_signatures.len() as i64,
                );
                ecdsa_payload_metrics.payload_metrics_set(
                    "quaruples_in_creation",
                    payload.quadruples_in_creation.len() as i64,
                );
                Ok(Some(payload))
            }
        }
    }
}
pub ( crate ) fn create_data_payload ( subnet_id : SubnetId , registry_client : & dyn RegistryClient , crypto : & dyn ConsensusCrypto , pool_reader : & PoolReader < ' _ > , ecdsa_pool : Arc < RwLock < dyn EcdsaPool > > , state_manager : & dyn StateManager < State = ReplicatedState > , context : & ValidationContext , parent_block : & Block , ecdsa_payload_metrics : & EcdsaPayloadMetrics , log : ReplicaLogger , ) -> Result < ecdsa :: Payload , EcdsaPayloadError > { let height = parent_block . height ( ) . increment ( ) ; if ! ecdsa_feature_is_enabled ( subnet_id , registry_client , pool_reader , height ) ? { return Ok ( None ) ; } let block_payload = & parent_block . payload . as_ref ( ) ; if block_payload . is_summary ( ) { let summary = block_payload . as_summary ( ) ; match & summary . ecdsa { None => Ok ( None ) , Some ( ecdsa_summary ) => { let ( summary_registry_version , node_ids ) = get_registry_version_and_subnet_nodes_from_summary ( summary , registry_client , subnet_id , ) ? ; let ecdsa_config = registry_client . get_ecdsa_config ( subnet_id , summary_registry_version ) ? . ok_or ( EcdsaPayloadError :: EcdsaConfigNotFound ( summary_registry_version , ) ) ? ; let mut next_unused_transcript_id = ecdsa_summary . next_unused_transcript_id ; let quadruples_in_creation = next_quadruples_in_creation ( & node_ids , summary_registry_version , ecdsa_summary , ecdsa_config . as_ref ( ) , & mut next_unused_transcript_id , ) ? ; let payload = ecdsa :: EcdsaDataPayload { signature_agreements : BTreeMap :: new ( ) , ongoing_signatures : ecdsa_summary . ongoing_signatures . clone ( ) , available_quadruples : ecdsa_summary . available_quadruples . clone ( ) , quadruples_in_creation , next_unused_transcript_id , } ; Ok ( Some ( payload ) ) } } } else { match & block_payload . as_data ( ) . ecdsa { None => Ok ( None ) , Some ( prev_payload ) => { let summary_block = pool_reader . dkg_summary_block ( parent_block ) . unwrap_or_else ( | | { panic ! ( " " parent_block height ( ) ) } ) ; let summary = summary_block . payload . as_ref ( ) . as_summary ( ) ; let ecdsa_summary = summary . ecdsa . as_ref ( ) . unwrap_or_else ( | | { panic ! ( " " ) } ) ; let ( summary_registry_version , node_ids ) = get_registry_version_and_subnet_nodes_from_summary ( summary , registry_client , subnet_id , ) ? ; let mut payload = prev_payload . clone ( ) ; update_signature_agreements ( pool_reader . as_cache ( ) , ecdsa_pool . clone ( ) , crypto , & mut payload , ecdsa_payload_metrics , log . clone ( ) , ) ; let count = update_signing_requests ( state_manager , context , & mut payload , & ecdsa_summary . current_ecdsa_transcript , log . clone ( ) , ) ? ; // quadruples are consumed, need to produce more let next_available_quadruple_id = payload . available_quadruples . keys ( ) . last ( ) . cloned ( ) . map ( | x | x . increment ( ) ) . unwrap_or_default ( ) ; start_making_new_quadruples ( count , & node_ids , summary_registry_version , & mut payload . next_unused_transcript_id , & mut payload . quadruples_in_creation , next_available_quadruple_id , ) ? ; let mut completed_transcripts = BTreeMap :: new ( ) ; let transcript_builder = EcdsaTranscriptBuilderImpl :: new ( pool_reader . as_cache ( ) , crypto , ecdsa_payload_metrics , log . clone ( ) , ) ; let ecdsa_pool = ecdsa_pool . read ( ) . unwrap ( ) ; for transcript in transcript_builder . get_completed_transcripts ( ecdsa_pool . deref ( ) ) . into_iter ( ) { completed_transcripts . insert ( transcript . transcript_id , transcript ) ; } update_quadruples_in_creation ( None , & mut payload , & mut completed_transcripts , log ) ? ; ecdsa_payload_metrics . payload_metrics_set ( " " , payload . available_quadruples . len ( ) as i64 , ) ; ecdsa_payload_metrics . payload_metrics_set ( " " , payload . ongoing_signatures . len ( ) as i64 , ) ; ecdsa_payload_metrics . payload_metrics_set ( " " , payload . quadruples_in_creation . len ( ) as i64 , ) ; Ok ( Some ( payload ) ) } } } }
Creates a threshold ECDSA batch payload.
Creates a threshold ECDSA batch payload .
subnet_id
SubnetId
registry_client
&dyn RegistryClient
crypto
&dyn ConsensusCrypto
pool_reader
&PoolReader<'_>
ecdsa_pool
Arc<RwLock<dyn EcdsaPool>>
state_manager
&dyn StateManager<State = ReplicatedState>
context
&ValidationContext
parent_block
&Block
ecdsa_payload_metrics
&EcdsaPayloadMetrics
log
ReplicaLogger
subnet_id
SubnetId
registry_client
&dyn RegistryClient
crypto
&dyn ConsensusCrypto
pool_reader
&PoolReader<'_>
ecdsa_pool
Arc<RwLock<dyn EcdsaPool>>
state_manager
&dyn StateManager<State = ReplicatedState>
context
&ValidationContext
parent_block
&Block
ecdsa_payload_metrics
&EcdsaPayloadMetrics
log
ReplicaLogger