lib.rs 9.45 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
// Copyright 2019-2020 Parity Technologies (UK) Ltd.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#![cfg_attr(not(feature = "std"), no_std)]

17
pub use self::dns::DomainNameService;
18
19
20
21
use ink_lang as ink;

#[ink::contract(version = "0.1.0")]
mod dns {
22
23
24
25
26
    #[cfg(not(feature = "ink-as-dependency"))]
    use ink_core::storage2::{
        collections::HashMap as StorageHashMap,
        lazy::Lazy,
    };
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

    /// Emitted whenever a new name is being registered.
    #[ink(event)]
    struct Register {
        #[ink(topic)]
        name: Hash,
        #[ink(topic)]
        from: AccountId,
    }

    /// Emitted whenever an address changes.
    #[ink(event)]
    struct SetAddress {
        #[ink(topic)]
        name: Hash,
        from: AccountId,
        #[ink(topic)]
        old_address: Option<AccountId>,
        #[ink(topic)]
        new_address: AccountId,
    }

    /// Emitted whenver a name is being transferred.
    #[ink(event)]
    struct Transfer {
        #[ink(topic)]
        name: Hash,
        from: AccountId,
        #[ink(topic)]
        old_owner: Option<AccountId>,
        #[ink(topic)]
        new_owner: AccountId,
    }

    /// Domain name service contract inspired by ChainX's [blog post]
    /// (https://medium.com/@chainx_org/secure-and-decentralized-polkadot-domain-name-system-e06c35c2a48d).
    ///
    /// # Note
    ///
    /// This is a port from the blog post's ink! 1.0 based version of the contract
    /// to ink! 2.0.
    ///
    /// # Description
    ///
    /// The main function of this contract is domain name resolution which
    /// refers to the retrieval of numeric values corresponding to readable
    /// and easily memorable names such as “polka.dot” which can be used
    /// to facilitate transfers, voting and dapp-related operations instead
    /// of resorting to long IP addresses that are hard to remember.
    #[ink(storage)]
77
    #[derive(Default)]
78
79
    struct DomainNameService {
        /// A hashmap to store all name to addresses mapping.
80
        name_to_address: StorageHashMap<Hash, AccountId>,
81
        /// A hashmap to store all name to owners mapping.
82
        name_to_owner: StorageHashMap<Hash, AccountId>,
83
        /// The default address.
84
        default_address: Lazy<AccountId>,
85
86
87
88
    }

    /// Errors that can occur upon calling this contract.
    #[derive(Debug, PartialEq, Eq, scale::Encode, scale::Decode)]
89
    #[cfg_attr(feature = "std", derive(::scale_info::TypeInfo))]
90
91
92
93
94
95
96
97
98
99
100
101
102
    pub enum Error {
        /// Returned if the name already exists upon registration.
        NameAlreadyExists,
        /// Returned if caller is not owner while required to.
        CallerIsNotOwner,
    }

    /// Type alias for the contract's result type.
    pub type Result<T> = core::result::Result<T, Error>;

    impl DomainNameService {
        /// Creates a new domain name service contract.
        #[ink(constructor)]
103
104
        fn new() -> Self {
            Default::default()
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
        }

        /// Register specific name with caller as owner.
        #[ink(message)]
        fn register(&mut self, name: Hash) -> Result<()> {
            let caller = self.env().caller();
            if self.is_name_assigned(name) {
                return Err(Error::NameAlreadyExists)
            }
            self.name_to_owner.insert(name, caller);
            self.env().emit_event(Register { name, from: caller });
            Ok(())
        }

        /// Set address for specific name.
        #[ink(message)]
        fn set_address(&mut self, name: Hash, new_address: AccountId) -> Result<()> {
            let caller = self.env().caller();
            let owner = self.get_owner_or_default(name);
            if caller != owner {
                return Err(Error::CallerIsNotOwner)
            }
            let old_address = self.name_to_address.insert(name, new_address);
            self.env().emit_event(SetAddress {
                name,
                from: caller,
                old_address,
                new_address,
            });
            Ok(())
        }

        /// Transfer owner to another address.
        #[ink(message)]
        fn transfer(&mut self, name: Hash, to: AccountId) -> Result<()> {
            let caller = self.env().caller();
            let owner = self.get_owner_or_default(name);
            if caller != owner {
                return Err(Error::CallerIsNotOwner)
            }
            let old_owner = self.name_to_owner.insert(name, to);
            self.env().emit_event(Transfer {
                name,
                from: caller,
                old_owner,
                new_owner: to,
            });
            Ok(())
        }

