@@ -11,6 +11,7 @@ use primitive_types::U256;
11
11
use rustc_hex:: { FromHex , ToHex } ;
12
12
use serde:: { Deserialize , Serialize } ;
13
13
use ssz:: { Decode , Encode } ;
14
+ use std:: convert:: { TryFrom , TryInto } ;
14
15
use std:: env;
15
16
use std:: error:: Error ;
16
17
use std:: fmt;
@@ -34,6 +35,22 @@ impl From<std::io::Error> for ScoutError {
34
35
}
35
36
}
36
37
38
+ impl From < rustc_hex:: FromHexError > for ScoutError {
39
+ fn from ( error : rustc_hex:: FromHexError ) -> Self {
40
+ ScoutError {
41
+ 0 : error. description ( ) . to_string ( ) ,
42
+ }
43
+ }
44
+ }
45
+
46
+ impl From < serde_yaml:: Error > for ScoutError {
47
+ fn from ( error : serde_yaml:: Error ) -> Self {
48
+ ScoutError {
49
+ 0 : error. description ( ) . to_string ( ) ,
50
+ }
51
+ }
52
+ }
53
+
37
54
impl From < wasmi:: Error > for ScoutError {
38
55
fn from ( error : wasmi:: Error ) -> Self {
39
56
ScoutError {
@@ -705,117 +722,131 @@ struct TestFile {
705
722
deposit_receipts : Vec < TestDeposit > ,
706
723
}
707
724
708
- fn hex_to_slice ( input : & str , output : & mut [ u8 ] ) {
709
- let tmp = input. from_hex ( ) . expect ( "invalid hex data" ) ;
710
- assert ! ( tmp. len( ) == output. len( ) ) ;
725
+ fn hex_to_slice ( input : & str , output : & mut [ u8 ] ) -> Result < ( ) , ScoutError > {
726
+ let tmp = input. from_hex ( ) ?;
727
+ if tmp. len ( ) != output. len ( ) {
728
+ return Err ( ScoutError ( "Length mismatch from hex input" . to_string ( ) ) ) ;
729
+ }
711
730
output. copy_from_slice ( & tmp[ ..] ) ;
731
+ Ok ( ( ) )
712
732
}
713
733
714
- impl From < & String > for Bytes32 {
715
- fn from ( input : & String ) -> Self {
734
+ impl TryFrom < & String > for Bytes32 {
735
+ type Error = ScoutError ;
736
+ fn try_from ( input : & String ) -> Result < Self , Self :: Error > {
716
737
let mut ret = Bytes32 :: default ( ) ;
717
- hex_to_slice ( input, & mut ret. bytes ) ;
718
- ret
738
+ hex_to_slice ( input, & mut ret. bytes ) ? ;
739
+ Ok ( ret)
719
740
}
720
741
}
721
742
722
- impl From < String > for Hash {
723
- fn from ( input : String ) -> Self {
743
+ impl TryFrom < String > for Hash {
744
+ type Error = ScoutError ;
745
+ fn try_from ( input : String ) -> Result < Self , Self :: Error > {
724
746
let mut ret = Hash :: default ( ) ;
725
- hex_to_slice ( & input, & mut ret. 0 ) ;
726
- ret
747
+ hex_to_slice ( & input, & mut ret. 0 ) ? ;
748
+ Ok ( ret)
727
749
}
728
750
}
729
751
730
- impl From < String > for BLSPubKey {
731
- fn from ( input : String ) -> Self {
752
+ impl TryFrom < String > for BLSPubKey {
753
+ type Error = ScoutError ;
754
+ fn try_from ( input : String ) -> Result < Self , Self :: Error > {
732
755
let mut ret = BLSPubKey :: default ( ) ;
733
- hex_to_slice ( & input, & mut ret. 0 ) ;
734
- ret
756
+ hex_to_slice ( & input, & mut ret. 0 ) ? ;
757
+ Ok ( ret)
735
758
}
736
759
}
737
760
738
- impl From < String > for BLSSignature {
739
- fn from ( input : String ) -> Self {
761
+ impl TryFrom < String > for BLSSignature {
762
+ type Error = ScoutError ;
763
+ fn try_from ( input : String ) -> Result < Self , Self :: Error > {
740
764
let mut ret = BLSSignature :: default ( ) ;
741
- hex_to_slice ( & input, & mut ret. 0 ) ;
742
- ret
765
+ hex_to_slice ( & input, & mut ret. 0 ) ? ;
766
+ Ok ( ret)
743
767
}
744
768
}
745
769
746
- impl From < TestBeaconState > for BeaconState {
747
- fn from ( input : TestBeaconState ) -> Self {
748
- BeaconState {
749
- execution_scripts : input
750
- . execution_scripts
751
- . iter ( )
752
- . map ( |filename| ExecutionScript {
753
- code : std:: fs:: read ( filename) . expect ( "to load file" ) ,
770
+ impl TryFrom < TestBeaconState > for BeaconState {
771
+ type Error = ScoutError ;
772
+ fn try_from ( input : TestBeaconState ) -> Result < Self , Self :: Error > {
773
+ let scripts: Result < Vec < ExecutionScript > , ScoutError > = input
774
+ . execution_scripts
775
+ . iter ( )
776
+ . map ( |filename| {
777
+ Ok ( ExecutionScript {
778
+ code : std:: fs:: read ( filename) ?,
754
779
} )
755
- . collect ( ) ,
756
- }
780
+ } )
781
+ . collect ( ) ;
782
+ Ok ( BeaconState {
783
+ execution_scripts : scripts?,
784
+ } )
757
785
}
758
786
}
759
787
760
- impl From < TestShardBlock > for ShardBlock {
761
- fn from ( input : TestShardBlock ) -> Self {
762
- ShardBlock {
788
+ impl TryFrom < TestShardBlock > for ShardBlock {
789
+ type Error = ScoutError ;
790
+ fn try_from ( input : TestShardBlock ) -> Result < Self , Self :: Error > {
791
+ Ok ( ShardBlock {
763
792
env : input. env ,
764
793
data : ShardBlockBody {
765
- data : input. data . from_hex ( ) . expect ( "invalid hex data" ) ,
794
+ data : input. data . from_hex ( ) ? ,
766
795
} ,
767
- }
796
+ } )
768
797
}
769
798
}
770
799
771
- impl From < TestShardState > for ShardState {
772
- fn from ( input : TestShardState ) -> Self {
773
- ShardState {
774
- exec_env_states : input
775
- . exec_env_states
776
- . iter ( )
777
- . map ( |state| state. into ( ) )
778
- . collect ( ) ,
800
+ impl TryFrom < TestShardState > for ShardState {
801
+ type Error = ScoutError ;
802
+ fn try_from ( input : TestShardState ) -> Result < Self , Self :: Error > {
803
+ let states: Result < Vec < Bytes32 > , ScoutError > = input
804
+ . exec_env_states
805
+ . iter ( )
806
+ . map ( |state| state. try_into ( ) )
807
+ . collect ( ) ;
808
+
809
+ Ok ( ShardState {
810
+ exec_env_states : states?,
779
811
slot : 0 ,
780
812
parent_block : ShardBlockHeader { } ,
781
- }
813
+ } )
782
814
}
783
815
}
784
816
785
- impl From < TestDeposit > for Deposit {
786
- fn from ( input : TestDeposit ) -> Self {
787
- Deposit {
788
- pubkey : input. pubkey . into ( ) ,
789
- withdrawal_credentials : input. withdrawal_credentials . into ( ) ,
817
+ impl TryFrom < TestDeposit > for Deposit {
818
+ type Error = ScoutError ;
819
+ fn try_from ( input : TestDeposit ) -> Result < Self , Self :: Error > {
820
+ Ok ( Deposit {
821
+ pubkey : input. pubkey . try_into ( ) ?,
822
+ withdrawal_credentials : input. withdrawal_credentials . try_into ( ) ?,
790
823
amount : input. amount ,
791
- signature : input. signature . into ( ) ,
792
- }
824
+ signature : input. signature . try_into ( ) ? ,
825
+ } )
793
826
}
794
827
}
795
828
796
- fn process_yaml_test ( filename : & str ) {
829
+ fn process_yaml_test ( filename : & str ) -> Result < ( ) , ScoutError > {
797
830
info ! ( "Processing {}..." , filename) ;
798
- let content = std:: fs:: read ( & filename) . expect ( "to load file" ) ;
799
- let test_file: TestFile =
800
- serde_yaml:: from_slice :: < TestFile > ( & content[ ..] ) . expect ( "expected valid yaml" ) ;
831
+ let content = std:: fs:: read ( & filename) ?;
832
+ let test_file: TestFile = serde_yaml:: from_slice :: < TestFile > ( & content[ ..] ) ?;
801
833
debug ! ( "{:#?}" , test_file) ;
802
834
803
- let beacon_state: BeaconState = test_file. beacon_state . into ( ) ;
804
- let pre_state: ShardState = test_file. shard_pre_state . into ( ) ;
805
- let post_state: ShardState = test_file. shard_post_state . into ( ) ;
806
- let expected_deposit_receipts: Vec < Deposit > = test_file
835
+ let beacon_state: BeaconState = test_file. beacon_state . try_into ( ) ? ;
836
+ let pre_state: ShardState = test_file. shard_pre_state . try_into ( ) ? ;
837
+ let post_state: ShardState = test_file. shard_post_state . try_into ( ) ? ;
838
+ let expected_deposit_receipts: Result < Vec < Deposit > , ScoutError > = test_file
807
839
. deposit_receipts
808
840
. into_iter ( )
809
- . map ( |deposit| deposit. into ( ) )
841
+ . map ( |deposit| deposit. try_into ( ) )
810
842
. collect ( ) ;
843
+ let expected_deposit_receipts = expected_deposit_receipts?;
811
844
812
845
let mut shard_state = pre_state;
813
846
let mut deposit_receipts = Vec :: new ( ) ;
814
847
for block in test_file. shard_blocks {
815
848
deposit_receipts. append (
816
- process_shard_block ( & mut shard_state, & beacon_state, Some ( block. into ( ) ) )
817
- . expect ( "processing shard block to succeed" )
818
- . as_mut ( ) ,
849
+ process_shard_block ( & mut shard_state, & beacon_state, Some ( block. try_into ( ) ?) ) ?. as_mut ( ) ,
819
850
) ;
820
851
}
821
852
@@ -827,26 +858,33 @@ fn process_yaml_test(filename: &str) {
827
858
} else {
828
859
println ! ( "Expected deposit receipts: {:?}" , expected_deposit_receipts) ;
829
860
println ! ( "Got deposit receipts: {:?}" , deposit_receipts) ;
830
- std:: process:: exit ( 1 ) ;
861
+ // TODO: make this an error?
862
+ return Ok ( ( ) ) ;
831
863
}
832
864
833
865
debug ! ( "{}" , shard_state) ;
834
866
if shard_state != post_state {
835
867
println ! ( "Expected state: {}" , post_state) ;
836
868
println ! ( "Got state: {}" , shard_state) ;
837
- std:: process:: exit ( 1 ) ;
869
+ // TODO: make this an error?
870
+ return Ok ( ( ) ) ;
838
871
} else {
839
872
println ! ( "Matching state." ) ;
840
873
}
874
+
875
+ Ok ( ( ) )
841
876
}
842
877
843
878
fn main ( ) {
844
879
env_logger:: init ( ) ;
845
880
846
881
let args: Vec < String > = env:: args ( ) . collect ( ) ;
847
- process_yaml_test ( if args. len ( ) != 2 {
882
+ let ret = process_yaml_test ( if args. len ( ) != 2 {
848
883
"test.yaml"
849
884
} else {
850
885
& args[ 1 ]
851
886
} ) ;
887
+ if ret. is_err ( ) {
888
+ println ! ( "Unexpected test failure: {:?}" , ret. err( ) . unwrap( ) )
889
+ }
852
890
}
0 commit comments