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

BigInt -> BigInteger #302

Merged
merged 1 commit into from
Jan 29, 2024
Merged
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
72 changes: 38 additions & 34 deletions chain/rust/src/json/json_serialize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use itertools::Itertools;
use serde::{Deserialize, Deserializer, Serialize, Serializer};
use unicode_segmentation::UnicodeSegmentation;

use crate::utils::BigInt;
use crate::utils::BigInteger;

/**
* Value replaces traditional serde_json::Value in some places.
Expand All @@ -21,7 +21,7 @@ use crate::utils::BigInt;
pub enum Value {
Null,
Bool(bool),
Number(BigInt),
Number(BigInteger),
String(String),
Array(Vec<Value>),
Object(BTreeMap<String, Value>),
Expand Down Expand Up @@ -350,7 +350,7 @@ fn parse_raw_string(string: String) -> Result<Value, JsonParseError> {
"false" => Ok(Value::Bool(false)),
"true" => Ok(Value::Bool(true)),
string => {
let number = BigInt::from_str(string);
let number = BigInteger::from_str(string);
match number {
Ok(number) => Ok(Value::Number(number)),
Err(_) => Err(JsonParseError::InvalidRawString(String::from(string))),
Expand Down Expand Up @@ -553,12 +553,12 @@ impl From<String> for Value {

impl From<u64> for Value {
fn from(number: u64) -> Self {
Value::Number(BigInt::from(number))
Value::Number(BigInteger::from(number))
}
}

impl From<BigInt> for Value {
fn from(number: BigInt) -> Self {
impl From<BigInteger> for Value {
fn from(number: BigInteger) -> Self {
Value::Number(number)
}
}
Expand All @@ -576,7 +576,7 @@ mod tests {
use std::str::FromStr;

use super::{parse_json, tokenize_string, JsonToken, Value};
use crate::utils::BigInt;
use crate::utils::BigInteger;

#[test]
fn run_primitives() {
Expand All @@ -593,20 +593,24 @@ mod tests {
// supported uints
for integer in vec![0, u64::MAX].into_iter() {
assert_eq!(
Value::Number(BigInt::from(integer)).to_string().unwrap(),
Value::Number(BigInteger::from(integer))
.to_string()
.unwrap(),
serde_json::Value::from(integer).to_string()
);
}
// supported ints
for integer in vec![0, i64::MAX, i64::MIN].into_iter() {
assert_eq!(
Value::Number(BigInt::from(integer)).to_string().unwrap(),
Value::Number(BigInteger::from(integer))
.to_string()
.unwrap(),
serde_json::Value::from(integer).to_string()
);
}
// unsupported ints
assert_eq!(
Value::Number(BigInt::from_str("980949788381070983313748912887").unwrap())
Value::Number(BigInteger::from_str("980949788381070983313748912887").unwrap())
.to_string()
.unwrap(),
"980949788381070983313748912887"
Expand Down Expand Up @@ -636,12 +640,12 @@ mod tests {
cml_arr.extend(
vec![0, u64::MAX]
.into_iter()
.map(|integer| Value::Number(BigInt::from(integer))),
.map(|integer| Value::Number(BigInteger::from(integer))),
);
cml_arr.extend(
vec![0, i64::MAX, i64::MIN]
.into_iter()
.map(|integer| Value::Number(BigInt::from(integer))),
.map(|integer| Value::Number(BigInteger::from(integer))),
);
cml_arr.extend(
vec!["supported_string", ""]
Expand Down Expand Up @@ -675,17 +679,17 @@ mod tests {
cml_arr.extend(
vec![0, u64::MAX]
.into_iter()
.map(|integer| Value::Number(BigInt::from(integer))),
.map(|integer| Value::Number(BigInteger::from(integer))),
);
cml_arr.extend(
vec![0, i64::MAX, i64::MIN]
.into_iter()
.map(|integer| Value::Number(BigInt::from(integer))),
.map(|integer| Value::Number(BigInteger::from(integer))),
);
cml_arr.extend(
vec![
BigInt::from_str("980949788381070983313748912887").unwrap(),
BigInt::from_str("-980949788381070983313748912887").unwrap(),
BigInteger::from_str("980949788381070983313748912887").unwrap(),
BigInteger::from_str("-980949788381070983313748912887").unwrap(),
]
.into_iter()
.map(Value::Number),
Expand Down Expand Up @@ -720,12 +724,12 @@ mod tests {
cml_map.extend(vec![0, u64::MAX].into_iter().map(|integer| {
let local = index;
index += 1;
(local.to_string(), Value::Number(BigInt::from(integer)))
(local.to_string(), Value::Number(BigInteger::from(integer)))
}));
cml_map.extend(vec![0, i64::MAX, i64::MIN].into_iter().map(|integer| {
let local = index;
index += 1;
(local.to_string(), Value::Number(BigInt::from(integer)))
(local.to_string(), Value::Number(BigInteger::from(integer)))
}));
cml_map.extend(vec!["supported_string", ""].into_iter().map(|str| {
let local = index;
Expand All @@ -752,7 +756,7 @@ mod tests {

map.insert(
index.to_string(),
Value::Number(BigInt::from_str("980949788381070983313748912887").unwrap()),
Value::Number(BigInteger::from_str("980949788381070983313748912887").unwrap()),
);
index += 1;

Expand Down Expand Up @@ -941,22 +945,22 @@ mod tests {
vec![JsonToken::String {
raw: "1234".to_string(),
}],
Value::Number(BigInt::from_str("1234").unwrap()),
Value::Number(BigInteger::from_str("1234").unwrap()),
),
(
"-1234".to_string(),
vec![JsonToken::String {
raw: "-1234".to_string(),
}],
Value::Number(BigInt::from_str("-1234").unwrap()),
Value::Number(BigInteger::from_str("-1234").unwrap()),
),
(
"123456789876543212345678900000000000000000000".to_string(),
vec![JsonToken::String {
raw: "123456789876543212345678900000000000000000000".to_string(),
}],
Value::Number(
BigInt::from_str("123456789876543212345678900000000000000000000").unwrap(),
BigInteger::from_str("123456789876543212345678900000000000000000000").unwrap(),
),
),
(
Expand All @@ -965,22 +969,22 @@ mod tests {
raw: "-123456789876543212345678900000000000000000000".to_string(),
}],
Value::Number(
BigInt::from_str("-123456789876543212345678900000000000000000000").unwrap(),
BigInteger::from_str("-123456789876543212345678900000000000000000000").unwrap(),
),
),
(
"0".to_string(),
vec![JsonToken::String {
raw: "0".to_string(),
}],
Value::Number(BigInt::from_str("0").unwrap()),
Value::Number(BigInteger::from_str("0").unwrap()),
),
(
"-0".to_string(),
vec![JsonToken::String {
raw: "-0".to_string(),
}],
Value::Number(BigInt::from_str("-0").unwrap()),
Value::Number(BigInteger::from_str("-0").unwrap()),
),
]
}
Expand Down Expand Up @@ -1265,14 +1269,14 @@ mod tests {
"{\"kek\":1}",
Value::Object(BTreeMap::from_iter(vec![(
"kek".to_string(),
Value::Number(BigInt::from(1)),
Value::Number(BigInteger::from(1)),
)])),
),
(
"{\"kek\": 1}",
Value::Object(BTreeMap::from_iter(vec![(
"kek".to_string(),
Value::Number(BigInt::from(1)),
Value::Number(BigInteger::from(1)),
)])),
),
(
Expand Down Expand Up @@ -1337,7 +1341,7 @@ mod tests {
"kek".to_string(),
Value::Array(vec![Value::Object(BTreeMap::from_iter(vec![(
String::new(),
Value::Array(vec![Value::Number(BigInt::from(1))]),
Value::Array(vec![Value::Number(BigInteger::from(1))]),
)]))]),
)])),
),
Expand All @@ -1350,7 +1354,7 @@ mod tests {
Value::Array(vec![Value::Object(BTreeMap::from_iter(vec![(
String::new(),
Value::Array(vec![
Value::Number(BigInt::from(1)),
Value::Number(BigInteger::from(1)),
Value::String("{}[]:,\"{}[]:,\"".to_string()),
]),
)]))]),
Expand All @@ -1359,7 +1363,7 @@ mod tests {
"kek".to_string(),
Value::Array(vec![Value::Object(BTreeMap::from_iter(vec![(
String::new(),
Value::Array(vec![Value::Number(BigInt::from(1))]),
Value::Array(vec![Value::Number(BigInteger::from(1))]),
)]))]),
)])),
]),
Expand All @@ -1371,14 +1375,14 @@ mod tests {
"kek".to_string(),
Value::Array(vec![Value::Object(BTreeMap::from_iter(vec![(
String::new(),
Value::Array(vec![Value::Number(BigInt::from(1))]),
Value::Array(vec![Value::Number(BigInteger::from(1))]),
)]))]),
)])),
Value::Object(BTreeMap::from_iter(vec![(
"kek".to_string(),
Value::Array(vec![Value::Object(BTreeMap::from_iter(vec![(
String::new(),
Value::Array(vec![Value::Number(BigInt::from(1))]),
Value::Array(vec![Value::Number(BigInteger::from(1))]),
)]))]),
)])),
]),
Expand All @@ -1396,14 +1400,14 @@ mod tests {
"kek".to_string(),
Value::Array(vec![Value::Object(BTreeMap::from_iter(vec![(
String::new(),
Value::Array(vec![Value::Number(BigInt::from(1))]),
Value::Array(vec![Value::Number(BigInteger::from(1))]),
)]))]),
)])),
Value::Object(BTreeMap::from_iter(vec![(
"kek".to_string(),
Value::Array(vec![Value::Object(BTreeMap::from_iter(vec![(
String::new(),
Value::Array(vec![Value::Number(BigInt::from(1))]),
Value::Array(vec![Value::Number(BigInteger::from(1))]),
)]))]),
)])),
]),
Expand Down
28 changes: 14 additions & 14 deletions chain/rust/src/json/metadatums.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use wasm_bindgen::prelude::wasm_bindgen;

use crate::{
json::json_serialize::{JsonParseError, Value as JSONValue},
utils::BigInt,
utils::BigInteger,
};

use cml_core::{
Expand Down Expand Up @@ -81,7 +81,7 @@ pub enum MetadataJsonError {
#[error("Bytes not allowed in BasicConversions schema")]
BytesInNoConversions,
#[error("Metadatum ints must fit in 8 bytes: {0}")]
IntTooBig(BigInt),
IntTooBig(BigInteger),
#[error("key type {0:?} not allowed in JSON under specified schema")]
InvalidKeyType(TransactionMetadatum),
#[error("Metadatum structure error (e.g. too big for bounds): {0}")]
Expand Down Expand Up @@ -155,17 +155,17 @@ pub fn encode_json_value_to_metadatum(
JSONValue::Object(json_obj) => {
let mut map = MetadatumMap::new();
for (raw_key, value) in json_obj {
let key = if schema == MetadataJsonSchema::BasicConversions {
match raw_key.parse::<i128>() {
Ok(x) => TransactionMetadatum::new_int(
Int::try_from(x)
.map_err(|_e| MetadataJsonError::IntTooBig(BigInt::from(x)))?,
),
Err(_) => encode_string(raw_key, schema)?,
}
} else {
TransactionMetadatum::new_text(raw_key)?
};
let key =
if schema == MetadataJsonSchema::BasicConversions {
match raw_key.parse::<i128>() {
Ok(x) => TransactionMetadatum::new_int(Int::try_from(x).map_err(
|_e| MetadataJsonError::IntTooBig(BigInteger::from(x)),
)?),
Err(_) => encode_string(raw_key, schema)?,
}
} else {
TransactionMetadatum::new_text(raw_key)?
};
map.set(key, encode_json_value_to_metadatum(value, schema)?);
}
Ok(TransactionMetadatum::new_map(map))
Expand Down Expand Up @@ -312,7 +312,7 @@ pub fn decode_metadatum_to_json_value(
.collect::<Result<Vec<_>, MetadataJsonError>>()?,
),
),
TransactionMetadatum::Int(x) => ("int", JSONValue::Number(BigInt::from_int(x))),
TransactionMetadatum::Int(x) => ("int", JSONValue::Number(BigInteger::from_int(x))),
TransactionMetadatum::Bytes { bytes, .. } => (
"bytes",
match schema {
Expand Down
6 changes: 3 additions & 3 deletions chain/rust/src/json/plutus_datums.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::{
json::json_serialize::{JsonParseError, Value as JSONValue},
plutus::{ConstrPlutusData, PlutusData, PlutusMap},
utils::BigInt,
utils::BigInteger,
};
use std::collections::BTreeMap;
use std::str::FromStr;
Expand Down Expand Up @@ -41,7 +41,7 @@ pub enum CardanoNodePlutusDatumSchema {
/// 1. For ConstrPlutusData there must be two fields "constructor" contianing a number and "fields" containing its fields
/// e.g. { "constructor": 2, "fields": [{"int": 2}, {"list": [{"bytes": "CAFEF00D"}]}]}
/// 2. For all other cases there must be only one field named "int", "bytes", "list" or "map"
/// BigInt's value is a JSON number e.g. {"int": 100}
/// BigInteger's value is a JSON number e.g. {"int": 100}
/// Bytes' value is a hex string representing the bytes WITHOUT any prefix e.g. {"bytes": "CAFEF00D"}
/// Lists' value is a JSON list of its elements encoded via the same schema e.g. {"list": [{"bytes": "CAFEF00D"}]}
/// Maps' value is a JSON list of objects, one for each key-value pair in the map, with keys "k" and "v"
Expand Down Expand Up @@ -115,7 +115,7 @@ pub fn encode_json_value_to_plutus_datum(
.map_err(Into::into)
} else if is_key {
// try as an integer
match BigInt::from_str(s) {
match BigInteger::from_str(s) {
Ok(x) => Ok(PlutusData::new_integer(x)),
// if not, we use the utf8 bytes of the string instead directly
Err(_err) => Ok(PlutusData::new_bytes(s.as_bytes().to_vec())),
Expand Down
6 changes: 3 additions & 3 deletions chain/rust/src/plutus/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ pub mod utils;
use self::cbor_encodings::PlutusV3ScriptEncoding;

use super::{Rational, SubCoin};
use crate::utils::BigInt;
use crate::utils::BigInteger;
use cbor_encodings::{
CostModelsEncoding, ExUnitPricesEncoding, ExUnitsEncoding, PlutusV1ScriptEncoding,
PlutusV2ScriptEncoding, RedeemerEncoding,
Expand Down Expand Up @@ -128,7 +128,7 @@ pub enum PlutusData {
#[serde(skip)]
list_encoding: LenEncoding,
},
Integer(BigInt),
Integer(BigInteger),
Bytes {
bytes: Vec<u8>,
#[derivative(
Expand Down Expand Up @@ -158,7 +158,7 @@ impl PlutusData {
}
}

pub fn new_integer(integer: BigInt) -> Self {
pub fn new_integer(integer: BigInteger) -> Self {
Self::Integer(integer)
}

Expand Down
Loading
Loading