@@ -22,17 +22,17 @@ fn apply_changeset_to_checkpoint(
22
22
23
23
for cp in init_cp. iter ( ) {
24
24
if cp. height ( ) >= start_height {
25
- extension. insert ( cp. height ( ) , cp. hash ( ) ) ;
25
+ extension. insert ( cp. height ( ) , * cp. data ( ) ) ;
26
26
} else {
27
27
base = Some ( cp) ;
28
28
break ;
29
29
}
30
30
}
31
31
32
- for ( & height, & hash ) in & changeset. blocks {
33
- match hash {
34
- Some ( hash ) => {
35
- extension. insert ( height, hash ) ;
32
+ for ( & height, & data ) in & changeset. blocks {
33
+ match data {
34
+ Some ( data ) => {
35
+ extension. insert ( height, data ) ;
36
36
}
37
37
None => {
38
38
extension. remove ( & height) ;
@@ -42,7 +42,7 @@ fn apply_changeset_to_checkpoint(
42
42
43
43
let new_tip = match base {
44
44
Some ( base) => base
45
- . extend ( extension. into_iter ( ) . map ( BlockId :: from ) )
45
+ . extend_data ( extension)
46
46
. expect ( "extension is strictly greater than base" ) ,
47
47
None => LocalChain :: from_blocks ( extension) ?. tip ( ) ,
48
48
} ;
@@ -53,12 +53,24 @@ fn apply_changeset_to_checkpoint(
53
53
}
54
54
55
55
/// This is a local implementation of [`ChainOracle`].
56
- #[ derive( Debug , Clone , PartialEq ) ]
57
- pub struct LocalChain {
58
- tip : CheckPoint ,
56
+ #[ derive( Debug , Clone ) ]
57
+ pub struct LocalChain < B = BlockHash > {
58
+ tip : CheckPoint < B > ,
59
59
}
60
60
61
- impl ChainOracle for LocalChain {
61
+ impl < B > PartialEq for LocalChain < B >
62
+ where
63
+ B : Copy + core:: cmp:: PartialEq ,
64
+ {
65
+ fn eq ( & self , other : & Self ) -> bool {
66
+ self . tip == other. tip
67
+ }
68
+ }
69
+
70
+ impl < B > ChainOracle for LocalChain < B >
71
+ where
72
+ B : Copy ,
73
+ {
62
74
type Error = Infallible ;
63
75
64
76
fn is_block_in_chain (
@@ -83,18 +95,18 @@ impl ChainOracle for LocalChain {
83
95
}
84
96
}
85
97
86
- impl LocalChain {
98
+ impl LocalChain < BlockHash > {
87
99
/// Get the genesis hash.
88
100
pub fn genesis_hash ( & self ) -> BlockHash {
89
- self . tip . get ( 0 ) . expect ( "genesis must exist" ) . hash ( )
101
+ self . tip . get ( 0 ) . expect ( "genesis must exist" ) . block_id ( ) . hash
90
102
}
91
103
92
104
/// Construct [`LocalChain`] from genesis `hash`.
93
105
#[ must_use]
94
106
pub fn from_genesis_hash ( hash : BlockHash ) -> ( Self , ChangeSet ) {
95
107
let height = 0 ;
96
108
let chain = Self {
97
- tip : CheckPoint :: new ( BlockId { height, hash } ) ,
109
+ tip : CheckPoint :: from_data ( height, hash) ,
98
110
} ;
99
111
let changeset = chain. initial_changeset ( ) ;
100
112
( chain, changeset)
@@ -134,7 +146,7 @@ impl LocalChain {
134
146
return Err ( MissingGenesisError ) ;
135
147
}
136
148
137
- let mut tip: Option < CheckPoint > = None ;
149
+ let mut tip: Option < CheckPoint < BlockHash > > = None ;
138
150
for block in & blocks {
139
151
match tip {
140
152
Some ( curr) => {
@@ -153,7 +165,7 @@ impl LocalChain {
153
165
}
154
166
155
167
/// Get the highest checkpoint.
156
- pub fn tip ( & self ) -> CheckPoint {
168
+ pub fn tip ( & self ) -> CheckPoint < BlockHash > {
157
169
self . tip . clone ( )
158
170
}
159
171
@@ -405,17 +417,25 @@ impl LocalChain {
405
417
}
406
418
407
419
/// The [`ChangeSet`] represents changes to [`LocalChain`].
408
- #[ derive( Debug , Default , Clone , PartialEq ) ]
420
+ #[ derive( Debug , Clone , PartialEq ) ]
409
421
#[ cfg_attr( feature = "serde" , derive( serde:: Deserialize , serde:: Serialize ) ) ]
410
- pub struct ChangeSet {
422
+ pub struct ChangeSet < B = BlockHash > {
411
423
/// Changes to the [`LocalChain`] blocks.
412
424
///
413
425
/// The key represents the block height, and the value either represents added a new [`CheckPoint`]
414
426
/// (if [`Some`]), or removing a [`CheckPoint`] (if [`None`]).
415
- pub blocks : BTreeMap < u32 , Option < BlockHash > > ,
427
+ pub blocks : BTreeMap < u32 , Option < B > > ,
428
+ }
429
+
430
+ impl < B > Default for ChangeSet < B > {
431
+ fn default ( ) -> Self {
432
+ ChangeSet {
433
+ blocks : BTreeMap :: default ( ) ,
434
+ }
435
+ }
416
436
}
417
437
418
- impl Merge for ChangeSet {
438
+ impl < B > Merge for ChangeSet < B > {
419
439
fn merge ( & mut self , other : Self ) {
420
440
Merge :: merge ( & mut self . blocks , other. blocks )
421
441
}
@@ -425,24 +445,24 @@ impl Merge for ChangeSet {
425
445
}
426
446
}
427
447
428
- impl < B : IntoIterator < Item = ( u32 , Option < BlockHash > ) > > From < B > for ChangeSet {
429
- fn from ( blocks : B ) -> Self {
448
+ impl < B , I : IntoIterator < Item = ( u32 , Option < B > ) > > From < I > for ChangeSet < B > {
449
+ fn from ( blocks : I ) -> Self {
430
450
Self {
431
451
blocks : blocks. into_iter ( ) . collect ( ) ,
432
452
}
433
453
}
434
454
}
435
455
436
- impl FromIterator < ( u32 , Option < BlockHash > ) > for ChangeSet {
437
- fn from_iter < T : IntoIterator < Item = ( u32 , Option < BlockHash > ) > > ( iter : T ) -> Self {
456
+ impl < B > FromIterator < ( u32 , Option < B > ) > for ChangeSet < B > {
457
+ fn from_iter < T : IntoIterator < Item = ( u32 , Option < B > ) > > ( iter : T ) -> Self {
438
458
Self {
439
459
blocks : iter. into_iter ( ) . collect ( ) ,
440
460
}
441
461
}
442
462
}
443
463
444
- impl FromIterator < ( u32 , BlockHash ) > for ChangeSet {
445
- fn from_iter < T : IntoIterator < Item = ( u32 , BlockHash ) > > ( iter : T ) -> Self {
464
+ impl < B > FromIterator < ( u32 , B ) > for ChangeSet < B > {
465
+ fn from_iter < T : IntoIterator < Item = ( u32 , B ) > > ( iter : T ) -> Self {
446
466
Self {
447
467
blocks : iter
448
468
. into_iter ( )
0 commit comments