Skip to content

Commit 31d5223

Browse files
committed
lazy migration for accoun
1 parent 32ca22e commit 31d5223

File tree

3 files changed

+38
-17
lines changed

3 files changed

+38
-17
lines changed

Cargo.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

contracts/satoshi-bridge/src/account.rs

Lines changed: 35 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,39 @@ pub struct Account {
2020
pub btc_pending_verify_list: HashSet<String>,
2121
}
2222

23+
/// Old Account format (v0.7.5 and earlier) with single pending sign id.
24+
#[near(serializers = [borsh])]
25+
#[derive(Clone)]
26+
pub struct AccountV0 {
27+
pub account_id: AccountId,
28+
pub btc_pending_sign_id: Option<String>,
29+
pub btc_pending_verify_list: HashSet<String>,
30+
}
31+
32+
impl From<AccountV0> for Account {
33+
fn from(v: AccountV0) -> Self {
34+
let mut btc_pending_sign_ids = HashSet::new();
35+
if let Some(id) = v.btc_pending_sign_id {
36+
btc_pending_sign_ids.insert(id);
37+
}
38+
Self {
39+
account_id: v.account_id,
40+
btc_pending_sign_ids,
41+
btc_pending_verify_list: v.btc_pending_verify_list,
42+
}
43+
}
44+
}
45+
2346
#[near(serializers = [borsh])]
2447
pub enum VAccount {
48+
V0(AccountV0),
2549
Current(Account),
2650
}
2751

2852
impl From<VAccount> for Account {
2953
fn from(v: VAccount) -> Self {
3054
match v {
55+
VAccount::V0(c) => c.into(),
3156
VAccount::Current(c) => c,
3257
}
3358
}
@@ -36,23 +61,22 @@ impl From<VAccount> for Account {
3661
impl From<&VAccount> for Account {
3762
fn from(v: &VAccount) -> Self {
3863
match v {
64+
VAccount::V0(c) => c.clone().into(),
3965
VAccount::Current(c) => c.clone(),
4066
}
4167
}
4268
}
4369

4470
impl<'a> From<&'a mut VAccount> for &'a mut Account {
4571
fn from(v: &'a mut VAccount) -> Self {
46-
match v {
47-
VAccount::Current(c) => c,
72+
// Lazy migrate V0 -> Current on first mutable access
73+
if let VAccount::V0(old) = v {
74+
let migrated: Account = old.clone().into();
75+
*v = VAccount::Current(migrated);
4876
}
49-
}
50-
}
51-
52-
impl<'a> From<&'a VAccount> for &'a Account {
53-
fn from(v: &'a VAccount) -> Self {
5477
match v {
5578
VAccount::Current(c) => c,
79+
_ => unreachable!(),
5680
}
5781
}
5882
}
@@ -78,15 +102,12 @@ impl Contract {
78102
self.data().accounts.contains_key(account_id)
79103
}
80104

81-
pub fn internal_get_account(&self, account_id: &AccountId) -> Option<&Account> {
82-
self.data().accounts.get(account_id).map(Into::into)
105+
pub fn internal_get_account(&self, account_id: &AccountId) -> Option<Account> {
106+
self.data().accounts.get(account_id).map(Account::from)
83107
}
84108

85-
pub fn internal_unwrap_account(&self, account_id: &AccountId) -> &Account {
86-
self.data()
87-
.accounts
88-
.get(account_id)
89-
.map(|o| o.into())
109+
pub fn internal_unwrap_account(&self, account_id: &AccountId) -> Account {
110+
self.internal_get_account(account_id)
90111
.unwrap_or_else(|| {
91112
env::panic_str(&format!("ERR_ACCOUNT_NOT_REGISTERED: {}", account_id))
92113
})

contracts/satoshi-bridge/src/api/view.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ impl Contract {
7979
}
8080

8181
pub fn get_account(&self, account_id: AccountId) -> Option<Account> {
82-
self.internal_get_account(&account_id).cloned()
82+
self.internal_get_account(&account_id)
8383
}
8484

8585
pub fn list_accounts(
@@ -88,7 +88,7 @@ impl Contract {
8888
) -> HashMap<AccountId, Option<Account>> {
8989
account_ids
9090
.into_iter()
91-
.map(|key| (key.clone(), self.internal_get_account(&key).cloned()))
91+
.map(|key| (key.clone(), self.internal_get_account(&key)))
9292
.collect()
9393
}
9494

0 commit comments

Comments
 (0)