penumbra_asset/asset/cache.rs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237
use std::{collections::BTreeMap, ops::Deref, sync::Arc};
use super::{denom_metadata, Id, Metadata, REGISTRY};
use crate::asset::denom_metadata::Unit;
/// On-chain data structures only record a fixed-size [`Id`], so this type
/// allows caching known [`BaseDenom`]s.
///
/// The cache is backed by a [`BTreeMap`] accessed through a [`Deref`] impl.
///
/// For (de)serialization, [`From`] conversions are provided to a `BTreeMap<Id,
/// String>` with the string representations of the base denominations.
#[derive(Clone, Default, Debug)]
pub struct Cache {
cache: BTreeMap<Id, Metadata>,
units: BTreeMap<String, Unit>,
}
impl Cache {
pub fn get_by_id(&self, id: Id) -> Option<Metadata> {
self.cache.get(&id).cloned()
}
pub fn get_unit(&self, raw_denom: &str) -> Option<Unit> {
self.units.get(raw_denom).cloned()
}
pub fn with_known_assets() -> Self {
let mut cache = Cache::default();
let known_assets = vec![
Metadata {
inner: Arc::new(denom_metadata::Inner::new(
"upenumbra".to_string(),
vec![
denom_metadata::BareDenomUnit {
exponent: 6,
denom: "penumbra".to_string(),
},
denom_metadata::BareDenomUnit {
exponent: 3,
denom: "mpenumbra".to_string(),
},
],
)),
},
Metadata {
inner: Arc::new(denom_metadata::Inner::new(
"ugn".to_string(),
vec![
denom_metadata::BareDenomUnit {
exponent: 6,
denom: "gn".to_string(),
},
denom_metadata::BareDenomUnit {
exponent: 3,
denom: "mgn".to_string(),
},
],
)),
},
Metadata {
inner: Arc::new(denom_metadata::Inner::new(
"ugm".to_string(),
vec![
denom_metadata::BareDenomUnit {
exponent: 6,
denom: "gm".to_string(),
},
denom_metadata::BareDenomUnit {
exponent: 3,
denom: "mgm".to_string(),
},
],
)),
},
Metadata {
inner: Arc::new(denom_metadata::Inner::new(
"wtest_usd".to_string(),
vec![denom_metadata::BareDenomUnit {
exponent: 6,
denom: "test_usd".to_string(),
}],
)),
},
Metadata {
inner: Arc::new(denom_metadata::Inner::new(
"test_sat".to_string(),
vec![denom_metadata::BareDenomUnit {
exponent: 8,
denom: "test_btc".to_string(),
}],
)),
},
Metadata {
inner: Arc::new(denom_metadata::Inner::new(
"utest_atom".to_string(),
vec![
denom_metadata::BareDenomUnit {
exponent: 6,
denom: "test_atom".to_string(),
},
denom_metadata::BareDenomUnit {
exponent: 3,
denom: "mtest_atom".to_string(),
},
],
)),
},
Metadata {
inner: Arc::new(denom_metadata::Inner::new(
"utest_osmo".to_string(),
vec![
denom_metadata::BareDenomUnit {
exponent: 6,
denom: "test_osmo".to_string(),
},
denom_metadata::BareDenomUnit {
exponent: 3,
denom: "mtest_osmo".to_string(),
},
],
)),
},
Metadata {
inner: Arc::new(denom_metadata::Inner::new(
"uubtc".to_string(),
vec![denom_metadata::BareDenomUnit {
exponent: 6,
denom: "ubtc".to_string(),
}],
)),
},
Metadata {
inner: Arc::new(denom_metadata::Inner::new(
"ucube".to_string(),
vec![denom_metadata::BareDenomUnit {
exponent: 1,
denom: "cube".to_string(),
}],
)),
},
Metadata {
inner: Arc::new(denom_metadata::Inner::new(
"unala".to_string(),
vec![
denom_metadata::BareDenomUnit {
exponent: 6,
denom: "nala".to_string(),
},
denom_metadata::BareDenomUnit {
exponent: 3,
denom: "mnala".to_string(),
},
],
)),
},
];
cache.extend(known_assets);
cache
}
}
// Implementing Deref but not DerefMut means people get unlimited read access,
// but can only write into the cache through approved methods.
impl Deref for Cache {
type Target = BTreeMap<Id, Metadata>;
fn deref(&self) -> &Self::Target {
&self.cache
}
}
impl From<Cache> for BTreeMap<Id, Metadata> {
fn from(cache: Cache) -> Self {
cache
.cache
.into_iter()
.map(|(id, denom)| (id, denom))
.collect()
}
}
impl TryFrom<BTreeMap<Id, Metadata>> for Cache {
type Error = anyhow::Error;
fn try_from(map: BTreeMap<Id, Metadata>) -> Result<Self, Self::Error> {
let mut cache = BTreeMap::default();
let mut units: BTreeMap<String, Unit> = BTreeMap::default();
for (provided_id, denom) in map.into_iter() {
if let Some(denom) = REGISTRY.parse_denom(&denom.base_denom().denom) {
let id = denom.id();
if provided_id != id {
anyhow::bail!(
"provided id {} for denom {} does not match computed id {}",
provided_id,
denom,
id
);
}
cache.insert(id, denom.clone());
units.insert(denom.base_denom().denom, denom.base_unit());
} else {
anyhow::bail!("invalid base denom {}", denom.base_denom().denom);
}
}
Ok(Self { cache, units })
}
}
// BaseDenom already has a validated Id, so by implementing Extend<BaseDenom> we
// can ensure we don't insert any invalid Ids
impl Extend<Metadata> for Cache {
fn extend<T>(&mut self, iter: T)
where
T: IntoIterator<Item = Metadata>,
{
for denom in iter {
let id = denom.id();
self.cache.insert(id, denom.clone());
for unit in denom.units() {
self.units.insert(unit.to_string(), unit);
}
}
}
}
impl FromIterator<Metadata> for Cache {
fn from_iter<T: IntoIterator<Item = Metadata>>(iter: T) -> Self {
let mut cache = Cache::default();
cache.extend(iter);
cache
}
}