Skip to content

Commit 3c83225

Browse files
authored
Merge pull request #671 from openmina/develop
Merge `develop` to `main`
2 parents 37fd0cf + feba39c commit 3c83225

File tree

21 files changed

+125
-1267
lines changed

21 files changed

+125
-1267
lines changed

node/common/src/service/block_producer/mod.rs

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,9 @@ mod vrf_evaluator;
33
use ledger::proofs::{
44
block::BlockParams, gates::get_provers, generate_block_proof, transaction::ProofError,
55
};
6-
use mina_p2p_messages::v2::{
7-
MinaBaseProofStableV2, ProverExtendBlockchainInputStableV2, StateHash,
6+
use mina_p2p_messages::{
7+
binprot::BinProtWrite,
8+
v2::{MinaBaseProofStableV2, ProverExtendBlockchainInputStableV2, StateHash},
89
};
910
use node::{
1011
account::AccountSecretKey,
@@ -98,8 +99,27 @@ impl node::service::BlockProducerService for crate::NodeService {
9899

99100
let tx = self.event_sender().clone();
100101
thread::spawn(move || {
101-
let res = prove(input, keypair, false).map_err(|err| format!("{err:?}"));
102+
let res = prove(input.clone(), keypair, false).map_err(|err| format!("{err:?}"));
103+
if res.is_err() {
104+
// IMPORTANT: Make sure that `input` here is a copy from before `prove` is called, we don't
105+
// want to leak the private key.
106+
if let Err(error) = dump_failed_block_proof_input(block_hash.clone(), input) {
107+
eprintln!("ERROR when dumping failed block proof inputs: {}", error);
108+
}
109+
}
102110
let _ = tx.send(BlockProducerEvent::BlockProve(block_hash, res).into());
103111
});
104112
}
105113
}
114+
115+
fn dump_failed_block_proof_input(
116+
block_hash: StateHash,
117+
input: Box<ProverExtendBlockchainInputStableV2>,
118+
) -> std::io::Result<()> {
119+
let filename = format!("/tmp/failed_block_proof_input_{block_hash}.binprot");
120+
println!("Dumping failed block proof to {filename}");
121+
let mut file = std::fs::File::create(&filename)?;
122+
input.binprot_write(&mut file)?;
123+
file.sync_all()?;
124+
Ok(())
125+
}

node/src/ledger/ledger_manager.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -386,6 +386,21 @@ impl LedgerManager {
386386
}
387387
}
388388

389+
pub fn get_accounts(
390+
&self,
391+
ledger_hash: &LedgerHash,
392+
account_ids: Vec<AccountId>,
393+
) -> Result<Vec<Account>, String> {
394+
// TODO: this should be asynchronous
395+
match self.call_sync(LedgerRequest::AccountsGet {
396+
ledger_hash: ledger_hash.clone(),
397+
account_ids,
398+
}) {
399+
Ok(LedgerResponse::AccountsGet(result)) => result,
400+
_ => panic!("get_accounts failed"),
401+
}
402+
}
403+
389404
#[allow(clippy::type_complexity)]
390405
pub fn producers_with_delegates(
391406
&self,

node/src/transaction_pool/transaction_pool_actions.rs

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use ledger::{
55
diff::{self, BestTipDiff, DiffVerified},
66
ValidCommandWithHash,
77
},
8-
Account, AccountId, BaseLedger as _,
8+
Account, AccountId,
99
};
1010
use mina_p2p_messages::{
1111
list::List,
@@ -108,29 +108,30 @@ impl TransactionPoolEffectfulAction {
108108
openmina_core::log::system_time();
109109
kind = "Info",
110110
summary = "fetching accounts for tx pool");
111-
let best_tip_mask = match store.service().ledger_manager().get_mask(&ledger_hash) {
112-
Some((mask, _)) => mask,
113-
None => {
111+
// FIXME: the ledger ctx `get_accounts` function doesn't ensure that every account we
112+
// asked for is included in the result.
113+
// TODO: should be asynchronous. Once asynchronous, watch out for race
114+
// conditions between tx pool and transition frontier. By the time the
115+
// accounts have been fetched the best tip may have changed already.
116+
let accounts = match store
117+
.service()
118+
.ledger_manager()
119+
.get_accounts(&ledger_hash, account_ids.iter().cloned().collect())
120+
{
121+
Ok(accounts) => accounts,
122+
Err(err) => {
114123
openmina_core::log::error!(
115124
openmina_core::log::system_time();
116125
kind = "Error",
117126
summary = "failed to fetch accounts for tx pool",
118-
error = format!("ledger {:?} not found", ledger_hash));
127+
error = format!("ledger {:?}, error: {:?}", ledger_hash, err));
119128
return;
120129
}
121130
};
122131

123-
let accounts = account_ids
132+
let accounts = accounts
124133
.into_iter()
125-
.filter_map(|account_id| {
126-
best_tip_mask
127-
.location_of_account(&account_id)
128-
.and_then(|addr| {
129-
best_tip_mask
130-
.get(addr)
131-
.map(|account| (account_id, *account))
132-
})
133-
})
134+
.map(|account| (account.id(), account))
134135
.collect::<BTreeMap<_, _>>();
135136

136137
store.dispatch_callback(on_result, (accounts, pending_id, from_rpc));

p2p/build.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,5 @@ fn main() {
88
],
99
&["src/network/pubsub", "src/network/identify"],
1010
)
11-
.unwrap();
11+
.expect("Proto build failed");
1212
}

