Coverage Report

Created: 2022-11-10 19:56

/home/runner/work/creditcoin/creditcoin/pallets/creditcoin/src/migrations/v4.rs
Line
Count
Source (jump to first uncovered line)
1
// address registration now verifies ownership, so removed existing addresses
2
use super::v3;
3
use codec::{Decode, Encode};
4
use frame_support::traits::Get;
5
use frame_support::Blake2_128Concat;
6
use frame_support::{dispatch::Weight, generate_storage_alias};
7
use sp_runtime::SaturatedConversion;
8
9
use crate::{Config, ExternalAddress};
10
pub use v3::*;
11
12
use crate::AddressId;
13
use v3::Blockchain as OldBlockchain;
14
15
13
#[derive(
Encode11
, Decode)]
16
0
#[cfg_attr(test, derive(Debug, PartialEq, Eq))]
17
pub struct Address<AccountId> {
18
  pub blockchain: OldBlockchain,
19
  pub value: ExternalAddress,
20
  pub owner: AccountId,
21
}
22
23
generate_storage_alias!(
24
  Creditcoin,
25
  Addresses<T: Config> => Map<(Blake2_128Concat, AddressId<T::Hash>), Address<T::AccountId>>
26
);
27
28
1
pub(crate) fn migrate<T: Config>() -> Weight {
29
1
  let count_removed = match Addresses::<T>::remove_all(None) {
30
1
    sp_io::KillStorageResult::AllRemoved(count) => count,
31
0
    sp_io::KillStorageResult::SomeRemaining(count) => count,
32
  };
33
34
1
  T::DbWeight::get().writes(count_removed.saturated_into())
35
1
}
36
37
#[cfg(test)]
38
mod tests {
39
  use super::{Address, Addresses, OldBlockchain};
40
  use crate::{
41
    concatenate,
42
    mock::{AccountId, ExtBuilder, Test},
43
    AddressId,
44
  };
45
  use sp_core::H256;
46
  use sp_runtime::traits::Hash as HashT;
47
  use sp_std::convert::TryInto;
48
49
  #[extend::ext]
50
  impl<H> AddressId<H> {
51
10
    fn with_old_blockchain<T: frame_system::Config>(
52
10
      blockchain: &OldBlockchain,
53
10
      address: &[u8],
54
10
    ) -> AddressId<H>
55
10
    where
56
10
      <T as frame_system::Config>::Hashing: HashT<Output = H>,
57
10
    {
58
10
      let key = concatenate!(blockchain.as_bytes(), address);
59
10
      AddressId::make(T::Hashing::hash(&key))
60
10
    }
61
  }
62
63
1
  #[test]
64
1
  fn migrate_works() {
65
1
    ExtBuilder::default().build_and_execute(|| {
66
1
      let mut ids = Vec::new();
67
11
      for 
i10
in 0u8..10u8 {
68
10
        let id = AddressId::<H256>::with_old_blockchain::<Test>(
69
10
          &OldBlockchain::Ethereum,
70
10
          &i.to_be_bytes(),
71
10
        );
72
10
        let address = Address {
73
10
          blockchain: OldBlockchain::Ethereum,
74
10
          value: i.to_be_bytes().to_vec().try_into().unwrap(),
75
10
          owner: AccountId::new([i; 32]),
76
10
        };
77
10
        Addresses::<Test>::insert(&id, address);
78
10
        ids.push(id);
79
10
      }
80
81
1
      super::migrate::<Test>();
82
83
11
      for 
id10
in ids {
84
10
        assert!(!Addresses::<Test>::contains_key(id));
85
      }
86
1
    });
87
1
  }
88
}