5
5
// library. accordingly, it is grouped by conversions needed for each RPC endpoint.
6
6
7
7
use crate :: util:: tendermint_proxy:: v1 as penumbra_pb;
8
+ use anyhow:: anyhow;
8
9
9
10
// === get_tx ===
10
11
@@ -323,9 +324,7 @@ impl From<tendermint::merkle::proof::ProofOp> for crate::tendermint::crypto::Pro
323
324
// === get_block_by_height ===
324
325
325
326
impl TryFrom < tendermint_rpc:: endpoint:: block:: Response > for penumbra_pb:: GetBlockByHeightResponse {
326
- // TODO(kate): ideally this would not return a tonic status object, but we'll use this for
327
- // now to avoid invasively refactoring this code.
328
- type Error = tonic:: Status ;
327
+ type Error = anyhow:: Error ;
329
328
fn try_from (
330
329
tendermint_rpc:: endpoint:: block:: Response {
331
330
block,
@@ -338,11 +337,8 @@ impl TryFrom<tendermint_rpc::endpoint::block::Response> for penumbra_pb::GetBloc
338
337
} )
339
338
}
340
339
}
341
-
342
340
impl TryFrom < tendermint:: Block > for crate :: tendermint:: types:: Block {
343
- // TODO(kate): ideally this would not return a tonic status object, but we'll use this for
344
- // now to avoid invasively refactoring this code.
345
- type Error = tonic:: Status ;
341
+ type Error = anyhow:: Error ;
346
342
fn try_from (
347
343
tendermint:: Block {
348
344
header,
@@ -356,14 +352,76 @@ impl TryFrom<tendermint::Block> for crate::tendermint::types::Block {
356
352
header : header. try_into ( ) . map ( Some ) ?,
357
353
data : Some ( crate :: tendermint:: types:: Data { txs : data } ) ,
358
354
evidence : evidence. try_into ( ) . map ( Some ) ?,
359
- last_commit : Some (
360
- last_commit
361
- . map ( crate :: tendermint:: types:: Commit :: try_from)
362
- . transpose ( ) ?
363
- // TODO(kate): this probably should not panic, but this is here to preserve
364
- // existing behavior. panic if no last commit is set.
365
- . expect ( "last_commit" ) ,
366
- ) ,
355
+ last_commit : last_commit
356
+ . map ( crate :: tendermint:: types:: Commit :: try_from)
357
+ . transpose ( ) ?,
358
+ } )
359
+ }
360
+ }
361
+
362
+ impl TryFrom < crate :: tendermint:: types:: PartSetHeader > for tendermint:: block:: parts:: Header {
363
+ type Error = anyhow:: Error ;
364
+ fn try_from (
365
+ crate :: tendermint:: types:: PartSetHeader { total, hash } : crate :: tendermint:: types:: PartSetHeader ,
366
+ ) -> Result < Self , Self :: Error > {
367
+ Ok ( Self :: new ( total, hash. try_into ( ) ?) ?)
368
+ }
369
+ }
370
+
371
+ impl TryFrom < crate :: tendermint:: types:: Header > for tendermint:: block:: Header {
372
+ type Error = anyhow:: Error ;
373
+ fn try_from (
374
+ crate :: tendermint:: types:: Header {
375
+ version,
376
+ chain_id,
377
+ height,
378
+ time,
379
+ last_block_id,
380
+ last_commit_hash,
381
+ data_hash,
382
+ validators_hash,
383
+ next_validators_hash,
384
+ consensus_hash,
385
+ app_hash,
386
+ last_results_hash,
387
+ evidence_hash,
388
+ proposer_address,
389
+ } : crate :: tendermint:: types:: Header ,
390
+ ) -> Result < Self , Self :: Error > {
391
+ Ok ( Self {
392
+ version : tendermint:: block:: header:: Version {
393
+ block : version. clone ( ) . ok_or ( anyhow ! ( "version" ) ) ?. block ,
394
+ app : version. ok_or ( anyhow ! ( "version" ) ) ?. app ,
395
+ } ,
396
+ chain_id : tendermint:: chain:: Id :: try_from ( chain_id) ?,
397
+ height : tendermint:: block:: Height :: try_from ( height) ?,
398
+ time : tendermint:: Time :: from_unix_timestamp (
399
+ time. clone ( ) . ok_or ( anyhow ! ( "time" ) ) ?. seconds ,
400
+ time. clone ( )
401
+ . ok_or ( anyhow ! ( "missing time" ) ) ?
402
+ . nanos
403
+ . try_into ( ) ?,
404
+ ) ?,
405
+ last_block_id : match last_block_id {
406
+ Some ( last_block_id) => Some ( tendermint:: block:: Id {
407
+ hash : tendermint:: Hash :: try_from ( last_block_id. hash ) ?,
408
+ part_set_header : tendermint:: block:: parts:: Header :: try_from (
409
+ last_block_id
410
+ . part_set_header
411
+ . ok_or ( anyhow:: anyhow!( "bad part set header" ) ) ?,
412
+ ) ?,
413
+ } ) ,
414
+ None => None ,
415
+ } ,
416
+ last_commit_hash : Some ( last_commit_hash. try_into ( ) ?) ,
417
+ data_hash : Some ( data_hash. try_into ( ) ?) ,
418
+ validators_hash : validators_hash. try_into ( ) ?,
419
+ next_validators_hash : next_validators_hash. try_into ( ) ?,
420
+ consensus_hash : consensus_hash. try_into ( ) ?,
421
+ app_hash : app_hash. try_into ( ) ?,
422
+ last_results_hash : Some ( last_results_hash. try_into ( ) ?) ,
423
+ evidence_hash : Some ( evidence_hash. try_into ( ) ?) ,
424
+ proposer_address : proposer_address. try_into ( ) ?,
367
425
} )
368
426
}
369
427
}
@@ -394,7 +452,11 @@ impl TryFrom<tendermint::block::Header> for crate::tendermint::types::Header {
394
452
// around a `time::PrimitiveDateTime` however it's private so we
395
453
// have to use string parsing to get to the prost type we want :(
396
454
let header_time = chrono:: DateTime :: parse_from_rfc3339 ( time. to_rfc3339 ( ) . as_str ( ) )
397
- . expect ( "timestamp should roundtrip to string" ) ;
455
+ . or_else ( |_| {
456
+ Err ( tonic:: Status :: invalid_argument (
457
+ "timestamp should roundtrip to string" ,
458
+ ) )
459
+ } ) ?;
398
460
Ok ( Self {
399
461
version : Some ( crate :: tendermint:: version:: Consensus {
400
462
block : version. block ,
@@ -404,10 +466,7 @@ impl TryFrom<tendermint::block::Header> for crate::tendermint::types::Header {
404
466
height : height. into ( ) ,
405
467
time : Some ( pbjson_types:: Timestamp {
406
468
seconds : header_time. timestamp ( ) ,
407
- nanos : header_time
408
- . timestamp_nanos_opt ( )
409
- . ok_or_else ( || tonic:: Status :: invalid_argument ( "missing header_time nanos" ) ) ?
410
- as i32 ,
469
+ nanos : header_time. timestamp_subsec_nanos ( ) as i32 ,
411
470
} ) ,
412
471
last_block_id : last_block_id. map ( |id| crate :: tendermint:: types:: BlockId {
413
472
hash : id. hash . into ( ) ,
@@ -430,9 +489,7 @@ impl TryFrom<tendermint::block::Header> for crate::tendermint::types::Header {
430
489
}
431
490
432
491
impl TryFrom < tendermint:: evidence:: List > for crate :: tendermint:: types:: EvidenceList {
433
- // TODO(kate): ideally this would not return a tonic status object, but we'll use this for
434
- // now to avoid invasively refactoring this code.
435
- type Error = tonic:: Status ;
492
+ type Error = anyhow:: Error ;
436
493
fn try_from ( list : tendermint:: evidence:: List ) -> Result < Self , Self :: Error > {
437
494
list. into_vec ( )
438
495
. into_iter ( )
@@ -443,11 +500,9 @@ impl TryFrom<tendermint::evidence::List> for crate::tendermint::types::EvidenceL
443
500
}
444
501
445
502
// TODO(kate): this should be decomposed further at a later point, i am refraining from doing
446
- // so right now. there are `Option::expect()` calls below that should be considered.
503
+ // so right now.
447
504
impl TryFrom < tendermint:: evidence:: Evidence > for crate :: tendermint:: types:: Evidence {
448
- // TODO(kate): ideally this would not return a tonic status object, but we'll use this for
449
- // now to avoid invasively refactoring this code.
450
- type Error = tonic:: Status ;
505
+ type Error = anyhow:: Error ;
451
506
fn try_from ( evidence : tendermint:: evidence:: Evidence ) -> Result < Self , Self :: Error > {
452
507
use { chrono:: DateTime , std:: ops:: Deref } ;
453
508
Ok ( Self {
@@ -469,21 +524,27 @@ impl TryFrom<tendermint::evidence::Evidence> for crate::tendermint::types::Evide
469
524
height : e. votes ( ) . 0 . height . into ( ) ,
470
525
round : e. votes ( ) . 0 . round . into ( ) ,
471
526
block_id : Some ( crate :: tendermint:: types:: BlockId {
472
- hash : e. votes ( ) . 0 . block_id . expect ( "block id" ) . hash . into ( ) ,
527
+ hash : e
528
+ . votes ( )
529
+ . 0
530
+ . block_id
531
+ . ok_or ( anyhow ! ( "block id" ) ) ?
532
+ . hash
533
+ . into ( ) ,
473
534
part_set_header : Some (
474
535
crate :: tendermint:: types:: PartSetHeader {
475
536
total : e
476
537
. votes ( )
477
538
. 0
478
539
. block_id
479
- . expect ( "block id" )
540
+ . ok_or ( anyhow ! ( "block id" ) ) ?
480
541
. part_set_header
481
542
. total ,
482
543
hash : e
483
544
. votes ( )
484
545
. 0
485
546
. block_id
486
- . expect ( "block id" )
547
+ . ok_or ( anyhow ! ( "block id" ) ) ?
487
548
. part_set_header
488
549
. hash
489
550
. into ( ) ,
@@ -492,18 +553,25 @@ impl TryFrom<tendermint::evidence::Evidence> for crate::tendermint::types::Evide
492
553
} ) ,
493
554
timestamp : Some ( pbjson_types:: Timestamp {
494
555
seconds : DateTime :: parse_from_rfc3339 (
495
- & e. votes ( ) . 0 . timestamp . expect ( "timestamp" ) . to_rfc3339 ( ) ,
556
+ & e. votes ( )
557
+ . 0
558
+ . timestamp
559
+ . ok_or ( tonic:: Status :: invalid_argument (
560
+ "bad timestamp" ,
561
+ ) ) ?
562
+ . to_rfc3339 ( ) ,
496
563
)
497
- . expect ( "timestamp should roundtrip to string" )
564
+ . or_else ( |_| {
565
+ Err ( tonic:: Status :: invalid_argument ( "bad timestamp" ) )
566
+ } ) ?
498
567
. timestamp ( ) ,
499
568
nanos : DateTime :: parse_from_rfc3339 (
500
569
& e. votes ( ) . 0 . timestamp . expect ( "timestamp" ) . to_rfc3339 ( ) ,
501
570
)
502
571
. expect ( "timestamp should roundtrip to string" )
503
- . timestamp_nanos_opt ( )
504
- . ok_or_else ( || {
505
- tonic:: Status :: invalid_argument ( "missing timestamp nanos" )
506
- } ) ? as i32 ,
572
+ . timestamp_subsec_nanos ( )
573
+ . try_into ( )
574
+ . expect ( "good round trip timestamps" ) ,
507
575
} ) ,
508
576
validator_address : e. votes ( ) . 0 . validator_address . into ( ) ,
509
577
validator_index : e. votes ( ) . 0 . validator_index . into ( ) ,
@@ -558,10 +626,9 @@ impl TryFrom<tendermint::evidence::Evidence> for crate::tendermint::types::Evide
558
626
& e. votes ( ) . 1 . timestamp . expect ( "timestamp" ) . to_rfc3339 ( ) ,
559
627
)
560
628
. expect ( "timestamp should roundtrip to string" )
561
- . timestamp_nanos_opt ( )
562
- . ok_or_else ( || {
563
- tonic:: Status :: invalid_argument ( "missing timestamp nanos" )
564
- } ) ? as i32 ,
629
+ . timestamp_subsec_nanos ( )
630
+ . try_into ( )
631
+ . expect ( "good round trip timestamps" ) ,
565
632
} ) ,
566
633
validator_address : e. votes ( ) . 1 . validator_address . into ( ) ,
567
634
validator_index : e. votes ( ) . 1 . validator_index . into ( ) ,
@@ -652,12 +719,11 @@ impl TryFrom<tendermint::block::CommitSig> for crate::tendermint::types::CommitS
652
719
. timestamp ( ) ,
653
720
nanos : DateTime :: parse_from_rfc3339 ( & timestamp. to_rfc3339 ( ) )
654
721
. expect ( "timestamp should roundtrip to string" )
655
- . timestamp_nanos_opt ( )
656
- . ok_or_else ( || {
657
- tonic:: Status :: invalid_argument ( "missing timestamp nanos" )
658
- } ) ? as i32 ,
722
+ . timestamp_subsec_nanos ( )
723
+ . try_into ( )
724
+ . expect ( "good round trip timestamps" ) ,
659
725
} ) ,
660
- signature : signature. expect ( "signature" ) . into ( ) ,
726
+ signature : signature. map ( Into :: into ) . unwrap_or_default ( ) ,
661
727
} ,
662
728
tendermint:: block:: CommitSig :: BlockIdFlagNil {
663
729
validator_address,
@@ -672,10 +738,9 @@ impl TryFrom<tendermint::block::CommitSig> for crate::tendermint::types::CommitS
672
738
. timestamp ( ) ,
673
739
nanos : DateTime :: parse_from_rfc3339 ( & timestamp. to_rfc3339 ( ) )
674
740
. expect ( "timestamp should roundtrip to string" )
675
- . timestamp_nanos_opt ( )
676
- . ok_or_else ( || {
677
- tonic:: Status :: invalid_argument ( "missing timestamp nanos" )
678
- } ) ? as i32 ,
741
+ . timestamp_subsec_nanos ( )
742
+ . try_into ( )
743
+ . expect ( "good round trip timestamps" ) ,
679
744
} ) ,
680
745
signature : signature. expect ( "signature" ) . into ( ) ,
681
746
} ,
0 commit comments