pub fn produce_blocks(
    eid: sgx_enclave_id_t,
    api: &Api<sr25519::Pair>,
    last_synced_head: Header,
) -> Header {
    // obtain latest finalized block from layer one
    debug!("Getting current head");
    let curr_head: SignedBlock = api
        .get_finalized_head()
        .unwrap()
        .map(|hash| api.get_signed_block(Some(hash)).unwrap())
        .unwrap()
        .unwrap();

    let mut blocks_to_sync = Vec::<SignedBlock>::new();

    // add blocks to sync if not already up to date
    if curr_head.block.header.hash() != last_synced_head.hash() {
        blocks_to_sync.push(curr_head.clone());

        // Todo: Check, is this dangerous such that it could be an eternal or too big loop?
        let mut head = curr_head.clone();
        let no_blocks_to_sync = head.block.header.number - last_synced_head.number;
        if no_blocks_to_sync > 1 {
            println!(
                "Chain Relay is synced until block: {:?}",
                last_synced_head.number
            );
            println!(
                "Last finalized block number: {:?}\n",
                head.block.header.number
            );
        }
        while head.block.header.parent_hash != last_synced_head.hash() {
            debug!("Getting head of hash: {:?}", head.block.header.parent_hash);
            head = api
                .get_signed_block(Some(head.block.header.parent_hash))
                .unwrap()
                .unwrap();
            blocks_to_sync.push(head.clone());

            if head.block.header.number % BLOCK_SYNC_BATCH_SIZE == 0 {
                println!(
                    "Remaining blocks to fetch until last synced header: {:?}",
                    head.block.header.number - last_synced_head.number
                )
            }
        }
        blocks_to_sync.reverse();
    }

    let tee_accountid = enclave_account(eid);

    // only feed BLOCK_SYNC_BATCH_SIZE blocks at a time into the enclave to save enclave state regularly
    let mut i = if curr_head.block.header.hash() == last_synced_head.hash() {
        curr_head.block.header.number as usize
    } else {
        blocks_to_sync[0].block.header.number as usize
    };
    for chunk in blocks_to_sync.chunks(BLOCK_SYNC_BATCH_SIZE as usize) {
        let tee_nonce = get_nonce(&api, &tee_accountid);
        // Produce blocks
        if let Err(e) = enclave_produce_blocks(eid, chunk.to_vec(), tee_nonce) {
            error!("{}", e);
            // enclave might not have synced
            return last_synced_head;
        };
        i += chunk.len();
        println!(
            "Synced {} blocks out of {} finalized blocks",
            i,
            blocks_to_sync[0].block.header.number as usize + blocks_to_sync.len()
        )
    }

    curr_head.block.header
}
/// Starts block production
///
/// Returns the last synced header of layer one
Starts block production
Returns the last synced header of layer one
Starts block production Returns the last synced header of layer one
pub fn produce_blocks(
    eid: sgx_enclave_id_t,
    api: &Api<sr25519::Pair>,
    last_synced_head: Header,
) -> Header {
    // obtain latest finalized block from layer one
    debug!("Getting current head");
    let curr_head: SignedBlock = api
        .get_finalized_head()
        .unwrap()
        .map(|hash| api.get_signed_block(Some(hash)).unwrap())
        .unwrap()
        .unwrap();
    let mut blocks_to_sync = Vec::<SignedBlock>::new();
    // add blocks to sync if not already up to date
    if curr_head.block.header.hash() != last_synced_head.hash() {
        blocks_to_sync.push(curr_head.clone());
        // Todo: Check, is this dangerous such that it could be an eternal or too big loop?
        let mut head = curr_head.clone();
        let no_blocks_to_sync = head.block.header.number - last_synced_head.number;
        if no_blocks_to_sync > 1 {
            println!(
                "Chain Relay is synced until block: {:?}",
                last_synced_head.number
            );
            println!(
                "Last finalized block number: {:?}\n",
                head.block.header.number
            );
        }
        while head.block.header.parent_hash != last_synced_head.hash() {
            debug!("Getting head of hash: {:?}", head.block.header.parent_hash);
            head = api
                .get_signed_block(Some(head.block.header.parent_hash))
                .unwrap()
                .unwrap();
            blocks_to_sync.push(head.clone());
            if head.block.header.number % BLOCK_SYNC_BATCH_SIZE == 0 {
                println!(
                    "Remaining blocks to fetch until last synced header: {:?}",
                    head.block.header.number - last_synced_head.number
                )
            }
        }
        blocks_to_sync.reverse();
    }
    let tee_accountid = enclave_account(eid);
    // only feed BLOCK_SYNC_BATCH_SIZE blocks at a time into the enclave to save enclave state regularly
    let mut i = if curr_head.block.header.hash() == last_synced_head.hash() {
        curr_head.block.header.number as usize
    } else {
        blocks_to_sync[0].block.header.number as usize
    };
    for chunk in blocks_to_sync.chunks(BLOCK_SYNC_BATCH_SIZE as usize) {
        let tee_nonce = get_nonce(&api, &tee_accountid);
        // Produce blocks
        if let Err(e) = enclave_produce_blocks(eid, chunk.to_vec(), tee_nonce) {
            error!("{}", e);
            // enclave might not have synced
            return last_synced_head;
        };
        i += chunk.len();
        println!(
            "Synced {} blocks out of {} finalized blocks",
            i,
            blocks_to_sync[0].block.header.number as usize + blocks_to_sync.len()
        )
    }
    curr_head.block.header
}
pub fn produce_blocks ( eid : sgx_enclave_id_t , api : & Api < sr25519 :: Pair > , last_synced_head : Header , ) -> Header { // obtain latest finalized block from layer one debug ! ( " " ) ; let curr_head : SignedBlock = api . get_finalized_head ( ) . unwrap ( ) . map ( | hash | api . get_signed_block ( Some ( hash ) ) . unwrap ( ) ) . unwrap ( ) . unwrap ( ) ; let mut blocks_to_sync = Vec :: < SignedBlock > :: new ( ) ; // add blocks to sync if not already up to date if curr_head . block . header . hash ( ) != last_synced_head . hash ( ) { blocks_to_sync . push ( curr_head . clone ( ) ) ; // Todo: Check, is this dangerous such that it could be an eternal or too big loop? let mut head = curr_head . clone ( ) ; let no_blocks_to_sync = head . block . header . number - last_synced_head . number ; if no_blocks_to_sync > 1 { println ! ( " " last_synced_head number ) ; println ! ( " \n " head block header number ) ; } while head . block . header . parent_hash != last_synced_head . hash ( ) { debug ! ( " " head block header parent_hash ) ; head = api . get_signed_block ( Some ( head . block . header . parent_hash ) ) . unwrap ( ) . unwrap ( ) ; blocks_to_sync . push ( head . clone ( ) ) ; if head . block . header . number % BLOCK_SYNC_BATCH_SIZE == 0 { println ! ( " " head block header number last_synced_head number ) } } blocks_to_sync . reverse ( ) ; } let tee_accountid = enclave_account ( eid ) ; // only feed BLOCK_SYNC_BATCH_SIZE blocks at a time into the enclave to save enclave state regularly let mut i = if curr_head . block . header . hash ( ) == last_synced_head . hash ( ) { curr_head . block . header . number as usize } else { blocks_to_sync [ 0 ] . block . header . number as usize } ; for chunk in blocks_to_sync . chunks ( BLOCK_SYNC_BATCH_SIZE as usize ) { let tee_nonce = get_nonce ( & api , & tee_accountid ) ; // Produce blocks if let Err ( e ) = enclave_produce_blocks ( eid , chunk . to_vec ( ) , tee_nonce ) { error ! ( " " e ) ; // enclave might not have synced return last_synced_head ; } ; i += chunk . len ( ) ; println ! ( " " i blocks_to_sync [ 0 ] block header number as usize blocks_to_sync len ( ) ) } curr_head . block . header }
Starts block production
Returns the last synced header of layer one
Starts block production Returns the last synced header of layer one
eid
sgx_enclave_id_t
api
&Api<sr25519::Pair>
last_synced_head
Header
eid
sgx_enclave_id_t
api
&Api<sr25519::Pair>
last_synced_head
Header