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

Use protobuf instead of postcard for batch serialization #10

Open
wants to merge 1 commit into
base: main
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
78 changes: 67 additions & 11 deletions Cargo.lock

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

78 changes: 68 additions & 10 deletions decode-batch/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
use std::io::{self, Read, Write};
use std::result::Result;

use message::Batch;
use message::proto_message;

fn to_json_string(str: &str) -> Result<String, Box<dyn std::error::Error>> {
let batch = Batch::from_bytes(&alloy_core::hex::decode(str.as_bytes())?)?;
let batch = proto_message::Batch
::from_bytes(&alloy_core::hex::decode(str.as_bytes())?)?;
Ok(serde_json::to_string(&batch)?)
}

Expand All @@ -24,17 +25,74 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {

#[cfg(test)]
mod tests {
use alloy_core::primitives::{address, Bytes, Parity, Signature, U256};
use message::{SignedTransaction, SigningMessage};

use super::*;

#[test]
fn test() {
let j
= to_json_string("0x1463f9725f107358c9115bc9d86c72dd5823e9b1e60114ab7528bb862fb57e8a2bcd567a2e929a0be56a5e000a0d48656c6c6f2c20576f726c643f2076a270f52ade97cd95ef7be45e08ea956bfdaf14b7fc4f8816207fa9eb3a5c17207ccdd94ac1bd86a749b66526fff6579e2b6bf1698e831955332ad9d5ed44da7208000000000000001c").unwrap();

assert_eq!(
j,
r#"{"sequencer_payment_address":"0x63F9725f107358c9115BC9d86c72dD5823E9B1E6","txs":[{"app":"0xab7528bb862fB57E8A2BCd567a2e929a0Be56a5e","nonce":0,"max_gas_price":10,"data":[72,101,108,108,111,44,32,87,111,114,108,100,63],"signature":{"r":"0x76a270f52ade97cd95ef7be45e08ea956bfdaf14b7fc4f8816207fa9eb3a5c17","s":"0x7ccdd94ac1bd86a749b66526fff6579e2b6bf1698e831955332ad9d5ed44da72","v":"0x1c"}}]}"#
);
println!("{:?}", j);
let txns = vec![
SignedTransaction{
message: SigningMessage{
app: address!("ab7528bb862fB57E8A2BCd567a2e929a0Be56a5e"),
nonce: 1,
max_gas_price: 10,
data: Bytes::from(vec![71,101,108,108,111,44,32,87,111,114,108,100,61]),
},
signature: Signature::new(
U256::from(10),
U256::from(20),
Parity::Eip155(30),
),
},
SignedTransaction{
message: SigningMessage{
app: address!("ab7528bb862fB57E8A2BCd567a2e929a0Be56a5e"),
nonce: 101,
max_gas_price: 10001,
data: Bytes::from(vec![72,101,108,108,111,44,32,87,111,114,108,100,62]),
},
signature: Signature::new(
U256::from(101),
U256::from(202),
Parity::NonEip155(true),
),
},
SignedTransaction{
message: SigningMessage{
app: address!("ab7528bb862fB57E8A2BCd567a2e929a0Be56a5e"),
nonce: 1001,
max_gas_price: 10000001,
data: Bytes::from(vec![73,101,108,108,111,44,32,87,111,114,108,100,63]),
},
signature: Signature::new(
U256::from(1001),
U256::from(2002),
Parity::Parity(true),
),
},
];

let proto_txns: Vec<proto_message::Transaction> = txns
.iter()
.map(proto_message::Transaction::from_signed_transaction)
.collect();

let batch = proto_message::Batch{
sequencer_payment_address: Vec::new(),
transactions: proto_txns,
};

let encoded = alloy_core::hex::encode(batch.to_bytes());
let decoded_json = to_json_string(&encoded).unwrap();
let decoded: proto_message::Batch = serde_json::from_str(&decoded_json).unwrap();

let decoded_txns: Vec<SignedTransaction> = decoded.transactions
.iter()
.map(|t| t.to_signed_transaction())
.collect();

assert_eq!(txns, decoded_txns);
}
}
6 changes: 6 additions & 0 deletions message/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
anyhow = "1.0.86"

tracing = "0.1.40"

alloy-core = { version = "0.8.5", features = ["sol-types", "serde"] }
Expand All @@ -16,7 +18,11 @@ postcard = { version = "1.0", features = ["use-std"] }
base64-bytes = "0.1"
ark-serialize = { version = "0.4.2", features = ["derive"] }
derive_more = "0.99.17"
prost = "0.13.3"

[dev-dependencies]
serde_json = "1.0"
alloy-signer-local = { version = "0.3.6", features = ["eip712"] }

[build-dependencies]
prost-build = "0.13.3"
3 changes: 3 additions & 0 deletions message/build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
fn main(){
prost_build::compile_protos(&["src/proto/message.proto"], &["src/proto/"]).unwrap();
}
Loading