Skip to content
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
26 changes: 23 additions & 3 deletions node/common/src/service/block_producer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ mod vrf_evaluator;
use ledger::proofs::{
block::BlockParams, gates::get_provers, generate_block_proof, transaction::ProofError,
};
use mina_p2p_messages::v2::{
MinaBaseProofStableV2, ProverExtendBlockchainInputStableV2, StateHash,
use mina_p2p_messages::{
binprot::BinProtWrite,
v2::{MinaBaseProofStableV2, ProverExtendBlockchainInputStableV2, StateHash},
};
use node::{
account::AccountSecretKey,
Expand Down Expand Up @@ -98,8 +99,27 @@ impl node::service::BlockProducerService for crate::NodeService {

let tx = self.event_sender().clone();
thread::spawn(move || {
let res = prove(input, keypair, false).map_err(|err| format!("{err:?}"));
let res = prove(input.clone(), keypair, false).map_err(|err| format!("{err:?}"));
if res.is_err() {
// IMPORTANT: Make sure that `input` here is a copy from before `prove` is called, we don't
// want to leak the private key.
if let Err(error) = dump_failed_block_proof_input(block_hash.clone(), input) {
eprintln!("ERROR when dumping failed block proof inputs: {}", error);
}
}
let _ = tx.send(BlockProducerEvent::BlockProve(block_hash, res).into());
});
}
}

fn dump_failed_block_proof_input(
block_hash: StateHash,
input: Box<ProverExtendBlockchainInputStableV2>,
) -> std::io::Result<()> {
let filename = format!("/tmp/failed_block_proof_input_{block_hash}.binprot");
println!("Dumping failed block proof to {filename}");
let mut file = std::fs::File::create(&filename)?;
input.binprot_write(&mut file)?;
file.sync_all()?;
Ok(())
}
15 changes: 15 additions & 0 deletions node/src/ledger/ledger_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -386,6 +386,21 @@ impl LedgerManager {
}
}

pub fn get_accounts(
&self,
ledger_hash: &LedgerHash,
account_ids: Vec<AccountId>,
) -> Result<Vec<Account>, String> {
// TODO: this should be asynchronous
match self.call_sync(LedgerRequest::AccountsGet {
ledger_hash: ledger_hash.clone(),
account_ids,
}) {
Ok(LedgerResponse::AccountsGet(result)) => result,
_ => panic!("get_accounts failed"),
}
}

#[allow(clippy::type_complexity)]
pub fn producers_with_delegates(
&self,
Expand Down
31 changes: 16 additions & 15 deletions node/src/transaction_pool/transaction_pool_actions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use ledger::{
diff::{self, BestTipDiff, DiffVerified},
ValidCommandWithHash,
},
Account, AccountId, BaseLedger as _,
Account, AccountId,
};
use mina_p2p_messages::{
list::List,
Expand Down Expand Up @@ -108,29 +108,30 @@ impl TransactionPoolEffectfulAction {
openmina_core::log::system_time();
kind = "Info",
summary = "fetching accounts for tx pool");
let best_tip_mask = match store.service().ledger_manager().get_mask(&ledger_hash) {
Some((mask, _)) => mask,
None => {
// FIXME: the ledger ctx `get_accounts` function doesn't ensure that every account we
// asked for is included in the result.
// TODO: should be asynchronous. Once asynchronous, watch out for race
// conditions between tx pool and transition frontier. By the time the
// accounts have been fetched the best tip may have changed already.
let accounts = match store
.service()
.ledger_manager()
.get_accounts(&ledger_hash, account_ids.iter().cloned().collect())
{
Ok(accounts) => accounts,
Err(err) => {
openmina_core::log::error!(
openmina_core::log::system_time();
kind = "Error",
summary = "failed to fetch accounts for tx pool",
error = format!("ledger {:?} not found", ledger_hash));
error = format!("ledger {:?}, error: {:?}", ledger_hash, err));
return;
}
};

let accounts = account_ids
let accounts = accounts
.into_iter()
.filter_map(|account_id| {
best_tip_mask
.location_of_account(&account_id)
.and_then(|addr| {
best_tip_mask
.get(addr)
.map(|account| (account_id, *account))
})
})
.map(|account| (account.id(), account))
.collect::<BTreeMap<_, _>>();

store.dispatch_callback(on_result, (accounts, pending_id, from_rpc));
Expand Down
2 changes: 1 addition & 1 deletion p2p/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@ fn main() {
],
&["src/network/pubsub", "src/network/identify"],
)
.unwrap();
.expect("Proto build failed");
}
26 changes: 10 additions & 16 deletions p2p/src/identity/peer_id.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,9 @@ impl FromStr for PeerId {
if size != 33 {
return Err(bs58::decode::Error::BufferTooSmall);
}
Ok(Self::from_bytes(bytes[1..33].try_into().unwrap()))
Ok(Self::from_bytes(
bytes[1..33].try_into().expect("Size checked above"),
))
}
}

