Skip to content

Commit 31a1843

Browse files
ethDreamerWoodpile37
authored andcommitted
Added bls_to_execution_changes to PersistedOpPool (sigp#3857)
* Added bls_to_execution_changes to PersistedOpPool
1 parent 493f1f9 commit 31a1843

6 files changed

Lines changed: 146 additions & 20 deletions

File tree

beacon_node/beacon_chain/src/schema_change.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
//! Utilities for managing database schema changes.
22
mod migration_schema_v12;
33
mod migration_schema_v13;
4+
mod migration_schema_v14;
45

56
use crate::beacon_chain::{BeaconChainTypes, ETH1_CACHE_DB_KEY};
67
use crate::eth1_chain::SszEth1;
@@ -114,6 +115,14 @@ pub fn migrate_schema<T: BeaconChainTypes>(
114115

115116
Ok(())
116117
}
118+
(SchemaVersion(13), SchemaVersion(14)) => {
119+
let ops = migration_schema_v14::upgrade_to_v14::<T>(db.clone(), log)?;
120+
db.store_schema_version_atomically(to, ops)
121+
}
122+
(SchemaVersion(14), SchemaVersion(13)) => {
123+
let ops = migration_schema_v14::downgrade_from_v14::<T>(db.clone(), log)?;
124+
db.store_schema_version_atomically(to, ops)
125+
}
117126
// Anything else is an error.
118127
(_, _) => Err(HotColdDBError::UnsupportedSchemaVersion {
119128
target_version: to,

beacon_node/beacon_chain/src/schema_change/migration_schema_v12.rs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -168,16 +168,14 @@ pub fn downgrade_from_v12<T: BeaconChainTypes>(
168168
log: Logger,
169169
) -> Result<Vec<KeyValueStoreOp>, Error> {
170170
// Load a V12 op pool and transform it to V5.
171-
let PersistedOperationPoolV12 {
171+
let PersistedOperationPoolV12::<T::EthSpec> {
172172
attestations,
173173
sync_contributions,
174174
attester_slashings,
175175
proposer_slashings,
176176
voluntary_exits,
177-
} = if let Some(PersistedOperationPool::<T::EthSpec>::V12(op_pool)) =
178-
db.get_item(&OP_POOL_DB_KEY)?
179-
{
180-
op_pool
177+
} = if let Some(op_pool_v12) = db.get_item(&OP_POOL_DB_KEY)? {
178+
op_pool_v12
181179
} else {
182180
debug!(log, "Nothing to do, no operation pool stored");
183181
return Ok(vec![]);
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
use crate::beacon_chain::{BeaconChainTypes, OP_POOL_DB_KEY};
2+
use operation_pool::{
3+
PersistedOperationPool, PersistedOperationPoolV12, PersistedOperationPoolV14,
4+
};
5+
use slog::{debug, info, Logger};
6+
use std::sync::Arc;
7+
use store::{Error, HotColdDB, KeyValueStoreOp, StoreItem};
8+
9+
pub fn upgrade_to_v14<T: BeaconChainTypes>(
10+
db: Arc<HotColdDB<T::EthSpec, T::HotStore, T::ColdStore>>,
11+
log: Logger,
12+
) -> Result<Vec<KeyValueStoreOp>, Error> {
13+
// Load a V12 op pool and transform it to V14.
14+
let PersistedOperationPoolV12::<T::EthSpec> {
15+
attestations,
16+
sync_contributions,
17+
attester_slashings,
18+
proposer_slashings,
19+
voluntary_exits,
20+
} = if let Some(op_pool_v12) = db.get_item(&OP_POOL_DB_KEY)? {
21+
op_pool_v12
22+
} else {
23+
debug!(log, "Nothing to do, no operation pool stored");
24+
return Ok(vec![]);
25+
};
26+
27+
// initialize with empty vector
28+
let bls_to_execution_changes = vec![];
29+
let v14 = PersistedOperationPool::V14(PersistedOperationPoolV14 {
30+
attestations,
31+
sync_contributions,
32+
attester_slashings,
33+
proposer_slashings,
34+
voluntary_exits,
35+
bls_to_execution_changes,
36+
});
37+
Ok(vec![v14.as_kv_store_op(OP_POOL_DB_KEY)])
38+
}
39+
40+
pub fn downgrade_from_v14<T: BeaconChainTypes>(
41+
db: Arc<HotColdDB<T::EthSpec, T::HotStore, T::ColdStore>>,
42+
log: Logger,
43+
) -> Result<Vec<KeyValueStoreOp>, Error> {
44+
// Load a V14 op pool and transform it to V12.
45+
let PersistedOperationPoolV14 {
46+
attestations,
47+
sync_contributions,
48+
attester_slashings,
49+
proposer_slashings,
50+
voluntary_exits,
51+
bls_to_execution_changes,
52+
} = if let Some(PersistedOperationPool::<T::EthSpec>::V14(op_pool)) =
53+
db.get_item(&OP_POOL_DB_KEY)?
54+
{
55+
op_pool
56+
} else {
57+
debug!(log, "Nothing to do, no operation pool stored");
58+
return Ok(vec![]);
59+
};
60+
61+
info!(
62+
log,
63+
"Dropping bls_to_execution_changes from pool";
64+
"count" => bls_to_execution_changes.len(),
65+
);
66+
67+
let v12 = PersistedOperationPoolV12 {
68+
attestations,
69+
sync_contributions,
70+
attester_slashings,
71+
proposer_slashings,
72+
voluntary_exits,
73+
};
74+
Ok(vec![v12.as_kv_store_op(OP_POOL_DB_KEY)])
75+
}

beacon_node/operation_pool/src/lib.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ pub use attestation::AttMaxCover;
1212
pub use attestation_storage::{AttestationRef, SplitAttestation};
1313
pub use max_cover::MaxCover;
1414
pub use persistence::{
15-
PersistedOperationPool, PersistedOperationPoolV12, PersistedOperationPoolV5,
15+
PersistedOperationPool, PersistedOperationPoolV12, PersistedOperationPoolV14,
16+
PersistedOperationPoolV5,
1617
};
1718
pub use reward_cache::RewardCache;
1819

beacon_node/operation_pool/src/persistence.rs

Lines changed: 56 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ type PersistedSyncContributions<T> = Vec<(SyncAggregateId, Vec<SyncCommitteeCont
1818
/// Operations are stored in arbitrary order, so it's not a good idea to compare instances
1919
/// of this type (or its encoded form) for equality. Convert back to an `OperationPool` first.
2020
#[superstruct(
21-
variants(V5, V12),
21+
variants(V5, V12, V14),
2222
variant_attributes(
2323
derive(Derivative, PartialEq, Debug, Encode, Decode),
2424
derivative(Clone),
@@ -32,28 +32,31 @@ pub struct PersistedOperationPool<T: EthSpec> {
3232
#[superstruct(only(V5))]
3333
pub attestations_v5: Vec<(AttestationId, Vec<Attestation<T>>)>,
3434
/// Attestations and their attesting indices.
35-
#[superstruct(only(V12))]
35+
#[superstruct(only(V12, V14))]
3636
pub attestations: Vec<(Attestation<T>, Vec<u64>)>,
3737
/// Mapping from sync contribution ID to sync contributions and aggregate.
3838
pub sync_contributions: PersistedSyncContributions<T>,
3939
/// [DEPRECATED] Attester slashings.
4040
#[superstruct(only(V5))]
4141
pub attester_slashings_v5: Vec<(AttesterSlashing<T>, ForkVersion)>,
4242
/// Attester slashings.
43-
#[superstruct(only(V12))]
43+
#[superstruct(only(V12, V14))]
4444
pub attester_slashings: Vec<SigVerifiedOp<AttesterSlashing<T>, T>>,
4545
/// [DEPRECATED] Proposer slashings.
4646
#[superstruct(only(V5))]
4747
pub proposer_slashings_v5: Vec<ProposerSlashing>,
4848
/// Proposer slashings with fork information.
49-
#[superstruct(only(V12))]
49+
#[superstruct(only(V12, V14))]
5050
pub proposer_slashings: Vec<SigVerifiedOp<ProposerSlashing, T>>,
5151
/// [DEPRECATED] Voluntary exits.
5252
#[superstruct(only(V5))]
5353
pub voluntary_exits_v5: Vec<SignedVoluntaryExit>,
5454
/// Voluntary exits with fork information.
55-
#[superstruct(only(V12))]
55+
#[superstruct(only(V12, V14))]
5656
pub voluntary_exits: Vec<SigVerifiedOp<SignedVoluntaryExit, T>>,
57+
/// BLS to Execution Changes
58+
#[superstruct(only(V14))]
59+
pub bls_to_execution_changes: Vec<SigVerifiedOp<SignedBlsToExecutionChange, T>>,
5760
}
5861

5962
impl<T: EthSpec> PersistedOperationPool<T> {
@@ -99,12 +102,20 @@ impl<T: EthSpec> PersistedOperationPool<T> {
99102
.map(|(_, exit)| exit.clone())
100103
.collect();
101104

102-
PersistedOperationPool::V12(PersistedOperationPoolV12 {
105+
let bls_to_execution_changes = operation_pool
106+
.bls_to_execution_changes
107+
.read()
108+
.iter()
109+
.map(|(_, bls_to_execution_change)| bls_to_execution_change.clone())
110+
.collect();
111+
112+
PersistedOperationPool::V14(PersistedOperationPoolV14 {
103113
attestations,
104114
sync_contributions,
105115
attester_slashings,
106116
proposer_slashings,
107117
voluntary_exits,
118+
bls_to_execution_changes,
108119
})
109120
}
110121

@@ -127,23 +138,41 @@ impl<T: EthSpec> PersistedOperationPool<T> {
127138
);
128139
let sync_contributions = RwLock::new(self.sync_contributions().iter().cloned().collect());
129140
let attestations = match self {
130-
PersistedOperationPool::V5(_) => return Err(OpPoolError::IncorrectOpPoolVariant),
131-
PersistedOperationPool::V12(pool) => {
141+
PersistedOperationPool::V5(_) | PersistedOperationPool::V12(_) => {
142+
return Err(OpPoolError::IncorrectOpPoolVariant)
143+
}
144+
PersistedOperationPool::V14(ref pool) => {
132145
let mut map = AttestationMap::default();
133-
for (att, attesting_indices) in pool.attestations {
146+
for (att, attesting_indices) in pool.attestations.clone() {
134147
map.insert(att, attesting_indices);
135148
}
136149
RwLock::new(map)
137150
}
138151
};
152+
let bls_to_execution_changes = match self {
153+
PersistedOperationPool::V5(_) | PersistedOperationPool::V12(_) => {
154+
return Err(OpPoolError::IncorrectOpPoolVariant)
155+
}
156+
PersistedOperationPool::V14(pool) => RwLock::new(
157+
pool.bls_to_execution_changes
158+
.iter()
159+
.cloned()
160+
.map(|bls_to_execution_change| {
161+
(
162+
bls_to_execution_change.as_inner().message.validator_index,
163+
bls_to_execution_change,
164+
)
165+
})
166+
.collect(),
167+
),
168+
};
139169
let op_pool = OperationPool {
140170
attestations,
141171
sync_contributions,
142172
attester_slashings,
143173
proposer_slashings,
144174
voluntary_exits,
145-
// FIXME(capella): implement schema migration for address changes in op pool
146-
bls_to_execution_changes: Default::default(),
175+
bls_to_execution_changes,
147176
reward_cache: Default::default(),
148177
_phantom: Default::default(),
149178
};
@@ -165,6 +194,20 @@ impl<T: EthSpec> StoreItem for PersistedOperationPoolV5<T> {
165194
}
166195
}
167196

197+
impl<T: EthSpec> StoreItem for PersistedOperationPoolV12<T> {
198+
fn db_column() -> DBColumn {
199+
DBColumn::OpPool
200+
}
201+
202+
fn as_store_bytes(&self) -> Vec<u8> {
203+
self.as_ssz_bytes()
204+
}
205+
206+
fn from_store_bytes(bytes: &[u8]) -> Result<Self, StoreError> {
207+
PersistedOperationPoolV12::from_ssz_bytes(bytes).map_err(Into::into)
208+
}
209+
}
210+
168211
/// Deserialization for `PersistedOperationPool` defaults to `PersistedOperationPool::V12`.
169212
impl<T: EthSpec> StoreItem for PersistedOperationPool<T> {
170213
fn db_column() -> DBColumn {
@@ -177,8 +220,8 @@ impl<T: EthSpec> StoreItem for PersistedOperationPool<T> {
177220

178221
fn from_store_bytes(bytes: &[u8]) -> Result<Self, StoreError> {
179222
// Default deserialization to the latest variant.
180-
PersistedOperationPoolV12::from_ssz_bytes(bytes)
181-
.map(Self::V12)
223+
PersistedOperationPoolV14::from_ssz_bytes(bytes)
224+
.map(Self::V14)
182225
.map_err(Into::into)
183226
}
184227
}

beacon_node/store/src/metadata.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use ssz::{Decode, Encode};
44
use ssz_derive::{Decode, Encode};
55
use types::{Checkpoint, Hash256, Slot};
66

7-
pub const CURRENT_SCHEMA_VERSION: SchemaVersion = SchemaVersion(13);
7+
pub const CURRENT_SCHEMA_VERSION: SchemaVersion = SchemaVersion(14);
88

99
// All the keys that get stored under the `BeaconMeta` column.
1010
//

0 commit comments

Comments
 (0)