@@ -23,85 +23,55 @@ use std::marker::PhantomData;
23
23
use codec:: { Decode , Encode , Error as DecodeError } ;
24
24
use sp_runtime:: {
25
25
generic:: SignedBlock ,
26
- traits:: { Block as BlockT , DigestFor , Header as HeaderT } ,
26
+ traits:: { Block as BlockT , Header as HeaderT } ,
27
27
} ;
28
28
29
29
use substrate_archive_common:: { models:: BlockModel , types} ;
30
30
31
- struct GenericParts < B : BlockT > (
32
- <B :: Header as HeaderT >:: Hash ,
33
- <B :: Header as HeaderT >:: Hash ,
34
- <B :: Header as HeaderT >:: Hash ,
35
- ) ;
36
-
37
- pub struct BlockBuilder < B : BlockT > {
31
+ pub struct SqlBlockBuilder < B : BlockT > {
38
32
_marker : PhantomData < B > ,
39
33
}
40
34
41
- impl < ' a , B : BlockT > BlockBuilder < B > {
35
+ impl < ' a , B : BlockT > SqlBlockBuilder < B > {
42
36
/// With a vector of BlockModel
43
37
pub fn with_vec ( blocks : Vec < BlockModel > ) -> Result < Vec < types:: Block < B > > , DecodeError > {
44
38
blocks
45
39
. into_iter ( )
46
40
. map ( |b| {
47
- let ( b , s ) = Self :: with_single ( b) ?;
48
- let b = SignedBlock { block : b , justification : None } ;
49
- Ok ( types:: Block :: new ( b , s ) )
41
+ let ( block , spec ) = Self :: with_single ( b) ?;
42
+ let block = SignedBlock { block, justification : None } ;
43
+ Ok ( types:: Block :: new ( block , spec ) )
50
44
} )
51
45
. collect ( )
52
46
}
53
47
48
+ /// With a single BlockModel
54
49
pub fn with_single ( block : BlockModel ) -> Result < ( B , u32 ) , DecodeError > {
55
- let digest: DigestFor < B > = Decode :: decode ( & mut block. digest . as_slice ( ) ) ?;
56
- let GenericParts ( parent_hash, state_root, extrinsics_root) = Self :: generic_parts_from (
57
- block. parent_hash . as_slice ( ) ,
58
- block. state_root . as_slice ( ) ,
59
- block. extrinsics_root . as_slice ( ) ,
60
- ) ?;
61
- let num: <B :: Header as HeaderT >:: Number = Decode :: decode ( & mut ( block. block_num as u32 ) . encode ( ) . as_slice ( ) ) ?;
62
-
63
- let header = <B :: Header as HeaderT >:: new ( num, extrinsics_root, state_root, parent_hash, digest) ;
64
- let ext: Vec < B :: Extrinsic > = Decode :: decode ( & mut block. ext . as_slice ( ) ) ?;
65
- let spec = block. spec ;
66
- Ok ( ( B :: new ( header, ext) , spec as u32 ) )
50
+ let BlockDecoder { header, ext, spec } = BlockDecoder :: < B > :: decode ( block) ?;
51
+ let block = B :: new ( header, ext) ;
52
+ Ok ( ( block, spec) )
67
53
}
54
+ }
68
55
69
- fn generic_parts_from (
70
- mut parent_hash : & [ u8 ] ,
71
- mut state_root : & [ u8 ] ,
72
- mut extrinsics_root : & [ u8 ] ,
73
- ) -> Result < GenericParts < B > , DecodeError > {
74
- Ok ( GenericParts (
75
- Decode :: decode ( & mut parent_hash) ?,
76
- Decode :: decode ( & mut state_root) ?,
77
- Decode :: decode ( & mut extrinsics_root) ?,
78
- ) )
79
- }
56
+ struct BlockDecoder < B : BlockT > {
57
+ header : B :: Header ,
58
+ ext : Vec < B :: Extrinsic > ,
59
+ spec : u32 ,
80
60
}
81
- /* TODO: This test need to be rewritten. We shouldn't depend on test_util
82
- * or rocksdb for tests.
83
- #[cfg(test)]
84
- mod tests {
85
- use super::*;
86
- use crate::backend::test_util;
87
- use crate::queries;
88
- use polkadot_service::Block;
89
- use sp_runtime::generic::BlockId;
90
- use sqlx::PgPool;
91
61
92
- pub const DB_STR: &str = "/home/insipx/.local/share/polkadot/chains/ksmcc3/db";
62
+ impl < B : BlockT > BlockDecoder < B > {
63
+ fn decode ( block : BlockModel ) -> Result < Self , DecodeError > {
64
+ let block_num = Decode :: decode ( & mut ( block. block_num as u32 ) . encode ( ) . as_slice ( ) ) ?;
65
+ let extrinsics_root = Decode :: decode ( & mut block. extrinsics_root . as_slice ( ) ) ?;
66
+ let state_root = Decode :: decode ( & mut block. state_root . as_slice ( ) ) ?;
67
+ let parent_hash = Decode :: decode ( & mut block. parent_hash . as_slice ( ) ) ?;
68
+ let digest = Decode :: decode ( & mut block. digest . as_slice ( ) ) ?;
69
+ let header = <B :: Header as HeaderT >:: new ( block_num, extrinsics_root, state_root, parent_hash, digest) ;
70
+
71
+ let ext = Decode :: decode ( & mut block. ext . as_slice ( ) ) ?;
93
72
94
- #[test]
95
- #[ignore]
96
- fn block_should_be_identical() {
97
- let url = std::env::var("DATABASE_URL").unwrap();
98
- let pool = futures::executor::block_on(PgPool::builder().max_size(1).build(&url)).unwrap();
99
- let backend = test_util::backend(DB_STR);
100
- let block = backend.block(&BlockId::Number(500)).unwrap();
73
+ let spec = block. spec as u32 ;
101
74
102
- let sql_block = futures::executor::block_on(queries::get_full_block(&pool, 500)).unwrap();
103
- let full_sql_block = BlockBuilder::<Block>::new().with_single(sql_block).unwrap();
104
- assert_eq!(block.block, full_sql_block);
75
+ Ok ( Self { header, ext, spec } )
105
76
}
106
77
}
107
- */
0 commit comments