fn upsert_replica(who: &<T as system::Config>::AccountId,
                      cid: &MerkleRoot,
                      reported_file_size: u64,
                      anchor: &TarsAnchor,
                      valid_at: BlockNumber,
                      maybe_members: &Option<BTreeSet<<T as system::Config>::AccountId>>
    ) -> u64 {
        // Judge if file_info.file_size == reported_file_size or not
        Self::maybe_upsert_file_size(who, cid, reported_file_size);

        // `is_counted` is a concept in tars-side, which means if this `cid`'s `used` size is counted by `(who, anchor)`
        // if the file doesn't exist(aka. is_counted == false), return false(doesn't increase used size) cause it's junk.
        // if the file exist, is_counted == true, will change it later.
        let mut used_size: u64 = 0;
        if let Some((mut file_info, mut used_info)) = <Files<T>>::get(cid) {
            let mut is_counted = true;
            // 1. Check if the file is stored by other members
            if let Some(members) = maybe_members {
                for replica in file_info.replicas.iter() {
                    if used_info.groups.contains_key(&replica.anchor) && members.contains(&replica.who) {
                        if T::TarsInterface::check_anchor(&replica.who, &replica.anchor) {
                            // duplicated in group and set is_counted to false
                            is_counted = false;
                        }
                    }
                }
            }

            // 2. Prepare new replica info
            let new_replica = Replica {
                who: who.clone(),
                valid_at,
                anchor: anchor.clone(),
                is_reported: true
            };
            Self::insert_replica(&mut file_info, new_replica);
            file_info.reported_replica_count += 1;

            // 3. Update used_info
            if is_counted {
                used_size = Self::add_used_group(&mut used_info, anchor, file_info.file_size); // need to add the used_size after the update
            };

            // 4. The first join the replicas and file become live(expired_on > calculated_at)
            let curr_bn = Self::get_current_block_number();
            if file_info.replicas.len() == 1 {
                file_info.calculated_at = curr_bn;
                file_info.expired_on = curr_bn + T::FileDuration::get();
            }

            // 5. Update files
            <Files<T>>::insert(cid, (file_info, used_info));
        }
        return used_size
    }
/// Upsert new replica
/// Accept id(who, anchor), reported_file_size, cid, valid_at and maybe_member
/// Returns the real used size of this file
/// used size is decided by murphy
Upsert new replica
Accept id(who, anchor), reported_file_size, cid, valid_at and maybe_member
Returns the real used size of this file
used size is decided by murphy
Upsert new replica Accept id ( who anchor ) reported_file_size cid valid_at and maybe_member Returns the real used size of this file used size is decided by murphy
fn upsert_replica(who: &<T as system::Config>::AccountId,
                      cid: &MerkleRoot,
                      reported_file_size: u64,
                      anchor: &TarsAnchor,
                      valid_at: BlockNumber,
                      maybe_members: &Option<BTreeSet<<T as system::Config>::AccountId>>
    ) -> u64 {
        // Judge if file_info.file_size == reported_file_size or not
        Self::maybe_upsert_file_size(who, cid, reported_file_size);
        // `is_counted` is a concept in tars-side, which means if this `cid`'s `used` size is counted by `(who, anchor)`
        // if the file doesn't exist(aka. is_counted == false), return false(doesn't increase used size) cause it's junk.
        // if the file exist, is_counted == true, will change it later.
        let mut used_size: u64 = 0;
        if let Some((mut file_info, mut used_info)) = <Files<T>>::get(cid) {
            let mut is_counted = true;
            // 1. Check if the file is stored by other members
            if let Some(members) = maybe_members {
                for replica in file_info.replicas.iter() {
                    if used_info.groups.contains_key(&replica.anchor) && members.contains(&replica.who) {
                        if T::TarsInterface::check_anchor(&replica.who, &replica.anchor) {
                            // duplicated in group and set is_counted to false
                            is_counted = false;
                        }
                    }
                }
            }
            // 2. Prepare new replica info
            let new_replica = Replica {
                who: who.clone(),
                valid_at,
                anchor: anchor.clone(),
                is_reported: true
            };
            Self::insert_replica(&mut file_info, new_replica);
            file_info.reported_replica_count += 1;
            // 3. Update used_info
            if is_counted {
                used_size = Self::add_used_group(&mut used_info, anchor, file_info.file_size); // need to add the used_size after the update
            };
            // 4. The first join the replicas and file become live(expired_on > calculated_at)
            let curr_bn = Self::get_current_block_number();
            if file_info.replicas.len() == 1 {
                file_info.calculated_at = curr_bn;
                file_info.expired_on = curr_bn + T::FileDuration::get();
            }
            // 5. Update files
            <Files<T>>::insert(cid, (file_info, used_info));
        }
        return used_size
    }
fn upsert_replica ( who : & < T as system :: Config > :: AccountId , cid : & MerkleRoot , reported_file_size : u64 , anchor : & TarsAnchor , valid_at : BlockNumber , maybe_members : & Option < BTreeSet < < T as system :: Config > :: AccountId > > ) -> u64 { // Judge if file_info.file_size == reported_file_size or not Self :: maybe_upsert_file_size ( who , cid , reported_file_size ) ; // `is_counted` is a concept in tars-side, which means if this `cid`'s `used` size is counted by `(who, anchor)` // if the file doesn't exist(aka. is_counted == false), return false(doesn't increase used size) cause it's junk. // if the file exist, is_counted == true, will change it later. let mut used_size : u64 = 0 ; if let Some ( ( mut file_info , mut used_info ) ) = < Files < T > > :: get ( cid ) { let mut is_counted = true ; // 1. Check if the file is stored by other members if let Some ( members ) = maybe_members { for replica in file_info . replicas . iter ( ) { if used_info . groups . contains_key ( & replica . anchor ) && members . contains ( & replica . who ) { if T :: TarsInterface :: check_anchor ( & replica . who , & replica . anchor ) { // duplicated in group and set is_counted to false is_counted = false ; } } } } // 2. Prepare new replica info let new_replica = Replica { who : who . clone ( ) , valid_at , anchor : anchor . clone ( ) , is_reported : true } ; Self :: insert_replica ( & mut file_info , new_replica ) ; file_info . reported_replica_count += 1 ; // 3. Update used_info if is_counted { used_size = Self :: add_used_group ( & mut used_info , anchor , file_info . file_size ) ; // need to add the used_size after the update } ; // 4. The first join the replicas and file become live(expired_on > calculated_at) let curr_bn = Self :: get_current_block_number ( ) ; if file_info . replicas . len ( ) == 1 { file_info . calculated_at = curr_bn ; file_info . expired_on = curr_bn + T :: FileDuration :: get ( ) ; } // 5. Update files < Files < T > > :: insert ( cid , ( file_info , used_info ) ) ; } return used_size }
Upsert new replica
Accept id(who, anchor), reported_file_size, cid, valid_at and maybe_member
Returns the real used size of this file
used size is decided by murphy
Upsert new replica Accept id ( who anchor ) reported_file_size cid valid_at and maybe_member Returns the real used size of this file used size is decided by murphy
who
&<T as system::Config>::AccountId
cid
&MerkleRoot
reported_file_size
u64
anchor
&TarsAnchor
valid_at
BlockNumber
maybe_members
&Option<BTreeSet<<T as system::Config>::AccountId>>