@@ -13,21 +13,64 @@ pub(crate) trait Index {
1313use dftx_rs:: { deserialize, Block , DfTx } ;
1414use log:: debug;
1515
16- use crate :: { model:: BlockContext , Result } ;
16+ use crate :: {
17+ model:: { BlockContext , Block as BlockMapper } ,
18+ repository:: RepositoryOps ,
19+ Result ,
20+ SERVICES ,
21+ } ;
1722
18- pub fn index_block ( block : String , block_height : u32 ) -> Result < ( ) > {
23+ pub struct BlockV2Info {
24+ pub height : u32 ,
25+ pub difficulty : u32 ,
26+ pub version : i32 ,
27+ pub median_time : i64 ,
28+ pub minter_block_count : u64 ,
29+ pub size : usize ,
30+ pub size_stripped : usize ,
31+ pub weight : i64 ,
32+ pub stake_modifier : String ,
33+ pub minter : String ,
34+ pub masternode : String ,
35+ }
36+
37+ pub fn index_block ( encoded_block : String , info : & BlockV2Info ) -> Result < ( ) > {
1938 debug ! ( "[index_block] Indexing block..." ) ;
2039
21- let hex = hex:: decode ( block ) ?;
40+ let hex = hex:: decode ( & encoded_block ) ?;
2241 debug ! ( "got hex" ) ;
2342 let block = deserialize :: < Block > ( & hex) ?;
2443 debug ! ( "got block" ) ;
44+ let block_hash = block. block_hash ( ) ;
2545 let ctx = BlockContext {
26- height : block_height ,
27- hash : block . block_hash ( ) ,
46+ height : info . height ,
47+ hash : block_hash,
2848 time : 0 , // TODO
2949 median_time : 0 , // TODO
3050 } ;
51+ let block_mapper = BlockMapper {
52+ id : block_hash. to_string ( ) ,
53+ hash : block_hash. to_string ( ) ,
54+ previous_hash : block. header . prev_blockhash . to_string ( ) ,
55+ height : info. height ,
56+ version : info. version ,
57+ time : block. header . time ,
58+ median_time : info. median_time ,
59+ transaction_count : block. txdata . len ( ) ,
60+ difficulty : info. difficulty ,
61+ masternode : info. masternode . to_owned ( ) ,
62+ minter : info. minter . to_owned ( ) ,
63+ minter_block_count : info. minter_block_count ,
64+ stake_modifier : info. stake_modifier . to_owned ( ) ,
65+ merkleroot : block. header . merkle_root . to_string ( ) ,
66+ size : info. size ,
67+ size_stripped : info. size_stripped ,
68+ weight : info. weight ,
69+ } ;
70+
71+ SERVICES . block . raw . put ( & ctx. hash , & encoded_block) ?;
72+ SERVICES . block . by_id . put ( & ctx. hash , & block_mapper) ?;
73+ SERVICES . block . by_height . put ( & ctx. height , & block_hash) ?;
3174
3275 for ( idx, tx) in block. txdata . into_iter ( ) . enumerate ( ) {
3376 let bytes = tx. output [ 0 ] . script_pubkey . as_bytes ( ) ;
@@ -60,6 +103,6 @@ pub fn index_block(block: String, block_height: u32) -> Result<()> {
60103 Ok ( ( ) )
61104}
62105
63- pub fn invalidate_block ( block : String , block_height : u32 ) -> Result < ( ) > {
106+ pub fn invalidate_block ( block : String , info : & BlockV2Info ) -> Result < ( ) > {
64107 Ok ( ( ) )
65108}
0 commit comments