-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlib.rs
More file actions
164 lines (145 loc) · 5.6 KB
/
Copy pathlib.rs
File metadata and controls
164 lines (145 loc) · 5.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
//! VULNERABLE: anyone may repay a borrower and reset their reward checkpoint.
//!
//! A third party can pay down a borrower's debt via `repay_for()` and the
//! contract will reset the borrower's reward checkpoint without preserving
//! accrued rewards. This erases pending rewards and alters the borrower's
//! reward accounting without their consent.
#![no_std]
use soroban_sdk::{contract, contractimpl, contracttype, Address, Env};
#[cfg(not(target_family = "wasm"))]
pub mod secure;
#[contracttype]
pub enum DataKey {
Admin,
RewardRate,
Debt(Address),
RewardCheckpoint(Address),
AccruedReward(Address),
}
#[contract]
pub struct RepayForRewardGrief;
#[contractimpl]
impl RepayForRewardGrief {
pub fn initialize(env: Env, admin: Address, reward_rate: i128) {
if env.storage().persistent().has(&DataKey::Admin) {
panic!("already initialized");
}
if reward_rate <= 0 {
panic!("reward_rate must be positive");
}
env.storage().persistent().set(&DataKey::Admin, &admin);
env.storage()
.persistent()
.set(&DataKey::RewardRate, &reward_rate);
}
pub fn borrow(env: Env, borrower: Address, amount: i128) {
borrower.require_auth();
if amount <= 0 {
panic!("amount must be positive");
}
let key = DataKey::Debt(borrower.clone());
let current: i128 = env.storage().persistent().get(&key).unwrap_or(0);
env.storage().persistent().set(&key, &(current + amount));
env.storage()
.persistent()
.set(&DataKey::RewardCheckpoint(borrower), &env.ledger().timestamp());
}
/// Repay debt for another borrower.
///
/// VULNERABILITY: resets the borrower's reward checkpoint without preserving
/// accrued rewards, so pending rewards are erased by a third party.
pub fn repay_for(env: Env, payer: Address, borrower: Address, amount: i128) {
payer.require_auth();
if amount <= 0 {
panic!("amount must be positive");
}
let key = DataKey::Debt(borrower.clone());
let debt: i128 = env.storage().persistent().get(&key).unwrap_or(0);
if amount > debt {
panic!("repay amount exceeds debt");
}
env.storage().persistent().set(&key, &(debt - amount));
// ❌ BUG: reset checkpoint and throw away accrued reward history.
env.storage()
.persistent()
.set(&DataKey::RewardCheckpoint(borrower), &env.ledger().timestamp());
}
pub fn claim_rewards(env: Env, borrower: Address) -> i128 {
borrower.require_auth();
let debt = Self::get_debt(&env, &borrower);
let reward_rate = Self::get_reward_rate(&env);
let last: u64 = env
.storage()
.persistent()
.get(&DataKey::RewardCheckpoint(borrower.clone()))
.unwrap_or(env.ledger().timestamp());
let elapsed = (env.ledger().timestamp() - last) as i128;
let accrued: i128 = env
.storage()
.persistent()
.get(&DataKey::AccruedReward(borrower.clone()))
.unwrap_or(0);
let reward = accrued + debt * reward_rate * elapsed;
env.storage()
.persistent()
.set(&DataKey::AccruedReward(borrower), &0i128);
env.storage()
.persistent()
.set(&DataKey::RewardCheckpoint(borrower), &env.ledger().timestamp());
reward
}
fn get_debt(env: &Env, borrower: &Address) -> i128 {
env.storage()
.persistent()
.get(&DataKey::Debt(borrower.clone()))
.unwrap_or(0)
}
fn get_reward_rate(env: &Env) -> i128 {
env.storage()
.persistent()
.get(&DataKey::RewardRate)
.unwrap_or(0)
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::secure::SecureRepayForRewardGriefClient;
use soroban_sdk::{testutils::Address as _, Address, Env};
fn setup() -> (Env, RepayForRewardGriefClient<'static>, Address, Address, Address) {
let env = Env::default();
env.mock_all_auths();
let admin = Address::generate(&env);
let borrower = Address::generate(&env);
let attacker = Address::generate(&env);
let id = env.register_contract(None, RepayForRewardGrief);
let client = RepayForRewardGriefClient::new(&env, &id);
(env, client, admin, borrower, attacker)
}
#[test]
fn test_third_party_repay_erases_pending_rewards() {
let (env, client, admin, borrower, attacker) = setup();
client.initialize(&admin, &1);
client.borrow(&borrower, &100);
env.ledger().with_mut(|l| l.timestamp += 10);
client.repay_for(&attacker, &borrower, &10);
// After third-party repayment, the reward checkpoint is reset and the
// borrower's accrued reward from the previous interval is lost.
assert_eq!(client.claim_rewards(&borrower), 0);
}
#[test]
fn test_secure_repay_for_preserves_pending_rewards() {
let env = Env::default();
env.mock_all_auths();
let admin = Address::generate(&env);
let borrower = Address::generate(&env);
let attacker = Address::generate(&env);
let id = env.register_contract(None, secure::SecureRepayForRewardGrief);
let client = SecureRepayForRewardGriefClient::new(&env, &id);
client.initialize(&admin, &1);
client.borrow(&borrower, &100);
env.ledger().with_mut(|l| l.timestamp += 10);
client.repay_for(&attacker, &borrower, &10);
assert_eq!(client.claim_rewards(&borrower), 100);
}
}