Skip to content

Commit

Permalink
improve uuid
Browse files Browse the repository at this point in the history
  • Loading branch information
loloicci committed Apr 17, 2024
1 parent 7bb5350 commit 4cb5358
Showing 1 changed file with 8 additions and 14 deletions.
22 changes: 8 additions & 14 deletions packages/std/src/uuid.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,13 @@ pub fn new_uuid(env: &Env, storage: &mut dyn Storage, api: &dyn Api) -> StdResul
None => 0,
};
let next_seq_num: u16 = seq_num.wrapping_add(1);

let uuid_name = format!("{} {} {}", env.contract.address, env.block.height, seq_num);
let uuid_name = &[env.contract.address.as_bytes(), &env.block.height.to_be_bytes(), &seq_num.to_be_bytes()].concat();
storage.set(CONTRACT_UUID_SEQ_NUM_KEY, &(to_vec(&next_seq_num).unwrap()));

Uuid::new_v5(
api,
&Uuid(raw_uuid::Uuid::NAMESPACE_OID),
uuid_name.as_bytes(),
uuid_name
)
}

Expand Down Expand Up @@ -90,17 +89,12 @@ mod tests {
let api = MockApi::default();
let mut storage = MockStorage::new();

let uuid = new_uuid(&env, &mut storage, &api).unwrap();
let uuid1 = new_uuid(&env, &mut storage, &api).unwrap();
let uuid2 = new_uuid(&env, &mut storage, &api).unwrap();

assert_eq!(uuid.to_string(), "417d3461-5f6c-584b-8035-482a70997aee");
assert_eq!(uuid.get_variant(), uuid::Variant::RFC4122);
assert_eq!(uuid.get_version(), Some(uuid::Version::Sha1));
let parsed_uuid = Uuid::from_str("417d3461-5f6c-584b-8035-482a70997aee");
assert_eq!(uuid, parsed_uuid.unwrap());

assert_eq!(uuid2.to_string(), "61b5574c-d3e4-5d06-8a87-ab28a7353dfd");
assert_ne!(uuid, uuid2);
assert_eq!(uuid1.get_variant(), uuid::Variant::RFC4122);
assert_eq!(uuid1.get_version(), Some(uuid::Version::Sha1));
assert_ne!(uuid1, uuid2);
}

#[test]
Expand All @@ -110,8 +104,8 @@ mod tests {
let mut storage = MockStorage::new();
let our_uuid = new_uuid(&env, &mut storage, &api).unwrap();

let uuid_name = format!("{} {} {}", env.contract.address, env.block.height, 0);
let raw = raw_uuid::Uuid::new_v5(&raw_uuid::Uuid::NAMESPACE_OID, uuid_name.as_bytes());
let uuid_name = &[env.contract.address.as_bytes(), &env.block.height.to_be_bytes(), &0u16.to_be_bytes()].concat();
let raw = raw_uuid::Uuid::new_v5(&raw_uuid::Uuid::NAMESPACE_OID, uuid_name);

assert_eq!(our_uuid.to_string(), raw.to_string());
}
Expand Down

0 comments on commit 4cb5358

Please sign in to comment.