@@ -20,14 +20,19 @@ pub struct Proposal {
2020 pub prev_state_root : MerkleRoot ,
2121 pub transactions_root : MerkleRoot ,
2222 pub signed_txs_hash : Hash ,
23+ #[ serde( serialize_with = "serde_hex::serialize_uint" ) ]
2324 pub timestamp : u64 ,
25+ #[ serde( serialize_with = "serde_hex::serialize_uint" ) ]
2426 pub number : BlockNumber ,
2527 pub gas_limit : U256 ,
28+ #[ serde( serialize_with = "serde_hex::serialize_bytes" ) ]
2629 pub extra_data : Bytes ,
2730 pub mixed_hash : Option < Hash > ,
2831 pub base_fee_per_gas : U256 ,
2932 pub proof : Proof ,
33+ #[ serde( serialize_with = "serde_hex::serialize_uint" ) ]
3034 pub chain_id : u64 ,
35+ #[ serde( serialize_with = "serde_hex::serialize_uint" ) ]
3136 pub call_system_script_count : u32 ,
3237 pub tx_hashes : Vec < Hash > ,
3338}
@@ -145,16 +150,21 @@ pub struct Header {
145150 pub receipts_root : MerkleRoot ,
146151 pub log_bloom : Bloom ,
147152 pub difficulty : U256 ,
153+ #[ serde( serialize_with = "serde_hex::serialize_uint" ) ]
148154 pub timestamp : u64 ,
155+ #[ serde( serialize_with = "serde_hex::serialize_uint" ) ]
149156 pub number : BlockNumber ,
150157 pub gas_used : U256 ,
151158 pub gas_limit : U256 ,
159+ #[ serde( serialize_with = "serde_hex::serialize_bytes" ) ]
152160 pub extra_data : Bytes ,
153161 pub mixed_hash : Option < Hash > ,
154162 pub nonce : H64 ,
155163 pub base_fee_per_gas : U256 ,
156164 pub proof : Proof ,
165+ #[ serde( serialize_with = "serde_hex::serialize_uint" ) ]
157166 pub call_system_script_count : u32 ,
167+ #[ serde( serialize_with = "serde_hex::serialize_uint" ) ]
158168 pub chain_id : u64 ,
159169}
160170
@@ -172,10 +182,14 @@ impl Header {
172182 RlpEncodable , RlpDecodable , Serialize , Deserialize , Default , Clone , Debug , PartialEq , Eq ,
173183) ]
174184pub struct Proof {
185+ #[ serde( serialize_with = "serde_hex::serialize_uint" ) ]
175186 pub number : u64 ,
187+ #[ serde( serialize_with = "serde_hex::serialize_uint" ) ]
176188 pub round : u64 ,
177189 pub block_hash : Hash ,
190+ #[ serde( serialize_with = "serde_hex::serialize_bytes" ) ]
178191 pub signature : Bytes ,
192+ #[ serde( serialize_with = "serde_hex::serialize_bytes" ) ]
179193 pub bitmap : Bytes ,
180194}
181195
@@ -185,6 +199,65 @@ pub struct RichBlock {
185199 pub txs : Vec < SignedTransaction > ,
186200}
187201
202+ mod serde_hex {
203+ use serde:: Serializer ;
204+
205+ use crate :: types:: { Bytes , Hex , U256 } ;
206+
207+ static CHARS : & [ u8 ] = b"0123456789abcdef" ;
208+
209+ pub fn serialize_bytes < S > ( val : & Bytes , s : S ) -> Result < S :: Ok , S :: Error >
210+ where
211+ S : Serializer ,
212+ {
213+ s. serialize_str ( & Hex :: encode ( val) . as_string ( ) )
214+ }
215+
216+ pub fn serialize_uint < S , U > ( val : & U , s : S ) -> Result < S :: Ok , S :: Error >
217+ where
218+ S : Serializer ,
219+ U : Into < U256 > + Copy ,
220+ {
221+ let val: U256 = ( * val) . into ( ) ;
222+ let mut slice = [ 0u8 ; 2 + 64 ] ;
223+ let mut bytes = [ 0u8 ; 32 ] ;
224+ val. to_big_endian ( & mut bytes) ;
225+ let non_zero = bytes. iter ( ) . take_while ( |b| * * b == 0 ) . count ( ) ;
226+ let bytes = & bytes[ non_zero..] ;
227+
228+ if bytes. is_empty ( ) {
229+ s. serialize_str ( "0x0" )
230+ } else {
231+ s. serialize_str ( to_hex_raw ( & mut slice, bytes, true ) )
232+ }
233+ }
234+
235+ fn to_hex_raw < ' a > ( v : & ' a mut [ u8 ] , bytes : & [ u8 ] , skip_leading_zero : bool ) -> & ' a str {
236+ assert ! ( v. len( ) > 1 + bytes. len( ) * 2 ) ;
237+
238+ v[ 0 ] = b'0' ;
239+ v[ 1 ] = b'x' ;
240+
241+ let mut idx = 2 ;
242+ let first_nibble = bytes[ 0 ] >> 4 ;
243+ if first_nibble != 0 || !skip_leading_zero {
244+ v[ idx] = CHARS [ first_nibble as usize ] ;
245+ idx += 1 ;
246+ }
247+ v[ idx] = CHARS [ ( bytes[ 0 ] & 0xf ) as usize ] ;
248+ idx += 1 ;
249+
250+ for & byte in bytes. iter ( ) . skip ( 1 ) {
251+ v[ idx] = CHARS [ ( byte >> 4 ) as usize ] ;
252+ v[ idx + 1 ] = CHARS [ ( byte & 0xf ) as usize ] ;
253+ idx += 2 ;
254+ }
255+
256+ // SAFETY: all characters come either from CHARS or "0x", therefore valid UTF8
257+ unsafe { std:: str:: from_utf8_unchecked ( & v[ 0 ..idx] ) }
258+ }
259+ }
260+
188261#[ cfg( test) ]
189262mod tests {
190263 use crate :: types:: {
0 commit comments