Skip to content

Commit

Permalink
Replace instances of REGISTRY::parse_denom with asset::Cache::with_kn…
Browse files Browse the repository at this point in the history
…own_assets::get_unit when the asset is known
  • Loading branch information
aubrika committed Jun 23, 2023
1 parent fb5b9d4 commit 65940ff
Show file tree
Hide file tree
Showing 16 changed files with 179 additions and 61 deletions.
20 changes: 16 additions & 4 deletions crates/bin/pcli/tests/proof.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,10 @@ fn spend_proof_parameters_vs_current_spend_circuit() {

let value_to_send = Value {
amount: 1u64.into(),
asset_id: asset::REGISTRY.parse_denom("upenumbra").unwrap().id(),
asset_id: asset::Cache::with_known_assets()
.get_unit("upenumbra")
.unwrap()
.id(),
};

let note = Note::generate(&mut OsRng, &sender, value_to_send);
Expand Down Expand Up @@ -92,7 +95,10 @@ fn delegator_vote_proof_parameters_vs_current_delegator_vote_circuit() {

let value_to_send = Value {
amount: 2u64.into(),
asset_id: asset::REGISTRY.parse_denom("upenumbra").unwrap().id(),
asset_id: asset::Cache::with_known_assets()
.get_unit("upenumbra")
.unwrap()
.id(),
};

let note = Note::generate(&mut OsRng, &sender, value_to_send);
Expand Down Expand Up @@ -306,7 +312,10 @@ fn output_proof_parameters_vs_current_output_circuit() {

let value_to_send = Value {
amount: 1u64.into(),
asset_id: asset::REGISTRY.parse_denom("upenumbra").unwrap().id(),
asset_id: asset::Cache::with_known_assets()
.get_unit("upenumbra")
.unwrap()
.id(),
};
let v_blinding = Fr::rand(&mut OsRng);

Expand Down Expand Up @@ -344,7 +353,10 @@ fn nullifier_derivation_parameters_vs_current_nullifier_derivation_circuit() {

let value_to_send = Value {
amount: 1u128.into(),
asset_id: asset::REGISTRY.parse_denom("upenumbra").unwrap().id(),
asset_id: asset::Cache::with_known_assets()
.get_unit("upenumbra")
.unwrap()
.id(),
};

let note = Note::generate(&mut rng, &sender, value_to_send);
Expand Down
10 changes: 8 additions & 2 deletions crates/core/component/dex/src/batch_swap_output_data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -372,8 +372,14 @@ mod tests {
impl ParameterSetup for ProRataOutputCircuit {
fn generate_test_parameters() -> (ProvingKey<Bls12_377>, VerifyingKey<Bls12_377>) {
let trading_pair = TradingPair {
asset_1: asset::REGISTRY.parse_denom("upenumbra").unwrap().id(),
asset_2: asset::REGISTRY.parse_denom("nala").unwrap().id(),
asset_1: asset::Cache::with_known_assets()
.get_unit("upenumbra")
.unwrap()
.id(),
asset_2: asset::Cache::with_known_assets()
.get_unit("nala")
.unwrap()
.id(),
};
let circuit = ProRataOutputCircuit {
delta_1_i: Amount::from(1u32),
Expand Down
4 changes: 2 additions & 2 deletions crates/core/component/dex/src/lp/nft.rs
Original file line number Diff line number Diff line change
Expand Up @@ -161,8 +161,8 @@ mod tests {
fn lpnft_denom_parsing_roundtrip() {
let pair = DirectedTradingPair {
start: penumbra_crypto::STAKING_TOKEN_ASSET_ID.clone(),
end: penumbra_crypto::asset::REGISTRY
.parse_denom("cube")
end: asset::Cache::with_known_assets()
.get_unit("cube")
.unwrap()
.id(),
};
Expand Down
15 changes: 12 additions & 3 deletions crates/core/component/dex/src/swap/plaintext.rs
Original file line number Diff line number Diff line change
Expand Up @@ -429,8 +429,14 @@ mod tests {
let ovk = fvk.outgoing();
let (dest, _dtk_d) = ivk.payment_address(0u32.into());
let trading_pair = TradingPair::new(
asset::REGISTRY.parse_denom("upenumbra").unwrap().id(),
asset::REGISTRY.parse_denom("nala").unwrap().id(),
asset::Cache::with_known_assets()
.get_unit("upenumbra")
.unwrap()
.id(),
asset::Cache::with_known_assets()
.get_unit("nala")
.unwrap()
.id(),
);

let swap = SwapPlaintext::new(
Expand All @@ -440,7 +446,10 @@ mod tests {
1u64.into(),
Fee(Value {
amount: 3u64.into(),
asset_id: asset::REGISTRY.parse_denom("upenumbra").unwrap().id(),
asset_id: asset::Cache::with_known_assets()
.get_unit("upenumbra")
.unwrap()
.id(),
}),
dest,
);
Expand Down
5 changes: 4 additions & 1 deletion crates/core/component/dex/src/swap/proof.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,10 @@ impl ParameterSetup for SwapCircuit {
delta_2_i: 1u64.into(),
claim_fee: Fee(Value {
amount: 3u64.into(),
asset_id: asset::REGISTRY.parse_denom("upenumbra").unwrap().id(),
asset_id: asset::Cache::with_known_assets()
.get_unit("upenumbra")
.unwrap()
.id(),
}),
claim_address: address,
rseed: Rseed([1u8; 32]),
Expand Down
15 changes: 12 additions & 3 deletions crates/core/component/dex/src/swap_claim/proof.rs
Original file line number Diff line number Diff line change
Expand Up @@ -202,8 +202,14 @@ impl ConstraintSynthesizer<Fq> for SwapClaimCircuit {
impl ParameterSetup for SwapClaimCircuit {
fn generate_test_parameters() -> (ProvingKey<Bls12_377>, VerifyingKey<Bls12_377>) {
let trading_pair = TradingPair {
asset_1: asset::REGISTRY.parse_denom("upenumbra").unwrap().id(),
asset_2: asset::REGISTRY.parse_denom("nala").unwrap().id(),
asset_1: asset::Cache::with_known_assets()
.get_unit("upenumbra")
.unwrap()
.id(),
asset_2: asset::Cache::with_known_assets()
.get_unit("nala")
.unwrap()
.id(),
};

let seed_phrase = SeedPhrase::from_randomness([b'f'; 32]);
Expand All @@ -221,7 +227,10 @@ impl ParameterSetup for SwapClaimCircuit {
delta_2_i,
claim_fee: Fee(Value {
amount: 3u64.into(),
asset_id: asset::REGISTRY.parse_denom("upenumbra").unwrap().id(),
asset_id: asset::Cache::with_known_assets()
.get_unit("upenumbra")
.unwrap()
.id(),
}),
claim_address: address,
rseed: Rseed([1u8; 32]),
Expand Down
21 changes: 14 additions & 7 deletions crates/core/crypto/src/asset.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,10 @@ mod tests {
#[test]
fn test_registry_native_token() {
// We should be able to use `parse_base` with the valid base denomination.
let base_denom = REGISTRY.parse_denom("upenumbra").unwrap();
let base_denom = crate::asset::Cache::with_known_assets()
.get_unit("upenumbra")
.unwrap()
.base();
assert_eq!(format!("{base_denom}"), "upenumbra".to_string());

// If we try to use `parse_base` with a display denomination, we should get `None`.
Expand Down Expand Up @@ -141,7 +144,10 @@ mod tests {

#[test]
fn best_unit_for() {
let base_denom = REGISTRY.parse_denom("upenumbra").unwrap();
let base_denom = crate::asset::Cache::with_known_assets()
.get_unit("upenumbra")
.unwrap()
.base();

assert_eq!(
base_denom.best_unit_for(0u64.into()).to_string(),
Expand Down Expand Up @@ -194,11 +200,12 @@ mod tests {
}

#[test]
fn test_registry_fallthrough() {
// We should be able to use `parse_base` with a base denomination for assets
// not included in the hardcoded registry.
let base_denom = REGISTRY.parse_denom("cube").unwrap();
assert_eq!(format!("{base_denom}"), "cube".to_string());
fn test_get_unit() {
let unit = crate::asset::Cache::with_known_assets()
.get_unit("cube")
.unwrap();

assert_eq!(format!("{unit}"), "cube".to_string());
}

proptest! {
Expand Down
30 changes: 27 additions & 3 deletions crates/core/crypto/src/asset/cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@ pub struct Cache {
}

impl Cache {
fn _get_by_id(&self, id: Id) -> Option<DenomMetadata> {
pub fn get_by_id(&self, id: Id) -> Option<DenomMetadata> {
self.cache.get(&id).cloned()
}

fn _get_unit(&self, raw_denom: &str) -> Option<Unit> {
pub fn get_unit(&self, raw_denom: &str) -> Option<Unit> {
self.units.get(raw_denom).cloned()
}

Expand Down Expand Up @@ -159,6 +159,30 @@ impl Cache {
}],
)),
},
DenomMetadata {
inner: Arc::new(denom_metadata::Inner::new(
"ucube".to_string(),
vec![denom_metadata::BareDenomUnit {
exponent: 1,
denom: "cube".to_string(),
}],
)),
},
DenomMetadata {
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);
Expand Down Expand Up @@ -229,7 +253,7 @@ impl Extend<DenomMetadata> for Cache {
self.cache.insert(id, denom.clone());

for unit in denom.units() {
self.units.insert(unit.base().base_denom().denom, unit);
self.units.insert(unit.to_string(), unit);
}
}
}
Expand Down
4 changes: 4 additions & 0 deletions crates/core/crypto/src/asset/denom_metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -223,11 +223,15 @@ impl Inner {
.as_bytes(),
));

// Perform validity check for each unit.

for unit in &units {
assert_ne!(unit.exponent, 0);
assert_ne!(&unit.denom, &base_denom);
}

// Add the base denom as a unit.

units.push(BareDenomUnit {
exponent: 0,
denom: base_denom.clone(),
Expand Down
16 changes: 12 additions & 4 deletions crates/core/crypto/src/balance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -488,12 +488,20 @@ mod test {
}

// Two sample denom/asset id pairs, for testing
static DENOM_1: Lazy<asset::DenomMetadata> =
Lazy::new(|| asset::REGISTRY.parse_denom("a").unwrap());
static DENOM_1: Lazy<asset::DenomMetadata> = Lazy::new(|| {
crate::asset::Cache::with_known_assets()
.get_unit("cube")
.unwrap()
.base()
});
static ASSET_ID_1: Lazy<asset::Id> = Lazy::new(|| DENOM_1.id());

static DENOM_2: Lazy<asset::DenomMetadata> =
Lazy::new(|| asset::REGISTRY.parse_denom("b").unwrap());
static DENOM_2: Lazy<asset::DenomMetadata> = Lazy::new(|| {
crate::asset::Cache::with_known_assets()
.get_unit("nala")
.unwrap()
.base()
});
static ASSET_ID_2: Lazy<asset::Id> = Lazy::new(|| DENOM_2.id());

fn gen_expression() -> impl proptest::strategy::Strategy<Value = Expression> {
Expand Down
8 changes: 6 additions & 2 deletions crates/core/crypto/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@ fn fmt_hex<T: AsRef<[u8]>>(data: T, f: &mut std::fmt::Formatter) -> std::fmt::Re

use once_cell::sync::Lazy;

pub static STAKING_TOKEN_DENOM: Lazy<asset::DenomMetadata> =
Lazy::new(|| asset::REGISTRY.parse_denom("upenumbra").unwrap());
pub static STAKING_TOKEN_DENOM: Lazy<asset::DenomMetadata> = Lazy::new(|| {
crate::asset::Cache::with_known_assets()
.get_unit("upenumbra")
.unwrap()
.base()
});
pub static STAKING_TOKEN_ASSET_ID: Lazy<asset::Id> = Lazy::new(|| STAKING_TOKEN_DENOM.id());
5 changes: 4 additions & 1 deletion crates/core/crypto/src/memo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,10 @@ mod tests {

let value = Value {
amount: 10u64.into(),
asset_id: asset::REGISTRY.parse_denom("upenumbra").unwrap().id(),
asset_id: crate::asset::Cache::with_known_assets()
.get_unit("upenumbra")
.unwrap()
.id(),
};
let note = Note::generate(&mut rng, &dest, value);

Expand Down
15 changes: 12 additions & 3 deletions crates/core/crypto/src/note.rs
Original file line number Diff line number Diff line change
Expand Up @@ -541,7 +541,10 @@ mod tests {

let value = Value {
amount: 10u64.into(),
asset_id: asset::REGISTRY.parse_denom("upenumbra").unwrap().id(),
asset_id: crate::asset::Cache::with_known_assets()
.get_unit("upenumbra")
.unwrap()
.id(),
};
let note = Note::generate(&mut rng, &dest, value);

Expand Down Expand Up @@ -574,7 +577,10 @@ mod tests {

let value = Value {
amount: 10u64.into(),
asset_id: asset::REGISTRY.parse_denom("upenumbra").unwrap().id(),
asset_id: crate::asset::Cache::with_known_assets()
.get_unit("upenumbra")
.unwrap()
.id(),
};
let note = Note::generate(&mut rng, &dest, value);

Expand Down Expand Up @@ -605,7 +611,10 @@ mod tests {

let value = Value {
amount: 10u64.into(),
asset_id: asset::REGISTRY.parse_denom("upenumbra").unwrap().id(),
asset_id: crate::asset::Cache::with_known_assets()
.get_unit("upenumbra")
.unwrap()
.id(),
};
let note = Note::generate(&mut rng, &dest, value);

Expand Down
Loading

0 comments on commit 65940ff

Please sign in to comment.