@@ -7,26 +7,68 @@ pub mod tx_result;
77use dftx_rs:: { deserialize, Block , DfTx , Transaction } ;
88use log:: debug;
99
10- use crate :: { model:: BlockContext , Result } ;
10+ use crate :: {
11+ model:: { Block as BlockMapper , BlockContext } ,
12+ repository:: RepositoryOps ,
13+ Result , SERVICES ,
14+ } ;
1115
1216pub ( crate ) trait Index {
1317 fn index ( & self , ctx : & BlockContext , tx : Transaction , idx : usize ) -> Result < ( ) > ;
1418 fn invalidate ( & self , context : & BlockContext , tx : Transaction , idx : usize ) -> Result < ( ) > ;
1519}
1620
17- pub fn index_block ( block : String , block_height : u32 ) -> Result < ( ) > {
21+ pub struct BlockV2Info {
22+ pub height : u32 ,
23+ pub difficulty : u32 ,
24+ pub version : i32 ,
25+ pub median_time : i64 ,
26+ pub minter_block_count : u64 ,
27+ pub size : usize ,
28+ pub size_stripped : usize ,
29+ pub weight : i64 ,
30+ pub stake_modifier : String ,
31+ pub minter : String ,
32+ pub masternode : String ,
33+ }
34+
35+ pub fn index_block ( encoded_block : String , info : & BlockV2Info ) -> Result < ( ) > {
1836 debug ! ( "[index_block] Indexing block..." ) ;
1937
20- let hex = hex:: decode ( block ) ?;
38+ let hex = hex:: decode ( & encoded_block ) ?;
2139 debug ! ( "got hex" ) ;
2240 let block = deserialize :: < Block > ( & hex) ?;
2341 debug ! ( "got block" ) ;
42+ let block_hash = block. block_hash ( ) ;
2443 let ctx = BlockContext {
25- height : block_height ,
26- hash : block . block_hash ( ) ,
44+ height : info . height ,
45+ hash : block_hash,
2746 time : 0 , // TODO
2847 median_time : 0 , // TODO
2948 } ;
49+ let block_mapper = BlockMapper {
50+ id : block_hash. to_string ( ) ,
51+ hash : block_hash. to_string ( ) ,
52+ previous_hash : block. header . prev_blockhash . to_string ( ) ,
53+ height : info. height ,
54+ version : info. version ,
55+ time : block. header . time ,
56+ median_time : info. median_time ,
57+ transaction_count : block. txdata . len ( ) ,
58+ difficulty : info. difficulty ,
59+ masternode : info. masternode . to_owned ( ) ,
60+ minter : info. minter . to_owned ( ) ,
61+ minter_block_count : info. minter_block_count ,
62+ stake_modifier : info. stake_modifier . to_owned ( ) ,
63+ merkleroot : block. header . merkle_root . to_string ( ) ,
64+ size : info. size ,
65+ size_stripped : info. size_stripped ,
66+ weight : info. weight ,
67+ } ;
68+
69+ SERVICES . block . raw . put ( & ctx. hash , & encoded_block) ?;
70+ SERVICES . block . by_id . put ( & ctx. hash , & block_mapper) ?;
71+ SERVICES . block . by_height . put ( & ctx. height , & block_hash) ?;
3072
3173 for ( idx, tx) in block. txdata . into_iter ( ) . enumerate ( ) {
3274 let bytes = tx. output [ 0 ] . script_pubkey . as_bytes ( ) ;
@@ -60,6 +102,6 @@ pub fn index_block(block: String, block_height: u32) -> Result<()> {
60102 Ok ( ( ) )
61103}
62104
63- pub fn invalidate_block ( block : String , block_height : u32 ) -> Result < ( ) > {
105+ pub fn invalidate_block ( block : String , info : & BlockV2Info ) -> Result < ( ) > {
64106 Ok ( ( ) )
65107}
0 commit comments