fn merge_and_trim(
    chains: Vec<MsgChain>,
    result: Vec<SignedMessage>,
    base_fee: &BigInt,
    gas_limit: i64,
    min_gas: i64,
) -> Result<(Vec<SignedMessage>, i64), Error> {
    let mut chains = chains;
    let mut result = result;
    let mut gas_limit = gas_limit;
    // 2. Sort the chains
    chains.sort_by(|a, b| b.compare(&a));

    if !chains.is_empty() && chains[0].curr().gas_perf < 0.0 {
        return Ok((Vec::new(), gas_limit));
    }

    // 3. Merge chains until the block limit, as long as they have non-negative gas performance
    let mut last = chains.len();
    for (i, chain) in chains.iter().enumerate() {
        if chain.curr().gas_perf < 0.0 {
            break;
        }
        if chain.curr().gas_limit <= gas_limit {
            gas_limit -= chains[i].curr().gas_limit;
            result.extend(chain.curr().msgs.clone());
            continue;
        }
        last = i;
        break;
    }
    'tail_loop: while gas_limit >= min_gas && last < chains.len() {
        // trim, discard negative performing messages
        chains[last].trim(gas_limit, base_fee);

        // push down if it hasn't been invalidated
        if chains[last].curr().valid {
            for i in last..chains.len() - 1 {
                if chains[i].compare(&chains[i + 1]) == Ordering::Greater {
                    break;
                }
                chains.swap(i, i + 1);
            }
        }

        // select the next (valid and fitting) chain for inclusion
        for (i, chain) in chains.iter_mut().skip(last).enumerate() {
            if !chain.curr().valid {
                continue;
            }

            // if gas_perf < 0 then we have no more profitable chains
            if chain.curr().gas_perf < 0.0 {
                break 'tail_loop;
            }

            // does it fit in the block?
            if chain.curr().gas_limit <= gas_limit {
                gas_limit -= chain.curr().gas_limit;
                result.append(&mut chain.curr_mut().msgs);
                continue;
            }
            last += i;
            continue 'tail_loop;
        }
        break;
    }
    Ok((result, gas_limit))
}
/// Returns merged and trimmed messages with the gas limit
Returns merged and trimmed messages with the gas limit
Returns merged and trimmed messages with the gas limit
fn merge_and_trim(
    chains: Vec<MsgChain>,
    result: Vec<SignedMessage>,
    base_fee: &BigInt,
    gas_limit: i64,
    min_gas: i64,
) -> Result<(Vec<SignedMessage>, i64), Error> {
    let mut chains = chains;
    let mut result = result;
    let mut gas_limit = gas_limit;
    // 2. Sort the chains
    chains.sort_by(|a, b| b.compare(&a));
    if !chains.is_empty() && chains[0].curr().gas_perf < 0.0 {
        return Ok((Vec::new(), gas_limit));
    }
    // 3. Merge chains until the block limit, as long as they have non-negative gas performance
    let mut last = chains.len();
    for (i, chain) in chains.iter().enumerate() {
        if chain.curr().gas_perf < 0.0 {
            break;
        }
        if chain.curr().gas_limit <= gas_limit {
            gas_limit -= chains[i].curr().gas_limit;
            result.extend(chain.curr().msgs.clone());
            continue;
        }
        last = i;
        break;
    }
    'tail_loop: while gas_limit >= min_gas && last < chains.len() {
        // trim, discard negative performing messages
        chains[last].trim(gas_limit, base_fee);
        // push down if it hasn't been invalidated
        if chains[last].curr().valid {
            for i in last..chains.len() - 1 {
                if chains[i].compare(&chains[i + 1]) == Ordering::Greater {
                    break;
                }
                chains.swap(i, i + 1);
            }
        }
        // select the next (valid and fitting) chain for inclusion
        for (i, chain) in chains.iter_mut().skip(last).enumerate() {
            if !chain.curr().valid {
                continue;
            }
            // if gas_perf < 0 then we have no more profitable chains
            if chain.curr().gas_perf < 0.0 {
                break 'tail_loop;
            }
            // does it fit in the block?
            if chain.curr().gas_limit <= gas_limit {
                gas_limit -= chain.curr().gas_limit;
                result.append(&mut chain.curr_mut().msgs);
                continue;
            }
            last += i;
            continue 'tail_loop;
        }
        break;
    }
    Ok((result, gas_limit))
}
fn merge_and_trim ( chains : Vec < MsgChain > , result : Vec < SignedMessage > , base_fee : & BigInt , gas_limit : i64 , min_gas : i64 , ) -> Result < ( Vec < SignedMessage > , i64 ) , Error > { let mut chains = chains ; let mut result = result ; let mut gas_limit = gas_limit ; // 2. Sort the chains chains . sort_by ( | a , b | b . compare ( & a ) ) ; if ! chains . is_empty ( ) && chains [ 0 ] . curr ( ) . gas_perf < 0.0 { return Ok ( ( Vec :: new ( ) , gas_limit ) ) ; } // 3. Merge chains until the block limit, as long as they have non-negative gas performance let mut last = chains . len ( ) ; for ( i , chain ) in chains . iter ( ) . enumerate ( ) { if chain . curr ( ) . gas_perf < 0.0 { break ; } if chain . curr ( ) . gas_limit <= gas_limit { gas_limit -= chains [ i ] . curr ( ) . gas_limit ; result . extend ( chain . curr ( ) . msgs . clone ( ) ) ; continue ; } last = i ; break ; } ' tail_loop : while gas_limit >= min_gas && last < chains . len ( ) { // trim, discard negative performing messages chains [ last ] . trim ( gas_limit , base_fee ) ; // push down if it hasn't been invalidated if chains [ last ] . curr ( ) . valid { for i in last .. chains . len ( ) - 1 { if chains [ i ] . compare ( & chains [ i + 1 ] ) == Ordering :: Greater { break ; } chains . swap ( i , i + 1 ) ; } } // select the next (valid and fitting) chain for inclusion for ( i , chain ) in chains . iter_mut ( ) . skip ( last ) . enumerate ( ) { if ! chain . curr ( ) . valid { continue ; } // if gas_perf < 0 then we have no more profitable chains if chain . curr ( ) . gas_perf < 0.0 { break ' tail_loop ; } // does it fit in the block? if chain . curr ( ) . gas_limit <= gas_limit { gas_limit -= chain . curr ( ) . gas_limit ; result . append ( & mut chain . curr_mut ( ) . msgs ) ; continue ; } last += i ; continue ' tail_loop ; } break ; } Ok ( ( result , gas_limit ) ) }
Returns merged and trimmed messages with the gas limit
Returns merged and trimmed messages with the gas limit
chains
Vec<MsgChain>
result
Vec<SignedMessage>
base_fee
&BigInt
gas_limit
i64
min_gas
i64