44//! Structures defining the set of owners and super owners, as well as the consensus
55//! round types and timeouts for chains.
66
7- use std:: { collections:: BTreeMap , iter} ;
7+ use std:: {
8+ collections:: { BTreeMap , BTreeSet } ,
9+ iter,
10+ } ;
811
912use custom_debug_derive:: Debug ;
1013use linera_witty:: { WitLoad , WitStore , WitType } ;
1114use serde:: { Deserialize , Serialize } ;
1215use thiserror:: Error ;
1316
1417use crate :: {
15- crypto:: PublicKey ,
1618 data_types:: { Round , TimeDelta } ,
1719 doc_scalar,
1820 identifiers:: Owner ,
@@ -50,11 +52,11 @@ impl Default for TimeoutConfig {
5052) ]
5153pub struct ChainOwnership {
5254 /// Super owners can propose fast blocks in the first round, and regular blocks in any round.
53- #[ debug( skip_if = BTreeMap :: is_empty) ]
54- pub super_owners : BTreeMap < Owner , PublicKey > ,
55+ #[ debug( skip_if = BTreeSet :: is_empty) ]
56+ pub super_owners : BTreeSet < Owner > ,
5557 /// The regular owners, with their weights that determine how often they are round leader.
5658 #[ debug( skip_if = BTreeMap :: is_empty) ]
57- pub owners : BTreeMap < Owner , ( PublicKey , u64 ) > ,
59+ pub owners : BTreeMap < Owner , u64 > ,
5860 /// The number of initial rounds after 0 in which all owners are allowed to propose blocks.
5961 pub multi_leader_rounds : u32 ,
6062 /// The timeout configuration: how long fast, multi-leader and single-leader rounds last.
@@ -63,46 +65,42 @@ pub struct ChainOwnership {
6365
6466impl ChainOwnership {
6567 /// Creates a `ChainOwnership` with a single super owner.
66- pub fn single_super ( public_key : PublicKey ) -> Self {
68+ pub fn single_super ( owner : Owner ) -> Self {
6769 ChainOwnership {
68- super_owners : iter:: once ( ( Owner :: from ( public_key ) , public_key ) ) . collect ( ) ,
70+ super_owners : iter:: once ( owner ) . collect ( ) ,
6971 owners : BTreeMap :: new ( ) ,
7072 multi_leader_rounds : 2 ,
7173 timeout_config : TimeoutConfig :: default ( ) ,
7274 }
7375 }
7476
7577 /// Creates a `ChainOwnership` with a single regular owner.
76- pub fn single ( public_key : PublicKey ) -> Self {
78+ pub fn single ( owner : Owner ) -> Self {
7779 ChainOwnership {
78- super_owners : BTreeMap :: new ( ) ,
79- owners : iter:: once ( ( Owner :: from ( public_key ) , ( public_key , 100 ) ) ) . collect ( ) ,
80+ super_owners : BTreeSet :: new ( ) ,
81+ owners : iter:: once ( ( owner , 100 ) ) . collect ( ) ,
8082 multi_leader_rounds : 2 ,
8183 timeout_config : TimeoutConfig :: default ( ) ,
8284 }
8385 }
8486
8587 /// Creates a `ChainOwnership` with the specified regular owners.
8688 pub fn multiple (
87- keys_and_weights : impl IntoIterator < Item = ( PublicKey , u64 ) > ,
89+ owners_and_weights : impl IntoIterator < Item = ( Owner , u64 ) > ,
8890 multi_leader_rounds : u32 ,
8991 timeout_config : TimeoutConfig ,
9092 ) -> Self {
9193 ChainOwnership {
92- super_owners : BTreeMap :: new ( ) ,
93- owners : keys_and_weights
94- . into_iter ( )
95- . map ( |( public_key, weight) | ( Owner :: from ( public_key) , ( public_key, weight) ) )
96- . collect ( ) ,
94+ super_owners : BTreeSet :: new ( ) ,
95+ owners : owners_and_weights. into_iter ( ) . collect ( ) ,
9796 multi_leader_rounds,
9897 timeout_config,
9998 }
10099 }
101100
102101 /// Adds a regular owner.
103- pub fn with_regular_owner ( mut self , public_key : PublicKey , weight : u64 ) -> Self {
104- self . owners
105- . insert ( Owner :: from ( public_key) , ( public_key, weight) ) ;
102+ pub fn with_regular_owner ( mut self , owner : Owner , weight : u64 ) -> Self {
103+ self . owners . insert ( owner, weight) ;
106104 self
107105 }
108106
@@ -113,13 +111,9 @@ impl ChainOwnership {
113111 || self . timeout_config . fallback_duration == TimeDelta :: ZERO
114112 }
115113
116- /// Returns the given owner's public key, if they are an owner or super owner.
117- pub fn verify_owner ( & self , owner : & Owner ) -> Option < PublicKey > {
118- if let Some ( public_key) = self . super_owners . get ( owner) {
119- Some ( * public_key)
120- } else {
121- self . owners . get ( owner) . map ( |( public_key, _) | * public_key)
122- }
114+ /// Returns `true` if this is an owner or super owner.
115+ pub fn verify_owner ( & self , owner : & Owner ) -> bool {
116+ self . super_owners . contains ( owner) || self . owners . contains_key ( owner)
123117 }
124118
125119 /// Returns the duration of the given round.
@@ -157,14 +151,7 @@ impl ChainOwnership {
157151
158152 /// Returns an iterator over all super owners, followed by all owners.
159153 pub fn all_owners ( & self ) -> impl Iterator < Item = & Owner > {
160- self . super_owners . keys ( ) . chain ( self . owners . keys ( ) )
161- }
162-
163- /// Returns an iterator over all super owners' keys, followed by all owners'.
164- pub fn all_public_keys ( & self ) -> impl Iterator < Item = & PublicKey > {
165- self . super_owners
166- . values ( )
167- . chain ( self . owners . values ( ) . map ( |( public_key, _) | public_key) )
154+ self . super_owners . iter ( ) . chain ( self . owners . keys ( ) )
168155 }
169156
170157 /// Returns the round following the specified one, if any.
@@ -207,8 +194,8 @@ mod tests {
207194 let owner = Owner :: from ( pub_key) ;
208195
209196 let ownership = ChainOwnership {
210- super_owners : BTreeMap :: from_iter ( [ ( super_owner, super_pub_key ) ] ) ,
211- owners : BTreeMap :: from_iter ( [ ( owner, ( pub_key , 100 ) ) ] ) ,
197+ super_owners : BTreeSet :: from_iter ( [ super_owner] ) ,
198+ owners : BTreeMap :: from_iter ( [ ( owner, 100 ) ] ) ,
212199 multi_leader_rounds : 10 ,
213200 timeout_config : TimeoutConfig {
214201 fast_round_duration : Some ( TimeDelta :: from_secs ( 5 ) ) ,
0 commit comments