# Entanglement: 2lifetime problem pluse 2 short question

I'm looking for a solution that does not have implications for other parts of the code. I think it is better to see the repository code and also introduce me to a template for a complete program.
At the first is Error rised for:

``````                let mut c=Cell::new(maked_transactions_of_a_block.as_slice());
let mut r=Rc::new(c);
let mut rc=&mut r;
``````
```````r` does not live long enough
borrowed value does not live long enoughrustc[E0597](https://doc.rust-lang.org/error-index.html#E0597)

factory.rs(64, 9): `r` dropped here while still borrowed

factory.rs(12, 33): lifetime `'1` appears in the type of `blockchain`

factory.rs(54, 45): argument requires that `r` is borrowed for `'1`
``````

In Addition I have two question my friends:
1.I defined blockchain as you can see but I do not know which it would have conseqence problems that related to scope function and 'for'?

``````pub fn blockchain_factory<'a,F>(mut blockchain:Blockchain,difficulty:u128, f : F) -> Result<(),CustomError>
where
F: FnOnce()->  Result<serde_json::Value,CustomError>
{
let serde_values_transactions:serde_json::Value= serde_json::from_value(f().unwrap()).unwrap();
let blocks_val:serde_json::Value=serde_values_transactions["blocks"].clone();
{
//let mut blockchain=Blockchain::new();
``````
1. I defined lifetime 'a for embeded structs Blockchain, Block,OptionTransation. But I do not it for Transaction and Value. How does you think about consequenses and entanglement in program? except that do you know a good example repository?
```````maked_transactions_of_a_block` does not live long enough
borrowed value does not live long enoughrustcE0597
factory.rs(64, 9): `maked_transactions_of_a_block` dropped here while still borrowed
factory.rs(12, 33): lifetime `'1` appears in the type of `blockchain`
factory.rs(47, 27): argument requires that `maked_transactions_of_a_block` is borrowed for `'1`
``````
``````use std::rc::Rc;
use std::cell::{RefCell, Ref, Cell};
use std::borrow::{BorrowMut, Borrow};
use super::*;
use library_blockchain::transaction::{Value as ModelValue, OptionTransaction};
use library_blockchain::{*};
use serde_json::Value;

#[allow(unused_mut)]
pub fn blockchain_factory<'a,F>(mut blockchain:Blockchain,difficulty:u128, f : F) -> Result<(),CustomError>
where
F: FnOnce()->  Result<serde_json::Value,CustomError>
{
let serde_values_transactions:serde_json::Value= serde_json::from_value(f().unwrap()).unwrap();
let blocks_val:serde_json::Value=serde_values_transactions["blocks"].clone();
{
//let mut blockchain=Blockchain::new();

blocks_val[0].as_object().unwrap().into_iter().enumerate().for_each(|(_i, block)| {

let mut prev_hash:Box<[u8]>=Box::default();
let mut maked_transactions_of_a_block:Vec<OptionTransaction>=vec![];
//println!("\nBlock {:?}\n",block);
block.1.as_array().unwrap().into_iter().enumerate().for_each(|(_j, trxs)| {

let transactions=trxs.get("transactions").unwrap();
let obg_trx=transactions.as_array().unwrap();
let trx=obg_trx[0].as_object().unwrap();
let length=&trx.keys().len()+1;
//println!("\n{:?}\n",trx);

for c in 1..length{
let trx_name=concat_string!("transaction",c.to_string());
let trx=(transactions[0].as_object().unwrap()).get(&trx_name).unwrap();
let puts=transaction_split(trx).unwrap();
&maked_transactions_of_a_block.push(puts);
}

});

//let dd=pp(maked_transactions_of_a_block);
//let  rc_maked_transactions_of_a_block=  call_maked_trx(|| dd);
//let refcell_trx=rc_maked_transactions_of_a_block;
let mut c=Cell::new(maked_transactions_of_a_block.as_slice());
let mut r=Rc::new(c);
let mut rc=&mut r;
if _i==0{
//let u=rc_maked_transactions_of_a_block(vecopt);
//  let y=(||rc_maked_transactions_of_a_block(vecopt);

let mut genesis_block = Block::new(0, now(),vec![0; 32], rc, difficulty);
prev_hash=genesis_block.mine().unwrap().into_boxed_slice();
let _=&blockchain.update_with_block(genesis_block);
}
else if _i >0{
let mut maked_block:Block = Block::new(_i as u32, now(), prev_hash.to_vec(), rc, difficulty);
prev_hash=maked_block.mine().unwrap().into_boxed_slice();
let _=&blockchain.update_with_block(maked_block);
//println!("**maked_hash:**\n{:?}\n",&blockchain.blocks[_i].prev_block_hash.clone());
}
});
}
Ok(())
}

fn transaction_split( trx:&serde_json::Value) -> Result<OptionTransaction,CustomError>{

let mut trx_inputs_model_vec :Vec<ModelValue> = vec![];
let mut new_transaction:OptionTransaction;

if trx.is_null(){
return Err(CustomError::BlockchainFactory)
}
if trx[0].as_object().unwrap().is_empty() || trx[0].as_object().unwrap().len()<1usize{
return Err(CustomError::BlockchainFactory)
}
let trx_inputs=(trx[0].as_object().unwrap()).get("inputs").unwrap();

if !(trx_inputs.is_null()) && !(trx_inputs.as_array().is_none()){
let trx_inputs_vec=trx_inputs.as_array().unwrap();

for item_internal_inputs in trx_inputs_vec {
let mut trx_inputs_model:ModelValue=ModelValue{
value: 0,
};

if !item_internal_inputs.is_null(){

trx_inputs_model= ModelValue{
value:item_internal_inputs["value"].as_str().unwrap().parse::<u64>().unwrap()
};
trx_inputs_model_vec.push(trx_inputs_model);
}

}
}
}

let mut trx_outputs_model_vec :Vec<ModelValue> = vec![];
let trx_outputs=(trx[0].as_object().unwrap()).get("outputs").unwrap();

if !(trx_outputs.is_null()) && !(trx_outputs.as_array().is_none()){
let trx_outputs_vec=trx_outputs.as_array().unwrap();

for item_internal_outputs in trx_outputs_vec {
let mut trx_outputs_model:ModelValue=ModelValue{
value: 0,
};

if !item_internal_outputs.is_null(){

trx_outputs_model= ModelValue{
value:item_internal_outputs["value"].as_str().unwrap().parse::<u64>().unwrap()
};
trx_outputs_model_vec.push(trx_outputs_model);
}

}
}
new_transaction= Transaction::new(trx_inputs_model_vec,trx_outputs_model_vec);

return Ok(new_transaction);
}
return Err(CustomError::BlockchainFactory) ;
}
``````
``````pub struct Block<'a> {
pub index: u32,
pub timestamp: u128,
pub hash: Hash,
pub prev_block_hash: Hash,
pub nonce: u64,
pub option_transactions: &'a mut Rc<Cell<&'a [OptionTransaction]>>,
pub difficulty: u128,
}

impl<'a> Debug for Block<'a> {
fn fmt (&self, f: &mut Formatter) -> fmt::Result {
let optrx=self.option_transactions.take();
let result =  write!(f, "Prev hash of {} the Block[{}]: {} at: {} trx.len: {} nonce: {}",
&hex::encode(&self.prev_block_hash),
&self.index,
&hex::encode(&self.hash),
&self.timestamp,
&self.option_transactions.take().len(),
&self.nonce,
);
self.option_transactions.set(optrx);
result
}
}

impl<'a> Block<'a> {
pub fn new (index: u32, timestamp: u128, prev_block_hash: Hash, option_transactions: &'a mut Rc<Cell<&'a [OptionTransaction]>>, difficulty: u128) -> Self {
Block {
index,
timestamp,
hash: vec![0; 32],
prev_block_hash,
nonce: 0,
option_transactions,
difficulty,
}
}

``````

@kornel @jbe @legionmammal978
I really appreciate
(Branch armanriazi)
OpenSourceCode-Latest

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.