Expand Down Expand Up @@ -142,14 +144,6 @@ impl TryFrom<PeerId> for libp2p_identity::PeerId {
}
}

impl PartialEq<libp2p_identity::PeerId> for PeerId {
fn eq(&self, other: &libp2p_identity::PeerId) -> bool {
let key = libp2p_identity::PublicKey::try_decode_protobuf(other.as_ref().digest()).unwrap();
let bytes = key.try_into_ed25519().unwrap().to_bytes();
self == &PeerId::from_bytes(bytes)
}
}

impl Serialize for PeerId {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
Expand Down Expand Up @@ -209,26 +203,26 @@ mod tests {
#[test]
fn test_peer_id_bs58() {
let s = "2bEgBrPTzL8wov2D4Kz34WVLCxR4uCarsBmHYXWKQA5wvBQzd9H";
let id: PeerId = s.parse().unwrap();
let id: PeerId = s.parse().expect("Parsing failed");
assert_eq!(s, id.to_string());
}

#[test]
fn test_libp2p_peer_id_conv() {
let s = "12D3KooWEiGVAFC7curXWXiGZyMWnZK9h8BKr88U8D5PKV3dXciv";
let id: libp2p_identity::PeerId = s.parse().unwrap();
let conv: PeerId = id.try_into().unwrap();
let id_conv: libp2p_identity::PeerId = conv.try_into().unwrap();
let id: libp2p_identity::PeerId = s.parse().expect("Parsing failed");
let conv: PeerId = id.try_into().expect("Parsing failed");
let id_conv: libp2p_identity::PeerId = conv.try_into().expect("Parsing failed");
assert_eq!(id_conv, id);
}

#[test]
#[ignore = "doesn't work"]
fn test_bare_base58btc_pk() {
let s = "QmSXffHzFVSEoQCYBS1bPpCn4vgGEpQnCA9NLYuhamPBU3";
let id: libp2p_identity::PeerId = s.parse().unwrap();
let conv: PeerId = id.try_into().unwrap();
let id_conv: libp2p_identity::PeerId = conv.try_into().unwrap();
let id: libp2p_identity::PeerId = s.parse().expect("Error parsing");
let conv: PeerId = id.try_into().expect("Error converting");
let id_conv: libp2p_identity::PeerId = conv.try_into().expect("Error converting");
assert_eq!(id_conv, id);
}
}
2 changes: 1 addition & 1 deletion p2p/src/identity/public_key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ impl FromStr for PublicKey {
bs58::decode::Error::BufferTooSmall.to_string(),
));
}
Self::from_bytes(bytes[1..33].try_into().unwrap())
Self::from_bytes(bytes[1..33].try_into().expect("Size checked above"))
.map_err(|err| PublicKeyFromStrError::Ed25519(err.to_string()))
}
}
Expand Down
4 changes: 3 additions & 1 deletion p2p/src/identity/secret_key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,9 @@ impl FromStr for SecretKey {
bs58::decode::Error::BufferTooSmall.to_string(),
));
}
Ok(Self::from_bytes(bytes[1..33].try_into().unwrap()))
Ok(Self::from_bytes(
bytes[1..33].try_into().expect("Size checked above"),
))
}
}

