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

breaking: Felt type migration + 0.7.1 RPC update #2126

Merged
merged 31 commits into from
Jul 6, 2024
Merged
Changes from 1 commit
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
102 changes: 66 additions & 36 deletions crates/saya/core/src/prover/program_input.rs
Copy link
Member Author

Choose a reason for hiding this comment

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

@neotheprogramist @glihm does changing the serde from decimal to hex break anything here?

Copy link
Collaborator

@glihm glihm Jul 5, 2024

Choose a reason for hiding this comment

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

@neotheprogramist the serialization is mostly used to sent this input into the calldata for the world on settlement layer, correct? If yes, not breaking to have hex instead of decimal.

Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,10 @@
let mut seq = serializer.serialize_seq(Some(da.len()))?;

for d in da {
let decimal: BigDecimal = d.to_bigint().into(); // Convert with no decimal places
let num = decimal.to_string();
seq.serialize_element(&num)?;
// let decimal: BigDecimal = d.to_bigint().into(); // Convert with no decimal places
// let num = decimal.to_string();
// seq.serialize_element(&num)?;
seq.serialize_element(&d)?;
}

seq.end()
Expand Down Expand Up @@ -181,7 +182,7 @@
})
}

pub fn da_as_calldata(&self, world: Felt) -> Vec<Felt> {

Check warning on line 185 in crates/saya/core/src/prover/program_input.rs

View check run for this annotation

Codecov / codecov/patch

crates/saya/core/src/prover/program_input.rs#L185

Added line #L185 was not covered by tests
let updates = self
.state_updates
.storage_updates
Expand Down Expand Up @@ -259,10 +260,10 @@
let serialized =
inputs.iter().flat_map(|input| input.serialize_to_prover_args()).collect::<Vec<_>>();

let joined = serialized
.iter()
.map(|f| BigDecimal::from(f.to_bigint()).to_string())
.collect::<Vec<_>>();

Check warning on line 266 in crates/saya/core/src/prover/program_input.rs

View check run for this annotation

Codecov / codecov/patch

crates/saya/core/src/prover/program_input.rs#L263-L266

Added lines #L263 - L266 were not covered by tests

format!("[{} {}]", inputs.len(), joined.join(" "))
}
Expand All @@ -289,9 +290,10 @@
let serialized = message.serialize().unwrap();
// Instead of adding serialized as an array, add each element individually
for field_element in serialized {
let decimal: BigDecimal = field_element.to_bigint().into(); // Assuming no decimal places for simplicity
let num = decimal.to_string();
seq.serialize_element(&num)?;
// let decimal: BigDecimal = field_element.to_bigint().into(); // Assuming no
// decimal places for simplicity let num = decimal.to_string();
// seq.serialize_element(&num)?;
seq.serialize_element(&field_element)?;
}
}
seq.end()
Expand Down Expand Up @@ -332,8 +334,22 @@
.next_element::<String>()?
.map(|num| Felt::from_str(&num.to_string()).unwrap())
.unwrap_or_default();

let payload_length_str = seq.next_element::<String>()?.unwrap_or_default();
let payload_length: usize = payload_length_str.parse().unwrap_or_default();
// TODO: for compatibility reason, the length can be in either decimal or hex
// format. maybe should just expect all values to be in hex format.
let payload_length = payload_length_str
.parse::<usize>()
.or_else(|_| {
usize::from_str_radix(
payload_length_str
.strip_prefix("0x")
.unwrap_or(&payload_length_str),
16,
)
})
.expect("invalid length value");

let mut payload = Vec::new();
for _ in 0..payload_length {
if let Some(element) = seq
Expand Down Expand Up @@ -379,9 +395,10 @@
for message in messages {
let serialized = message.serialize().unwrap();
for field_element in serialized {
let decimal: BigDecimal = field_element.to_bigint().into(); // Assuming no decimal places for simplicity
let num = decimal.to_string();
seq.serialize_element(&num)?;
// let decimal: BigDecimal = field_element.to_bigint().into(); // Assuming no
// decimal places for simplicity let num = decimal.to_string();
// seq.serialize_element(&num)?;
seq.serialize_element(&field_element)?;
}
}
seq.end()
Expand Down Expand Up @@ -430,8 +447,22 @@
.next_element::<String>()?
.map(|num| Felt::from_str(&num.to_string()).unwrap())
.unwrap_or_default();

let payload_length_str = seq.next_element::<String>()?.unwrap_or_default();
let payload_length: usize = payload_length_str.parse().unwrap_or_default();
// TODO: for compatibility reason, the length can be in either decimal or hex
// format. maybe should just expect all values to be in hex format.
let payload_length = payload_length_str
.parse::<usize>()
.or_else(|_| {
usize::from_str_radix(
payload_length_str
.strip_prefix("0x")
.unwrap_or(&payload_length_str),
16,
)
})
.expect("invalid length value");

let mut payload = Vec::new();
for _ in 0..payload_length {
if let Some(element) = seq
Expand Down Expand Up @@ -462,30 +493,29 @@
use std::str::FromStr;

let input = r#"{
"prev_state_root":"101",
"block_number":102,
"block_hash":"103",
"config_hash":"104",
"message_to_starknet_segment":["105","106","1","1"],
"message_to_appchain_segment":["108","109","110","111","1","112"],
"prev_state_root":"0x65",
"block_number": 102,
"block_hash":"0x67",
"config_hash":"0x68",
"message_to_starknet_segment":["0x69","0x6a","0x1","0x1"],
"message_to_appchain_segment":["0x6c","0x6d","0x6e","0x6f","0x1","0x70"],
"storage_updates":{
"42": {
"2010": "1200",
"2012": "1300"
"0x2a": {
"0x7dc": "0x514",
"0x7da": "0x4b0"
}
},
"nonce_updates":{
"1111": "22222",
"1116": "22223"
"0x457": "0x56ce",
"0x45c": "0x56cf"
},
"contract_updates":{
"3": "437267489"
"0x3": "0x1a102c21"
},
"declared_classes":{
"1234": "12345"
"0x4d2": "0x3039"
}
}"#;

let mut expected = ProgramInput {
prev_state_root: Felt::from_str("101")?,
block_number: 102,
Expand Down Expand Up @@ -620,31 +650,31 @@
#[test]
fn test_serialize_to_prover_args() -> anyhow::Result<()> {
let input = r#"{
"prev_state_root":"101",
"prev_state_root":"0x65",
"block_number":102,
"block_hash":"103",
"config_hash":"104",
"block_hash":"0x67",
"config_hash":"0x68",
"nonce_updates":{
"1111": "22222"
"0x457": "0x56ce"
},
"storage_updates":{
"333": {
"4444": "555"
"0x14d": {
"0x115c": "0x22b"
}
},
"contract_updates":{
"66666": "7777"
"0x1046a": "0x1e61"
},
"declared_classes":{
"88888": "99999"
"0x15b38": "0x1869f"
},
"message_to_starknet_segment":["123","456","123","128"],
"message_to_appchain_segment":["108","109","110","111","1","112"]
"message_to_starknet_segment":["0x7b","0x1c8","0x7b","0x80"],
"message_to_appchain_segment":["0x6c","0x6d","0x6e","0x6f","0x1","0x70"]
}"#;
let mut input = serde_json::from_str::<ProgramInput>(input)?;
input.fill_da(Felt::from_str("333")?);

println!("{:?}", input);
// println!("{:?}", input);

let serialized = input.serialize_to_prover_args();

Expand Down
Loading