Skip to content

Commit

Permalink
fix!: deserialization of former required field
Browse files Browse the repository at this point in the history
  • Loading branch information
shumkov committed Jun 13, 2024
1 parent 2c9368a commit c8a82a8
Show file tree
Hide file tree
Showing 9 changed files with 231 additions and 245 deletions.
112 changes: 36 additions & 76 deletions packages/rs-dpp/src/data_contract/document_type/property/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -390,15 +390,12 @@ impl DocumentPropertyType {
pub fn read_optionally_from(
&self,
buf: &mut BufReader<&[u8]>,
required: bool,
) -> Result<(Option<Value>, bool), DataContractError> {
if !required {
let marker = buf.read_u8().ok();
match marker {
None => return Ok((None, true)), // we have no more data
Some(0) => return Ok((None, false)),
_ => {}
}
let marker = buf.read_u8().ok();
match marker {
None => return Ok((None, true)), // we have no more data
Some(0) => return Ok((None, false)),
_ => {}
}
match self {
DocumentPropertyType::String(_, _) => {
Expand Down Expand Up @@ -506,7 +503,7 @@ impl DocumentPropertyType {

let read_value = field
.property_type
.read_optionally_from(&mut object_buf_reader, field.required);
.read_optionally_from(&mut object_buf_reader);

match read_value {
Ok(read_value) => {
Expand Down Expand Up @@ -534,11 +531,7 @@ impl DocumentPropertyType {
}
}

pub fn encode_value_with_size(
&self,
value: Value,
required: bool,
) -> Result<Vec<u8>, ProtocolError> {
pub fn encode_value_with_size(&self, value: Value) -> Result<Vec<u8>, ProtocolError> {
if value.is_null() {
return Ok(vec![]);
}
Expand All @@ -556,38 +549,29 @@ impl DocumentPropertyType {
DocumentPropertyType::Date => {
let value_as_f64 = value.into_float().map_err(ProtocolError::ValueError)?;
let mut value_bytes = value_as_f64.to_be_bytes().to_vec();
if required {
Ok(value_bytes)
} else {
// if the value wasn't required we need to add a byte to prove it existed
let mut r_vec = vec![255u8];
r_vec.append(&mut value_bytes);
Ok(r_vec)
}

// we need to add a byte to prove it existed
let mut r_vec = vec![255u8];
r_vec.append(&mut value_bytes);
Ok(r_vec)
}
DocumentPropertyType::Integer => {
let value_as_i64: i64 = value.into_integer().map_err(ProtocolError::ValueError)?;
let mut value_bytes = value_as_i64.to_be_bytes().to_vec();
if required {
Ok(value_bytes)
} else {
// if the value wasn't required we need to add a byte to prove it existed
let mut r_vec = vec![255u8];
r_vec.append(&mut value_bytes);
Ok(r_vec)
}

// we need to add a byte to prove it existed
let mut r_vec = vec![255u8];
r_vec.append(&mut value_bytes);
Ok(r_vec)
}
DocumentPropertyType::Number => {
let value_as_f64 = value.into_float().map_err(ProtocolError::ValueError)?;
let mut value_bytes = value_as_f64.to_be_bytes().to_vec();
if required {
Ok(value_bytes)
} else {
// if the value wasn't required we need to add a byte to prove it existed
let mut r_vec = vec![255u8];
r_vec.append(&mut value_bytes);
Ok(r_vec)
}

// we need to add a byte to prove it existed
let mut r_vec = vec![255u8];
r_vec.append(&mut value_bytes);
Ok(r_vec)
}
DocumentPropertyType::ByteArray(_, _) => {
let mut bytes = value.into_binary_bytes()?;
Expand Down Expand Up @@ -619,19 +603,12 @@ impl DocumentPropertyType {
let mut r_vec = vec![];
inner_fields.iter().try_for_each(|(key, field)| {
if let Some(value) = value_map.remove(key) {
let mut serialized_value = field
.property_type
.encode_value_with_size(value, field.required)?;
let mut serialized_value =
field.property_type.encode_value_with_size(value)?;
r_vec.append(&mut serialized_value);
Ok(())
} else if field.required {
Err(ProtocolError::DataContractError(
DataContractError::MissingRequiredKey(
"a required field is not present".to_string(),
),
))
Ok::<(), ProtocolError>(())
} else {
// We don't have something that wasn't required
// We don't have something
r_vec.push(0);
Ok(())
}
Expand Down Expand Up @@ -666,11 +643,7 @@ impl DocumentPropertyType {
}
}

pub fn encode_value_ref_with_size(
&self,
value: &Value,
required: bool,
) -> Result<Vec<u8>, ProtocolError> {
pub fn encode_value_ref_with_size(&self, value: &Value) -> Result<Vec<u8>, ProtocolError> {
if value.is_null() {
return Ok(vec![]);
}
Expand All @@ -687,14 +660,11 @@ impl DocumentPropertyType {
DocumentPropertyType::Date => {
let value_as_f64 = value.to_float().map_err(ProtocolError::ValueError)?;
let mut value_bytes = value_as_f64.to_be_bytes().to_vec();
if required {
Ok(value_bytes)
} else {
// if the value wasn't required we need to add a byte to prove it existed
let mut r_vec = vec![255u8];
r_vec.append(&mut value_bytes);
Ok(r_vec)
}

// if the value wasn't required we need to add a byte to prove it existed
let mut r_vec = vec![255u8];
r_vec.append(&mut value_bytes);
Ok(r_vec)
}
DocumentPropertyType::Integer => {
let value_as_i64: i64 = value.to_integer().map_err(ProtocolError::ValueError)?;
Expand Down Expand Up @@ -732,22 +702,12 @@ impl DocumentPropertyType {
let mut r_vec = vec![];
inner_fields.iter().try_for_each(|(key, field)| {
if let Some(value) = value_map.get(key) {
if !field.required {
r_vec.push(1);
}
let value = field
.property_type
.encode_value_ref_with_size(value, field.required)?;
r_vec.push(1);
let value = field.property_type.encode_value_ref_with_size(value)?;
r_vec.extend(value.as_slice());
Ok(())
} else if field.required {
Err(ProtocolError::DataContractError(
DataContractError::MissingRequiredKey(
"a required field is not present".to_string(),
),
))
Ok::<(), ProtocolError>(())
} else {
// We don't have something that wasn't required
// We don't have something
r_vec.push(0);
Ok(())
}
Expand Down
Loading

0 comments on commit c8a82a8

Please sign in to comment.