|
1 | 1 | use crate::accountant::scanners::payable_scanner::data_structures::priced_new_tx_template::{ |
2 | 2 | PricedNewTxTemplate, PricedNewTxTemplates, |
3 | 3 | }; |
4 | | -use crate::accountant::scanners::payable_scanner::data_structures::priced_retry_tx_template::PricedRetryTxTemplates; |
5 | | -use itertools::Either; |
| 4 | +use crate::accountant::scanners::payable_scanner::data_structures::priced_retry_tx_template::{ |
| 5 | + PricedRetryTxTemplate, PricedRetryTxTemplates, |
| 6 | +}; |
| 7 | +use bytes::Buf; |
| 8 | +use itertools::{Either, Itertools}; |
| 9 | +use std::collections::HashMap; |
6 | 10 | use std::ops::Deref; |
7 | 11 | use web3::types::Address; |
8 | 12 |
|
@@ -58,7 +62,44 @@ impl SignableTxTemplates { |
58 | 62 | priced_retry_tx_templates: PricedRetryTxTemplates, |
59 | 63 | latest_nonce: u64, |
60 | 64 | ) -> Self { |
61 | | - todo!() |
| 65 | + let mut hashmap = priced_retry_tx_templates |
| 66 | + .iter() |
| 67 | + .map(|template| (template.prev_nonce, template)) |
| 68 | + .collect::<HashMap<u64, &PricedRetryTxTemplate>>(); |
| 69 | + |
| 70 | + let final_nonce = latest_nonce + hashmap.len() as u64 - 1; |
| 71 | + |
| 72 | + let mut signable_tx_templates = Vec::with_capacity(hashmap.len()); |
| 73 | + |
| 74 | + for nonce in latest_nonce..=final_nonce { |
| 75 | + match hashmap.remove(&nonce) { |
| 76 | + None => { |
| 77 | + let min_nonce = hashmap |
| 78 | + .keys() |
| 79 | + .min() |
| 80 | + .cloned() |
| 81 | + .expect("No minimum nonce found"); // GH-605: Test me |
| 82 | + let found = hashmap.remove(&min_nonce).expect("Entry not found"); // GH-605: Test me |
| 83 | + |
| 84 | + signable_tx_templates.push(SignableTxTemplate { |
| 85 | + receiver_address: found.base.receiver_address, |
| 86 | + amount_in_wei: found.base.amount_in_wei, |
| 87 | + gas_price_wei: found.computed_gas_price_wei, |
| 88 | + nonce, |
| 89 | + }) |
| 90 | + } |
| 91 | + Some(template) => { |
| 92 | + signable_tx_templates.push(SignableTxTemplate { |
| 93 | + receiver_address: template.base.receiver_address, |
| 94 | + amount_in_wei: template.base.amount_in_wei, |
| 95 | + gas_price_wei: template.computed_gas_price_wei, |
| 96 | + nonce, |
| 97 | + }); |
| 98 | + } |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | + SignableTxTemplates(signable_tx_templates) |
62 | 103 | } |
63 | 104 |
|
64 | 105 | pub fn first_nonce(&self) -> u64 { |
@@ -133,17 +174,18 @@ mod tests { |
133 | 174 | #[test] |
134 | 175 | fn signable_tx_templates_can_be_created_from_priced_retry_tx_templates() { |
135 | 176 | let nonce = 10; |
| 177 | + // n is same as prev_nonce here |
136 | 178 | let retries = PricedRetryTxTemplates(vec![ |
137 | | - make_priced_retry_tx_template(6), // n is same as prev_nonce here |
138 | | - make_priced_retry_tx_template(8), |
| 179 | + make_priced_retry_tx_template(12), |
| 180 | + make_priced_retry_tx_template(6), |
139 | 181 | make_priced_retry_tx_template(10), |
| 182 | + make_priced_retry_tx_template(8), |
140 | 183 | make_priced_retry_tx_template(11), |
141 | | - make_priced_retry_tx_template(12), |
142 | 184 | ]); |
143 | 185 |
|
144 | 186 | let result = SignableTxTemplates::new(Either::Right(retries.clone()), nonce); |
145 | 187 |
|
146 | | - let expected_order = vec![2, 3, 4, 0, 1]; |
| 188 | + let expected_order = vec![2, 4, 0, 1, 3]; |
147 | 189 | result |
148 | 190 | .iter() |
149 | 191 | .enumerate() |
|
0 commit comments