Skip to content

feat(json_rpc): accept reading bigints from strings when parsing raw transactions #2621

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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
30 changes: 29 additions & 1 deletion data_structures/src/chain/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,12 @@ use std::{

use bech32::{FromBase32, ToBase32};
use bls_signatures_rs::{bn256, bn256::Bn256, MultiSignature};
use core::fmt::Display;
use ethereum_types::U256;
use failure::Fail;
use futures::future::BoxFuture;
use ordered_float::OrderedFloat;
use serde::{Deserialize, Serialize};
use serde::{Deserialize, Deserializer, Serialize};

use partial_struct::PartialStruct;
use witnet_crypto::{
Expand Down Expand Up @@ -1604,13 +1605,36 @@ pub struct ValueTransferOutput {
/// Address that will receive the value
pub pkh: PublicKeyHash,
/// Transferred value in nanowits
#[serde(deserialize_with = "number_from_string")]
pub value: u64,
/// The value attached to a time-locked output cannot be spent before the specified
/// timestamp. That is, they cannot be used as an input in any transaction of a
/// subsequent block proposed for an epoch whose opening timestamp predates the time lock.
pub time_lock: u64,
}

pub fn number_from_string<'de, T, D>(deserializer: D) -> Result<T, D::Error>
where
D: Deserializer<'de>,
T: FromStr + serde::Deserialize<'de>,
<T as FromStr>::Err: Display,
{
#[derive(Deserialize)]
#[serde(untagged)]
enum StringOrInt<T> {
String(String),
Number(T),
}
if deserializer.is_human_readable() {
match StringOrInt::<T>::deserialize(deserializer)? {
StringOrInt::String(s) => s.parse::<T>().map_err(serde::de::Error::custom),
StringOrInt::Number(i) => Ok(i),
}
} else {
T::deserialize(deserializer)
}
}

impl ValueTransferOutput {
#[inline]
pub fn value(&self) -> u64 {
Expand All @@ -1630,10 +1654,12 @@ pub struct DataRequestOutput {
/// Data request structure
pub data_request: RADRequest,
/// Reward in nanowits that will be earned by honest witnesses
#[serde(deserialize_with = "number_from_string")]
pub witness_reward: u64,
/// Number of witnesses that will resolve this data request
pub witnesses: u16,
/// This fee will be earn by the miner when include commits and/or reveals in the block
#[serde(deserialize_with = "number_from_string")]
pub commit_and_reveal_fee: u64,
/// The minimum percentage of non-error reveals required to consider this data request as
/// "resolved" instead of as "error".
Expand All @@ -1645,6 +1671,7 @@ pub struct DataRequestOutput {
/// this data request.
/// This field must be >= collateral_minimum, or zero.
/// If zero, it will be treated as collateral_minimum.
#[serde(deserialize_with = "number_from_string")]
pub collateral: u64,
}

Expand Down Expand Up @@ -1694,6 +1721,7 @@ impl DataRequestOutput {
pub struct StakeOutput {
pub authorization: KeyedSignature,
pub key: StakeKey<PublicKeyHash>,
#[serde(deserialize_with = "number_from_string")]
pub value: u64,
}

Expand Down
27 changes: 26 additions & 1 deletion data_structures/src/transaction.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
use std::convert::TryFrom;
use std::str::FromStr;
use std::sync::{Arc, RwLock};

use protobuf::Message;
use serde::{Deserialize, Serialize};
use serde::{Deserialize, Deserializer, Serialize};

use witnet_crypto::{
hash::calculate_sha256, merkle::FullMerkleTree, secp256k1::Message as Secp256k1Message,
Expand Down Expand Up @@ -863,14 +864,38 @@ impl UnstakeTransaction {
pub struct UnstakeTransactionBody {
pub operator: PublicKeyHash,
pub withdrawal: ValueTransferOutput,
#[serde(deserialize_with = "number_from_string")]
pub fee: u64,
#[serde(deserialize_with = "number_from_string")]
pub nonce: u64,

#[protobuf_convert(skip)]
#[serde(skip)]
hash: MemoHash,
}

pub fn number_from_string<'de, T, D>(deserializer: D) -> Result<T, D::Error>
where
D: Deserializer<'de>,
T: FromStr + serde::Deserialize<'de>,
<T as FromStr>::Err: std::fmt::Display,
{
#[derive(Deserialize)]
#[serde(untagged)]
enum StringOrInt<T> {
String(String),
Number(T),
}
if deserializer.is_human_readable() {
match StringOrInt::<T>::deserialize(deserializer)? {
StringOrInt::String(s) => s.parse::<T>().map_err(serde::de::Error::custom),
StringOrInt::Number(i) => Ok(i),
}
} else {
T::deserialize(deserializer)
}
}

impl UnstakeTransactionBody {
/// Creates a new stake transaction body.
pub fn new(
Expand Down
Loading