@@ -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 candidate 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 ( ) ;
@@ -146,33 +147,33 @@ impl<M: Message> Action<M> {
146147 } ) ?
147148 . parent_hash ( ) ;
148149
149- let parent_state = client. state_at ( parent_block_hash. into ( ) ) . ok_or_else ( || {
150- SyntaxError :: InvalidCustomAction ( String :: from (
151- "Cannot read state from the given message's parent hash" ,
152- ) )
153- } ) ?;
150+ let parent_state = client
151+ . state_at ( parent_block_hash. into ( ) )
152+ . ok_or_else ( || TrieError :: InvalidStateRoot ( parent_block_hash) ) ?;
154153 let signers_vec: Vec < Public > = get_validators ( & parent_state)
155- . map ( |validators| validators. into_iter ( ) . map ( |val| * val. pubkey ( ) ) . collect ( ) )
156- . map_err ( |_| {
157- SyntaxError :: InvalidCustomAction ( String :: from ( "Cannot get validators from parent_state" ) )
158- } ) ?;
154+ . map ( |validators| validators. into_iter ( ) . map ( |val| * val. pubkey ( ) ) . collect ( ) ) ?;
159155
160- let signer1 = signers_vec. get ( signer_idx1) . ok_or_else ( || {
161- SyntaxError :: InvalidCustomAction ( String :: from ( "Invalid signer index in message1" ) )
156+ let signer1 = signers_vec. get ( signer_idx1) . ok_or_else ( || RuntimeError :: InvalidValidatorIndex {
157+ idx : signer_idx1,
158+ parent_block_hash,
162159 } ) ?;
163- let signer2 = signers_vec. get ( signer_idx2) . ok_or_else ( || {
164- SyntaxError :: InvalidCustomAction ( String :: from ( "Invalid signer index in message2" ) )
160+ let signer2 = signers_vec. get ( signer_idx2) . ok_or_else ( || RuntimeError :: InvalidValidatorIndex {
161+ idx : signer_idx2,
162+ parent_block_hash,
165163 } ) ?;
166164
167165 if signer1 != signer2 {
168166 return Err ( SyntaxError :: InvalidCustomAction ( format ! (
169167 "Two messages are signed by different signers: {}, {}" ,
170168 signer1, signer2
171- ) ) )
169+ ) )
170+ . into ( ) )
172171 }
173172
174173 if message1. verify ( signer1) != Ok ( true ) || message2. verify ( signer2) != Ok ( true ) {
175- return Err ( SyntaxError :: InvalidCustomAction ( String :: from ( "Schnorr signature verification fails" ) ) )
174+ return Err (
175+ SyntaxError :: InvalidCustomAction ( String :: from ( "Schnorr signature verification fails" ) ) . into ( )
176+ )
176177 }
177178 }
178179 }
0 commit comments