Skip to content

Commit

Permalink
Make ScVal::Validate recursive (#310)
Browse files Browse the repository at this point in the history
* Make ScVal::Validate recursive

* Placate clippy
  • Loading branch information
graydon authored Oct 18, 2023
1 parent 9c97e4f commit b7102b5
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 2 deletions.
13 changes: 12 additions & 1 deletion src/curr/scval_validations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,18 @@ impl Validate for ScVal {
| ScVal::I256(_)
| ScVal::Bytes(_)
| ScVal::String(_)
| ScVal::Vec(Some(_))
| ScVal::Address(_)
| ScVal::LedgerKeyContractInstance
| ScVal::LedgerKeyNonce(_)
| ScVal::ContractInstance(_) => Ok(()),

ScVal::Vec(Some(v)) => {
for e in v.iter() {
e.validate()?;
}
Ok(())
}

ScVal::Symbol(s) => {
// Symbol is defined as valid per https://github.com/stellar/rs-stellar-contract-env/blob/94c1717516c8fad4ad65caa148183b9fcbc408db/stellar-contract-env-common/src/symbol.rs#L107-L111.
if s.iter()
Expand All @@ -53,6 +59,11 @@ impl Validate for ScMap {
type Error = Error;

fn validate(&self) -> Result<(), Self::Error> {
// Check every element for validity itself.
for pair in self.iter() {
pair.key.validate()?;
pair.val.validate()?;
}
// Check the map is sorted by key, and there are no keys that are
// duplicates.
if self.windows(2).all(|w| w[0].key < w[1].key) {
Expand Down
13 changes: 12 additions & 1 deletion src/next/scval_validations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,18 @@ impl Validate for ScVal {
| ScVal::I256(_)
| ScVal::Bytes(_)
| ScVal::String(_)
| ScVal::Vec(Some(_))
| ScVal::Address(_)
| ScVal::LedgerKeyContractInstance
| ScVal::LedgerKeyNonce(_)
| ScVal::ContractInstance(_) => Ok(()),

ScVal::Vec(Some(v)) => {
for e in v.iter() {
e.validate()?;
}
Ok(())
}

ScVal::Symbol(s) => {
// Symbol is defined as valid per https://github.com/stellar/rs-stellar-contract-env/blob/94c1717516c8fad4ad65caa148183b9fcbc408db/stellar-contract-env-common/src/symbol.rs#L107-L111.
if s.iter()
Expand All @@ -53,6 +59,11 @@ impl Validate for ScMap {
type Error = Error;

fn validate(&self) -> Result<(), Self::Error> {
// Check every element for validity itself.
for pair in self.iter() {
pair.key.validate()?;
pair.val.validate()?;
}
// Check the map is sorted by key, and there are no keys that are
// duplicates.
if self.windows(2).all(|w| w[0].key < w[1].key) {
Expand Down

0 comments on commit b7102b5

Please sign in to comment.