| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | | use std::fmt; |
| 5 | |
|
| 6 | | #[derive(Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)] |
| 7 | | pub struct ObjectRef(pub [u8; 32]); |
| 8 | |
|
| 9 | |
|
| 10 | | pub type ChangeHash = ObjectRef; |
| 11 | |
|
| 12 | | pub type KeyId = ObjectRef; |
| 13 | |
|
| 14 | | impl ObjectRef { |
| 15 | | |
| 16 | | pub const ZERO: ObjectRef = ObjectRef([0; 32]); |
| 17 | |
|
| 18 | 29858× | pub fn is_zero(&self) -> bool { |
| 19 | 29858× | *self == Self::ZERO |
| 20 | 29858× | } |
| 21 | 120441× | pub fn to_hex(&self) -> String { |
| 22 | 120441× | hex::encode(self.0) |
| 23 | 120441× | } |
| 24 | 33279× | pub fn from_hex(s: &str) -> Option<ObjectRef> { |
| 25 | 33279× | let v = hex::decode(s).ok()?; |
| 26 | 33275× | let a: [u8; 32] = v.try_into().ok()?; |
| 27 | 33275× | Some(ObjectRef(a)) |
| 28 | 33279× | } |
| 29 | 152370× | pub fn from_slice(b: &[u8]) -> Option<ObjectRef> { |
| 30 | 152370× | let a: [u8; 32] = b.try_into().ok()?; |
| 31 | 152370× | Some(ObjectRef(a)) |
| 32 | 152370× | } |
| 33 | | |
| 34 | 3318× | pub fn short(&self, n: usize) -> String { |
| 35 | 3318× | let h = self.to_hex(); |
| 36 | 3318× | h[..n.min(h.len())].to_string() |
| 37 | 3318× | } |
| 38 | 113× | pub fn starts_with_hex(&self, prefix: &str) -> bool { |
| 39 | 113× | self.to_hex().starts_with(prefix) |
| 40 | 113× | } |
| 41 | | } |
| 42 | |
|
| 43 | | impl fmt::Display for ObjectRef { |
| 44 | 120× | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 45 | 120× | f.write_str(&self.to_hex()) |
| 46 | 120× | } |
| 47 | | } |
| 48 | |
|
| 49 | | impl fmt::Debug for ObjectRef { |
| 50 | 2594× | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 51 | 2594× | write!(f, "#{}", self.short(12)) |
| 52 | 2594× | } |
| 53 | | } |
| 54 | |
|
| 55 | | #[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)] |
| 56 | | pub enum ObjectType { |
| 57 | | Chunk, |
| 58 | | Blob, |
| 59 | | Change, |
| 60 | | Checkpoint, |
| 61 | | Op, |
| 62 | | Attest, |
| 63 | | Identity, |
| 64 | | Policy, |
| 65 | | State, |
| 66 | | Receipt, |
| 67 | | } |
| 68 | |
|
| 69 | | impl ObjectType { |
| 70 | | pub const ALL: [ObjectType; 10] = [ |
| 71 | | ObjectType::Chunk, |
| 72 | | ObjectType::Blob, |
| 73 | | ObjectType::Change, |
| 74 | | ObjectType::Checkpoint, |
| 75 | | ObjectType::Op, |
| 76 | | ObjectType::Attest, |
| 77 | | ObjectType::Identity, |
| 78 | | ObjectType::Policy, |
| 79 | | ObjectType::State, |
| 80 | | ObjectType::Receipt, |
| 81 | | ]; |
| 82 | |
|
| 83 | 315003× | pub fn name(self) -> &'static str { |
| 84 | 315003× | match self { |
| 85 | 79263× | ObjectType::Chunk => "chunk", |
| 86 | 46398× | ObjectType::Blob => "blob", |
| 87 | 73904× | ObjectType::Change => "change", |
| 88 | 24195× | ObjectType::Checkpoint => "checkpoint", |
| 89 | 50652× | ObjectType::Op => "op", |
| 90 | 8842× | ObjectType::Attest => "attest", |
| 91 | 8337× | ObjectType::Identity => "identity", |
| 92 | 6224× | ObjectType::Policy => "policy", |
| 93 | 17141× | ObjectType::State => "state", |
| 94 | 47× | ObjectType::Receipt => "receipt", |
| 95 | | } |
| 96 | 315003× | } |
| 97 | |
|
| 98 | 45621× | pub fn from_name(s: &str) -> Option<ObjectType> { |
| 99 | 200660× | Self::ALL.iter().copied().find(|t| t.name() == s) |
| 100 | 45621× | } |
| 101 | | } |
| 102 | |
|
| 103 | |
|
| 104 | 103073× | pub fn hash_object(ty: ObjectType, body: &[u8]) -> ObjectRef { |
| 105 | 103073× | let mut h = blake3::Hasher::new(); |
| 106 | 103073× | h.update(&header(ty, body.len())); |
| 107 | 103073× | h.update(body); |
| 108 | 103073× | ObjectRef(*h.finalize().as_bytes()) |
| 109 | 103073× | } |
| 110 | |
|
| 111 | 114249× | pub fn header(ty: ObjectType, len: usize) -> Vec<u8> { |
| 112 | 114249× | let mut v = Vec::with_capacity(32); |
| 113 | 114249× | v.extend_from_slice(b"oxy1 "); |
| 114 | 114249× | v.extend_from_slice(ty.name().as_bytes()); |
| 115 | 114249× | v.push(b' '); |
| 116 | 114249× | v.extend_from_slice(len.to_string().as_bytes()); |
| 117 | 114249× | v.push(0); |
| 118 | 114249× | v |
| 119 | 114249× | } |
| 120 | |
|
| 121 | | #[cfg(test)] |
| 122 | | mod tests { |
| 123 | | use super::*; |
| 124 | |
|
| 125 | | #[test] |
| 126 | 1× | fn header_matches_spec() { |
| 127 | 1× | assert_eq!(header(ObjectType::Chunk, 5), b"oxy1 chunk 5\0"); |
| 128 | 1× | assert_eq!(header(ObjectType::Change, 1234), b"oxy1 change 1234\0"); |
| 129 | 1× | } |
| 130 | |
|
| 131 | | #[test] |
| 132 | 1× | fn types_are_domain_separated() { |
| 133 | 1× | let body = b"same bytes"; |
| 134 | 1× | let a = hash_object(ObjectType::Chunk, body); |
| 135 | 1× | let b = hash_object(ObjectType::Blob, body); |
| 136 | 1× | assert_ne!(a, b); |
| 137 | 1× | assert_eq!(a, hash_object(ObjectType::Chunk, body)); |
| 138 | | |
| 139 | 1× | assert_eq!( |
| 140 | 1× | hash_object(ObjectType::Chunk, b"").to_hex(), |
| 141 | 1× | blake3::hash(b"oxy1 chunk 0\0").to_hex().as_str() |
| 142 | | ); |
| 143 | 1× | } |
| 144 | |
|
| 145 | | #[test] |
| 146 | 1× | fn hex_round_trip() { |
| 147 | 1× | let r = hash_object(ObjectType::Chunk, b"x"); |
| 148 | 1× | assert_eq!(ObjectRef::from_hex(&r.to_hex()), Some(r)); |
| 149 | 1× | assert_eq!(ObjectRef::from_hex("zz"), None); |
| 150 | 1× | assert!(r.starts_with_hex(&r.short(7))); |
| 151 | 1× | assert!(ObjectRef::ZERO.is_zero()); |
| 152 | 10× | for t in ObjectType::ALL { |
| 153 | 10× | assert_eq!(ObjectType::from_name(t.name()), Some(t)); |
| 154 | | } |
| 155 | 1× | } |
| 156 | | } |