From fa3720ee56fe082fe5d1421ce01622689abb3d79 Mon Sep 17 00:00:00 2001 From: Sergio Chouhy Date: Wed, 12 Aug 2026 12:18:31 -0300 Subject: [PATCH] refactor(sequencer_stake)use the vacant/occupied pattern in the stake function --- lez/programs/sequencer_stake/src/main.rs | 53 +++++++++++++----------- 1 file changed, 28 insertions(+), 25 deletions(-) diff --git a/lez/programs/sequencer_stake/src/main.rs b/lez/programs/sequencer_stake/src/main.rs index 64751c1c2..16f351ee5 100644 --- a/lez/programs/sequencer_stake/src/main.rs +++ b/lez/programs/sequencer_stake/src/main.rs @@ -1,3 +1,5 @@ +use std::collections::btree_map::Entry; + use lee_core::{ account::{AccountId, AccountWithMetadata}, program::{ @@ -154,34 +156,35 @@ fn stake( ); } - if let Some(entry) = config.entries.get_mut(&sequencer_key) { - // top up: same already-claimed account only - assert!( - is_claimed, - "this sequencer key already has an ownership account" - ); - assert_eq!( - entry.account_id, ownership_account.account_id, - "config entry points at a different ownership account" - ); - entry.total_staked = entry - .total_staked - .checked_add(amount) - .expect("total staked overflow"); - } else { - // first stake for this key, or a new one after a full exit - assert!( - amount >= minimum_sequencer_stake, - "an initial stake must already meet the minimum" - ); - config.entries.insert( - sequencer_key, - SequencerEntry { + match config.entries.entry(sequencer_key) { + Entry::Occupied(mut occupied) => { + // top up: same already-claimed account only + assert!( + is_claimed, + "this sequencer key already has an ownership account" + ); + let entry = occupied.get_mut(); + assert_eq!( + entry.account_id, ownership_account.account_id, + "config entry points at a different ownership account" + ); + entry.total_staked = entry + .total_staked + .checked_add(amount) + .expect("total staked overflow"); + } + Entry::Vacant(vacant) => { + // first stake for this key, or a new one after a full exit + assert!( + amount >= minimum_sequencer_stake, + "an initial stake must already meet the minimum" + ); + vacant.insert(SequencerEntry { account_id: ownership_account.account_id, total_staked: amount, total_pending_unstake: 0, - }, - ); + }); + } } // pass-through: propagates authorization into the nested mover call