Fix validator selection for apply, address and policy command
Before this commit, we would require those target a specific handler, whereas they are in fact global to the entire validator. So now, we recover the behaviour from before where we default to the only available validator when there's no ambiguity. Note that this also solves the need for repeatedly applying parameters to each handler of a parameterized validator. The command now rightfully apply parameters to each corresponding handler.
This commit is contained in:
@@ -52,7 +52,7 @@ pub struct Compiler {
|
||||
|
||||
#[derive(Debug, PartialEq, Clone)]
|
||||
pub enum LookupResult<'a, T> {
|
||||
One(&'a T),
|
||||
One(String, &'a T),
|
||||
Many,
|
||||
}
|
||||
|
||||
@@ -91,17 +91,48 @@ impl Blueprint {
|
||||
}
|
||||
|
||||
impl Blueprint {
|
||||
pub fn lookup(&self, title: Option<&String>) -> Option<LookupResult<Validator>> {
|
||||
pub fn lookup(
|
||||
&self,
|
||||
want_module_name: Option<&str>,
|
||||
want_validator_name: Option<&str>,
|
||||
) -> Option<LookupResult<Validator>> {
|
||||
let mut validator = None;
|
||||
|
||||
for v in self.validators.iter() {
|
||||
let match_title = Some(&v.title) == title.or(Some(&v.title));
|
||||
if match_title {
|
||||
validator = Some(if validator.is_none() {
|
||||
LookupResult::One(v)
|
||||
} else {
|
||||
LookupResult::Many
|
||||
})
|
||||
let mut split = v.title.split('.');
|
||||
|
||||
let known_module_name = split
|
||||
.next()
|
||||
.expect("validator's name must have two dot-separated components.");
|
||||
|
||||
let known_validator_name = split
|
||||
.next()
|
||||
.expect("validator's name must have two dot-separated components.");
|
||||
|
||||
let is_target = match (want_module_name, want_validator_name) {
|
||||
(None, None) => true,
|
||||
(Some(want_module_name), None) => want_module_name == known_module_name,
|
||||
(None, Some(want_validator_name)) => want_validator_name == known_validator_name,
|
||||
(Some(want_module_name), Some(want_validator_name)) => {
|
||||
want_module_name == known_module_name
|
||||
&& want_validator_name == known_validator_name
|
||||
}
|
||||
};
|
||||
|
||||
let title = format!("{known_module_name}.{known_validator_name}");
|
||||
|
||||
if is_target {
|
||||
match validator {
|
||||
Some(LookupResult::Many) => (),
|
||||
None => {
|
||||
validator = Some(LookupResult::One(title, v));
|
||||
}
|
||||
Some(LookupResult::One(ref known_title, _)) => {
|
||||
if title.as_str() != known_title {
|
||||
validator = Some(LookupResult::Many)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -110,16 +141,17 @@ impl Blueprint {
|
||||
|
||||
pub fn with_validator<F, A, E>(
|
||||
&self,
|
||||
title: Option<&String>,
|
||||
module_name: Option<&str>,
|
||||
validator_name: Option<&str>,
|
||||
when_too_many: fn(Vec<String>) -> E,
|
||||
when_missing: fn(Vec<String>) -> E,
|
||||
action: F,
|
||||
) -> Result<A, E>
|
||||
where
|
||||
F: Fn(Validator) -> Result<A, E>,
|
||||
F: Fn(&Validator) -> Result<A, E>,
|
||||
{
|
||||
match self.lookup(title) {
|
||||
Some(LookupResult::One(validator)) => action(validator.to_owned()),
|
||||
match self.lookup(module_name, validator_name) {
|
||||
Some(LookupResult::One(_, validator)) => action(validator),
|
||||
Some(LookupResult::Many) => Err(when_too_many(
|
||||
self.validators.iter().map(|v| v.title.clone()).collect(),
|
||||
)),
|
||||
|
||||
@@ -448,8 +448,9 @@ where
|
||||
|
||||
pub fn address(
|
||||
&self,
|
||||
title: Option<&String>,
|
||||
stake_address: Option<&String>,
|
||||
module_name: Option<&str>,
|
||||
validator_name: Option<&str>,
|
||||
stake_address: Option<&str>,
|
||||
blueprint_path: &Path,
|
||||
mainnet: bool,
|
||||
) -> Result<ShelleyAddress, Error> {
|
||||
@@ -481,35 +482,39 @@ where
|
||||
|known_validators| Error::MoreThanOneValidatorFound { known_validators };
|
||||
let when_missing = |known_validators| Error::NoValidatorNotFound { known_validators };
|
||||
|
||||
blueprint.with_validator(title, when_too_many, when_missing, |validator| {
|
||||
// Make sure we're not calculating the address for a minting validator
|
||||
if let Some(title) = title {
|
||||
if !title.ends_with("else") && !title.ends_with("spend") {
|
||||
return Err(blueprint::error::Error::UnexpectedMintingValidator.into());
|
||||
}
|
||||
}
|
||||
blueprint.with_validator(
|
||||
module_name,
|
||||
validator_name,
|
||||
when_too_many,
|
||||
when_missing,
|
||||
|validator| {
|
||||
let n = validator.parameters.len();
|
||||
|
||||
let n = validator.parameters.len();
|
||||
|
||||
if n > 0 {
|
||||
Err(blueprint::error::Error::ParameterizedValidator { n }.into())
|
||||
} else {
|
||||
let network = if mainnet {
|
||||
Network::Mainnet
|
||||
if n > 0 {
|
||||
Err(blueprint::error::Error::ParameterizedValidator { n }.into())
|
||||
} else {
|
||||
Network::Testnet
|
||||
};
|
||||
let network = if mainnet {
|
||||
Network::Mainnet
|
||||
} else {
|
||||
Network::Testnet
|
||||
};
|
||||
|
||||
Ok(validator.program.inner().address(
|
||||
network,
|
||||
delegation_part.to_owned(),
|
||||
&self.config.plutus.into(),
|
||||
))
|
||||
}
|
||||
})
|
||||
Ok(validator.program.inner().address(
|
||||
network,
|
||||
delegation_part.to_owned(),
|
||||
&self.config.plutus.into(),
|
||||
))
|
||||
}
|
||||
},
|
||||
)
|
||||
}
|
||||
|
||||
pub fn policy(&self, title: Option<&String>, blueprint_path: &Path) -> Result<PolicyId, Error> {
|
||||
pub fn policy(
|
||||
&self,
|
||||
module_name: Option<&str>,
|
||||
validator_name: Option<&str>,
|
||||
blueprint_path: &Path,
|
||||
) -> Result<PolicyId, Error> {
|
||||
// Read blueprint
|
||||
let blueprint = File::open(blueprint_path)
|
||||
.map_err(|_| blueprint::error::Error::InvalidOrMissingFile)?;
|
||||
@@ -520,19 +525,20 @@ where
|
||||
|known_validators| Error::MoreThanOneValidatorFound { known_validators };
|
||||
let when_missing = |known_validators| Error::NoValidatorNotFound { known_validators };
|
||||
|
||||
blueprint.with_validator(title, when_too_many, when_missing, |validator| {
|
||||
// Make sure we're not calculating the policy for a spending validator
|
||||
if validator.datum.is_some() {
|
||||
return Err(blueprint::error::Error::UnexpectedSpendingValidator.into());
|
||||
}
|
||||
|
||||
let n = validator.parameters.len();
|
||||
if n > 0 {
|
||||
Err(blueprint::error::Error::ParameterizedValidator { n }.into())
|
||||
} else {
|
||||
Ok(validator.program.compiled_code_and_hash().1)
|
||||
}
|
||||
})
|
||||
blueprint.with_validator(
|
||||
module_name,
|
||||
validator_name,
|
||||
when_too_many,
|
||||
when_missing,
|
||||
|validator| {
|
||||
let n = validator.parameters.len();
|
||||
if n > 0 {
|
||||
Err(blueprint::error::Error::ParameterizedValidator { n }.into())
|
||||
} else {
|
||||
Ok(validator.program.compiled_code_and_hash().1)
|
||||
}
|
||||
},
|
||||
)
|
||||
}
|
||||
|
||||
pub fn export(&self, module: &str, name: &str, tracing: Tracing) -> Result<Export, Error> {
|
||||
@@ -571,7 +577,8 @@ where
|
||||
|
||||
pub fn construct_parameter_incrementally<F>(
|
||||
&self,
|
||||
title: Option<&String>,
|
||||
module_name: Option<&str>,
|
||||
validator_name: Option<&str>,
|
||||
blueprint_path: &Path,
|
||||
ask: F,
|
||||
) -> Result<PlutusData, Error>
|
||||
@@ -591,18 +598,25 @@ where
|
||||
|known_validators| Error::MoreThanOneValidatorFound { known_validators };
|
||||
let when_missing = |known_validators| Error::NoValidatorNotFound { known_validators };
|
||||
|
||||
let data = blueprint.with_validator(title, when_too_many, when_missing, |validator| {
|
||||
validator
|
||||
.ask_next_parameter(&blueprint.definitions, &ask)
|
||||
.map_err(|e| e.into())
|
||||
})?;
|
||||
let data = blueprint.with_validator(
|
||||
module_name,
|
||||
validator_name,
|
||||
when_too_many,
|
||||
when_missing,
|
||||
|validator| {
|
||||
validator
|
||||
.ask_next_parameter(&blueprint.definitions, &ask)
|
||||
.map_err(|e| e.into())
|
||||
},
|
||||
)?;
|
||||
|
||||
Ok(data)
|
||||
}
|
||||
|
||||
pub fn apply_parameter(
|
||||
&self,
|
||||
title: Option<&String>,
|
||||
module_name: Option<&str>,
|
||||
validator_name: Option<&str>,
|
||||
blueprint_path: &Path,
|
||||
param: &PlutusData,
|
||||
) -> Result<Blueprint, Error> {
|
||||
@@ -616,21 +630,28 @@ where
|
||||
|known_validators| Error::MoreThanOneValidatorFound { known_validators };
|
||||
let when_missing = |known_validators| Error::NoValidatorNotFound { known_validators };
|
||||
|
||||
let applied_validator =
|
||||
blueprint.with_validator(title, when_too_many, when_missing, |validator| {
|
||||
let applied_validator = blueprint.with_validator(
|
||||
module_name,
|
||||
validator_name,
|
||||
when_too_many,
|
||||
when_missing,
|
||||
|validator| {
|
||||
validator
|
||||
.clone()
|
||||
.apply(&blueprint.definitions, param)
|
||||
.map_err(|e| e.into())
|
||||
})?;
|
||||
},
|
||||
)?;
|
||||
|
||||
let prefix = |v: &str| v.split('.').take(2).collect::<Vec<&str>>().join(".");
|
||||
|
||||
// Overwrite validator
|
||||
blueprint.validators = blueprint
|
||||
.validators
|
||||
.into_iter()
|
||||
.map(|validator| {
|
||||
let same_title = validator.title == applied_validator.title;
|
||||
if same_title {
|
||||
applied_validator.to_owned()
|
||||
if prefix(&applied_validator.title) == prefix(&validator.title) {
|
||||
applied_validator.clone()
|
||||
} else {
|
||||
validator
|
||||
}
|
||||
|
||||
@@ -143,14 +143,16 @@ where
|
||||
return Err(ExitFailure::into_report());
|
||||
}
|
||||
|
||||
eprintln!(
|
||||
"{}",
|
||||
Summary {
|
||||
check_count: project.checks_count,
|
||||
error_count: 0,
|
||||
warning_count
|
||||
}
|
||||
);
|
||||
if project.checks_count.unwrap_or_default() + warning_count > 0 {
|
||||
eprintln!(
|
||||
"{}",
|
||||
Summary {
|
||||
check_count: project.checks_count,
|
||||
error_count: 0,
|
||||
warning_count
|
||||
}
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
if warning_count > 0 && deny {
|
||||
|
||||
Reference in New Issue
Block a user