@@ -19,9 +19,11 @@ use std::sync::Arc;
1919use ccrypto:: Blake ;
2020use ckey:: { recover, Address , Public , Signature } ;
2121use client:: ConsensusClient ;
22+ use cmerkle:: TrieError ;
2223use consensus:: stake:: get_validators;
2324use consensus:: vote_collector:: Message ;
24- use ctypes:: errors:: SyntaxError ;
25+ use cstate:: VerifyResult ;
26+ use ctypes:: errors:: { RuntimeError , SyntaxError } ;
2527use ctypes:: CommonParams ;
2628use primitives:: { Bytes , H256 } ;
2729use rlp:: { Decodable , DecoderError , Encodable , RlpStream , UntrustedRlp } ;
@@ -63,11 +65,7 @@ pub enum Action<M: Message> {
6365}
6466
6567impl < M : Message > Action < M > {
66- pub fn verify (
67- & self ,
68- current_params : & CommonParams ,
69- client : Option < Arc < ConsensusClient > > ,
70- ) -> Result < ( ) , SyntaxError > {
68+ pub fn verify ( & self , current_params : & CommonParams , client : Option < Arc < ConsensusClient > > ) -> VerifyResult < ( ) > {
7169 match self {
7270 Action :: TransferCCS {
7371 ..
@@ -86,7 +84,8 @@ impl<M: Message> Action<M> {
8684 return Err ( SyntaxError :: InvalidCustomAction ( format ! (
8785 "Too long candidata metadata: the size limit is {}" ,
8886 current_params. max_candidate_metadata_size( )
89- ) ) )
87+ ) )
88+ . into ( ) )
9089 }
9190 }
9291 Action :: ChangeParams {
@@ -100,7 +99,8 @@ impl<M: Message> Action<M> {
10099 return Err ( SyntaxError :: InvalidCustomAction ( format ! (
101100 "The current network id is {} but the transaction tries to change the network id to {}" ,
102101 current_network_id, transaction_network_id
103- ) ) )
102+ ) )
103+ . into ( ) )
104104 }
105105 params. verify ( ) . map_err ( SyntaxError :: InvalidCustomAction ) ?;
106106 let action = Action :: < M > :: ChangeParams {
@@ -121,12 +121,13 @@ impl<M: Message> Action<M> {
121121 message2,
122122 } => {
123123 if message1 == message2 {
124- return Err ( SyntaxError :: InvalidCustomAction ( String :: from ( "messages are duplicated" ) ) )
124+ return Err ( SyntaxError :: InvalidCustomAction ( String :: from ( "messages are duplicated" ) ) . into ( ) )
125125 }
126126 if message1. round ( ) != message2. round ( ) {
127127 return Err ( SyntaxError :: InvalidCustomAction ( String :: from (
128128 "The messages are from two different voting rounds" ,
129- ) ) )
129+ ) )
130+ . into ( ) )
130131 }
131132
132133 let signer_idx1 = message1. signer_index ( ) ;
@@ -136,40 +137,36 @@ impl<M: Message> Action<M> {
136137 let client = client. expect ( "Client should be initialized" ) ;
137138 let parent_block_hash = signed_block_hash
138139 . and_then ( |hash| client. block_header ( & hash. into ( ) ) )
139- . ok_or_else ( || {
140- SyntaxError :: InvalidCustomAction ( String :: from (
141- "Cannot get parent header from message's block_hash" ,
142- ) )
143- } ) ?
140+ . ok_or_else ( || SyntaxError :: InvalidCustomAction ( String :: from ( "Invalid block header" ) ) ) ?
144141 . parent_hash ( ) ;
145142
146- let parent_state = client. state_at ( parent_block_hash. into ( ) ) . ok_or_else ( || {
147- SyntaxError :: InvalidCustomAction ( String :: from (
148- "Cannot read state from the given message's parent hash" ,
149- ) )
150- } ) ?;
143+ let parent_state = client
144+ . state_at ( parent_block_hash. into ( ) )
145+ . ok_or_else ( || TrieError :: InvalidStateRoot ( parent_block_hash) ) ?;
151146 let signers_vec: Vec < Public > = get_validators ( & parent_state)
152- . map ( |validators| validators. into_iter ( ) . map ( |val| * val. pubkey ( ) ) . collect ( ) )
153- . map_err ( |_| {
154- SyntaxError :: InvalidCustomAction ( String :: from ( "Cannot get validators from parent_state" ) )
155- } ) ?;
147+ . map ( |validators| validators. into_iter ( ) . map ( |val| * val. pubkey ( ) ) . collect ( ) ) ?;
156148
157- let signer1 = signers_vec. get ( signer_idx1) . ok_or_else ( || {
158- SyntaxError :: InvalidCustomAction ( String :: from ( "Invalid signer index in message1" ) )
149+ let signer1 = signers_vec. get ( signer_idx1) . ok_or_else ( || RuntimeError :: InvalidValidatorIndex {
150+ idx : signer_idx1,
151+ parent_block_hash,
159152 } ) ?;
160- let signer2 = signers_vec. get ( signer_idx2) . ok_or_else ( || {
161- SyntaxError :: InvalidCustomAction ( String :: from ( "Invalid signer index in message2" ) )
153+ let signer2 = signers_vec. get ( signer_idx2) . ok_or_else ( || RuntimeError :: InvalidValidatorIndex {
154+ idx : signer_idx2,
155+ parent_block_hash,
162156 } ) ?;
163157
164158 if signer1 != signer2 {
165159 return Err ( SyntaxError :: InvalidCustomAction ( format ! (
166160 "Two messages are signed by different signers: {}, {}" ,
167161 signer1, signer2
168- ) ) )
162+ ) )
163+ . into ( ) )
169164 }
170165
171166 if message1. verify ( signer1) != Ok ( true ) || message2. verify ( signer2) != Ok ( true ) {
172- return Err ( SyntaxError :: InvalidCustomAction ( String :: from ( "Schnorr signature verification fails" ) ) )
167+ return Err (
168+ SyntaxError :: InvalidCustomAction ( String :: from ( "Schnorr signature verification fails" ) ) . into ( )
169+ )
173170 }
174171 }
175172 }
0 commit comments