| 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 | 36702× | pub fn is_zero(&self) -> bool { |
| 19 | 36702× | *self == Self::ZERO |
| 20 | 36702× | } |
| 21 | 139495× | pub fn to_hex(&self) -> String { |
| 22 | 139495× | hex::encode(self.0) |
| 23 | 139495× | } |
| 24 | 38505× | pub fn from_hex(s: &str) -> Option<ObjectRef> { |
| 25 | 38505× | let v = hex::decode(s).ok()?; |
| 26 | 38501× | let a: [u8; 32] = v.try_into().ok()?; |
| 27 | 38501× | Some(ObjectRef(a)) |
| 28 | 38505× | } |
| 29 | 185056× | pub fn from_slice(b: &[u8]) -> Option<ObjectRef> { |
| 30 | 185056× | let a: [u8; 32] = b.try_into().ok()?; |
| 31 | 185056× | Some(ObjectRef(a)) |
| 32 | 185056× | } |
| 33 | | |
| 34 | 3711× | pub fn short(&self, n: usize) -> String { |
| 35 | 3711× | let h = self.to_hex(); |
| 36 | 3711× | h[..n.min(h.len())].to_string() |
| 37 | 3711× | } |
| 38 | 122× | pub fn starts_with_hex(&self, prefix: &str) -> bool { |
| 39 | 122× | self.to_hex().starts_with(prefix) |
| 40 | 122× | } |
| 41 | | } |
| 42 | |
|
| 43 | | impl fmt::Display for ObjectRef { |
| 44 | 123× | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 45 | 123× | f.write_str(&self.to_hex()) |
| 46 | 123× | } |
| 47 | | } |
| 48 | |
|
| 49 | | impl fmt::Debug for ObjectRef { |
| 50 | 2926× | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 51 | 2926× | write!(f, "#{}", self.short(12)) |
| 52 | 2926× | } |
| 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 | | Certificate, |
| 69 | | |
| 70 | | Grant, |
| 71 | | |
| 72 | | |
| 73 | | Artifact, |
| 74 | | } |
| 75 | |
|
| 76 | | impl ObjectType { |
| 77 | | pub const ALL: [ObjectType; 13] = [ |
| 78 | | ObjectType::Chunk, |
| 79 | | ObjectType::Blob, |
| 80 | | ObjectType::Change, |
| 81 | | ObjectType::Checkpoint, |
| 82 | | ObjectType::Op, |
| 83 | | ObjectType::Attest, |
| 84 | | ObjectType::Identity, |
| 85 | | ObjectType::Policy, |
| 86 | | ObjectType::State, |
| 87 | | ObjectType::Receipt, |
| 88 | | ObjectType::Certificate, |
| 89 | | ObjectType::Grant, |
| 90 | | ObjectType::Artifact, |
| 91 | | ]; |
| 92 | |
|
| 93 | 404410× | pub fn name(self) -> &'static str { |
| 94 | 404410× | match self { |
| 95 | 97729× | ObjectType::Chunk => "chunk", |
| 96 | 58757× | ObjectType::Blob => "blob", |
| 97 | 92581× | ObjectType::Change => "change", |
| 98 | 31745× | ObjectType::Checkpoint => "checkpoint", |
| 99 | 65273× | ObjectType::Op => "op", |
| 100 | 13519× | ObjectType::Attest => "attest", |
| 101 | 13636× | ObjectType::Identity => "identity", |
| 102 | 8990× | ObjectType::Policy => "policy", |
| 103 | 21228× | ObjectType::State => "state", |
| 104 | 244× | ObjectType::Receipt => "receipt", |
| 105 | 649× | ObjectType::Certificate => "certificate", |
| 106 | 25× | ObjectType::Grant => "grant", |
| 107 | 34× | ObjectType::Artifact => "artifact", |
| 108 | | } |
| 109 | 404410× | } |
| 110 | |
|
| 111 | 58149× | pub fn from_name(s: &str) -> Option<ObjectType> { |
| 112 | 260955× | Self::ALL.iter().copied().find(|t| t.name() == s) |
| 113 | 58149× | } |
| 114 | | } |
| 115 | |
|
| 116 | |
|
| 117 | 129120× | pub fn hash_object(ty: ObjectType, body: &[u8]) -> ObjectRef { |
| 118 | 129120× | let mut h = blake3::Hasher::new(); |
| 119 | 129120× | h.update(&header(ty, body.len())); |
| 120 | 129120× | h.update(body); |
| 121 | 129120× | ObjectRef(*h.finalize().as_bytes()) |
| 122 | 129120× | } |
| 123 | |
|
| 124 | 143229× | pub fn header(ty: ObjectType, len: usize) -> Vec<u8> { |
| 125 | 143229× | let mut v = Vec::with_capacity(32); |
| 126 | 143229× | v.extend_from_slice(b"oxy1 "); |
| 127 | 143229× | v.extend_from_slice(ty.name().as_bytes()); |
| 128 | 143229× | v.push(b' '); |
| 129 | 143229× | v.extend_from_slice(len.to_string().as_bytes()); |
| 130 | 143229× | v.push(0); |
| 131 | 143229× | v |
| 132 | 143229× | } |
| 133 | |
|
| 134 | | #[cfg(test)] |
| 135 | | mod tests { |
| 136 | | use super::*; |
| 137 | |
|
| 138 | | #[test] |
| 139 | 1× | fn header_matches_spec() { |
| 140 | 1× | assert_eq!(header(ObjectType::Chunk, 5), b"oxy1 chunk 5\0"); |
| 141 | 1× | assert_eq!(header(ObjectType::Change, 1234), b"oxy1 change 1234\0"); |
| 142 | 1× | } |
| 143 | |
|
| 144 | | #[test] |
| 145 | 1× | fn types_are_domain_separated() { |
| 146 | 1× | let body = b"same bytes"; |
| 147 | 1× | let a = hash_object(ObjectType::Chunk, body); |
| 148 | 1× | let b = hash_object(ObjectType::Blob, body); |
| 149 | 1× | assert_ne!(a, b); |
| 150 | 1× | assert_eq!(a, hash_object(ObjectType::Chunk, body)); |
| 151 | | |
| 152 | 1× | assert_eq!( |
| 153 | 1× | hash_object(ObjectType::Chunk, b"").to_hex(), |
| 154 | 1× | blake3::hash(b"oxy1 chunk 0\0").to_hex().as_str() |
| 155 | | ); |
| 156 | 1× | } |
| 157 | |
|
| 158 | | #[test] |
| 159 | 1× | fn hex_round_trip() { |
| 160 | 1× | let r = hash_object(ObjectType::Chunk, b"x"); |
| 161 | 1× | assert_eq!(ObjectRef::from_hex(&r.to_hex()), Some(r)); |
| 162 | 1× | assert_eq!(ObjectRef::from_hex("zz"), None); |
| 163 | 1× | assert!(r.starts_with_hex(&r.short(7))); |
| 164 | 1× | assert!(ObjectRef::ZERO.is_zero()); |
| 165 | 13× | for t in ObjectType::ALL { |
| 166 | 13× | assert_eq!(ObjectType::from_name(t.name()), Some(t)); |
| 167 | | } |
| 168 | 1× | } |
| 169 | | } |