Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bound VariadicValue multiple variant #1488

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 21 additions & 6 deletions client/rpc-core/src/types/filter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ use serde_json::{from_value, Value};

use crate::types::{BlockNumberOrHash, Log};

const VARIADIC_MULTIPLE_MAX_SIZE: usize = 16;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

16 will be too low for FilterAddress as mentioned by @boundless-forest


/// Variadic value
#[derive(Clone, Debug, Eq, PartialEq, Hash)]
pub enum VariadicValue<T>
Expand All @@ -55,13 +57,26 @@ where
let v: Value = Deserialize::deserialize(deserializer)?;

if v.is_null() {
return Ok(VariadicValue::Null);
Ok(VariadicValue::Null)
} else if let Ok(value) = from_value::<T>(v.clone()) {
Ok(VariadicValue::Single(value))
} else {
match from_value::<Vec<T>>(v) {
Ok(vec) => {
if vec.len() <= VARIADIC_MULTIPLE_MAX_SIZE {
Ok(VariadicValue::Multiple(vec))
} else {
Err(D::Error::custom(format!(
"Invalid variadic value type: too big array"
)))
}
}
Err(err) => Err(D::Error::custom(format!(
"Invalid variadic value type: {}",
err
))),
}
}

from_value(v.clone())
.map(VariadicValue::Single)
.or_else(|_| from_value(v).map(VariadicValue::Multiple))
.map_err(|err| D::Error::custom(format!("Invalid variadic value type: {}", err)))
}
}

Expand Down
Loading