p2p/src/identity/peer_id.rs

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,9 @@ impl FromStr for PeerId {
107107
if size != 33 {
108108
return Err(bs58::decode::Error::BufferTooSmall);
109109
}
110-
Ok(Self::from_bytes(bytes[1..33].try_into().unwrap()))
110+
Ok(Self::from_bytes(
111+
bytes[1..33].try_into().expect("Size checked above"),
112+
))
111113
}
112114
}
113115

@@ -142,14 +144,6 @@ impl TryFrom<PeerId> for libp2p_identity::PeerId {
142144
}
143145
}
144146

145-
impl PartialEq<libp2p_identity::PeerId> for PeerId {
146-
fn eq(&self, other: &libp2p_identity::PeerId) -> bool {
147-
let key = libp2p_identity::PublicKey::try_decode_protobuf(other.as_ref().digest()).unwrap();
148-
let bytes = key.try_into_ed25519().unwrap().to_bytes();
149-
self == &PeerId::from_bytes(bytes)
150-
}
151-
}
152-
153147
impl Serialize for PeerId {
154148
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
155149
where
@@ -209,26 +203,26 @@ mod tests {
209203
#[test]
210204
fn test_peer_id_bs58() {
211205
let s = "2bEgBrPTzL8wov2D4Kz34WVLCxR4uCarsBmHYXWKQA5wvBQzd9H";
212-
let id: PeerId = s.parse().unwrap();
206+
let id: PeerId = s.parse().expect("Parsing failed");
213207
assert_eq!(s, id.to_string());
214208
}
215209

216210
#[test]
217211
fn test_libp2p_peer_id_conv() {
218212
let s = "12D3KooWEiGVAFC7curXWXiGZyMWnZK9h8BKr88U8D5PKV3dXciv";
219-
let id: libp2p_identity::PeerId = s.parse().unwrap();
220-
let conv: PeerId = id.try_into().unwrap();
221-
let id_conv: libp2p_identity::PeerId = conv.try_into().unwrap();
213+
let id: libp2p_identity::PeerId = s.parse().expect("Parsing failed");
214+
let conv: PeerId = id.try_into().expect("Parsing failed");
215+
let id_conv: libp2p_identity::PeerId = conv.try_into().expect("Parsing failed");
222216
assert_eq!(id_conv, id);
223217
}
224218

225219
#[test]
226220
#[ignore = "doesn't work"]
227221
fn test_bare_base58btc_pk() {
228222
let s = "QmSXffHzFVSEoQCYBS1bPpCn4vgGEpQnCA9NLYuhamPBU3";
229-
let id: libp2p_identity::PeerId = s.parse().unwrap();
230-
let conv: PeerId = id.try_into().unwrap();
231-
let id_conv: libp2p_identity::PeerId = conv.try_into().unwrap();
223+
let id: libp2p_identity::PeerId = s.parse().expect("Error parsing");
224+
let conv: PeerId = id.try_into().expect("Error converting");
225+
let id_conv: libp2p_identity::PeerId = conv.try_into().expect("Error converting");
232226
assert_eq!(id_conv, id);
233227
}
234228
}

p2p/src/identity/public_key.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ impl FromStr for PublicKey {
6161
bs58::decode::Error::BufferTooSmall.to_string(),
6262
));
6363
}
64-
Self::from_bytes(bytes[1..33].try_into().unwrap())
64+
Self::from_bytes(bytes[1..33].try_into().expect("Size checked above"))
6565
.map_err(|err| PublicKeyFromStrError::Ed25519(err.to_string()))
6666
}
6767
}

