-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathlib.rs
executable file
·242 lines (205 loc) · 7.78 KB
/
lib.rs
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
#![cfg_attr(not(feature = "std"), no_std)]
#[ink::contract]
mod greeting {
use ink::prelude::string::String;
use ink::prelude::vec::Vec;
use ink::storage::Mapping;
use ink_sdk::{cross_chain_helper, CrossChainSQoS, MultiDestContracts, Ownable};
use payload::message_define::{IContent, IContext, IRequestMessage, ISQoS};
use payload::message_protocol::{MessagePayload, MsgDetail};
#[derive(::scale::Encode, ::scale::Decode, Debug, PartialEq, Eq, Copy, Clone)]
#[cfg_attr(feature = "std", derive(scale_info::TypeInfo))]
pub enum Error {
MethodNotRegisterd,
}
/// Defines the storage of your contract.
/// Add new fields to the below struct in order
/// to add new static storage fields to your contract.
#[ink(storage)]
// #[derive(SpreadAllocate)]
pub struct Greeting {
/// Account id of owner
owner: Option<AccountId>,
cross_chain_contract: Option<AccountId>,
ret: Mapping<(String, u128), String>,
dest_contract_map: Mapping<(String, String), (Vec<u8>, Vec<u8>)>,
}
/// We use `CrossChainBase` here, to be able to use the sdk functionalities.
impl cross_chain_helper::CrossChainBase for Greeting {
fn get_cross_chain_contract_address(&self) -> AccountId {
self.cross_chain_contract.unwrap()
}
}
/// We need access control.
impl Ownable for Greeting {
/// Returns the account id of the current owner
#[ink(message)]
fn owner(&self) -> Option<AccountId> {
self.owner
}
/// Renounces ownership of the contract
#[ink(message)]
fn renounce_ownership(&mut self) -> Result<(), u8> {
self.only_owner()?;
self.owner = None;
Ok(())
}
/// Transfer ownership to a new account id
#[ink(message)]
fn transfer_ownership(&mut self, new_owner: AccountId) -> Result<(), u8> {
self.only_owner()?;
self.owner = Some(new_owner);
Ok(())
}
}
/// We use `MultiDestContracts` of SDK here, to be able to send messages to multi chains.
impl MultiDestContracts for Greeting {
#[ink(message)]
fn get_dest_contract_info(
&self,
chain_name: String,
action: String,
) -> Option<(Vec<u8>, Vec<u8>)> {
self.dest_contract_map.get((chain_name, action))
}
#[ink(message)]
fn register_dest_contract(
&mut self,
chain_name: String,
action: String,
contract: Vec<u8>,
dest_action: Vec<u8>,
) -> Result<(), u8> {
self.only_owner()?;
self.dest_contract_map
.insert((chain_name, action), &(contract, dest_action));
Ok(())
}
}
/// We use `CrossChainSQoS` here, because
impl CrossChainSQoS for Greeting {
/// Inserts one SQoS item.
/// If the item exists, it will be replaced.
#[ink(message)]
fn set_sqos(&mut self, sqos_item: ISQoS) {
// self.only_owner()?;
let account_id = Self::env().account_id();
cross_chain_helper::set_sqos(self, sqos_item, account_id);
}
/// Removes one SQoS item.
#[ink(message)]
fn remove_sqos(&mut self) {
// self.only_owner()?;
let account_id = Self::env().account_id();
if let Some(_) = cross_chain_helper::get_sqos(self, account_id) {
cross_chain_helper::remove_sqos(self, account_id);
}
}
/// Returns SQoS items
#[ink(message)]
fn get_sqos(&self) -> Option<ISQoS> {
let account_id = Self::env().account_id();
cross_chain_helper::get_sqos(self, account_id)
}
}
impl Greeting {
#[ink(constructor)]
pub fn new() -> Self {
Self {
owner: Some(Self::env().caller()),
cross_chain_contract: None,
ret: Default::default(),
dest_contract_map: Default::default(),
}
}
/// Initializes the contract with the specified chain name.
// fn new_init(&mut self) {
// let caller = Self::env().caller();
// self.owner = Some(caller);
// }
/// Sets cross-chain contract address
#[ink(message)]
pub fn set_cross_chain_contract(&mut self, contract: AccountId) {
// self.only_owner()?;
self.cross_chain_contract = Some(contract);
// Ok(())
}
#[ink(message)]
pub fn clear_ret(&mut self, chain_name: String, id: u128) -> Result<(), u8>{
self.only_owner()?;
self.ret.remove(&(chain_name, id));
Ok(())
}
/// If caller is the owner of the contract
fn only_owner(&self) -> Result<(), u8> {
let caller = self.env().caller();
if self.owner.unwrap() != caller {
return Err(1);
}
Ok(())
}
/// Sends greeting to another chain
#[ink(message)]
pub fn send_greeting(
&mut self,
chain_name: String,
greeting: Vec<String>,
) -> Result<(), Error> {
let dest = self
.get_dest_contract_info(
chain_name.clone(),
String::try_from("receive_greeting").unwrap(),
)
.ok_or(Error::MethodNotRegisterd)?;
let contract = dest.0;
let action = dest.1;
let mut msg_payload = MessagePayload::new();
msg_payload.push_item(
String::try_from("greeting").unwrap(),
MsgDetail::InkStringArray(greeting.clone()),
);
let data = msg_payload.to_bytes();
let sqos = Vec::<ISQoS>::new();
// sqos.push(ISQoS::new(ISQoSType::Reveal, Vec::new()));
let content = IContent::new(contract, action, data);
let message = IRequestMessage::new(chain_name, sqos, content);
cross_chain_helper::cross_chain_send_message(self, message);
Ok(())
}
/// Receives greeting from another chain
#[ink(message)]
pub fn receive_greeting(&mut self, payload: MessagePayload) -> String {
let item = payload
.get_item(String::try_from("greeting").unwrap())
.unwrap();
// let param: Vec<String> = scale::Decode::decode(&mut item.v.as_slice()).unwrap();
let param = item.in_to::<Vec<String>>();
let context: IContext = cross_chain_helper::get_context(self).unwrap();
// let payload
let mut s = String::new();
s = s + &ink::prelude::format!("{:?}", param);
self.ret.insert((context.from_chain, context.id), &s);
s
}
/// Receives message from another chain
#[ink(message)]
pub fn get_ret(&self, key: (String, u128)) -> String {
self.ret.get(key).unwrap_or(String::from("No value"))
}
}
/// Unit tests in Rust are normally defined within such a `#[cfg(test)]`
/// module and test functions are marked with a `#[test]` attribute.
/// The below code is technically just normal Rust code.
#[cfg(test)]
mod tests {
/// Imports all the definitions from the outer scope so we can use them here.
use super::*;
/// We test if set_cross_chain_contract works.
#[ink::test]
fn set_cross_chain_contract_works() {
let mut locker = Greeting::new();
let contract_id = ink::env::test::callee::<ink::env::DefaultEnvironment>();
locker.set_cross_chain_contract(contract_id);
}
}
}