From 426c59486fa1a8cdc39b711d444bbaf499bbdae1 Mon Sep 17 00:00:00 2001 From: Ethan Frey Date: Fri, 23 Jun 2023 10:40:01 +0200 Subject: [PATCH] Remove locking around distribution --- .../provider/external-staking/src/contract.rs | 71 +++++-------------- 1 file changed, 17 insertions(+), 54 deletions(-) diff --git a/contracts/provider/external-staking/src/contract.rs b/contracts/provider/external-staking/src/contract.rs index 53c99317..dd7672a4 100644 --- a/contracts/provider/external-staking/src/contract.rs +++ b/contracts/provider/external-staking/src/contract.rs @@ -41,7 +41,7 @@ pub struct ExternalStakingContract<'a> { /// Stakes indexed by `(owner, validator)` pair pub stakes: Map<'a, (&'a Addr, &'a str), Lockable>, /// Per-validator distribution information - pub distribution: Map<'a, &'a str, Lockable>, + pub distribution: Map<'a, &'a str, Distribution>, /// Pending txs information pub tx_count: Item<'a, u64>, pub pending_txs: Map<'a, u64, Tx>, @@ -130,7 +130,10 @@ impl ExternalStakingContract<'_> { .load(ctx.deps.storage, (&tx_user, &tx_validator))?; // Load distribution - let mut distribution_lock = self.distribution.load(ctx.deps.storage, &tx_validator)?; + let mut distribution = self + .distribution + .may_load(ctx.deps.storage, &tx_validator)? + .unwrap_or_default(); // Commit amount (need to unlock it first) stake_lock.unlock_write()?; @@ -138,8 +141,6 @@ impl ExternalStakingContract<'_> { stake.stake += tx_amount; // Commit distribution (need to unlock it first) - distribution_lock.unlock_write()?; - let distribution = distribution_lock.write()?; // Distribution alignment stake .points_alignment @@ -152,7 +153,7 @@ impl ExternalStakingContract<'_> { // Save distribution self.distribution - .save(ctx.deps.storage, &tx_validator, &distribution_lock)?; + .save(ctx.deps.storage, &tx_validator, &distribution)?; // Remove tx self.pending_txs.remove(ctx.deps.storage, tx_id); @@ -199,9 +200,6 @@ impl ExternalStakingContract<'_> { .stakes .load(ctx.deps.storage, (&tx_user, &tx_validator))?; - // Load distribution - let mut distribution_lock = self.distribution.load(ctx.deps.storage, &tx_validator)?; - // Release stake lock stake_lock.unlock_write()?; @@ -209,13 +207,6 @@ impl ExternalStakingContract<'_> { self.stakes .save(ctx.deps.storage, (&tx_user, &tx_validator), &stake_lock)?; - // Release distribution lock - distribution_lock.unlock_write()?; - - // Save distribution - self.distribution - .save(ctx.deps.storage, &tx_validator, &distribution_lock)?; - // Remove tx self.pending_txs.remove(ctx.deps.storage, tx_id); @@ -265,12 +256,6 @@ impl ExternalStakingContract<'_> { &stake_lock, )?; - let mut distribution_lock = self.distribution.load(ctx.deps.storage, &validator)?; - - distribution_lock.lock_write()?; - self.distribution - .save(ctx.deps.storage, &validator, &distribution_lock)?; - // Create new tx let tx_id = self.next_tx_id(ctx.deps.storage)?; @@ -329,7 +314,10 @@ impl ExternalStakingContract<'_> { .load(ctx.deps.storage, (&tx_user, &tx_validator))?; // Load distribution - let mut distribution_lock = self.distribution.load(ctx.deps.storage, &tx_validator)?; + let mut distribution = self + .distribution + .may_load(ctx.deps.storage, &tx_validator)? + .unwrap_or_default(); // Commit amount (need to unlock it first) stake_lock.unlock_write()?; @@ -344,9 +332,6 @@ impl ExternalStakingContract<'_> { }; stake.pending_unbonds.push(unbond); - // Commit distribution (need to unlock it first) - distribution_lock.unlock_write()?; - let distribution = distribution_lock.write()?; // Distribution alignment stake .points_alignment @@ -359,7 +344,7 @@ impl ExternalStakingContract<'_> { // Save distribution self.distribution - .save(ctx.deps.storage, &tx_validator, &distribution_lock)?; + .save(ctx.deps.storage, &tx_validator, &distribution)?; // Remove tx self.pending_txs.remove(ctx.deps.storage, tx_id); @@ -403,9 +388,6 @@ impl ExternalStakingContract<'_> { .stakes .load(ctx.deps.storage, (&tx_user, &tx_validator))?; - // Load distribution - let mut distribution_lock = self.distribution.load(ctx.deps.storage, &tx_validator)?; - // Release stake lock stake_lock.unlock_write()?; @@ -413,13 +395,6 @@ impl ExternalStakingContract<'_> { self.stakes .save(ctx.deps.storage, (&tx_user, &tx_validator), &stake_lock)?; - // Release distribution lock - distribution_lock.unlock_write()?; - - // Save distribution - self.distribution - .save(ctx.deps.storage, &tx_validator, &distribution_lock)?; - // Remove tx self.pending_txs.remove(ctx.deps.storage, tx_id); Ok(Response::new()) @@ -495,11 +470,10 @@ impl ExternalStakingContract<'_> { let config = self.config.load(ctx.deps.storage)?; let amount = must_pay(&ctx.info, &config.rewards_denom)?; - let mut distribution_lock = self + let mut distribution = self .distribution .may_load(ctx.deps.storage, &validator)? .unwrap_or_default(); - let mut distribution = distribution_lock.write()?; let total_stake = Uint256::from(distribution.total_stake); let points_distributed = @@ -510,7 +484,7 @@ impl ExternalStakingContract<'_> { distribution.points_per_stake += points_per_stake; self.distribution - .save(ctx.deps.storage, &validator, &distribution_lock)?; + .save(ctx.deps.storage, &validator, &distribution)?; let resp = Response::new() .add_attribute("action", "distribute_rewards") @@ -535,13 +509,12 @@ impl ExternalStakingContract<'_> { let stake = stake_lock.write()?; - let mut distribution_lock = self + let distribution = self .distribution .may_load(ctx.deps.storage, &validator)? .unwrap_or_default(); - let distribution = distribution_lock.write()?; - let amount = Self::calculate_reward(stake, distribution)?; + let amount = Self::calculate_reward(stake, &distribution)?; let mut resp = Response::new() .add_attribute("action", "withdraw_rewards") @@ -716,13 +689,12 @@ impl ExternalStakingContract<'_> { .unwrap_or_default(); let stake = stake_lock.read()?; - let distribution_lock = self + let distribution = self .distribution .may_load(ctx.deps.storage, &validator)? .unwrap_or_default(); - let distribution = distribution_lock.read()?; - let amount = Self::calculate_reward(stake, distribution)?; + let amount = Self::calculate_reward(stake, &distribution)?; let config = self.config.load(ctx.deps.storage)?; let resp = PendingRewards { @@ -786,20 +758,11 @@ pub mod cross_staking { .may_load(ctx.deps.storage, (&owner, &msg.validator))? .unwrap_or_default(); - let mut distribution_lock = self - .distribution - .may_load(ctx.deps.storage, &msg.validator)? - .unwrap_or_default(); - // Write lock and save stake and distribution stake_lock.lock_write()?; self.stakes .save(ctx.deps.storage, (&owner, &msg.validator), &stake_lock)?; - distribution_lock.lock_write()?; - self.distribution - .save(ctx.deps.storage, &msg.validator, &distribution_lock)?; - // TODO: Send proper IBC message to remote staking contract // Save tx