/
mod.rs
444 lines (391 loc) · 16.7 KB
/
mod.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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
use dpp::consensus::state::data_trigger::data_trigger_condition_error::DataTriggerConditionError;
use dpp::data_contract::accessors::v0::DataContractV0Getters;
use dpp::data_contracts::dpns_contract::document_types::domain::properties::PARENT_DOMAIN_NAME;
///! The `dpns_triggers` module contains data triggers specific to the DPNS data contract.
use dpp::util::hash::hash;
use std::collections::BTreeMap;
use crate::error::execution::ExecutionError;
use crate::error::Error;
use crate::execution::validation::state_transition::documents_batch::data_triggers::{
DataTriggerExecutionContext, DataTriggerExecutionResult,
};
use dpp::document::DocumentV0Getters;
use dpp::platform_value::btreemap_extensions::{BTreeValueMapHelper, BTreeValueMapPathHelper};
use dpp::platform_value::Value;
use dpp::ProtocolError;
use drive::state_transition_action::document::documents_batch::document_transition::document_base_transition_action::DocumentBaseTransitionActionAccessorsV0;
use drive::state_transition_action::document::documents_batch::document_transition::document_create_transition_action::DocumentCreateTransitionActionAccessorsV0;
use drive::state_transition_action::document::documents_batch::document_transition::DocumentTransitionAction;
use dpp::system_data_contracts::dpns_contract;
use dpp::system_data_contracts::dpns_contract::document_types::domain::properties::{ALLOW_SUBDOMAINS, DASH_ALIAS_IDENTITY_ID, DASH_UNIQUE_IDENTITY_ID, LABEL, NORMALIZED_LABEL, NORMALIZED_PARENT_DOMAIN_NAME, PREORDER_SALT, RECORDS};
use dpp::util::strings::convert_to_homograph_safe_chars;
use dpp::version::PlatformVersion;
use drive::drive::document::query::QueryDocumentsOutcomeV0Methods;
use drive::query::{DriveQuery, InternalClauses, WhereClause, WhereOperator};
use crate::execution::types::state_transition_execution_context::StateTransitionExecutionContextMethodsV0;
pub const MAX_PRINTABLE_DOMAIN_NAME_LENGTH: usize = 253;
/// Creates a data trigger for handling domain documents.
///
/// The trigger is executed whenever a new domain document is created on the blockchain.
/// It performs various actions depending on the state of the document and the context in which it was created.
///
/// # Arguments
///
/// * `document_transition` - A reference to the document transition that triggered the data trigger.
/// * `context` - A reference to the data trigger execution context.
/// * `dpns_contract::OWNER_ID` - An optional identifier for the top-level identity associated with the domain
/// document (if one exists).
///
/// # Returns
///
/// A `DataTriggerExecutionResult` indicating the success or failure of the trigger execution.
pub fn create_domain_data_trigger_v0(
document_transition: &DocumentTransitionAction,
context: &DataTriggerExecutionContext<'_>,
platform_version: &PlatformVersion,
) -> Result<DataTriggerExecutionResult, Error> {
let data_contract_fetch_info = document_transition.base().data_contract_fetch_info();
let data_contract = &data_contract_fetch_info.contract;
let is_dry_run = context.state_transition_execution_context.in_dry_run();
let document_create_transition = match document_transition {
DocumentTransitionAction::CreateAction(d) => d,
_ => {
return Err(Error::Execution(ExecutionError::DataTriggerExecutionError(
format!(
"the Document Transition {} isn't 'CREATE",
document_transition.base().id()
),
)))
}
};
let data = document_create_transition.data();
let owner_id = context.owner_id;
let label = data.get_string(LABEL).map_err(ProtocolError::ValueError)?;
let normalized_label = data
.get_str(NORMALIZED_LABEL)
.map_err(ProtocolError::ValueError)?;
let parent_domain_name = data
.get_string(PARENT_DOMAIN_NAME)
.map_err(ProtocolError::ValueError)?;
let normalized_parent_domain_name = data
.get_string(NORMALIZED_PARENT_DOMAIN_NAME)
.map_err(ProtocolError::ValueError)?;
let preorder_salt = data
.get_hash256_bytes(PREORDER_SALT)
.map_err(ProtocolError::ValueError)?;
let records = data
.get(RECORDS)
.ok_or(ExecutionError::DataTriggerExecutionError(format!(
"property '{}' doesn't exist",
RECORDS
)))?
.to_btree_ref_string_map()
.map_err(ProtocolError::ValueError)?;
let rule_allow_subdomains = data
.get_bool_at_path(ALLOW_SUBDOMAINS)
.map_err(ProtocolError::ValueError)?;
let full_domain_name = if parent_domain_name.is_empty() {
label.to_string()
} else {
format!("{normalized_label}.{parent_domain_name}")
};
let mut result = DataTriggerExecutionResult::default();
if !is_dry_run {
if full_domain_name.len() > MAX_PRINTABLE_DOMAIN_NAME_LENGTH {
let err = DataTriggerConditionError::new(
data_contract.id(),
document_transition.base().id(),
format!(
"Full domain name length can not be more than {} characters long but got {}",
MAX_PRINTABLE_DOMAIN_NAME_LENGTH,
full_domain_name.len()
),
);
result.add_error(err)
}
if normalized_label != convert_to_homograph_safe_chars(label.as_str()) {
let err = DataTriggerConditionError::new(
data_contract.id(),
document_transition.base().id(),
format!(
"Normalized label doesn't match label: {} != {}",
normalized_label, label
),
);
result.add_error(err);
}
if normalized_parent_domain_name
!= convert_to_homograph_safe_chars(parent_domain_name.as_str())
{
let err = DataTriggerConditionError::new(
data_contract.id(),
document_transition.base().id(),
format!(
"Normalized parent domain name doesn't match parent domain name: {} != {}",
normalized_parent_domain_name, parent_domain_name
),
);
result.add_error(err);
}
if let Some(id) = records
.get_optional_identifier(DASH_UNIQUE_IDENTITY_ID)
.map_err(ProtocolError::ValueError)?
{
if id != owner_id {
let err = DataTriggerConditionError::new(
data_contract.id(),
document_transition.base().id(),
format!(
"ownerId {} doesn't match {} {}",
owner_id, DASH_UNIQUE_IDENTITY_ID, id
),
);
result.add_error(err);
}
}
if let Some(id) = records
.get_optional_identifier(DASH_ALIAS_IDENTITY_ID)
.map_err(ProtocolError::ValueError)?
{
if id != owner_id {
let err = DataTriggerConditionError::new(
data_contract.id(),
document_transition.base().id(),
format!(
"ownerId {} doesn't match {} {}",
owner_id, DASH_ALIAS_IDENTITY_ID, id
),
);
result.add_error(err);
}
}
if normalized_parent_domain_name.is_empty() && context.owner_id != &dpns_contract::OWNER_ID
{
let err = DataTriggerConditionError::new(
data_contract.id(),
document_transition.base().id(),
"Can't create top level domain for this identity".to_string(),
);
result.add_error(err);
}
}
if !normalized_parent_domain_name.is_empty() {
//? What is the `normalized_parent_name`. Are we sure the content is a valid dot-separated data
let mut parent_domain_segments = normalized_parent_domain_name.split('.');
let parent_domain_label = parent_domain_segments.next().unwrap().to_string();
let grand_parent_domain_name = parent_domain_segments.collect::<Vec<&str>>().join(".");
let document_type = data_contract.document_type_for_name(
document_create_transition
.base()
.document_type_name()
.as_str(),
)?;
let drive_query = DriveQuery {
contract: data_contract,
document_type,
internal_clauses: InternalClauses {
primary_key_in_clause: None,
primary_key_equal_clause: None,
in_clause: None,
range_clause: None,
equal_clauses: BTreeMap::from([
(
"normalizedParentDomainName".to_string(),
WhereClause {
field: "normalizedParentDomainName".to_string(),
operator: WhereOperator::Equal,
value: Value::Text(grand_parent_domain_name),
},
),
(
"normalizedLabel".to_string(),
WhereClause {
field: "normalizedLabel".to_string(),
operator: WhereOperator::Equal,
value: Value::Text(parent_domain_label),
},
),
]),
},
offset: None,
limit: None,
order_by: Default::default(),
start_at: None,
start_at_included: false,
block_time_ms: None,
};
let documents = context
.platform
.drive
.query_documents(
drive_query,
None,
is_dry_run,
context.transaction,
Some(platform_version.protocol_version),
)?
.documents_owned();
if !is_dry_run {
if documents.is_empty() {
let err = DataTriggerConditionError::new(
data_contract.id(),
document_transition.base().id(),
"Parent domain is not present".to_string(),
);
result.add_error(err);
return Ok(result);
}
let parent_domain = &documents[0];
if rule_allow_subdomains {
let err = DataTriggerConditionError::new(
data_contract.id(),
document_transition.base().id(),
"Allowing subdomains registration is forbidden for this domain".to_string(),
);
result.add_error(err);
return Ok(result);
}
if (!parent_domain
.properties()
.get_bool_at_path(ALLOW_SUBDOMAINS)
.map_err(ProtocolError::ValueError)?)
&& context.owner_id != &parent_domain.owner_id()
{
let err = DataTriggerConditionError::new(
data_contract.id(),
document_transition.base().id(),
"The subdomain can be created only by the parent domain owner".to_string(),
);
result.add_error(err);
return Ok(result);
}
}
}
let mut salted_domain_buffer: Vec<u8> = vec![];
salted_domain_buffer.extend(preorder_salt);
salted_domain_buffer.extend(full_domain_name.as_bytes());
let salted_domain_hash = hash(salted_domain_buffer);
let document_type = data_contract.document_type_for_name("preorder")?;
let drive_query = DriveQuery {
contract: data_contract,
document_type,
internal_clauses: InternalClauses {
primary_key_in_clause: None,
primary_key_equal_clause: None,
in_clause: None,
range_clause: None,
equal_clauses: BTreeMap::from([(
"saltedDomainHash".to_string(),
WhereClause {
field: "saltedDomainHash".to_string(),
operator: WhereOperator::Equal,
value: Value::Bytes32(salted_domain_hash),
},
)]),
},
offset: None,
limit: None,
order_by: Default::default(),
start_at: None,
start_at_included: false,
block_time_ms: None,
};
let preorder_documents = context
.platform
.drive
.query_documents(
drive_query,
None,
is_dry_run,
context.transaction,
Some(platform_version.protocol_version),
)?
.documents_owned();
if is_dry_run {
return Ok(result);
}
if preorder_documents.is_empty() {
let err = DataTriggerConditionError::new(
data_contract.id(),
document_transition.base().id(),
"preorderDocument was not found".to_string(),
);
result.add_error(err)
}
Ok(result)
}
#[cfg(test)]
mod test {
use std::sync::Arc;
use dpp::platform_value::Bytes32;
use drive::state_transition_action::document::documents_batch::document_transition::document_create_transition_action::DocumentCreateTransitionAction;
use drive::state_transition_action::document::documents_batch::document_transition::DocumentTransitionActionType;
use dpp::tests::fixtures::{get_document_transitions_fixture, get_dpns_data_contract_fixture, get_dpns_parent_document_fixture, ParentDocumentOptions};
use dpp::tests::utils::generate_random_identifier_struct;
use dpp::version::{DefaultForPlatformVersion};
use drive::drive::contract::DataContractFetchInfo;
use crate::execution::types::state_transition_execution_context::{StateTransitionExecutionContext, StateTransitionExecutionContextMethodsV0};
use crate::platform_types::platform::PlatformStateRef;
use crate::platform_types::platform_state::v0::PlatformStateV0Methods;
use crate::test::helpers::setup::TestPlatformBuilder;
use super::*;
#[test]
fn should_return_execution_result_on_dry_run() {
let platform = TestPlatformBuilder::new()
.build_with_mock_rpc()
.set_initial_state_structure();
let state_read_guard = platform.state.read().unwrap();
let platform_ref = PlatformStateRef {
drive: &platform.drive,
state: &state_read_guard,
config: &platform.config,
};
let platform_version = state_read_guard
.current_platform_version()
.expect("should return a platform version");
let mut transition_execution_context =
StateTransitionExecutionContext::default_for_platform_version(platform_version)
.unwrap();
let owner_id = generate_random_identifier_struct();
let document = get_dpns_parent_document_fixture(
ParentDocumentOptions {
owner_id,
..Default::default()
},
state_read_guard.current_protocol_version_in_consensus(),
);
let data_contract = get_dpns_data_contract_fixture(
Some(owner_id),
state_read_guard.current_protocol_version_in_consensus(),
)
.data_contract_owned();
let document_type = data_contract
.document_type_for_name("domain")
.expect("expected to get domain document type");
let transitions = get_document_transitions_fixture([(
DocumentTransitionActionType::Create,
vec![(document, document_type, Bytes32::default())],
)]);
let first_transition = transitions.get(0).expect("transition should be present");
let document_create_transition = first_transition
.as_transition_create()
.expect("expected a document create transition");
transition_execution_context.enable_dry_run();
let data_trigger_context = DataTriggerExecutionContext {
platform: &platform_ref,
owner_id: &owner_id,
state_transition_execution_context: &transition_execution_context,
transaction: None,
};
let result = create_domain_data_trigger_v0(
&DocumentCreateTransitionAction::from_document_borrowed_create_transition_with_contract_lookup(
document_create_transition,|_identifier| {
Ok(Arc::new(DataContractFetchInfo::dpns_contract_fixture(platform_version.protocol_version)))
}).expect("expected to create action").into(),
&data_trigger_context,
platform_version,
)
.expect("the execution result should be returned");
assert!(result.is_valid());
}
}