Expand Down
23 changes: 15 additions & 8 deletions p2p/src/network/kad/p2p_network_kad_internals.rs
Original file line number Diff line number Diff line change
Expand Up @@ -643,7 +643,7 @@ mod tests {
}

fn entry_with_peer_id(peer_id: PeerId) -> P2pNetworkKadEntry {
let key = peer_id.try_into().unwrap();
let key = peer_id.try_into().expect("Error converting PeerId");
P2pNetworkKadEntry {
key,
peer_id,
Expand All @@ -655,11 +655,12 @@ mod tests {
#[test]
fn test_key_generation() {
let random_peer_id = SecretKey::rand().public_key().peer_id();
let libp2p_peer_id = libp2p_identity::PeerId::try_from(random_peer_id).unwrap();
let libp2p_peer_id =
libp2p_identity::PeerId::try_from(random_peer_id).expect("Conversion failed");
let cid = CID::from(libp2p_peer_id);

let key0 = P2pNetworkKadKey::try_from(&random_peer_id).unwrap();
let key1 = P2pNetworkKadKey::try_from(random_peer_id).unwrap();
let key0 = P2pNetworkKadKey::try_from(&random_peer_id).expect("Conversion failed");
let key1 = P2pNetworkKadKey::try_from(random_peer_id).expect("Conversion failed");
let key2 = P2pNetworkKadKey::from(cid);

assert_eq!(key0, key1);
Expand Down Expand Up @@ -756,14 +757,17 @@ mod tests {
let closest = BTreeSet::from_iter(iter);
println!("{}", closest.len());

let max_closest_dist = closest.iter().max_by_key(|e| entry.dist(e)).unwrap();
let max_closest_dist = closest
.iter()
.max_by_key(|e| entry.dist(e))
.expect("Failed to find entry");
let min_non_closest_dist = rt
.buckets
.iter()
.flat_map(|e| e.iter())
.filter(|e| !closest.contains(*e))
.min_by_key(|e| entry.dist(e))
.unwrap();
.expect("Failed to find entry");

let max = entry.dist(max_closest_dist);
let min = entry.dist(min_non_closest_dist);
Expand Down Expand Up @@ -791,15 +795,18 @@ mod tests {
let find_node = rt.find_node(&entry.key);
let closest = BTreeSet::from_iter(find_node);

let max_closest_dist = closest.iter().max_by_key(|e| entry.dist(e)).unwrap();
let max_closest_dist = closest
.iter()
.max_by_key(|e| entry.dist(e))
.expect("Error finding entry");
let min_non_closest_dist = rt
.buckets
.iter()
.flat_map(|e| e.iter())
.filter(|e| e.key != entry.key && e.key != rt.this_key)
.filter(|e| !closest.contains(*e))
.min_by_key(|e| entry.dist(e))
.unwrap();
.expect("Error finding entry");

let max = entry.dist(max_closest_dist);
let min = entry.dist(min_non_closest_dist);
Expand Down
24 changes: 14 additions & 10 deletions p2p/src/network/kad/p2p_network_kad_protocol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -302,9 +302,10 @@ pub mod tests {
#[test]
fn cid_generation() {
let random_peer_id = SecretKey::rand().public_key().peer_id();
let libp2p_peer_id = libp2p_identity::PeerId::try_from(random_peer_id).unwrap();
let libp2p_peer_id =
libp2p_identity::PeerId::try_from(random_peer_id).expect("PeerId conversion failed");

let cid0 = CID::try_from(random_peer_id).unwrap();
let cid0 = CID::try_from(random_peer_id).expect("Error generating CID");
let cid1 = CID::from(libp2p_peer_id);

assert_eq!(cid0, cid1);
Expand All @@ -319,14 +320,14 @@ pub mod tests {

let peer_id = "2bEgBrPTzL8wov2D4Kz34WVLCxR4uCarsBmHYXWKQA5wvBQzd9H"
.parse::<PeerId>()
.unwrap();
.expect("Error parsing peer id");
assert_eq!(
from_bytes(
&libp2p_identity::PeerId::try_from(peer_id)
.unwrap()
.expect("Error converting to PeerId")
.to_bytes()
)
.unwrap(),
.expect("Error generating from bytes"),
peer_id
);
}
Expand All @@ -347,16 +348,19 @@ pub mod tests {
"/ip4/198.51.100.1/tcp/80",
"/dns4/ams-2.bootstrap.libp2p.io/tcp/443",
] {
let multiaddr = multiaddr.parse::<Multiaddr>().unwrap();
assert_eq!(from_bytes(&multiaddr.to_vec()).unwrap(), multiaddr);
let multiaddr = multiaddr.parse::<Multiaddr>().expect("Failed to parse");
assert_eq!(
from_bytes(&multiaddr.to_vec()).expect("Error converting from bytes"),
multiaddr
);
}
}

#[test]
fn find_nodes_from_wire() {
let input = "2c0804500a1226002408011220bcbfc53faa51a1410b7599c1e4411d5ac45ed5a1ffdc4673c1a6e2b9e9125c4d";

let bytes = hex::decode(input).unwrap();
let bytes = hex::decode(input).expect("Error decoding");
let protobuf_message = BytesReader::from_bytes(&bytes)
.read_message::<super::super::Message>(&bytes)
.expect("should be able to decode");
Expand All @@ -375,9 +379,9 @@ pub mod tests {
fn find_nodes_from_wire_len() {
let input = "2c0804500a1226002408011220bcbfc53faa51a1410b7599c1e4411d5ac45ed5a1ffdc4673c1a6e2b9e9125c4d";

let bytes = hex::decode(input).unwrap();
let bytes = hex::decode(input).expect("Error decoding");
let from_bytes = &mut BytesReader::from_bytes(&bytes);
let len = from_bytes.read_varint32(&bytes).unwrap();
let len = from_bytes.read_varint32(&bytes).expect("Error reading len");

println!("{} {}", len, from_bytes.len());
let protobuf_message = BytesReader::from_bytes(&bytes)
Expand Down
10 changes: 0 additions & 10 deletions p2p/src/network/pubsub/p2p_network_pubsub_effects.rs
Original file line number Diff line number Diff line change
Expand Up @@ -222,16 +222,6 @@ impl P2pNetworkPubsubAction {
}
P2pNetworkPubsubAction::OutgoingMessage { msg, peer_id } => {
if !message_is_empty(&msg) {
// println!(
// "(pubsub) {this} -> {peer_id}, {:?}, {:?}, {}",
// msg.subscriptions,
// msg.control,
// msg.publish.len()
// );
// for ele in &msg.publish {
// let id = super::p2p_network_pubsub_state::compute_message_id(ele);
// println!("{}", std::str::from_utf8(&id).unwrap());
// }
let mut data = vec![];
if prost::Message::encode_length_delimited(&msg, &mut data).is_err() {
store.dispatch(P2pNetworkPubsubAction::OutgoingMessageError {
Expand Down
2 changes: 0 additions & 2 deletions p2p/src/p2p_reducer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,8 +126,6 @@ impl P2pState {
crate::identify::P2pIdentifyAction::UpdatePeerInformation { peer_id, info } => {
if let Some(peer) = state.peers.get_mut(peer_id) {
peer.identify = Some(*info.clone());
} else {
unreachable!()
}
}
}
Expand Down
Loading
Loading