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