Skip to content

Commit afd047f

Browse files
committed
Merge branch 'unstable' into revert-base-hash
2 parents 5489b0b + 0e884fc commit afd047f

File tree

16 files changed

+1030
-367
lines changed

16 files changed

+1030
-367
lines changed

Cargo.lock

Lines changed: 1 addition & 35 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

anchor/database/Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ authors = ["Sigma Prime <[email protected]>"]
66

77
[dependencies]
88
base64 = { workspace = true }
9-
multi_index_map = "0.15.0"
109
once_cell = { workspace = true }
1110
openssl = { workspace = true }
1211
r2d2 = { workspace = true }

anchor/database/src/cluster_operations.rs

Lines changed: 36 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,7 @@ use rusqlite::{Transaction, params};
22
use ssv_types::{Cluster, ClusterId, OperatorId, Share, ValidatorMetadata};
33
use types::{Address, PublicKeyBytes};
44

5-
use super::{
6-
ClusterIndexed, DatabaseError, MetadataIndexed, NetworkDatabase, ShareIndexed, sql_operations,
7-
};
5+
use super::{DatabaseError, NetworkDatabase, NonUniqueIndex, UniqueIndex, sql_operations};
86

97
/// Implements all cluster related functionality on the database
108
impl NetworkDatabase {
@@ -60,43 +58,33 @@ impl NetworkDatabase {
6058
// Record that we are a member of this cluster
6159
state.single_state.clusters.insert(cluster.cluster_id);
6260

63-
state.multi_state.shares.insert(ShareIndexed {
64-
validator_pubkey: validator.public_key,
65-
cluster_id: cluster.cluster_id,
66-
owner: cluster.owner,
67-
committee_id: cluster.committee_id(),
68-
share: share.to_owned(),
69-
});
61+
// Save the keyshare
62+
state.multi_state.shares.insert_or_update(
63+
&validator.public_key, // The validator this keyshare belongs to
64+
&cluster.cluster_id, // The id of the cluster
65+
&cluster.owner, // The owner of the cluster
66+
&cluster.committee_id(), // The committee id of the cluster
67+
share.to_owned(), // The keyshare itself
68+
);
7069
}
7170

7271
// Save all cluster related information
73-
// Check if we already have this cluster
74-
let existing = state
75-
.multi_state
76-
.clusters
77-
.get_by_cluster_id(&cluster.cluster_id)
78-
.is_some();
79-
80-
// Only insert if it doesn't exist yet
81-
if !existing {
82-
state.multi_state.clusters.insert(ClusterIndexed {
83-
cluster_id: cluster.cluster_id,
84-
owner: cluster.owner,
85-
committee_id: cluster.committee_id(),
86-
cluster: cluster.to_owned(),
87-
});
88-
}
89-
90-
state
91-
.multi_state
92-
.validator_metadata
93-
.insert(MetadataIndexed {
94-
validator_pubkey: validator.public_key,
95-
cluster_id: cluster.cluster_id,
96-
owner: cluster.owner,
97-
committee_id: cluster.committee_id(),
98-
metadata: validator.to_owned(),
99-
});
72+
state.multi_state.clusters.insert_or_update(
73+
&cluster.cluster_id, // The id of the cluster
74+
&validator.public_key, // The public key of validator added to the cluster
75+
&cluster.owner, // Owner of the cluster
76+
&cluster.committee_id(), // The committee id of the cluster
77+
cluster.to_owned(), // The Cluster and all containing information
78+
);
79+
80+
// Save the metadata for the validators
81+
state.multi_state.validator_metadata.insert_or_update(
82+
&validator.public_key, // The public key of the validator
83+
&cluster.cluster_id, // The id of the cluster the validator belongs to
84+
&cluster.owner, // The owner of the cluster
85+
&cluster.committee_id(), // The committee id of the cluster
86+
validator.to_owned(), // The metadata of the validator
87+
);
10088
});
10189

