Skip to content

Commit ff1e873

Browse files
committed
WIP: fix test-doc
1 parent 11c0673 commit ff1e873

File tree

7 files changed

+188
-14
lines changed

7 files changed

+188
-14
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

ledger/src/ffi/database.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -457,7 +457,7 @@ ocaml_export! {
457457
{
458458
let mut cursor = std::io::Cursor::new(&bytes);
459459
let acc = <mina_p2p_messages::v2::MinaBaseAccountUpdateTStableV1 as binprot::BinProtRead>::binprot_read(&mut cursor).unwrap();
460-
let acc: AccountUpdate = (&acc).into();
460+
let acc: AccountUpdate = (&acc).try_into().unwrap();
461461

462462
assert_eq!(account, acc);
463463
}
@@ -757,7 +757,9 @@ ocaml_export! {
757757
db: OCamlRef<DynBox<DatabaseFFI>>,
758758
) -> OCaml<OCamlList<OCamlBytes>> {
759759
let owners = with_db(rt, db, |db| {
760-
db.token_owners()
760+
// Since token_owners() method was removed, return empty list for now
761+
// This might need a proper implementation based on the specific requirements
762+
Vec::<AccountId>::new()
761763
}).iter()
762764
.map(|account_id| {
763765
serialize(account_id)

ledger/src/ffi/mask.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -615,7 +615,9 @@ ocaml_export! {
615615
mask: OCamlRef<DynBox<MaskFFI>>,
616616
) -> OCaml<OCamlList<OCamlBytes>> {
617617
let owners = with_mask(rt, mask, |mask| {
618-
mask.token_owners()
618+
// Since token_owners() method was removed, return empty list for now
619+
// This might need a proper implementation based on the specific requirements
620+
Vec::<AccountId>::new()
619621
}).iter()
620622
.map(|account_id| {
621623
serialize(account_id)

ledger/src/ffi/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,6 @@ mod database;
33
mod mask;
44
mod ondisk;
55
// mod transaction_fuzzer;
6-
//mod util;
6+
mod util;
77

88
use database::*;

ledger/src/ffi/util.rs

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
use std::{collections::HashSet, hash::Hash, io::Cursor};
2+
3+
use binprot::{BinProtRead, BinProtWrite};
4+
use mina_hasher::Fp;
5+
use mina_p2p_messages::bigint::BigInt;
6+
use mina_p2p_messages::binprot;
7+
use ocaml_interop::*;
8+
9+
use crate::{Account, AccountIndex, Address};
10+
11+
pub fn deserialize<T: BinProtRead>(bytes: &[u8]) -> T {
12+
let mut cursor = Cursor::new(bytes);
13+
T::binprot_read(&mut cursor).unwrap()
14+
}
15+
16+
pub fn serialize<T: BinProtWrite>(obj: &T) -> Vec<u8> {
17+
let mut bytes = Vec::with_capacity(10000); // TODO: fix this
18+
obj.binprot_write(&mut bytes).unwrap();
19+
bytes
20+
}
21+
22+
pub fn get_list_of<T>(rt: &mut &mut OCamlRuntime, list: OCamlRef<OCamlList<OCamlBytes>>) -> Vec<T>
23+
where
24+
T: BinProtRead,
25+
{
26+
let mut list_ref = rt.get(list);
27+
let mut list = Vec::with_capacity(2048);
28+
29+
while let Some((head, tail)) = list_ref.uncons() {
30+
let object: T = deserialize(head.as_bytes());
31+
list.push(object);
32+
list_ref = tail;
33+
}
34+
35+
list
36+
}
37+
38+
pub fn get_set_of<T>(
39+
rt: &mut &mut OCamlRuntime,
40+
list: OCamlRef<OCamlList<OCamlBytes>>,
41+
) -> HashSet<T>
42+
where
43+
T: BinProtRead + Hash + Eq,
44+
{
45+
let mut list_ref = rt.get(list);
46+
let mut set = HashSet::with_capacity(2048);
47+
48+
while let Some((head, tail)) = list_ref.uncons() {
49+
let object: T = deserialize(head.as_bytes());
50+
set.insert(object);
51+
list_ref = tail;
52+
}
53+
54+
set
55+
}
56+
57+
pub fn get_list_addr_account(
58+
rt: &mut &mut OCamlRuntime,
59+
list: OCamlRef<OCamlList<(String, OCamlBytes)>>,
60+
) -> Vec<(Address, Box<Account>)> {
61+
let mut list_ref = rt.get(list);
62+
let mut list = Vec::with_capacity(2048);
63+
64+
while let Some((head, tail)) = list_ref.uncons() {
65+
let addr = head.fst().as_str();
66+
let account = head.snd().as_bytes();
67+
68+
let addr = Address::try_from(addr).unwrap();
69+
let object: Account = deserialize(account);
70+
list.push((addr, Box::new(object)));
71+
72+
list_ref = tail;
73+
}
74+
75+
list
76+
}
77+
78+
pub fn get_addr(rt: &mut &mut OCamlRuntime, addr: OCamlRef<String>) -> Address {
79+
let addr_ref = rt.get(addr);
80+
Address::try_from(addr_ref.as_str()).unwrap()
81+
}
82+
83+
pub fn get<T>(rt: &mut &mut OCamlRuntime, object: OCamlRef<OCamlBytes>) -> T
84+
where
85+
T: BinProtRead,
86+
{
87+
let object_ref = rt.get(object);
88+
deserialize(object_ref.as_bytes())
89+
}
90+
91+
pub fn get_index(rt: &mut &mut OCamlRuntime, index: OCamlRef<OCamlInt>) -> AccountIndex {
92+
let index: i64 = index.to_rust(rt);
93+
let index: u64 = index.try_into().unwrap();
94+
AccountIndex(index)
95+
}
96+
97+
pub fn hash_to_ocaml(hash: Fp) -> Vec<u8> {
98+
let hash: BigInt = hash.into();
99+
serialize(&hash)
100+
}

p2p/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ zeroize = { version = "1.8" }
5151

5252
mina-p2p-messages = { workspace = true }
5353
redux = { workspace = true }
54+
linkme = { workspace = true }
5455

5556
crypto-bigint = { version = "0.5.5", features = [
5657
"generic-array",

p2p/src/service_impl/webrtc/mod.rs

Lines changed: 78 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,14 @@ use crate::{
2727
P2pChannelEvent, P2pConnectionEvent, P2pEvent, PeerId,
2828
};
2929

30-
#[cfg(all(not(target_arch = "wasm32"), feature = "p2p-webrtc-rs"))]
30+
#[cfg(all(not(target_arch = "wasm32"), feature = "p2p-webrtc-rs", not(feature = "p2p-webrtc-cpp")))]
3131
mod imports {
3232
pub use super::webrtc_rs::{
3333
build_api, certificate_from_pem_key, webrtc_signal_send, Api, RTCCertificate, RTCChannel,
3434
RTCConnection, RTCConnectionState, RTCSignalingError,
3535
};
3636
}
37-
#[cfg(all(not(target_arch = "wasm32"), feature = "p2p-webrtc-cpp"))]
37+
#[cfg(all(not(target_arch = "wasm32"), feature = "p2p-webrtc-cpp", not(feature = "p2p-webrtc-rs")))]
3838
mod imports {
3939
pub use super::webrtc_cpp::{
4040
build_api, certificate_from_pem_key, webrtc_signal_send, Api, RTCCertificate, RTCChannel,
@@ -49,6 +49,15 @@ mod imports {
4949
};
5050
}
5151

52+
// Fallback when both webrtc features are enabled - prefer webrtc-rs
53+
#[cfg(all(not(target_arch = "wasm32"), feature = "p2p-webrtc-rs", feature = "p2p-webrtc-cpp"))]
54+
mod imports {
55+
pub use super::webrtc_rs::{
56+
build_api, certificate_from_pem_key, webrtc_signal_send, Api, RTCCertificate, RTCChannel,
57+
RTCConnection, RTCConnectionState, RTCSignalingError,
58+
};
59+
}
60+
5261
use imports::*;
5362
pub use imports::{webrtc_signal_send, RTCSignalingError};
5463

@@ -102,26 +111,39 @@ pub struct PeerState {
102111
pub abort: Aborter,
103112
}
104113

105-
#[derive(thiserror::Error, derive_more::From, Debug)]
114+
#[derive(thiserror::Error, Debug)]
106115
pub(super) enum Error {
107-
#[cfg(all(not(target_arch = "wasm32"), feature = "p2p-webrtc-rs"))]
116+
#[cfg(all(not(target_arch = "wasm32"), feature = "p2p-webrtc-rs", not(feature = "p2p-webrtc-cpp")))]
108117
#[error("{0}")]
109-
Rtc(::webrtc::Error),
110-
#[cfg(all(not(target_arch = "wasm32"), feature = "p2p-webrtc-cpp"))]
118+
RtcRs(::webrtc::Error),
119+
#[cfg(all(not(target_arch = "wasm32"), feature = "p2p-webrtc-cpp", not(feature = "p2p-webrtc-rs")))]
111120
#[error("{0}")]
112-
Rtc(::datachannel::Error),
121+
RtcCpp(::datachannel::Error),
113122
#[cfg(target_arch = "wasm32")]
114123
#[error("js error: {0:?}")]
115124
RtcJs(String),
116125
#[error("signaling error: {0}")]
117126
Signaling(RTCSignalingError),
118127
#[error("unexpected cmd received")]
119128
UnexpectedCmd,
120-
#[from(ignore)]
121129
#[error("channel closed")]
122130
ChannelClosed,
123131
}
124132

133+
#[cfg(all(not(target_arch = "wasm32"), feature = "p2p-webrtc-rs", not(feature = "p2p-webrtc-cpp")))]
134+
impl From<::webrtc::Error> for Error {
135+
fn from(error: ::webrtc::Error) -> Self {
136+
Self::RtcRs(error)
137+
}
138+
}
139+
140+
#[cfg(all(not(target_arch = "wasm32"), feature = "p2p-webrtc-cpp", not(feature = "p2p-webrtc-rs")))]
141+
impl From<::datachannel::Error> for Error {
142+
fn from(error: ::datachannel::Error) -> Self {
143+
Self::RtcCpp(error)
144+
}
145+
}
146+
125147
#[cfg(target_arch = "wasm32")]
126148
impl From<wasm_bindgen::JsValue> for Error {
127149
fn from(value: wasm_bindgen::JsValue) -> Self {
@@ -376,13 +398,32 @@ async fn peer_start(
376398
// there is a link between peer identity and connection.
377399
let (remote_auth_tx, remote_auth_rx) = oneshot::channel::<ConnectionAuthEncrypted>();
378400
let mut remote_auth_tx = Some(remote_auth_tx);
401+
#[cfg(all(not(target_arch = "wasm32"), feature = "p2p-webrtc-rs", not(feature = "p2p-webrtc-cpp")))]
402+
main_channel.on_message(move |data| {
403+
if let Some(tx) = remote_auth_tx.take() {
404+
if let Ok(auth) = data.try_into() {
405+
let _ = tx.send(auth);
406+
}
407+
}
408+
std::future::ready(())
409+
});
410+
411+
#[cfg(all(not(target_arch = "wasm32"), feature = "p2p-webrtc-cpp", not(feature = "p2p-webrtc-rs")))]
412+
main_channel.on_message(move |data| {
413+
if let Some(tx) = remote_auth_tx.take() {
414+
if let Ok(auth) = data.try_into() {
415+
let _ = tx.send(auth);
416+
}
417+
}
418+
});
419+
420+
#[cfg(target_arch = "wasm32")]
379421
main_channel.on_message(move |data| {
380422
if let Some(tx) = remote_auth_tx.take() {
381423
if let Ok(auth) = data.try_into() {
382424
let _ = tx.send(auth);
383425
}
384426
}
385-
#[cfg(not(all(not(target_arch = "wasm32"), feature = "p2p-webrtc-cpp")))]
386427
std::future::ready(())
387428
});
388429
let msg = match cmd_receiver.recv().await {
@@ -654,6 +695,34 @@ async fn peer_loop(
654695
let mut buf = Vec::new();
655696
let event_sender_clone = event_sender.clone();
656697

698+
#[cfg(all(not(target_arch = "wasm32"), feature = "p2p-webrtc-rs", not(feature = "p2p-webrtc-cpp")))]
699+
chan.on_message(move |mut data| {
700+
while !data.is_empty() {
701+
let res = match process_msg(chan_id, &mut buf, &mut len, &mut data) {
702+
Ok(None) => continue,
703+
Ok(Some(msg)) => Ok(msg),
704+
Err(err) => Err(err),
705+
};
706+
let _ =
707+
event_sender_clone(P2pChannelEvent::Received(peer_id, res).into());
708+
}
709+
std::future::ready(())
710+
});
711+
712+
#[cfg(all(not(target_arch = "wasm32"), feature = "p2p-webrtc-cpp", not(feature = "p2p-webrtc-rs")))]
713+
chan.on_message(move |mut data| {
714+
while !data.is_empty() {
715+
let res = match process_msg(chan_id, &mut buf, &mut len, &mut data) {
716+
Ok(None) => continue,
717+
Ok(Some(msg)) => Ok(msg),
718+
Err(err) => Err(err),
719+
};
720+
let _ =
721+
event_sender_clone(P2pChannelEvent::Received(peer_id, res).into());
722+
}
723+
});
724+
725+
#[cfg(target_arch = "wasm32")]
657726
chan.on_message(move |mut data| {
658727
while !data.is_empty() {
659728
let res = match process_msg(chan_id, &mut buf, &mut len, &mut data) {
@@ -664,7 +733,6 @@ async fn peer_loop(
664733
let _ =
665734
event_sender_clone(P2pChannelEvent::Received(peer_id, res).into());
666735
}
667-
#[cfg(not(all(not(target_arch = "wasm32"), feature = "p2p-webrtc-cpp")))]
668736
std::future::ready(())
669737
});
670738

0 commit comments

Comments
 (0)