        /// Get address for specific name.
        #[ink(message)]
        fn get_address(&self, name: Hash) -> AccountId {
            self.get_address_or_default(name)
        }

        /// Returns `true` if the name already assigned.
        #[ink(message)]
        fn is_name_assigned(&self, name: Hash) -> bool {
            self.name_to_owner.get(&name).is_some()
        }

        /// Returns the owner given the hash or the default address.
        fn get_owner_or_default(&self, name: Hash) -> AccountId {
            *self
                .name_to_owner
                .get(&name)
                .unwrap_or(&*self.default_address)
        }

        /// Returns the address given the hash or the default address.
        fn get_address_or_default(&self, name: Hash) -> AccountId {
            *self
                .name_to_address
                .get(&name)
                .unwrap_or(&*self.default_address)
        }
    }

    #[cfg(test)]
    mod tests {
        use super::*;
        use ink_core::env;

189
190
191
192
193
194
195
196
197
198
199
200
        /// Executes the given test through the off-chain environment.
        fn run_test<F>(test_fn: F)
        where
            F: FnOnce(),
        {
            env::test::run_test::<env::DefaultEnvTypes, _>(|_| {
                test_fn();
                Ok(())
            })
            .unwrap()
        }

201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
        const DEFAULT_CALLEE_HASH: [u8; 32] = [0x07; 32];
        const DEFAULT_ENDOWMENT: Balance = 1_000_000;
        const DEFAULT_GAS_LIMIT: Balance = 1_000_000;

        fn default_accounts() -> env::test::DefaultAccounts<env::DefaultEnvTypes> {
            env::test::default_accounts::<env::DefaultEnvTypes>()
                .expect("off-chain environment should have been initialized already")
        }

        fn set_next_caller(caller: AccountId) {
            env::test::push_execution_context::<env::DefaultEnvTypes>(
                caller,
                AccountId::from(DEFAULT_CALLEE_HASH),
                DEFAULT_ENDOWMENT,
                DEFAULT_GAS_LIMIT,
216
                env::call::CallData::new(env::call::Selector::new([0x00; 4])),
217
218
219
220
221
            )
        }

        #[test]
        fn register_works() {
222
223
224
            run_test(|| {
                let default_accounts = default_accounts();
                let name = Hash::from([0x99; 32]);
225

226
227
                set_next_caller(default_accounts.alice);
                let mut contract = DomainNameService::new();
228

229
230
231
                assert_eq!(contract.register(name), Ok(()));
                assert_eq!(contract.register(name), Err(Error::NameAlreadyExists));
            })
232
233
234
235
        }

        #[test]
        fn set_address_works() {
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
            run_test(|| {
                let accounts = default_accounts();
                let name = Hash::from([0x99; 32]);

                set_next_caller(accounts.alice);

                let mut contract = DomainNameService::new();
                assert_eq!(contract.register(name), Ok(()));

                // Caller is not owner, `set_address` should fail.
                set_next_caller(accounts.bob);
                assert_eq!(
                    contract.set_address(name, accounts.bob),
                    Err(Error::CallerIsNotOwner)
                );

                // caller is owner, set_address will be successful
                set_next_caller(accounts.alice);
                assert_eq!(contract.set_address(name, accounts.bob), Ok(()));
                assert_eq!(contract.get_address(name), accounts.bob);
            })
257
258
259
260
        }

        #[test]
        fn transfer_works() {
261
262
263
            run_test(|| {
                let accounts = default_accounts();
                let name = Hash::from([0x99; 32]);
264

265
                set_next_caller(accounts.alice);
266

267
268
                let mut contract = DomainNameService::new();
                assert_eq!(contract.register(name), Ok(()));
269

270
271
                // Test transfer of owner.
                assert_eq!(contract.transfer(name, accounts.bob), Ok(()));
272

273
274
275
276
277
                // Owner is bob, alice `set_address` should fail.
                assert_eq!(
                    contract.set_address(name, accounts.bob),
                    Err(Error::CallerIsNotOwner)
                );
278

279
280
281
282
283
                set_next_caller(accounts.bob);
                // Now owner is bob, `set_address` should be successful.
                assert_eq!(contract.set_address(name, accounts.bob), Ok(()));
                assert_eq!(contract.get_address(name), accounts.bob);
            })
284
285
286
        }
    }
}