10290
Ok(())
@@ -117,12 +105,9 @@ impl NetworkDatabase {
117105

118106
// Update in memory status of cluster
119107
self.modify_state(|state| {
120-
state
121-
.multi_state
122-
.clusters
123-
.modify_by_cluster_id(&cluster_id, |cluster_idx| {
124-
cluster_idx.cluster.liquidated = status;
125-
});
108+
if let Some(cluster) = state.multi_state.clusters.get_mut_by(&cluster_id) {
109+
cluster.liquidated = status;
110+
}
126111
});
127112

128113
Ok(())
@@ -142,33 +127,24 @@ impl NetworkDatabase {
142127

143128
self.modify_state(|state| {
144129
// Remove from in memory
145-
state
146-
.multi_state
147-
.shares
148-
.remove_by_validator_pubkey(validator_pubkey);
149-
150-
let metadata_idx = state
130+
state.multi_state.shares.remove(validator_pubkey);
131+
let metadata = state
151132
.multi_state
152133
.validator_metadata
153-
.remove_by_validator_pubkey(validator_pubkey)
134+
.remove(validator_pubkey)
154135
.expect("Data should have existed");
155136

156137
// If there is no longer and validators for this cluster, remove it from both the
157138
// cluster multi index map and the cluster membership set
158139
if state
159140
.multi_state
160141
.validator_metadata
161-
.get_by_cluster_id(&metadata_idx.metadata.cluster_id)
162-
.is_empty()
142+
.get_all_by(&metadata.cluster_id)
143+
.next()
144+
.is_none()
163145
{
164-
state
165-
.multi_state
166-
.clusters
167-
.remove_by_cluster_id(&metadata_idx.metadata.cluster_id);
168-
state
169-
.single_state
170-
.clusters
171-
.remove(&metadata_idx.metadata.cluster_id);
146+
state.multi_state.clusters.remove(&metadata.cluster_id);
147+
state.single_state.clusters.remove(&metadata.cluster_id);
172148
}
173149
});
174150

anchor/database/src/lib.rs

Lines changed: 57 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ use std::{
44
time::Duration,
55
};
66

7-
use multi_index_map::MultiIndexMap;
87
use once_cell::sync::OnceCell;
98
use openssl::{pkey::Public, rsa::Rsa};
109
use r2d2_sqlite::SqliteConnectionManager;
@@ -19,64 +18,16 @@ use tokio::sync::{
1918
};
2019
use types::{Address, PublicKeyBytes};
2120

22-
pub use crate::{error::DatabaseError, state::NetworkState};
23-
24-
/// All the shares that belong to the current operator.
25-
/// IMPORTANT: There are parts of the code that assume this only contains shares that belong to the
26-
/// current operator. If this ever changes, make sure to update the code accordingly.
27-
#[derive(Debug, Clone, MultiIndexMap)]
28-
pub struct ShareIndexed {
29-
#[multi_index(hashed_unique)]
30-
pub validator_pubkey: PublicKeyBytes,
31-
32-
#[multi_index(hashed_non_unique)]
33-
pub cluster_id: ClusterId,
34-
35-
#[multi_index(hashed_non_unique)]
36-
pub owner: Address,
37-
38-
#[multi_index(hashed_non_unique)]
39-
pub committee_id: CommitteeId,
40-
41-
pub share: Share,
42-
}
43-
44-
/// Metadata for all validators in the network
45-
#[derive(Debug, Clone, MultiIndexMap)]
46-
pub struct MetadataIndexed {
47-
#[multi_index(hashed_unique)]
48-
pub validator_pubkey: PublicKeyBytes,
49-
50-
#[multi_index(hashed_non_unique)]
51-
pub cluster_id: ClusterId,
52-
53-
#[multi_index(hashed_non_unique)]
54-
pub owner: Address,
55-
56-
#[multi_index(hashed_non_unique)]
57-
pub committee_id: CommitteeId,
58-
59-
pub metadata: ValidatorMetadata,
60-
}
61-
62-
/// All the clusters in the network
63-
#[derive(Debug, Clone, MultiIndexMap)]
64-
pub struct ClusterIndexed {
65-
#[multi_index(hashed_unique)]
66-
pub cluster_id: ClusterId,
67-
68-
#[multi_index(hashed_non_unique)]
69-
pub owner: Address,
70-
71-
#[multi_index(hashed_non_unique)]
72-
pub committee_id: CommitteeId,
73-
74-
pub cluster: Cluster,
75-
}
21+
pub use crate::{
22+
error::DatabaseError,
23+
multi_index::{MultiIndexMap, *},
24+
state::NetworkState,
25+
};
7626

7727
mod cluster_operations;
7828
mod error;
7929
mod keysplit_operations;
30+
mod multi_index;
8031
mod operator_operations;
8132
mod schema;
8233
mod share_operations;
@@ -93,16 +44,57 @@ const CONNECTION_TIMEOUT: Duration = Duration::from_secs(5);
9344
type Pool = r2d2::Pool<SqliteConnectionManager>;
9445
type PoolConn = r2d2::PooledConnection<SqliteConnectionManager>;
9546

96-
// The actual map types are generated by the macro and are named:
97-
// - MultiIndexShareIndexedMap
98-
// - MultiIndexMetadataIndexedMap
99-
// - MultiIndexClusterIndexedMap
100-
//
101-
// Information that needs to be accessed via multiple different indices
47+
/// All the shares that belong to the current operator.
48+
/// IMPORTANT: There are parts of the code that assume this only contains shares that belong to the
49+
/// current operator. If this ever changes, make sure to update the code accordingly.
50+
/// Primary: public key of validator, uniquely identifies a share
51+
/// Secondary: cluster id, corresponds to a list of shares
52+
/// Tertiary: owner of the cluster, corresponds to a list of shares
53+
pub type ShareMultiIndexMap = MultiIndexMap<
54+
PublicKeyBytes,
55+
ClusterId,
56+
Address,
57+
CommitteeId,
58+
Share,
59+
NonUniqueTag,
60+
NonUniqueTag,
61+
NonUniqueTag,
62+
>;
63+
/// Metadata for all validators in the network
64+
/// Primary: public key of the validator. uniquely identifies the metadata
65+
/// Secondary: cluster id. corresponds to list of metadata for all validators
66+
/// Tertiary: owner of the cluster: corresponds to list of metadata for all validators
67+
pub type MetadataMultiIndexMap = MultiIndexMap<
68+
PublicKeyBytes,
69+
ClusterId,
70+
Address,
71+
CommitteeId,
72+
ValidatorMetadata,
73+
NonUniqueTag,
74+
NonUniqueTag,
75+
NonUniqueTag,
76+
>;
77+
/// All of the clusters in the network
78+
/// Primary: cluster id. uniquely identifies a cluster
79+
/// Secondary: public key of the validator. uniquely identifies a cluster
80+
/// Tertiary: owner of the cluster. does not uniquely identify a cluster
81+
pub type ClusterMultiIndexMap = MultiIndexMap<
82+
ClusterId,
83+
PublicKeyBytes,
84+
Address,
85+
CommitteeId,
86+
Cluster,
87+
UniqueTag,
88+
NonUniqueTag,
89+
NonUniqueTag,
90+
>;
91+
92+
// Information that needs to be accessed via multiple different indicies
93+
#[derive(Debug)]
10294
struct MultiState {
103-
shares: MultiIndexShareIndexedMap,
104-
validator_metadata: MultiIndexMetadataIndexedMap,
105-
clusters: MultiIndexClusterIndexedMap,
95+
shares: ShareMultiIndexMap,
96+
validator_metadata: MetadataMultiIndexMap,
97+
clusters: ClusterMultiIndexMap,
10698
// Be careful when adding new maps here. If you really must to, it must be updated in the
10799
// operations files
108100
}
@@ -132,6 +124,7 @@ enum PubkeyOrId {
132124

133125
/// Top level NetworkDatabase that contains in memory storage for quick access
134126
/// to relevant information and a connection to the database
127+
#[derive(Debug)]
135128
pub struct NetworkDatabase {
136129
/// The public key or ID of our operator
137130
operator: PubkeyOrId,

0 commit comments

Comments
 (0)