From e8defe89aebe2271824af6ca1d02c9cbbb9dd791 Mon Sep 17 00:00:00 2001 From: pshenmic Date: Wed, 25 Dec 2024 00:49:10 +0700 Subject: [PATCH] chore(wasm-dpp): cleanup --- .../src/document/document_factory/v0/mod.rs | 107 +------- .../specialized_document_factory/v0/mod.rs | 17 +- packages/wasm-dpp/src/document/factory.rs | 8 - .../document_transfer_transition.rs | 247 ------------------ 4 files changed, 4 insertions(+), 375 deletions(-) diff --git a/packages/rs-dpp/src/document/document_factory/v0/mod.rs b/packages/rs-dpp/src/document/document_factory/v0/mod.rs index 0280286f34..6dc531398c 100644 --- a/packages/rs-dpp/src/document/document_factory/v0/mod.rs +++ b/packages/rs-dpp/src/document/document_factory/v0/mod.rs @@ -263,25 +263,9 @@ impl DocumentFactoryV0 { nonce_counter, platform_version, ), - DocumentTransitionActionType::Transfer => Self::document_transfer_transitions( - documents - .into_iter() - .map(|(document, document_type, _)| (document, document_type)) - .collect(), - nonce_counter, - platform_version, - ), - DocumentTransitionActionType::Purchase => { - Err(ProtocolError::InvalidStateTransitionType( - "action type not accounted for Transfer".to_string(), - )) - } - DocumentTransitionActionType::UpdatePrice => Err(ProtocolError::InvalidStateTransitionType( - "action type not accounted for UpdatePrice".to_string(), + _ => Err(ProtocolError::InvalidStateTransitionType( + "action type not accounted for".to_string(), )), - DocumentTransitionActionType::IgnoreWhileBumpingRevision => Err(ProtocolError::InvalidStateTransitionType( - "action type not accounted for IgnoreWhileBumpingRevision".to_string(), - )) }) .collect::, ProtocolError>>()? .into_iter() @@ -524,93 +508,6 @@ impl DocumentFactoryV0 { // Ok(raw_transitions) } - #[cfg(feature = "state-transitions")] - fn document_transfer_transitions( - documents: Vec<(Document, DocumentTypeRef)>, - nonce_counter: &mut BTreeMap<(Identifier, Identifier), u64>, //IdentityID/ContractID -> nonce - platform_version: &PlatformVersion, - ) -> Result, ProtocolError> { - documents - .into_iter() - .map(|(mut document, document_type)| { - if !document_type.documents_mutable() { - return Err(DocumentError::TryingToReplaceImmutableDocument { - document: Box::new(document), - } - .into()); - } - if document.revision().is_none() { - return Err(DocumentError::RevisionAbsentError { - document: Box::new(document), - } - .into()); - }; - - document.increment_revision()?; - document.set_updated_at(Some(Utc::now().timestamp_millis() as TimestampMillis)); - - let recipient_owner_id = document.owner_id(); - - let nonce = nonce_counter - .entry((document.owner_id(), document_type.data_contract_id())) - .or_default(); - - let transition = DocumentTransferTransition::from_document( - document, - document_type, - *nonce, - recipient_owner_id, - platform_version, - None, - None, - )?; - - *nonce += 1; - - Ok(transition.into()) - }) - .collect() - // let mut raw_transitions = vec![]; - // for (document, document_type) in documents { - // if !document_type.documents_mutable() { - // return Err(DocumentError::TryingToReplaceImmutableDocument { - // document: Box::new(document), - // } - // .into()); - // } - // let Some(document_revision) = document.revision() else { - // return Err(DocumentError::RevisionAbsentError { - // document: Box::new(document), - // }.into()); - // }; - // let mut map = document.to_map_value()?; - // - // map.retain(|key, _| { - // !key.starts_with('$') || DOCUMENT_REPLACE_KEYS_TO_STAY.contains(&key.as_str()) - // }); - // map.insert( - // PROPERTY_ACTION.to_string(), - // Value::U8(DocumentTransitionActionType::Replace as u8), - // ); - // let new_revision = document_revision + 1; - // map.insert(PROPERTY_REVISION.to_string(), Value::U64(new_revision)); - // - // // If document have an originally set `updatedAt` - // // we should update it then - // let contains_updated_at = document_type - // .required_fields() - // .contains(PROPERTY_UPDATED_AT); - // - // if contains_updated_at { - // let now = Utc::now().timestamp_millis() as TimestampMillis; - // map.insert(PROPERTY_UPDATED_AT.to_string(), Value::U64(now)); - // } - // - // raw_transitions.push(map.into()); - // } - // Ok(raw_transitions) - } - #[cfg(feature = "state-transitions")] fn document_delete_transitions( documents: Vec<(Document, DocumentTypeRef)>, diff --git a/packages/rs-dpp/src/document/specialized_document_factory/v0/mod.rs b/packages/rs-dpp/src/document/specialized_document_factory/v0/mod.rs index 56193af6ad..e482d3822a 100644 --- a/packages/rs-dpp/src/document/specialized_document_factory/v0/mod.rs +++ b/packages/rs-dpp/src/document/specialized_document_factory/v0/mod.rs @@ -270,22 +270,9 @@ impl SpecializedDocumentFactoryV0 { nonce_counter, platform_version, ), - DocumentTransitionActionType::Transfer => { - Err(ProtocolError::InvalidStateTransitionType( - "action type not accounted for Transfer".to_string(), - )) - }, - DocumentTransitionActionType::Purchase => { - Err(ProtocolError::InvalidStateTransitionType( - "action type not accounted for Purchase".to_string(), - )) - } - DocumentTransitionActionType::UpdatePrice => Err(ProtocolError::InvalidStateTransitionType( - "action type not accounted for UpdatePrice".to_string(), + _ => Err(ProtocolError::InvalidStateTransitionType( + "action type not accounted for".to_string(), )), - DocumentTransitionActionType::IgnoreWhileBumpingRevision => Err(ProtocolError::InvalidStateTransitionType( - "action type not accounted for IgnoreWhileBumpingRevision".to_string(), - )) }) .collect::, ProtocolError>>()? .into_iter() diff --git a/packages/wasm-dpp/src/document/factory.rs b/packages/wasm-dpp/src/document/factory.rs index 038fb8db99..0b927a6080 100644 --- a/packages/wasm-dpp/src/document/factory.rs +++ b/packages/wasm-dpp/src/document/factory.rs @@ -32,7 +32,6 @@ pub struct DocumentTransitions { create: Vec, replace: Vec, delete: Vec, - transfer: Vec, } #[wasm_bindgen(js_class=DocumentTransitions)] @@ -56,11 +55,6 @@ impl DocumentTransitions { pub fn add_transition_delete(&mut self, transition: ExtendedDocumentWasm) { self.delete.push(transition) } - - #[wasm_bindgen(js_name = "addTransitionTransfer")] - pub fn add_transition_transfer(&mut self, transition: ExtendedDocumentWasm) { - self.transfer.push(transition) - } } #[wasm_bindgen(js_name = DocumentFactory)] @@ -284,12 +278,10 @@ fn extract_documents_by_action( let documents_create = extract_documents_of_action(documents, "create").with_js_error()?; let documents_replace = extract_documents_of_action(documents, "replace").with_js_error()?; let documents_delete = extract_documents_of_action(documents, "delete").with_js_error()?; - let documents_transfer = extract_documents_of_action(documents, "transfer").with_js_error()?; documents_by_action.insert(DocumentTransitionActionType::Create, documents_create); documents_by_action.insert(DocumentTransitionActionType::Replace, documents_replace); documents_by_action.insert(DocumentTransitionActionType::Delete, documents_delete); - documents_by_action.insert(DocumentTransitionActionType::Transfer, documents_transfer); Ok(documents_by_action) } diff --git a/packages/wasm-dpp/src/document/state_transition/document_batch_transition/document_transition/document_transfer_transition.rs b/packages/wasm-dpp/src/document/state_transition/document_batch_transition/document_transition/document_transfer_transition.rs index 0ebbf8e508..c9c745d409 100644 --- a/packages/wasm-dpp/src/document/state_transition/document_batch_transition/document_transition/document_transfer_transition.rs +++ b/packages/wasm-dpp/src/document/state_transition/document_batch_transition/document_transition/document_transfer_transition.rs @@ -21,254 +21,7 @@ impl From for DocumentTransferTransition { #[wasm_bindgen(js_class=DocumentTransferTransition)] impl DocumentTransferTransitionWasm { - // #[wasm_bindgen(constructor)] - // pub fn from_object( - // raw_object: JsValue, - // data_contract: &DataContractWasm, - // ) -> Result { - // let mut value = raw_object.with_serde_to_platform_value_map()?; - // let document_type_name = value - // .get_string(dpp::document::extended_document::property_names::DOCUMENT_TYPE_NAME) - // .map_err(ProtocolError::ValueError) - // .with_js_error()?; - // - // let document_type = data_contract - // .inner() - // .document_type_for_name(&document_type_name) - // .with_js_error()?; - // let identifier_paths = document_type.identifier_paths(); - // let binary_paths = document_type.binary_paths(); - // - // value - // .replace_at_paths(identifier_paths, ReplacementType::Identifier) - // .map_err(ProtocolError::ValueError) - // .with_js_error()?; - // - // value - // .replace_at_paths(binary_paths, ReplacementType::BinaryBytes) - // .map_err(ProtocolError::ValueError) - // .with_js_error()?; - // let transition = - // DocumentTransferTransition::from_value_map(value, data_contract).with_js_error()?; - // - // Ok(transition.into()) - // } - // - // #[wasm_bindgen(js_name=getAction)] - // pub fn action(&self) -> u8 { - // DocumentTransitionActionType::Transfer as u8; - // } - // - // #[wasm_bindgen(js_name=getRevision)] - // pub fn revision(&self) -> Revision { - // self.inner.revision() - // } - // - // #[wasm_bindgen(js_name=getUpdatedAt)] - // pub fn updated_at(&self) -> Option { - // self.inner - // .updated_at() - // .map(|timestamp| js_sys::Date::new(&JsValue::from_f64(timestamp as f64))) - // } - // - // #[wasm_bindgen(js_name=toObject)] - // pub fn to_object( - // &self, - // options: &JsValue, - // data_contract: DataContractWasm, - // ) -> Result { - // let document_type = data_contract - // .inner() - // .document_type_for_name(self.inner.base().document_type_name()) - // .with_js_error()?; - // let identifier_paths = document_type.identifier_paths(); - // let binary_paths = document_type.binary_paths(); - // - // to_object( - // self.inner.to_object().with_js_error()?, - // options, - // identifier_paths - // .into_iter() - // .chain(document_transfer_transition::v0::IDENTIFIER_FIELDS), - // binary_paths, - // ) - // } - // - // #[wasm_bindgen(js_name=toJSON)] - // pub fn to_json(&self) -> Result { - // let value = self.inner.to_json().with_js_error()?; - // let serializer = serde_wasm_bindgen::Serializer::json_compatible(); - // let js_value = value.serialize(&serializer)?; - // Ok(js_value) - // } - // - // // AbstractDataDocumentTransition - // #[wasm_bindgen(js_name=getData)] - // pub fn get_data(&self, data_contract: DataContractWasm) -> Result { - // let data = if let Some(ref data) = self.inner.data() { - // data - // } else { - // return Ok(JsValue::undefined()); - // }; - // - // let js_value = data.serialize(&serde_wasm_bindgen::Serializer::json_compatible())?; - // let document_type = data_contract - // .inner() - // .document_type_for_name(self.inner.base().document_type_name()) - // .with_js_error()?; - // let identifier_paths = document_type.identifier_paths(); - // let binary_paths = document_type.binary_paths(); - // - // for path in identifier_paths { - // let bytes = data - // .get_identifier_bytes_at_path(path) - // .map_err(ProtocolError::ValueError) - // .with_js_error()?; - // let id = >::from( - // Identifier::from_bytes(&bytes).unwrap(), - // ); - // lodash_set(&js_value, path, id.into()); - // } - // for path in binary_paths { - // let bytes = data - // .get_binary_bytes_at_path(path) - // .map_err(ProtocolError::ValueError) - // .with_js_error()?; - // let buffer = Buffer::from_bytes(&bytes); - // lodash_set(&js_value, path, buffer.into()); - // } - // - // Ok(js_value) - // } - // - // // AbstractDocumentTransition - // #[wasm_bindgen(js_name=getId)] - // pub fn id(&self) -> IdentifierWrapper { - // self.inner.base().id().into() - // } - // - // #[wasm_bindgen(js_name=getType)] - // pub fn document_type(&self) -> String { - // self.inner.base().document_type_name().clone() - // } - // - // #[wasm_bindgen(js_name=getDataContractId)] - // pub fn data_contract_id(&self) -> IdentifierWrapper { - // self.inner.base().data_contract_id().into() - // } - // - // #[wasm_bindgen(js_name=get)] - // pub fn get(&self, path: String) -> Result { - // let document_data = if let Some(ref data) = self.inner.data() { - // data - // } else { - // return Ok(JsValue::undefined()); - // }; - // - // let value = if let Ok(value) = document_data.get_at_path(&path) { - // value.to_owned() - // } else { - // return Ok(JsValue::undefined()); - // }; - // - // match self.get_binary_type_of_path(&path) { - // BinaryType::Buffer => { - // let bytes: Vec = serde_json::from_value( - // value - // .try_into() - // .map_err(ProtocolError::ValueError) - // .with_js_error()?, - // ) - // .unwrap(); - // let buffer = Buffer::from_bytes(&bytes); - // return Ok(buffer.into()); - // } - // BinaryType::Identifier => { - // let bytes: Vec = serde_json::from_value( - // value - // .try_into() - // .map_err(ProtocolError::ValueError) - // .with_js_error()?, - // ) - // .unwrap(); - // let id = >::from( - // Identifier::from_bytes(&bytes).unwrap(), - // ); - // return Ok(id.into()); - // } - // BinaryType::None => { - // // Do nothing. If is 'None' it means that binary may contain binary data - // // or may not captain it at all - // } - // } - // - // let json_value: JsonValue = value - // .clone() - // .try_into() - // .map_err(ProtocolError::ValueError) - // .with_js_error()?; - // let map = value - // .to_btree_ref_string_map() - // .map_err(ProtocolError::ValueError) - // .with_js_error()?; - // let js_value = json_value.serialize(&serde_wasm_bindgen::Serializer::json_compatible())?; - // let (identifier_paths, binary_paths) = self - // .inner - // .base() - // .data_contract() - // .get_identifiers_and_binary_paths(&self.inner.base().document_type_name()) - // .with_js_error()?; - // - // for property_path in identifier_paths { - // if property_path.starts_with(&path) { - // let (_, suffix) = property_path.split_at(path.len() + 1); - // - // if let Some(bytes) = map - // .get_optional_bytes_at_path(suffix) - // .map_err(ProtocolError::ValueError) - // .with_js_error()? - // { - // let id = >::from( - // Identifier::from_bytes(&bytes).unwrap(), - // ); - // lodash_set(&js_value, suffix, id.into()); - // } - // } - // } - // - // for property_path in binary_paths { - // if property_path.starts_with(&path) { - // let (_, suffix) = property_path.split_at(path.len() + 1); - // - // if let Some(bytes) = map - // .get_optional_bytes_at_path(suffix) - // .map_err(ProtocolError::ValueError) - // .with_js_error()? - // { - // let buffer = Buffer::from_bytes(&bytes); - // lodash_set(&js_value, suffix, buffer.into()); - // } - // } - // } - // - // Ok(js_value) - // } } impl DocumentTransferTransitionWasm { - // fn get_binary_type_of_path(&self, path: &String) -> BinaryType { - // let maybe_binary_properties = self - // .inner - // .get_binary_properties(&self.inner.base().document_type_name()); - // - // if let Ok(binary_properties) = maybe_binary_properties { - // if let Some(data) = binary_properties.get(path) { - // if data.is_type_of_identifier() { - // return BinaryType::Identifier; - // } - // return BinaryType::Buffer; - // } - // } - // BinaryType::None - // } }