p2p/src/identity/secret_key.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,9 @@ impl FromStr for SecretKey {
7777
bs58::decode::Error::BufferTooSmall.to_string(),
7878
));
7979
}
80-
Ok(Self::from_bytes(bytes[1..33].try_into().unwrap()))
80+
Ok(Self::from_bytes(
81+
bytes[1..33].try_into().expect("Size checked above"),
82+
))
8183
}
8284
}
8385

p2p/src/network/kad/p2p_network_kad_internals.rs

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -643,7 +643,7 @@ mod tests {
643643
}
644644

645645
fn entry_with_peer_id(peer_id: PeerId) -> P2pNetworkKadEntry {
646-
let key = peer_id.try_into().unwrap();
646+
let key = peer_id.try_into().expect("Error converting PeerId");
647647
P2pNetworkKadEntry {
648648
key,
649649
peer_id,
@@ -655,11 +655,12 @@ mod tests {
655655
#[test]
656656
fn test_key_generation() {
657657
let random_peer_id = SecretKey::rand().public_key().peer_id();
658-
let libp2p_peer_id = libp2p_identity::PeerId::try_from(random_peer_id).unwrap();
658+
let libp2p_peer_id =
659+
libp2p_identity::PeerId::try_from(random_peer_id).expect("Conversion failed");
659660
let cid = CID::from(libp2p_peer_id);
660661

661-
let key0 = P2pNetworkKadKey::try_from(&random_peer_id).unwrap();
662-
let key1 = P2pNetworkKadKey::try_from(random_peer_id).unwrap();
662+
let key0 = P2pNetworkKadKey::try_from(&random_peer_id).expect("Conversion failed");
663+
let key1 = P2pNetworkKadKey::try_from(random_peer_id).expect("Conversion failed");
663664
let key2 = P2pNetworkKadKey::from(cid);
664665

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

759-
let max_closest_dist = closest.iter().max_by_key(|e| entry.dist(e)).unwrap();
760+
let max_closest_dist = closest
761+
.iter()
762+
.max_by_key(|e| entry.dist(e))
763+
.expect("Failed to find entry");
760764
let min_non_closest_dist = rt
761765
.buckets
762766
.iter()
763767
.flat_map(|e| e.iter())
764768
.filter(|e| !closest.contains(*e))
765769
.min_by_key(|e| entry.dist(e))
766-
.unwrap();
770+
.expect("Failed to find entry");
767771

768772
let max = entry.dist(max_closest_dist);
769773
let min = entry.dist(min_non_closest_dist);
@@ -791,15 +795,18 @@ mod tests {
791795
let find_node = rt.find_node(&entry.key);
792796
let closest = BTreeSet::from_iter(find_node);
793797

794-
let max_closest_dist = closest.iter().max_by_key(|e| entry.dist(e)).unwrap();
798+
let max_closest_dist = closest
799+
.iter()
800+
.max_by_key(|e| entry.dist(e))
801+
.expect("Error finding entry");
795802
let min_non_closest_dist = rt
796803
.buckets
797804
.iter()
798805
.flat_map(|e| e.iter())
799806
.filter(|e| e.key != entry.key && e.key != rt.this_key)
800807
.filter(|e| !closest.contains(*e))
801808
.min_by_key(|e| entry.dist(e))
802-
.unwrap();
809+
.expect("Error finding entry");
803810

804811
let max = entry.dist(max_closest_dist);
805812
let min = entry.dist(min_non_closest_dist);

p2p/src/network/kad/p2p_network_kad_protocol.rs

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -302,9 +302,10 @@ pub mod tests {
302302
#[test]
303303
fn cid_generation() {
304304
let random_peer_id = SecretKey::rand().public_key().peer_id();
305-
let libp2p_peer_id = libp2p_identity::PeerId::try_from(random_peer_id).unwrap();
305+
let libp2p_peer_id =
306+
libp2p_identity::PeerId::try_from(random_peer_id).expect("PeerId conversion failed");
306307

307-
let cid0 = CID::try_from(random_peer_id).unwrap();
308+
let cid0 = CID::try_from(random_peer_id).expect("Error generating CID");
308309
let cid1 = CID::from(libp2p_peer_id);
309310

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

320321
let peer_id = "2bEgBrPTzL8wov2D4Kz34WVLCxR4uCarsBmHYXWKQA5wvBQzd9H"
321322
.parse::<PeerId>()
322-
.unwrap();
323+
.expect("Error parsing peer id");
323324
assert_eq!(
324325
from_bytes(
325326
&libp2p_identity::PeerId::try_from(peer_id)
326-
.unwrap()
327+
.expect("Error converting to PeerId")
327328
.to_bytes()
328329
)
329-
.unwrap(),
330+
.expect("Error generating from bytes"),
330331
peer_id
331332
);
332333
}
@@ -347,16 +348,19 @@ pub mod tests {
347348
"/ip4/198.51.100.1/tcp/80",
348349
"/dns4/ams-2.bootstrap.libp2p.io/tcp/443",
349350
] {
350-
let multiaddr = multiaddr.parse::<Multiaddr>().unwrap();
351-
assert_eq!(from_bytes(&multiaddr.to_vec()).unwrap(), multiaddr);
351+
let multiaddr = multiaddr.parse::<Multiaddr>().expect("Failed to parse");
352+
assert_eq!(
353+
from_bytes(&multiaddr.to_vec()).expect("Error converting from bytes"),
354+
multiaddr
355+
);
352356
}
353357
}
354358

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

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

378-
let bytes = hex::decode(input).unwrap();
382+
let bytes = hex::decode(input).expect("Error decoding");
379383
let from_bytes = &mut BytesReader::from_bytes(&bytes);
380-
let len = from_bytes.read_varint32(&bytes).unwrap();
384+
let len = from_bytes.read_varint32(&bytes).expect("Error reading len");
381385

382386
println!("{} {}", len, from_bytes.len());
383387
let protobuf_message = BytesReader::from_bytes(&bytes)

p2p/src/network/pubsub/p2p_network_pubsub_effects.rs

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -222,16 +222,6 @@ impl P2pNetworkPubsubAction {
222222
}
223223
P2pNetworkPubsubAction::OutgoingMessage { msg, peer_id } => {
224224
if !message_is_empty(&msg) {
225-
// println!(
226-
// "(pubsub) {this} -> {peer_id}, {:?}, {:?}, {}",
227-
// msg.subscriptions,
228-
// msg.control,
229-
// msg.publish.len()
230-
// );
231-
// for ele in &msg.publish {
232-
// let id = super::p2p_network_pubsub_state::compute_message_id(ele);
233-
// println!("{}", std::str::from_utf8(&id).unwrap());
234-
// }
235225
let mut data = vec![];
236226
if prost::Message::encode_length_delimited(&msg, &mut data).is_err() {
237227
store.dispatch(P2pNetworkPubsubAction::OutgoingMessageError {

0 commit comments

Comments
 (0)