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

Add mocked starknet transaction type for client node interaction #9

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions sequencer/node/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ edition = "2018"
publish = false

[dependencies]
cairo-felt = "0.5.2"
tokio = { version = "1.1.0", features = ["time", "macros", "net", "rt-multi-thread"] }
tokio-util = { version = "0.7.3", features = ["codec"] }
log = "0.4.0"
Expand Down
39 changes: 35 additions & 4 deletions sequencer/node/src/client.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
use anyhow::{Context, Result};
use bytes::BufMut as _;
use bytes::BytesMut;
use cairo_felt::Felt252;
use clap::Parser;
use env_logger::Env;
use futures::future::join_all;
use futures::sink::SinkExt as _;
use log::{info, warn};
use mempool::TransactionType;
use rand::Rng;
use rpc_endpoint::rpc::types::Transaction::Invoke;
use rpc_endpoint::rpc::InvokeTransaction::{self, V1};
use rpc_endpoint::rpc::InvokeTransactionV1;
use rpc_endpoint::rpc::Transaction;
use std::net::SocketAddr;
use tokio::net::TcpStream;
use tokio::time::{interval, sleep, Duration, Instant};
Expand Down Expand Up @@ -113,8 +118,19 @@ impl Client {
tx.put_u8(0u8); // Sample txs start with 0.
tx.put_u64(counter); // This counter identifies the tx.

let transaction_type = TransactionType::ExecuteFibonacci(200 + counter);
let bytes = bincode::serialize(&transaction_type).unwrap();
// let transaction_type = TransactionType::ExecuteFibonacci(200 + counter);
let starknet_transaction = InvokeTransactionV1 {
transaction_hash: Felt252::new(1920310231),
max_fee: Felt252::new(89853483),
signature: vec![Felt252::new(183728913)],
nonce: Felt252::new(762716321),
sender_address: Felt252::new(91232018),
calldata: vec![Felt252::new(8126371)],
};

let starknet_transaction_str =
serde_json::to_string(&starknet_transaction).unwrap();
let bytes = starknet_transaction_str.as_bytes().to_owned();
for b in bytes {
tx.put_u8(b);
}
Expand All @@ -123,13 +139,28 @@ impl Client {

tx.put_u8(1u8); // Standard txs start with 1.
tx.put_u64(r); // Ensures all clients send different txs.
let transaction_type = TransactionType::ExecuteFibonacci(200);
let bytes = bincode::serialize(&transaction_type).unwrap();

let starknet_transaction = InvokeTransactionV1 {
transaction_hash: Felt252::new(1920310231),
max_fee: Felt252::new(89853483),
signature: vec![Felt252::new(183728913)],
nonce: Felt252::new(762716321),
sender_address: Felt252::new(91232018),
calldata: vec![Felt252::new(8126371)],
};

let starknet_transaction_str =
serde_json::to_string(&starknet_transaction).unwrap();
let bytes = starknet_transaction_str.as_bytes().to_owned();

for b in bytes {
tx.put_u8(b);
}
};
if self.size < tx.len() {
warn!("Transaction size too big");
break 'main;
}
tx.resize(self.size, 0u8);
let bytes = tx.split().freeze();

Expand Down
57 changes: 35 additions & 22 deletions sequencer/node/src/node.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
use crate::config::Export as _;
use crate::config::{Committee, ConfigError, Parameters, Secret};
use cairo_felt::Felt252;
use consensus::{Block, Consensus};
use crypto::SignatureService;

use log::info;
use mempool::{Mempool, MempoolMessage, TransactionType};
use mempool::{Mempool, MempoolMessage};
use rpc_endpoint::new_server;

use rpc_endpoint::rpc::InvokeTransactionV1;
use store::Store;
use tokio::sync::mpsc::{channel, Receiver};

Expand All @@ -18,6 +20,8 @@ pub const CHANNEL_CAPACITY: usize = 1_000;
/// Default port offset for RPC endpoint
const RPC_PORT_OFFSET: u16 = 1000;

// What type is V1(InvokeTransactionV1)?

pub struct Node {
pub commit: Receiver<Block>,
pub store: Store,
Expand Down Expand Up @@ -123,27 +127,36 @@ impl Node {
);

for (i, m) in batch_txs.into_iter().enumerate() {
let transaction_type: TransactionType =
bincode::deserialize(&m[9..]).unwrap();
info!(
"Message {i} in {:?} is of tx_type {:?}",
p, transaction_type
);

match transaction_type {
TransactionType::ExecuteFibonacci(_) => {
let res = Command::new("../cairo_native/target/release/cli")
.arg("run")
.arg("-f")
.arg("fib::fib::main")
.arg("../cairo_programs/fib.cairo")
.arg("--available-gas")
.arg("900000000")
.output()
.expect("Failed to execute process");
info!("Output: {}", String::from_utf8_lossy(&res.stdout));
}
}
// Deserializes the bytes of the tx into a string. We are doing this to avoid default binary serialization.
let _starknet_tx_string =
String::from_utf8((&m[9..]).to_vec()).unwrap();

//Commented it for now due to "trailing characters" error.
// let starknet_tx: InvokeTransactionV1 =
// serde_json::from_str::<InvokeTransactionV1>(&starknet_tx_string)
// .unwrap();

let starknet_tx = InvokeTransactionV1 {
transaction_hash: Felt252::new(1920310231),
max_fee: Felt252::new(89853483),
signature: vec![Felt252::new(183728913)],
nonce: Felt252::new(762716321),
sender_address: Felt252::new(91232018),
calldata: vec![Felt252::new(8126371)],
};
info!("Message {i} in {:?} is of tx_type {:?}", p, starknet_tx);

//Executing fib with pre-refactor cairo_native
let res = Command::new("../cairo_native/target/release/cli")
.arg("run")
.arg("-f")
.arg("fib::fib::main")
.arg("../cairo_programs/fib.cairo")
.arg("--available-gas")
.arg("900000000")
.output()
.expect("Failed to execute process");
info!("Output: {}", String::from_utf8_lossy(&res.stdout));
}
}
MempoolMessage::BatchRequest(_, _) => {
Expand Down