Skip to content

Commit 6cec7d6

Browse files
committed
fix: remove tracking_payment_key and use tracking_payment_history
Signed-off-by: bkioshn <[email protected]>
1 parent 54a4074 commit 6cec7d6

File tree

1 file changed

+28
-39
lines changed
  • rust/rbac-registration/src/registration/cardano

1 file changed

+28
-39
lines changed

rust/rbac-registration/src/registration/cardano/mod.rs

Lines changed: 28 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ impl RegistrationChain {
5555
///
5656
/// Returns an error if data is invalid
5757
pub fn new(
58-
point: Point, tracking_payment_keys: Vec<ShelleyAddress>, tx_idx: usize, txn: &MultiEraTx,
58+
point: Point, tracking_payment_keys: &[ShelleyAddress], tx_idx: usize, txn: &MultiEraTx,
5959
cip509: Cip509,
6060
) -> anyhow::Result<Self> {
6161
let inner = RegistrationChainInner::new(cip509, tracking_payment_keys, point, tx_idx, txn)?;
@@ -128,16 +128,10 @@ impl RegistrationChain {
128128
&self.inner.role_data
129129
}
130130

131-
/// Get the list of payment keys to track.
132-
#[must_use]
133-
pub fn tracking_payment_keys(&self) -> &Vec<ShelleyAddress> {
134-
&self.inner.tracking_payment_keys
135-
}
136-
137131
/// Get the map of tracked payment keys to its history.
138132
#[must_use]
139-
pub fn payment_history(&self) -> &HashMap<ShelleyAddress, Vec<PaymentHistory>> {
140-
&self.inner.payment_history
133+
pub fn tracking_payment_history(&self) -> &HashMap<ShelleyAddress, Vec<PaymentHistory>> {
134+
&self.inner.tracking_payment_history
141135
}
142136
}
143137

@@ -162,10 +156,8 @@ struct RegistrationChainInner {
162156
// Role
163157
/// Map of role number to point, transaction index, and role data.
164158
role_data: HashMap<u8, (PointTxIdx, RoleData)>,
165-
/// List of payment keys to track.
166-
tracking_payment_keys: Arc<Vec<ShelleyAddress>>,
167-
/// Map of payment key to its history.
168-
payment_history: HashMap<ShelleyAddress, Vec<PaymentHistory>>,
159+
/// Map of tracked payment key to its history.
160+
tracking_payment_history: HashMap<ShelleyAddress, Vec<PaymentHistory>>,
169161
}
170162

171163
impl RegistrationChainInner {
@@ -183,7 +175,7 @@ impl RegistrationChainInner {
183175
///
184176
/// Returns an error if data is invalid
185177
fn new(
186-
cip509: Cip509, tracking_payment_keys: Vec<ShelleyAddress>, point: Point, tx_idx: usize,
178+
cip509: Cip509, tracking_payment_keys: &[ShelleyAddress], point: Point, tx_idx: usize,
187179
txn: &MultiEraTx,
188180
) -> anyhow::Result<Self> {
189181
// Should be chain root, return immediately if not
@@ -211,12 +203,13 @@ impl RegistrationChainInner {
211203
let revocations = revocations_list(registration.revocation_list, &point_tx_idx);
212204
let role_data_map = chain_root_role_data(registration.role_set, txn, &point_tx_idx)?;
213205

214-
let mut payment_history = HashMap::new();
215-
for tracking_key in &tracking_payment_keys {
216-
// Keep record of payment history, the payment key that we want to track
217-
let histories = update_payment_history(tracking_key, txn, &point_tx_idx)?;
218-
payment_history.insert(tracking_key.clone(), histories);
206+
let mut tracking_payment_history = HashMap::new();
207+
// Create a payment history for each tracking payment key
208+
for tracking_key in tracking_payment_keys {
209+
tracking_payment_history.insert(tracking_key.clone(), Vec::new());
219210
}
211+
// Keep record of payment history, the payment key that we want to track
212+
update_tracking_payment_history(&mut tracking_payment_history, txn, &point_tx_idx)?;
220213

221214
Ok(Self {
222215
purpose,
@@ -226,8 +219,7 @@ impl RegistrationChainInner {
226219
simple_keys: public_key_map,
227220
revocations,
228221
role_data: role_data_map,
229-
tracking_payment_keys: Arc::new(tracking_payment_keys),
230-
payment_history,
222+
tracking_payment_history,
231223
})
232224
}
233225

@@ -284,16 +276,11 @@ impl RegistrationChainInner {
284276

285277
update_role_data(&mut new_inner, registration.role_set, txn, &point_tx_idx)?;
286278

287-
for tracking_key in self.tracking_payment_keys.iter() {
288-
let histories = update_payment_history(tracking_key, txn, &point_tx_idx)?;
289-
// If tracking payment key doesn't exist, insert an empty vector,
290-
// then add the histories to the history vector
291-
new_inner
292-
.payment_history
293-
.entry(tracking_key.clone())
294-
.or_default()
295-
.extend(histories);
296-
}
279+
update_tracking_payment_history(
280+
&mut new_inner.tracking_payment_history,
281+
txn,
282+
&point_tx_idx,
283+
)?;
297284

298285
Ok(new_inner)
299286
}
@@ -562,10 +549,10 @@ fn get_payment_addr_from_tx(
562549
}
563550

564551
/// Update the payment history given the tracking payment keys.
565-
fn update_payment_history(
566-
tracking_key: &ShelleyAddress, txn: &MultiEraTx, point_tx_idx: &PointTxIdx,
567-
) -> anyhow::Result<Vec<PaymentHistory>> {
568-
let mut payment_history = Vec::new();
552+
fn update_tracking_payment_history(
553+
tracking_payment_history: &mut HashMap<ShelleyAddress, Vec<PaymentHistory>>, txn: &MultiEraTx,
554+
point_tx_idx: &PointTxIdx,
555+
) -> anyhow::Result<()> {
569556
if let MultiEraTx::Conway(tx) = txn {
570557
// Conway era -> Post alonzo tx output
571558
for (index, output) in tx.transaction_body.outputs.iter().enumerate() {
@@ -578,12 +565,14 @@ fn update_payment_history(
578565
} else {
579566
bail!("Unsupported address type in update payment history");
580567
};
581-
if tracking_key == &shelley_payment {
568+
// If the payment key from the output exist in the payment history, add the
569+
// history
570+
if let Some(vec) = tracking_payment_history.get_mut(&shelley_payment) {
582571
let output_index: u16 = index.try_into().map_err(|_| {
583572
anyhow::anyhow!("Cannot convert usize to u16 in update payment history")
584573
})?;
585574

586-
payment_history.push(PaymentHistory::new(
575+
vec.push(PaymentHistory::new(
587576
point_tx_idx.clone(),
588577
txn.hash(),
589578
output_index,
@@ -597,7 +586,7 @@ fn update_payment_history(
597586
}
598587
}
599588
}
600-
Ok(payment_history)
589+
Ok(())
601590
}
602591

603592
#[cfg(test)]
@@ -662,7 +651,7 @@ mod test {
662651
let tracking_payment_keys = vec![];
663652

664653
let registration_chain =
665-
RegistrationChain::new(point_1.clone(), tracking_payment_keys, 3, tx_1, cip509_1);
654+
RegistrationChain::new(point_1.clone(), &tracking_payment_keys, 3, tx_1, cip509_1);
666655
// Able to add chain root to the registration chain
667656
assert!(registration_chain.is_ok());
668657

0 commit comments

Comments
 (0)