pub fn prover_run_for_next_commit(
        &self,
        worker_: &str,
        prover_timeout: time::Duration,
        block_size: usize,
    ) -> QueryResult<Option<ProverRun>> {
        // Select the block to prove.
        self
            .0
            .conn()
            .transaction(|| {
                sql_query("LOCK TABLE prover_runs IN EXCLUSIVE MODE").execute(self.0.conn())?;

                // Find the block that satisfies the following criteria:
                // - Block number is greater than the index of last verified block.
                // - There is no proof for block.
                // - Either there is no ongoing job for the block, or the job exceeded the timeout.
                let query = format!(" \
                    WITH unsized_blocks AS ( \
                        SELECT * FROM operations o \
                        WHERE action_type = 'COMMIT' \
                            AND block_number > \
                                (SELECT COALESCE(max(block_number),0) FROM operations WHERE action_type = 'VERIFY') \
                            AND NOT EXISTS \
                                (SELECT * FROM proofs WHERE block_number = o.block_number) \
                            AND NOT EXISTS \
                                (SELECT * FROM prover_runs \
                                    WHERE block_number = o.block_number AND (now() - updated_at) < interval '{prover_timeout_secs} seconds') \
                    ) \
                    SELECT min(block_number) AS integer_value FROM unsized_blocks \
                    INNER JOIN blocks \
                        ON unsized_blocks.block_number = blocks.number AND blocks.block_size = {block_size} \
                    ",
                    prover_timeout_secs=prover_timeout.as_secs(), block_size=block_size
                );

                // Return the index of such a block.
                let job = diesel::sql_query(query).get_result::<Option<IntegerNumber>>(self.0.conn())?
                .map(|i| i.integer_value as BlockNumber);

                // If there is a block to prove, create a job and store it
                // in the `prover_runs` table; otherwise do nothing and return `None`.
                if let Some(block_number_) = job {
                    use crate::schema::prover_runs::dsl::*;
                    let inserted: ProverRun = insert_into(prover_runs)
                        .values(&vec![(
                            block_number.eq(i64::from(block_number_)),
                            worker.eq(worker_.to_string()),
                        )])
                        .get_result(self.0.conn())?;
                    Ok(Some(inserted))
                } else {
                    Ok(None)
                }
            })
    }
/// Given the block size, chooses the next block to prove for the certain prover.
/// Returns `None` if either there are no blocks of given size to prove, or
/// there is already an ongoing job for non-proved block.
Given the block size, chooses the next block to prove for the certain prover.
Returns `None` if either there are no blocks of given size to prove, or
there is already an ongoing job for non-proved block.
Given the block size chooses the next block to prove for the certain prover . Returns ` None ` if either there are no blocks of given size to prove or there is already an ongoing job for non - proved block .
pub fn prover_run_for_next_commit(
        &self,
        worker_: &str,
        prover_timeout: time::Duration,
        block_size: usize,
    ) -> QueryResult<Option<ProverRun>> {
        // Select the block to prove.
        self
            .0
            .conn()
            .transaction(|| {
                sql_query("LOCK TABLE prover_runs IN EXCLUSIVE MODE").execute(self.0.conn())?;
                // Find the block that satisfies the following criteria:
                // - Block number is greater than the index of last verified block.
                // - There is no proof for block.
                // - Either there is no ongoing job for the block, or the job exceeded the timeout.
                let query = format!(" \
                    WITH unsized_blocks AS ( \
                        SELECT * FROM operations o \
                        WHERE action_type = 'COMMIT' \
                            AND block_number > \
                                (SELECT COALESCE(max(block_number),0) FROM operations WHERE action_type = 'VERIFY') \
                            AND NOT EXISTS \
                                (SELECT * FROM proofs WHERE block_number = o.block_number) \
                            AND NOT EXISTS \
                                (SELECT * FROM prover_runs \
                                    WHERE block_number = o.block_number AND (now() - updated_at) < interval '{prover_timeout_secs} seconds') \
                    ) \
                    SELECT min(block_number) AS integer_value FROM unsized_blocks \
                    INNER JOIN blocks \
                        ON unsized_blocks.block_number = blocks.number AND blocks.block_size = {block_size} \
                    ",
                    prover_timeout_secs=prover_timeout.as_secs(), block_size=block_size
                );
                // Return the index of such a block.
                let job = diesel::sql_query(query).get_result::<Option<IntegerNumber>>(self.0.conn())?
                .map(|i| i.integer_value as BlockNumber);
                // If there is a block to prove, create a job and store it
                // in the `prover_runs` table; otherwise do nothing and return `None`.
                if let Some(block_number_) = job {
                    use crate::schema::prover_runs::dsl::*;
                    let inserted: ProverRun = insert_into(prover_runs)
                        .values(&vec![(
                            block_number.eq(i64::from(block_number_)),
                            worker.eq(worker_.to_string()),
                        )])
                        .get_result(self.0.conn())?;
                    Ok(Some(inserted))
                } else {
                    Ok(None)
                }
            })
    }
pub fn prover_run_for_next_commit ( & self , worker_ : & str , prover_timeout : time :: Duration , block_size : usize , ) -> QueryResult < Option < ProverRun > > { // Select the block to prove. self . 0 . conn ( ) . transaction ( | | { sql_query ( " " ) . execute ( self . 0 . conn ( ) ) ? ; // Find the block that satisfies the following criteria: // - Block number is greater than the index of last verified block. // - There is no proof for block. // - Either there is no ongoing job for the block, or the job exceeded the timeout. let query = format ! ( " \
 \
 \
 \
 \
 \
 \
 \
 \
 \
 \
 \
 \
 \
 \
 " prover_timeout_secs prover_timeout as_secs ( ) block_size block_size ) ; // Return the index of such a block. let job = diesel :: sql_query ( query ) . get_result :: < Option < IntegerNumber > > ( self . 0 . conn ( ) ) ? . map ( | i | i . integer_value as BlockNumber ) ; // If there is a block to prove, create a job and store it // in the `prover_runs` table; otherwise do nothing and return `None`. if let Some ( block_number_ ) = job { use crate :: schema :: prover_runs :: dsl :: * ; let inserted : ProverRun = insert_into ( prover_runs ) . values ( & vec ! [ ( block_number eq ( i64 from ( block_number_ ) ) worker eq ( worker_ to_string ( ) ) ) ] ) . get_result ( self . 0 . conn ( ) ) ? ; Ok ( Some ( inserted ) ) } else { Ok ( None ) } } ) }
Given the block size, chooses the next block to prove for the certain prover.
Given the block size chooses the next block to prove for the certain prover .
&self
worker_
&str
prover_timeout
time::Duration
block_size
usize