Skip to content

Commit

Permalink
Merge pull request AleoNet#1295 from ljedrz/fix_serde_deser_idx_panics
Browse files Browse the repository at this point in the history
Remove indexing from serde deserialization
  • Loading branch information
raychu86 authored Jan 20, 2023
2 parents 1fa8270 + e9ec794 commit 2b6ac73
Show file tree
Hide file tree
Showing 25 changed files with 190 additions and 127 deletions.
2 changes: 2 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions console/program/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ version = "0.9.11"
path = "../types"
version = "0.9.11"

[dependencies.snarkvm-utilities]
path = "../../utilities"
version = "0.9.11"

[dependencies.enum_index]
version = "0.2"

Expand Down
26 changes: 11 additions & 15 deletions console/program/src/request/input_id/serialize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

use super::*;

use snarkvm_utilities::DeserializeExt;

impl<N: Network> Serialize for InputID<N> {
/// Serializes the input ID into string or bytes.
fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
Expand Down Expand Up @@ -68,24 +70,18 @@ impl<'de, N: Network> Deserialize<'de> for InputID<N> {
// Parse the input ID from a string into a value.
let mut input = serde_json::Value::deserialize(deserializer)?;
// Recover the input.
let input_id = match input["type"].as_str() {
Some("constant") => {
InputID::Constant(serde_json::from_value(input["id"].take()).map_err(de::Error::custom)?)
}
Some("public") => {
InputID::Public(serde_json::from_value(input["id"].take()).map_err(de::Error::custom)?)
}
Some("private") => {
InputID::Private(serde_json::from_value(input["id"].take()).map_err(de::Error::custom)?)
}
let input_id = match input.get("type").and_then(|t| t.as_str()) {
Some("constant") => InputID::Constant(DeserializeExt::take_from_value::<D>(&mut input, "id")?),
Some("public") => InputID::Public(DeserializeExt::take_from_value::<D>(&mut input, "id")?),
Some("private") => InputID::Private(DeserializeExt::take_from_value::<D>(&mut input, "id")?),
Some("record") => InputID::Record(
serde_json::from_value(input["commitment"].take()).map_err(de::Error::custom)?,
serde_json::from_value(input["gamma"].take()).map_err(de::Error::custom)?,
serde_json::from_value(input["serial_number"].take()).map_err(de::Error::custom)?,
serde_json::from_value(input["tag"].take()).map_err(de::Error::custom)?,
DeserializeExt::take_from_value::<D>(&mut input, "commitment")?,
DeserializeExt::take_from_value::<D>(&mut input, "gamma")?,
DeserializeExt::take_from_value::<D>(&mut input, "serial_number")?,
DeserializeExt::take_from_value::<D>(&mut input, "tag")?,
),
Some("external_record") => {
InputID::ExternalRecord(serde_json::from_value(input["id"].take()).map_err(de::Error::custom)?)
InputID::ExternalRecord(DeserializeExt::take_from_value::<D>(&mut input, "id")?)
}
_ => return Err(de::Error::custom("Invalid input type")),
};
Expand Down
24 changes: 13 additions & 11 deletions console/program/src/request/serialize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

use super::*;

use snarkvm_utilities::DeserializeExt;

impl<N: Network> Serialize for Request<N> {
/// Serializes the request into string or bytes.
fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
Expand Down Expand Up @@ -50,27 +52,27 @@ impl<'de, N: Network> Deserialize<'de> for Request<N> {
// Recover the request.
Ok(Self::from((
// Retrieve the caller.
serde_json::from_value(request["caller"].take()).map_err(de::Error::custom)?,
DeserializeExt::take_from_value::<D>(&mut request, "caller")?,
// Retrieve the network ID.
serde_json::from_value(request["network"].take()).map_err(de::Error::custom)?,
DeserializeExt::take_from_value::<D>(&mut request, "network")?,
// Retrieve the program ID.
serde_json::from_value(request["program"].take()).map_err(de::Error::custom)?,
DeserializeExt::take_from_value::<D>(&mut request, "program")?,
// Retrieve the function name.
serde_json::from_value(request["function"].take()).map_err(de::Error::custom)?,
DeserializeExt::take_from_value::<D>(&mut request, "function")?,
// Retrieve the input IDs.
serde_json::from_value(request["input_ids"].take()).map_err(de::Error::custom)?,
DeserializeExt::take_from_value::<D>(&mut request, "input_ids")?,
// Retrieve the inputs.
serde_json::from_value(request["inputs"].take()).map_err(de::Error::custom)?,
DeserializeExt::take_from_value::<D>(&mut request, "inputs")?,
// Retrieve the signature.
serde_json::from_value(request["signature"].take()).map_err(de::Error::custom)?,
DeserializeExt::take_from_value::<D>(&mut request, "signature")?,
// Retrieve the `sk_tag`.
serde_json::from_value(request["sk_tag"].take()).map_err(de::Error::custom)?,
DeserializeExt::take_from_value::<D>(&mut request, "sk_tag")?,
// Retrieve the `tvk`.
serde_json::from_value(request["tvk"].take()).map_err(de::Error::custom)?,
DeserializeExt::take_from_value::<D>(&mut request, "tvk")?,
// Retrieve the `tsk`.
serde_json::from_value(request["tsk"].take()).map_err(de::Error::custom)?,
DeserializeExt::take_from_value::<D>(&mut request, "tsk")?,
// Retrieve the `tcm`.
serde_json::from_value(request["tcm"].take()).map_err(de::Error::custom)?,
DeserializeExt::take_from_value::<D>(&mut request, "tcm")?,
)))
}
false => FromBytesDeserializer::<Self>::deserialize_with_size_encoding(deserializer, "request"),
Expand Down
6 changes: 4 additions & 2 deletions console/program/src/state_path/header_leaf/serialize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

use super::*;

use snarkvm_utilities::DeserializeExt;

impl<N: Network> Serialize for HeaderLeaf<N> {
/// Serializes the leaf into string or bytes.
fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
Expand All @@ -41,9 +43,9 @@ impl<'de, N: Network> Deserialize<'de> for HeaderLeaf<N> {
// Recover the leaf.
Ok(Self::new(
// Retrieve the index.
serde_json::from_value(leaf["index"].take()).map_err(de::Error::custom)?,
DeserializeExt::take_from_value::<D>(&mut leaf, "index")?,
// Retrieve the id.
serde_json::from_value(leaf["id"].take()).map_err(de::Error::custom)?,
DeserializeExt::take_from_value::<D>(&mut leaf, "id")?,
))
}
false => FromBytesDeserializer::<Self>::deserialize_with_size_encoding(deserializer, "header leaf"),
Expand Down
8 changes: 5 additions & 3 deletions console/program/src/state_path/transaction_leaf/serialize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

use super::*;

use snarkvm_utilities::DeserializeExt;

impl<N: Network> Serialize for TransactionLeaf<N> {
/// Serializes the leaf into string or bytes.
fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
Expand All @@ -42,11 +44,11 @@ impl<'de, N: Network> Deserialize<'de> for TransactionLeaf<N> {
// Recover the leaf.
Ok(Self::from(
// Retrieve the variant.
serde_json::from_value(leaf["variant"].take()).map_err(de::Error::custom)?,
DeserializeExt::take_from_value::<D>(&mut leaf, "variant")?,
// Retrieve the index.
serde_json::from_value(leaf["index"].take()).map_err(de::Error::custom)?,
DeserializeExt::take_from_value::<D>(&mut leaf, "index")?,
// Retrieve the id.
serde_json::from_value(leaf["id"].take()).map_err(de::Error::custom)?,
DeserializeExt::take_from_value::<D>(&mut leaf, "id")?,
))
}
false => FromBytesDeserializer::<Self>::deserialize_with_size_encoding(deserializer, "transaction leaf"),
Expand Down
10 changes: 6 additions & 4 deletions console/program/src/state_path/transition_leaf/serialize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

use super::*;

use snarkvm_utilities::DeserializeExt;

impl<N: Network> Serialize for TransitionLeaf<N> {
/// Serializes the leaf into string or bytes.
fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
Expand Down Expand Up @@ -43,13 +45,13 @@ impl<'de, N: Network> Deserialize<'de> for TransitionLeaf<N> {
// Recover the leaf.
Ok(Self::from(
// Retrieve the version.
serde_json::from_value(leaf["version"].take()).map_err(de::Error::custom)?,
DeserializeExt::take_from_value::<D>(&mut leaf, "version")?,
// Retrieve the index.
serde_json::from_value(leaf["index"].take()).map_err(de::Error::custom)?,
DeserializeExt::take_from_value::<D>(&mut leaf, "index")?,
// Retrieve the variant.
serde_json::from_value(leaf["variant"].take()).map_err(de::Error::custom)?,
DeserializeExt::take_from_value::<D>(&mut leaf, "variant")?,
// Retrieve the id.
serde_json::from_value(leaf["id"].take()).map_err(de::Error::custom)?,
DeserializeExt::take_from_value::<D>(&mut leaf, "id")?,
))
}
false => FromBytesDeserializer::<Self>::deserialize_with_size_encoding(deserializer, "transition leaf"),
Expand Down
18 changes: 10 additions & 8 deletions synthesizer/src/block/header/metadata/serialize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

use super::*;

use snarkvm_utilities::DeserializeExt;

impl<N: Network> Serialize for Metadata<N> {
/// Serializes the metadata to a JSON-string or buffer.
fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
Expand Down Expand Up @@ -44,14 +46,14 @@ impl<'de, N: Network> Deserialize<'de> for Metadata<N> {
true => {
let mut metadata = serde_json::Value::deserialize(deserializer)?;
Ok(Self::new(
serde_json::from_value(metadata["network"].take()).map_err(de::Error::custom)?,
serde_json::from_value(metadata["round"].take()).map_err(de::Error::custom)?,
serde_json::from_value(metadata["height"].take()).map_err(de::Error::custom)?,
serde_json::from_value(metadata["coinbase_target"].take()).map_err(de::Error::custom)?,
serde_json::from_value(metadata["proof_target"].take()).map_err(de::Error::custom)?,
serde_json::from_value(metadata["last_coinbase_target"].take()).map_err(de::Error::custom)?,
serde_json::from_value(metadata["last_coinbase_timestamp"].take()).map_err(de::Error::custom)?,
serde_json::from_value(metadata["timestamp"].take()).map_err(de::Error::custom)?,
DeserializeExt::take_from_value::<D>(&mut metadata, "network")?,
DeserializeExt::take_from_value::<D>(&mut metadata, "round")?,
DeserializeExt::take_from_value::<D>(&mut metadata, "height")?,
DeserializeExt::take_from_value::<D>(&mut metadata, "coinbase_target")?,
DeserializeExt::take_from_value::<D>(&mut metadata, "proof_target")?,
DeserializeExt::take_from_value::<D>(&mut metadata, "last_coinbase_target")?,
DeserializeExt::take_from_value::<D>(&mut metadata, "last_coinbase_timestamp")?,
DeserializeExt::take_from_value::<D>(&mut metadata, "timestamp")?,
)
.map_err(de::Error::custom)?)
}
Expand Down
10 changes: 6 additions & 4 deletions synthesizer/src/block/header/serialize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

use super::*;

use snarkvm_utilities::DeserializeExt;

impl<N: Network> Serialize for Header<N> {
/// Serializes the header to a JSON-string or buffer.
fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
Expand All @@ -40,10 +42,10 @@ impl<'de, N: Network> Deserialize<'de> for Header<N> {
true => {
let mut header = serde_json::Value::deserialize(deserializer)?;
Ok(Self::from(
serde_json::from_value(header["previous_state_root"].take()).map_err(de::Error::custom)?,
serde_json::from_value(header["transactions_root"].take()).map_err(de::Error::custom)?,
serde_json::from_value(header["coinbase_accumulator_point"].take()).map_err(de::Error::custom)?,
serde_json::from_value(header["metadata"].take()).map_err(de::Error::custom)?,
DeserializeExt::take_from_value::<D>(&mut header, "previous_state_root")?,
DeserializeExt::take_from_value::<D>(&mut header, "transactions_root")?,
DeserializeExt::take_from_value::<D>(&mut header, "coinbase_accumulator_point")?,
DeserializeExt::take_from_value::<D>(&mut header, "metadata")?,
)
.map_err(de::Error::custom)?)
}
Expand Down
16 changes: 9 additions & 7 deletions synthesizer/src/block/serialize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

use super::*;

use snarkvm_utilities::DeserializeExt;

impl<N: Network> Serialize for Block<N> {
/// Serializes the block to a JSON-string or buffer.
fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
Expand Down Expand Up @@ -45,16 +47,16 @@ impl<'de, N: Network> Deserialize<'de> for Block<N> {
match deserializer.is_human_readable() {
true => {
let mut block = serde_json::Value::deserialize(deserializer)?;
let block_hash: N::BlockHash =
serde_json::from_value(block["block_hash"].take()).map_err(de::Error::custom)?;
let block_hash: N::BlockHash = DeserializeExt::take_from_value::<D>(&mut block, "block_hash")?;

// Recover the block.
let block = Self::from(
serde_json::from_value(block["previous_hash"].take()).map_err(de::Error::custom)?,
serde_json::from_value(block["header"].take()).map_err(de::Error::custom)?,
serde_json::from_value(block["transactions"].take()).map_err(de::Error::custom)?,
serde_json::from_value(block["coinbase"].take()).map_err(de::Error::custom)?,
serde_json::from_value(block["signature"].take()).map_err(de::Error::custom)?,
DeserializeExt::take_from_value::<D>(&mut block, "previous_hash")?,
DeserializeExt::take_from_value::<D>(&mut block, "header")?,
DeserializeExt::take_from_value::<D>(&mut block, "transactions")?,
serde_json::from_value(block.get_mut("coinbase").unwrap_or(&mut serde_json::Value::Null).take())
.map_err(de::Error::custom)?,
DeserializeExt::take_from_value::<D>(&mut block, "signature")?,
)
.map_err(de::Error::custom)?;

Expand Down
26 changes: 15 additions & 11 deletions synthesizer/src/block/transaction/serialize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

use super::*;

use snarkvm_utilities::DeserializeExt;

impl<N: Network> Serialize for Transaction<N> {
/// Serializes the transaction to a JSON-string or buffer.
fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
Expand Down Expand Up @@ -53,28 +55,30 @@ impl<'de, N: Network> Deserialize<'de> for Transaction<N> {
// Deserialize the transaction into a JSON value.
let mut transaction = serde_json::Value::deserialize(deserializer)?;
// Retrieve the transaction ID.
let id: N::TransactionID =
serde_json::from_value(transaction["id"].take()).map_err(de::Error::custom)?;
let id: N::TransactionID = DeserializeExt::take_from_value::<D>(&mut transaction, "id")?;

// Recover the transaction.
let transaction = match transaction["type"].as_str() {
let transaction = match transaction
.get("type")
.ok_or_else(|| de::Error::custom("The \"type\" field is missing"))?
.as_str()
{
Some("deploy") => {
// Retrieve the deployment.
let deployment =
serde_json::from_value(transaction["deployment"].take()).map_err(de::Error::custom)?;
let deployment = DeserializeExt::take_from_value::<D>(&mut transaction, "deployment")?;
// Retrieve the additional fee.
let additional_fee =
serde_json::from_value(transaction["additional_fee"].take()).map_err(de::Error::custom)?;
let additional_fee = DeserializeExt::take_from_value::<D>(&mut transaction, "additional_fee")?;
// Construct the transaction.
Transaction::from_deployment(deployment, additional_fee).map_err(de::Error::custom)?
}
Some("execute") => {
// Retrieve the execution.
let execution =
serde_json::from_value(transaction["execution"].take()).map_err(de::Error::custom)?;
let execution = DeserializeExt::take_from_value::<D>(&mut transaction, "execution")?;
// Retrieve the additional fee, if it exists.
let additional_fee =
serde_json::from_value(transaction["additional_fee"].take()).map_err(de::Error::custom)?;
let additional_fee = serde_json::from_value(
transaction.get_mut("additional_fee").unwrap_or(&mut serde_json::Value::Null).take(),
)
.map_err(de::Error::custom)?;
// Construct the transaction.
Transaction::from_execution(execution, additional_fee).map_err(de::Error::custom)?
}
Expand Down
Loading

0 comments on commit 2b6ac73

Please sign in to comment.