/home/runner/work/creditcoin/creditcoin/pallets/creditcoin/src/ocw/tasks.rs
Line | Count | Source |
1 | | pub mod collect_coins; |
2 | | pub mod verify_transfer; |
3 | | |
4 | | use crate::ocw::errors::{OffchainError, VerificationResult}; |
5 | | use crate::types::{ |
6 | | CollectedCoins, Task, TaskOutput, Transfer, UnverifiedCollectedCoins, UnverifiedTransfer, |
7 | | }; |
8 | | use crate::{CollectedCoinsId, Config, TaskData, TransferId}; |
9 | | use codec::Encode; |
10 | | use collect_coins::GCreContract; |
11 | | pub use sp_runtime::offchain::storage_lock::{BlockAndTime, Lockable, StorageLock}; |
12 | | use sp_runtime::traits::{UniqueSaturatedFrom, UniqueSaturatedInto}; |
13 | | use sp_std::vec::Vec; |
14 | | |
15 | | #[inline] |
16 | 33 | pub(crate) fn storage_key<Id: Encode>(id: &Id) -> Vec<u8> { |
17 | 33 | const TASK_GUARD: &[u8] = b"creditcoin/task/guard/"; |
18 | 33 | id.using_encoded(|encoded_id| TASK_GUARD.iter().chain(encoded_id).copied().collect()) |
19 | 33 | } |
20 | | |
21 | | impl<AccountId, BlockNum, Hash, Moment> UnverifiedTransfer<AccountId, BlockNum, Hash, Moment> |
22 | | where |
23 | | Moment: UniqueSaturatedInto<u64> + UniqueSaturatedFrom<u64>, |
24 | | BlockNum: UniqueSaturatedInto<u64>, |
25 | | { |
26 | 8 | pub fn verify_ocw<T>(&self) -> VerificationResult<Option<T::Moment>> |
27 | 8 | where |
28 | 8 | T: Config<AccountId = AccountId, BlockNumber = BlockNum, Hash = Hash, Moment = Moment>, |
29 | 8 | { |
30 | 8 | crate::Pallet::<T>::verify_transfer_ocw(self) |
31 | 8 | } |
32 | | |
33 | 3 | pub fn into_output<T: Config>( |
34 | 3 | self, |
35 | 3 | timestamp: Option<T::Moment>, |
36 | 3 | ) -> Transfer<AccountId, BlockNum, Hash, Moment> |
37 | 3 | where |
38 | 3 | T: Config<AccountId = AccountId, BlockNumber = BlockNum, Hash = Hash, Moment = Moment>, |
39 | 3 | { |
40 | 3 | Transfer { timestamp, ..self.transfer } |
41 | 3 | } |
42 | | |
43 | 3 | pub fn to_id<T: Config>(&self) -> TransferId<Hash> |
44 | 3 | where |
45 | 3 | T: Config<AccountId = AccountId, BlockNumber = BlockNum, Hash = Hash, Moment = Moment>, |
46 | 3 | { |
47 | 3 | TransferId::new::<T>(&self.transfer.blockchain, &self.transfer.tx_id) |
48 | 3 | } |
49 | | } |
50 | | |
51 | | impl UnverifiedCollectedCoins { |
52 | 7 | pub fn verify_ocw<T>(&self) -> VerificationResult<T::Balance> |
53 | 7 | where |
54 | 7 | T: Config, |
55 | 7 | { |
56 | 7 | crate::Pallet::<T>::verify_collect_coins_ocw(self) |
57 | 7 | } |
58 | | |
59 | 5 | pub fn into_output<T>(self, amount: T::Balance) -> CollectedCoins<T::Hash, T::Balance> |
60 | 5 | where |
61 | 5 | T: Config, |
62 | 5 | { |
63 | 5 | let Self { to, tx_id, contract: GCreContract { chain, .. } } = self; |
64 | 5 | let to = crate::AddressId::new::<T>(&chain, to.as_slice()); |
65 | 5 | CollectedCoins { amount, to, tx_id } |
66 | 5 | } |
67 | | |
68 | 5 | pub fn to_id<T>(&self) -> CollectedCoinsId<T::Hash> |
69 | 5 | where |
70 | 5 | T: Config, |
71 | 5 | { |
72 | 5 | CollectedCoinsId::new::<T>(&self.contract.chain, &self.tx_id) |
73 | 5 | } |
74 | | } |
75 | | |
76 | | impl<AccountId, BlockNum, Hash, Moment> Task<AccountId, BlockNum, Hash, Moment> |
77 | | where |
78 | | Moment: UniqueSaturatedInto<u64> + UniqueSaturatedFrom<u64>, |
79 | | BlockNum: UniqueSaturatedInto<u64>, |
80 | | { |
81 | 15 | pub fn verify_ocw<T>( |
82 | 15 | self, |
83 | 15 | ) -> Result<TaskData<AccountId, T::Balance, BlockNum, Hash, Moment>, (Self, OffchainError)> |
84 | 15 | where |
85 | 15 | T: Config<AccountId = AccountId, BlockNumber = BlockNum, Hash = Hash, Moment = Moment>, |
86 | 15 | { |
87 | 15 | match self { |
88 | 8 | Task::VerifyTransfer(transfer) => match transfer.verify_ocw::<T>() { |
89 | 3 | Ok(data) => Ok(TaskData::VerifyTransfer(transfer, data)), |
90 | 5 | Err(e) => Err((transfer.into(), e)), |
91 | | }, |
92 | 7 | Task::CollectCoins(collect_coins) => match collect_coins.verify_ocw::<T>() { |
93 | 5 | Ok(data) => Ok(TaskData::CollectCoins(collect_coins, data)), |
94 | 2 | Err(e) => Err((collect_coins.into(), e)), |
95 | | }, |
96 | | } |
97 | 15 | } |
98 | | } |
99 | | |
100 | | impl<AccountId, Balance, BlockNum, Hash, Moment> |
101 | | TaskData<AccountId, Balance, BlockNum, Hash, Moment> |
102 | | where |
103 | | Moment: UniqueSaturatedInto<u64> + UniqueSaturatedFrom<u64>, |
104 | | BlockNum: UniqueSaturatedInto<u64>, |
105 | | { |
106 | 8 | pub fn into_output<T: Config>( |
107 | 8 | self, |
108 | 8 | ) -> TaskOutput<T::AccountId, T::Balance, T::BlockNumber, T::Hash, T::Moment> |
109 | 8 | where |
110 | 8 | T: Config< |
111 | 8 | AccountId = AccountId, |
112 | 8 | Balance = Balance, |
113 | 8 | BlockNumber = BlockNum, |
114 | 8 | Hash = Hash, |
115 | 8 | Moment = Moment, |
116 | 8 | >, |
117 | 8 | { |
118 | 8 | match self { |
119 | 3 | TaskData::VerifyTransfer(transfer, data) => { |
120 | 3 | let id = transfer.to_id::<T>(); |
121 | 3 | TaskOutput::VerifyTransfer(id, transfer.into_output::<T>(data)) |
122 | | }, |
123 | 5 | TaskData::CollectCoins(collected_coins, data) => { |
124 | 5 | let id = collected_coins.to_id::<T>(); |
125 | 5 | TaskOutput::CollectCoins(id, collected_coins.into_output::<T>(data)) |
126 | | }, |
127 | | } |
128 | 8 | } |
129 | | } |
130 | | |
131 | | mod tests; |