Skip to content

Commit 40b9925

Browse files
drcpu-githubaesedepece
authored andcommitted
fix(node): remove obsolete ProtocolInfo from chain_manager
1 parent 82f3ce4 commit 40b9925

File tree

7 files changed

+48
-62
lines changed

7 files changed

+48
-62
lines changed

Diff for: data_structures/src/chain/mod.rs

+1-4
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ use crate::{
4343
},
4444
get_environment, get_protocol_version, get_protocol_version_activation_epoch,
4545
proto::{
46-
versioning::{ProtocolInfo, ProtocolVersion, Versioned, VersionedHashable},
46+
versioning::{ProtocolVersion, Versioned, VersionedHashable},
4747
ProtobufConvert,
4848
},
4949
staking::prelude::*,
@@ -76,9 +76,6 @@ pub trait Hashable {
7676
/// Data structure holding critical information about the chain state and protocol constants
7777
#[derive(Debug, Serialize, Deserialize, PartialEq, Clone, Default)]
7878
pub struct ChainInfo {
79-
/// Keeps track of protocol versions and their related data (e.g. activation epoch)
80-
pub protocol: ProtocolInfo,
81-
8279
/// Blockchain valid environment
8380
pub environment: Environment,
8481

Diff for: data_structures/src/lib.rs

+13-4
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,19 @@ pub fn set_environment(environment: Environment) {
126126
}
127127
}
128128

129+
/// Protocol version that we are running.
130+
pub fn get_protocol_info() -> ProtocolInfo {
131+
// This unwrap is safe as long as the lock is not poisoned.
132+
// The lock can only become poisoned when a writer panics.
133+
let protocol_info = PROTOCOL.read().unwrap();
134+
135+
ProtocolInfo {
136+
current_version: protocol_info.current_version,
137+
all_versions: protocol_info.all_versions.clone(),
138+
all_checkpoints_periods: protocol_info.all_checkpoints_periods.clone(),
139+
}
140+
}
141+
129142
/// Protocol version that we are running.
130143
pub fn get_protocol_version(epoch: Option<Epoch>) -> ProtocolVersion {
131144
// This unwrap is safe as long as the lock is not poisoned.
@@ -168,10 +181,6 @@ pub fn load_protocol_info(info: ProtocolInfo) {
168181
*protocol_info = info;
169182
}
170183

171-
pub fn initialize_default(default_checkpoint_period: u16) {
172-
register_protocol_version(ProtocolVersion::default(), 0, default_checkpoint_period);
173-
}
174-
175184
pub fn clear_protocol_info() {
176185
log::info!("Clearing all protocol versions");
177186
let mut protocol_info = PROTOCOL.write().unwrap();

Diff for: node/src/actors/chain_manager/actor.rs

-17
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,7 @@ use witnet_data_structures::{
1313
},
1414
data_request::DataRequestPool,
1515
get_environment, get_protocol_version_activation_epoch, get_protocol_version_period,
16-
initialize_default, load_protocol_info,
1716
proto::versioning::ProtocolVersion,
18-
refresh_protocol_version,
1917
staking::prelude::*,
2018
superblock::SuperBlockState,
2119
types::LastBeacon,
@@ -264,7 +262,6 @@ impl ChainManager {
264262
checkpoint: 0,
265263
hash_prev_vrf: hash_prev_block,
266264
},
267-
..ChainInfo::default()
268265
};
269266

270267
let bootstrap_committee = chain_info
@@ -376,20 +373,6 @@ impl ChainManager {
376373
chain_info.highest_block_checkpoint.hash_prev_block
377374
);
378375

379-
// Load protocol info into global state (overwrites whatever was set through
380-
// configuration). If possible, derives the current protocol version from that
381-
// info and the current epoch. This essentially allows a node to catch up with
382-
// a new protocol version if the transition happened while it was down.
383-
load_protocol_info(chain_info.protocol.clone());
384-
// If the protocol info we loaded above originates from a default initialization
385-
// of chain info, the default protocol versions are not registered yet. We cannot
386-
// use the Default trait to fix this because we do not have access to the required
387-
// checkpoint period from consensus constants in the data structures module.
388-
if get_protocol_version_activation_epoch(ProtocolVersion::default()) == u32::MAX {
389-
initialize_default(consensus_constants.checkpoints_period);
390-
}
391-
refresh_protocol_version(chain_info.highest_block_checkpoint.checkpoint);
392-
393376
// We may need to update the epoch constants in both the sessions and epoch manager to
394377
// correctly calculate the current epoch after wit/2 is activated while the configuration
395378
// has not been updated yet.

Diff for: node/src/actors/chain_manager/handlers.rs

+6-18
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,12 @@ use futures::future::Either;
1212
use itertools::Itertools;
1313
use witnet_data_structures::{
1414
chain::{
15-
tapi::ActiveWips, Block, ChainInfo, ChainState, CheckpointBeacon, ConsensusConstantsWit2,
15+
tapi::ActiveWips, Block, ChainState, CheckpointBeacon, ConsensusConstantsWit2,
1616
DataRequestInfo, Epoch, Hash, Hashable, NodeStats, PublicKeyHash, SuperBlockVote,
1717
SupplyInfo, SupplyInfo2, ValueTransferOutput,
1818
},
1919
error::{ChainInfoError, TransactionError::DataRequestNotFound},
20-
get_protocol_version,
20+
get_protocol_info, get_protocol_version, get_protocol_version_activation_epoch,
2121
proto::versioning::ProtocolVersion,
2222
refresh_protocol_version,
2323
staking::{
@@ -143,12 +143,8 @@ impl Handler<EpochNotification<EveryEpochPayload>> for ChainManager {
143143
// The best candidate must be cleared on every epoch
144144
let best_candidate = self.best_candidate.take();
145145

146-
// Make sure that the protocol version in the chain state is kept up to date
146+
// Update the global protocol version state if necessary
147147
let expected_protocol_version = get_protocol_version(self.current_epoch);
148-
if let Some(ChainInfo { protocol, .. }) = &mut self.chain_state.chain_info {
149-
protocol.current_version = expected_protocol_version;
150-
}
151-
// Also update the global protocol version state if necessary
152148
let current_protocol_version = get_protocol_version(None);
153149
if expected_protocol_version != current_protocol_version {
154150
refresh_protocol_version(current_epoch);
@@ -2038,11 +2034,8 @@ impl Handler<GetSupplyInfo2> for ChainManager {
20382034
let current_staked_supply = self.chain_state.stakes.total_staked().nanowits();
20392035

20402036
let wit1_block_reward = chain_info.consensus_constants.initial_block_reward;
2041-
let wit2_activated = chain_info.protocol.current_version == ProtocolVersion::V2_0;
2042-
let wit2_activation_epoch = chain_info
2043-
.protocol
2044-
.all_versions
2045-
.get_activation_epoch(chain_info.protocol.current_version);
2037+
let wit2_activated = get_protocol_version(None) == ProtocolVersion::V2_0;
2038+
let wit2_activation_epoch = get_protocol_version_activation_epoch(ProtocolVersion::V2_0);
20462039
let wit2_block_reward =
20472040
ConsensusConstantsWit2::default().get_validator_block_reward(current_epoch);
20482041

@@ -2438,12 +2431,7 @@ impl Handler<GetProtocolInfo> for ChainManager {
24382431
type Result = <GetProtocolInfo as Message>::Result;
24392432

24402433
fn handle(&mut self, _msg: GetProtocolInfo, _ctx: &mut Self::Context) -> Self::Result {
2441-
let chain_info = &self.chain_state.chain_info;
2442-
2443-
match chain_info {
2444-
None => Ok(None),
2445-
Some(ChainInfo { protocol, .. }) => Ok(Some(protocol.clone())),
2446-
}
2434+
Ok(Some(get_protocol_info()))
24472435
}
24482436
}
24492437

Diff for: node/src/actors/chain_manager/mod.rs

+1-12
Original file line numberDiff line numberDiff line change
@@ -1032,13 +1032,6 @@ impl ChainManager {
10321032
scheduled_epoch,
10331033
checkpoint_period,
10341034
);
1035-
// Register the 2_0 protocol into chain state (namely, chain info) so that
1036-
// the scheduled activation data eventually gets persisted into storage.
1037-
chain_info.protocol.register(
1038-
scheduled_epoch,
1039-
ProtocolVersion::V2_0,
1040-
checkpoint_period,
1041-
);
10421035

10431036
if let Some(epoch_constants) = &mut self.epoch_constants {
10441037
match epoch_constants.set_values_for_wit2(
@@ -3856,7 +3849,7 @@ mod tests {
38563849
KeyedSignature, OutputPointer, PartialConsensusConstants, PublicKey, SecretKey,
38573850
Signature, StakeOutput, ValueTransferOutput,
38583851
},
3859-
proto::versioning::{ProtocolInfo, VersionedHashable},
3852+
proto::versioning::VersionedHashable,
38603853
transaction::{
38613854
CommitTransaction, DRTransaction, MintTransaction, RevealTransaction, StakeTransaction,
38623855
StakeTransactionBody, UnstakeTransaction, UnstakeTransactionBody, VTTransaction,
@@ -4016,7 +4009,6 @@ mod tests {
40164009
hash_prev_block: Hash::SHA256([1; 32]),
40174010
},
40184011
highest_vrf_output: CheckpointVRF::default(),
4019-
protocol: ProtocolInfo::default(),
40204012
});
40214013

40224014
assert_eq!(
@@ -4438,7 +4430,6 @@ mod tests {
44384430
hash_prev_block: Hash::SHA256([1; 32]),
44394431
},
44404432
highest_vrf_output: CheckpointVRF::default(),
4441-
protocol: ProtocolInfo::default(),
44424433
});
44434434
chain_manager.chain_state.reputation_engine = Some(ReputationEngine::new(1000));
44444435
chain_manager.vrf_ctx = Some(VrfCtx::secp256k1().unwrap());
@@ -4573,7 +4564,6 @@ mod tests {
45734564
hash_prev_block: Hash::SHA256([1; 32]),
45744565
},
45754566
highest_vrf_output: CheckpointVRF::default(),
4576-
protocol: ProtocolInfo::default(),
45774567
});
45784568
let out_ptr = OutputPointer {
45794569
transaction_id: "0000000000000000000000000000000000000000000000000000000000000001"
@@ -4685,7 +4675,6 @@ mod tests {
46854675
hash_prev_block: Hash::SHA256([1; 32]),
46864676
},
46874677
highest_vrf_output: CheckpointVRF::default(),
4688-
protocol: ProtocolInfo::default(),
46894678
});
46904679
chain_manager.chain_state.stakes = stakes;
46914680

Diff for: node/src/storage_mngr/node_migrations.rs

+26-5
Original file line numberDiff line numberDiff line change
@@ -78,16 +78,32 @@ fn migrate_chain_state_v3_to_v4(old_chain_state_bytes: &[u8]) -> Vec<u8> {
7878
.concat()
7979
}
8080

81+
fn migrate_chain_state_v4_to_v5(old_chain_state_bytes: &[u8]) -> Vec<u8> {
82+
let db_version: u32 = 5;
83+
let db_version_bytes = db_version.to_le_bytes();
84+
85+
// Removal of fields in ChainState v5:
86+
let protocol_info = ProtocolInfo::default();
87+
let protocol_info_bytes = serialize(&protocol_info).unwrap();
88+
89+
[
90+
&db_version_bytes,
91+
&old_chain_state_bytes[4..5],
92+
&old_chain_state_bytes[5 + protocol_info_bytes.len()..],
93+
]
94+
.concat()
95+
}
96+
8197
fn migrate_chain_state(mut bytes: Vec<u8>) -> Result<ChainState, failure::Error> {
8298
loop {
8399
let version = check_chain_state_version(&bytes);
84-
log::debug!("Chain state version as read from storage is {:?}", version);
100+
log::info!("Chain state version as read from storage is {:?}", version);
85101

86102
match version {
87103
Ok(0) => {
88104
// Migrate from v0 to v2
89105
bytes = migrate_chain_state_v0_to_v2(&bytes);
90-
log::debug!("Successfully migrated ChainState v0 to v2");
106+
log::info!("Successfully migrated ChainState v0 to v2");
91107
}
92108
Ok(2) => {
93109
// Migrate from v2 to v3
@@ -96,14 +112,19 @@ fn migrate_chain_state(mut bytes: Vec<u8>) -> Result<ChainState, failure::Error>
96112
// and the UTXOs are stored in separate keys. But that operation is done in the
97113
// ChainManager on initialization, here we just update the db_version field.
98114
migrate_chain_state_v2_to_v3(&mut bytes);
99-
log::debug!("Successfully migrated ChainState v2 to v3");
115+
log::info!("Successfully migrated ChainState v2 to v3");
100116
}
101117
Ok(3) => {
102118
// Migrate from v3 to v4
103119
bytes = migrate_chain_state_v3_to_v4(&bytes);
104-
log::debug!("Successfully migrated ChainState v3 to v4");
120+
log::info!("Successfully migrated ChainState v3 to v4");
105121
}
106122
Ok(4) => {
123+
// Migrate from v3 to v4
124+
bytes = migrate_chain_state_v4_to_v5(&bytes);
125+
log::info!("Successfully migrated ChainState v4 to v5");
126+
}
127+
Ok(5) => {
107128
// Latest version
108129
// Skip the first 4 bytes because they are used to encode db_version
109130
return match deserialize(&bytes[4..]) {
@@ -207,7 +228,7 @@ pub fn put_chain_state_in_batch<K>(
207228
where
208229
K: serde::Serialize + 'static,
209230
{
210-
let db_version: u32 = 4;
231+
let db_version: u32 = 5;
211232
// The first byte of the ChainState db_version must never be 0 or 1,
212233
// because that can be confused with version 0.
213234
assert!(db_version.to_le_bytes()[0] >= 2);

Diff for: node/tests/serialization.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use bincode::{deserialize, serialize};
22
use serde::{de::DeserializeOwned, Serialize};
33
use std::fmt::Debug;
4-
use witnet_data_structures::{chain::*, proto::versioning::ProtocolInfo};
4+
use witnet_data_structures::chain::*;
55

66
fn t<T>(al: T)
77
where
@@ -57,7 +57,6 @@ fn chain_state() {
5757
checkpoint: 0,
5858
hash_prev_vrf: bootstrap_hash,
5959
},
60-
protocol: ProtocolInfo::default(),
6160
};
6261
let c = ChainState {
6362
chain_info: Some(chain_info),

0 commit comments

Comments
 (0)