Skip to content

Commit d4877c8

Browse files
committed
GH-645: from_priced_retry_tx_templates starts to work
1 parent 3137279 commit d4877c8

File tree

1 file changed

+49
-7
lines changed

1 file changed

+49
-7
lines changed

node/src/accountant/scanners/payable_scanner/data_structures/signable_tx_template.rs

Lines changed: 49 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
use crate::accountant::scanners::payable_scanner::data_structures::priced_new_tx_template::{
22
PricedNewTxTemplate, PricedNewTxTemplates,
33
};
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;
610
use std::ops::Deref;
711
use web3::types::Address;
812

@@ -58,7 +62,44 @@ impl SignableTxTemplates {
5862
priced_retry_tx_templates: PricedRetryTxTemplates,
5963
latest_nonce: u64,
6064
) -> 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)
62103
}
63104

64105
pub fn first_nonce(&self) -> u64 {
@@ -133,17 +174,18 @@ mod tests {
133174
#[test]
134175
fn signable_tx_templates_can_be_created_from_priced_retry_tx_templates() {
135176
let nonce = 10;
177+
// n is same as prev_nonce here
136178
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),
139181
make_priced_retry_tx_template(10),
182+
make_priced_retry_tx_template(8),
140183
make_priced_retry_tx_template(11),
141-
make_priced_retry_tx_template(12),
142184
]);
143185

144186
let result = SignableTxTemplates::new(Either::Right(retries.clone()), nonce);
145187

146-
let expected_order = vec![2, 3, 4, 0, 1];
188+
let expected_order = vec![2, 4, 0, 1, 3];
147189
result
148190
.iter()
149191
.enumerate()

0 commit comments

Comments
 (0)