-
Notifications
You must be signed in to change notification settings - Fork 219
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add boilerplate for base node FSM (#834)
Merge pull request #834 Adds stubs for the missing state structs in the base node FSM. Provide comments for allowed state conversions. What's quite nice about doing it this way is that the _compiler_ will tell you if you try and enact an illegal state change, e.g. from Startup -> Listening; becuase the From trait isn't written for that change.
- Loading branch information
Showing
6 changed files
with
202 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
// Copyright 2019. The Tari Project | ||
// | ||
// Redistribution and use in source and binary forms, with or without modification, are permitted provided that the | ||
// following conditions are met: | ||
// | ||
// 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following | ||
// disclaimer. | ||
// | ||
// 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the | ||
// following disclaimer in the documentation and/or other materials provided with the distribution. | ||
// | ||
// 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote | ||
// products derived from this software without specific prior written permission. | ||
// | ||
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, | ||
// INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | ||
// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | ||
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR | ||
// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, | ||
// WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE | ||
// USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
|
||
use crate::{ | ||
base_node::states::{ | ||
fetching_horizon_state::FetchHorizonState, | ||
listening::Listening, | ||
InitialSync, | ||
StateEvent, | ||
StateEvent::FatalError, | ||
}, | ||
chain_storage::BlockchainBackend, | ||
}; | ||
use log::*; | ||
|
||
const LOG_TARGET: &str = "base_node::block_sync"; | ||
|
||
pub struct BlockSync; | ||
|
||
impl BlockSync { | ||
pub fn next_event(&mut self) -> StateEvent { | ||
info!(target: LOG_TARGET, "Synchronizing missing blocks"); | ||
FatalError("Unimplemented".into()) | ||
} | ||
} | ||
|
||
/// State management for FetchingHorizonState -> BlockSync. This is a typical transition for new nodes joining the | ||
/// network. | ||
impl From<FetchHorizonState> for BlockSync { | ||
fn from(_old: FetchHorizonState) -> Self { | ||
unimplemented!() | ||
} | ||
} | ||
|
||
/// State management for Listening -> BlockSync. This change happens when a node has been temporarily disconnected | ||
/// from the network, or a reorg has occurred. | ||
impl From<Listening> for BlockSync { | ||
fn from(_old: Listening) -> Self { | ||
unimplemented!() | ||
} | ||
} | ||
|
||
/// State management for InitialSync -> BlockSync. This change happens when a (previously synced) node is restarted | ||
/// after being offline for some time. | ||
impl<B: BlockchainBackend> From<InitialSync<B>> for BlockSync { | ||
fn from(_old: InitialSync<B>) -> Self { | ||
unimplemented!() | ||
} | ||
} |
54 changes: 54 additions & 0 deletions
54
base_layer/core/src/base_node/states/fetching_horizon_state.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
// Copyright 2019. The Tari Project | ||
// | ||
// Redistribution and use in source and binary forms, with or without modification, are permitted provided that the | ||
// following conditions are met: | ||
// | ||
// 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following | ||
// disclaimer. | ||
// | ||
// 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the | ||
// following disclaimer in the documentation and/or other materials provided with the distribution. | ||
// | ||
// 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote | ||
// products derived from this software without specific prior written permission. | ||
// | ||
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, | ||
// INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | ||
// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | ||
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR | ||
// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, | ||
// WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE | ||
// USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
|
||
use crate::{ | ||
base_node::states::{listening::Listening, InitialSync, StateEvent, StateEvent::FatalError}, | ||
chain_storage::BlockchainBackend, | ||
}; | ||
use log::*; | ||
|
||
const LOG_TARGET: &str = "base_node::fetching_horizon_state"; | ||
|
||
pub struct FetchHorizonState; | ||
|
||
impl FetchHorizonState { | ||
pub fn next_event(&mut self) -> StateEvent { | ||
info!(target: LOG_TARGET, "Starting synchronization of pruning horizon state"); | ||
FatalError("Unimplemented".into()) | ||
} | ||
} | ||
|
||
/// State management for InitialSync -> FetchingHorizonState. This is the typical transition for a new node joining | ||
/// the network | ||
impl<B: BlockchainBackend> From<InitialSync<B>> for FetchHorizonState { | ||
fn from(_old: InitialSync<B>) -> Self { | ||
unimplemented!() | ||
} | ||
} | ||
|
||
/// State management for Listening -> FetchingHorizonState. This can occur if a node has been disconnected from the | ||
/// network for a long time. | ||
impl From<Listening> for FetchHorizonState { | ||
fn from(_old: Listening) -> Self { | ||
unimplemented!() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
// Copyright 2019. The Tari Project | ||
// | ||
// Redistribution and use in source and binary forms, with or without modification, are permitted provided that the | ||
// following conditions are met: | ||
// | ||
// 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following | ||
// disclaimer. | ||
// | ||
// 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the | ||
// following disclaimer in the documentation and/or other materials provided with the distribution. | ||
// | ||
// 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote | ||
// products derived from this software without specific prior written permission. | ||
// | ||
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, | ||
// INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | ||
// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | ||
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR | ||
// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, | ||
// WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE | ||
// USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
|
||
use crate::{ | ||
base_node::states::{block_sync::BlockSync, InitialSync, StateEvent, StateEvent::FatalError}, | ||
chain_storage::BlockchainBackend, | ||
}; | ||
use log::*; | ||
|
||
const LOG_TARGET: &str = "base_node::listening"; | ||
|
||
pub struct Listening; | ||
|
||
impl Listening { | ||
pub fn next_event(&mut self) -> StateEvent { | ||
info!(target: LOG_TARGET, "Listening for new blocks"); | ||
FatalError("Unimplemented".into()) | ||
} | ||
} | ||
|
||
/// State management for BlockSync -> Listening. This change is part of the typical flow for new nodes joining the | ||
/// network, or established nodes that have caught up to the chain tip again. | ||
impl From<BlockSync> for Listening { | ||
fn from(_old: BlockSync) -> Self { | ||
unimplemented!() | ||
} | ||
} | ||
|
||
/// State management for BlockSync -> Listening. This state change happens when a node restarts and still happens to | ||
/// be in sync with the network. | ||
impl<B: BlockchainBackend> From<InitialSync<B>> for Listening { | ||
fn from(_old: InitialSync<B>) -> Self { | ||
unimplemented!() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters