Goblin — Build 98
This commit is contained in:
+1059
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,304 @@
|
||||
// Copyright 2023 The Grim Developers
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
//! Build a block to mine: gathers transactions from the pool, assembles
|
||||
//! them into a block and returns it.
|
||||
|
||||
use chrono::prelude::{DateTime, Utc};
|
||||
use rand::{Rng, rng};
|
||||
use serde_json::{Value, json};
|
||||
use std::sync::Arc;
|
||||
use std::thread;
|
||||
use std::time::Duration;
|
||||
|
||||
use crate::node::stratum::StratumStopState;
|
||||
use grin_api;
|
||||
use grin_chain;
|
||||
use grin_core::core::{Output, TxKernel};
|
||||
use grin_core::libtx::ProofBuilder;
|
||||
use grin_core::libtx::secp_ser;
|
||||
use grin_core::{consensus, core, global};
|
||||
use grin_keychain::{ExtKeychain, Identifier, Keychain};
|
||||
use grin_servers::ServerTxPool;
|
||||
use grin_servers::common::types::Error;
|
||||
use log::{debug, error, trace, warn};
|
||||
use serde_derive::{Deserialize, Serialize};
|
||||
|
||||
/// Fees in block to use for coinbase amount calculation
|
||||
/// (Duplicated from Grin wallet project)
|
||||
#[derive(Serialize, Deserialize, Debug, Clone)]
|
||||
pub struct BlockFees {
|
||||
/// fees
|
||||
#[serde(with = "secp_ser::string_or_u64")]
|
||||
pub fees: u64,
|
||||
/// height
|
||||
#[serde(with = "secp_ser::string_or_u64")]
|
||||
pub height: u64,
|
||||
/// key id
|
||||
pub key_id: Option<Identifier>,
|
||||
}
|
||||
|
||||
impl BlockFees {
|
||||
/// return key id
|
||||
pub fn key_id(&self) -> Option<Identifier> {
|
||||
self.key_id.clone()
|
||||
}
|
||||
}
|
||||
|
||||
/// Response to build a coinbase output.
|
||||
#[derive(Serialize, Deserialize, Debug, Clone)]
|
||||
pub struct CbData {
|
||||
/// Output
|
||||
pub output: Output,
|
||||
/// Kernel
|
||||
pub kernel: TxKernel,
|
||||
/// Key Id
|
||||
pub key_id: Option<Identifier>,
|
||||
}
|
||||
|
||||
// Ensure a block suitable for mining is built and returned
|
||||
// If a wallet listener URL is not provided the reward will be "burnt"
|
||||
// Warning: This call does not return until/unless a new block can be built
|
||||
pub fn get_block(
|
||||
chain: &Arc<grin_chain::Chain>,
|
||||
tx_pool: &ServerTxPool,
|
||||
key_id: Option<Identifier>,
|
||||
wallet_listener_url: Option<String>,
|
||||
stop_state: &Arc<StratumStopState>,
|
||||
) -> Option<(core::Block, BlockFees)> {
|
||||
let wallet_retry_interval = 5;
|
||||
// get the latest chain state and build a block on top of it
|
||||
let mut result = build_block(chain, tx_pool, key_id.clone(), wallet_listener_url.clone());
|
||||
while let Err(e) = result {
|
||||
let mut new_key_id = key_id.to_owned();
|
||||
match e {
|
||||
Error::Chain(c) => match c {
|
||||
grin_chain::Error::DuplicateCommitment(_) => {
|
||||
debug!(
|
||||
"Duplicate commit for potential coinbase detected. Trying next derivation."
|
||||
);
|
||||
// use the next available key to generate a different coinbase commitment
|
||||
new_key_id = None;
|
||||
}
|
||||
_ => {
|
||||
error!("Chain Error: {:?}", c);
|
||||
}
|
||||
},
|
||||
Error::WalletComm(_) => {
|
||||
error!(
|
||||
"Error building new block: Can't connect to wallet listener at {:?}; will retry",
|
||||
wallet_listener_url.as_ref().unwrap()
|
||||
);
|
||||
thread::sleep(Duration::from_secs(wallet_retry_interval));
|
||||
}
|
||||
ae => {
|
||||
warn!("Error building new block: {:?}. Retrying.", ae);
|
||||
}
|
||||
}
|
||||
|
||||
// only wait if we are still using the same key: a different coinbase commitment is unlikely
|
||||
// to have duplication
|
||||
if new_key_id.is_some() {
|
||||
thread::sleep(Duration::from_millis(100));
|
||||
}
|
||||
|
||||
// Stop attempts to build a block on stop.
|
||||
if stop_state.is_stopped() {
|
||||
return None;
|
||||
}
|
||||
result = build_block(chain, tx_pool, new_key_id, wallet_listener_url.clone());
|
||||
}
|
||||
Some(result.unwrap())
|
||||
}
|
||||
|
||||
/// Builds a new block with the chain head as previous and eligible
|
||||
/// transactions from the pool.
|
||||
fn build_block(
|
||||
chain: &Arc<grin_chain::Chain>,
|
||||
tx_pool: &ServerTxPool,
|
||||
key_id: Option<Identifier>,
|
||||
wallet_listener_url: Option<String>,
|
||||
) -> Result<(core::Block, BlockFees), Error> {
|
||||
let head = chain.head_header()?;
|
||||
|
||||
// prepare the block header timestamp
|
||||
let mut now_sec = Utc::now().timestamp();
|
||||
let head_sec = head.timestamp.timestamp();
|
||||
if now_sec <= head_sec {
|
||||
now_sec = head_sec + 1;
|
||||
}
|
||||
|
||||
// Determine the difficulty our block should be at.
|
||||
// Note: do not keep the difficulty_iter in scope (it has an active batch).
|
||||
let difficulty = consensus::next_difficulty(head.height + 1, chain.difficulty_iter()?);
|
||||
|
||||
// Extract current "mineable" transactions from the pool.
|
||||
// If this fails for *any* reason then fallback to an empty vec of txs.
|
||||
// This will allow us to mine an "empty" block if the txpool is in an
|
||||
// invalid (and unexpected) state.
|
||||
let txs = match tx_pool.read().prepare_mineable_transactions() {
|
||||
Ok(txs) => txs,
|
||||
Err(e) => {
|
||||
error!(
|
||||
"build_block: Failed to prepare mineable txs from txpool: {:?}",
|
||||
e
|
||||
);
|
||||
warn!("build_block: Falling back to mining empty block.");
|
||||
vec![]
|
||||
}
|
||||
};
|
||||
|
||||
// build the coinbase and the block itself
|
||||
let fees = txs.iter().map(|tx| tx.fee()).sum();
|
||||
let height = head.height + 1;
|
||||
let block_fees = BlockFees {
|
||||
fees,
|
||||
key_id,
|
||||
height,
|
||||
};
|
||||
|
||||
let (output, kernel, block_fees) = get_coinbase(wallet_listener_url, block_fees)?;
|
||||
let mut b = core::Block::from_reward(&head, &txs, output, kernel, difficulty.difficulty)?;
|
||||
|
||||
// making sure we're not spending time mining a useless block
|
||||
b.validate(&head.total_kernel_offset)?;
|
||||
|
||||
b.header.pow.nonce = rng().random();
|
||||
b.header.pow.secondary_scaling = difficulty.secondary_scaling;
|
||||
b.header.timestamp = DateTime::from_timestamp(now_sec, 0).unwrap();
|
||||
|
||||
debug!(
|
||||
"Built new block with {} inputs and {} outputs, block difficulty: {}, cumulative difficulty {}",
|
||||
b.inputs().len(),
|
||||
b.outputs().len(),
|
||||
difficulty.difficulty,
|
||||
b.header.total_difficulty().to_num(),
|
||||
);
|
||||
|
||||
// Now set txhashset roots and sizes on the header of the block being built.
|
||||
match chain.set_txhashset_roots(&mut b) {
|
||||
Ok(_) => Ok((b, block_fees)),
|
||||
Err(e) => {
|
||||
match e {
|
||||
// If this is a duplicate commitment then likely trying to use
|
||||
// a key that hass already been derived but not in the wallet
|
||||
// for some reason, allow caller to retry.
|
||||
grin_chain::Error::DuplicateCommitment(e) => {
|
||||
Err(Error::Chain(grin_chain::Error::DuplicateCommitment(e)))
|
||||
}
|
||||
|
||||
// Some other issue, possibly duplicate kernel
|
||||
_ => {
|
||||
error!("Error setting txhashset root to build a block: {:?}", e);
|
||||
Err(Error::Chain(grin_chain::Error::Other(format!("{:?}", e))))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
///
|
||||
/// Probably only want to do this when testing.
|
||||
///
|
||||
fn burn_reward(block_fees: BlockFees) -> Result<(Output, TxKernel, BlockFees), Error> {
|
||||
warn!("Burning block fees: {:?}", block_fees);
|
||||
let keychain = ExtKeychain::from_random_seed(global::is_testnet())?;
|
||||
let key_id = ExtKeychain::derive_key_id(1, 1, 0, 0, 0);
|
||||
let (out, kernel) = grin_core::libtx::reward::output(
|
||||
&keychain,
|
||||
&ProofBuilder::new(&keychain),
|
||||
&key_id,
|
||||
block_fees.fees,
|
||||
false,
|
||||
)
|
||||
.unwrap();
|
||||
Ok((out, kernel, block_fees))
|
||||
}
|
||||
|
||||
// Connect to the wallet listener and get coinbase.
|
||||
// Warning: If a wallet listener URL is not provided the reward will be "burnt"
|
||||
fn get_coinbase(
|
||||
wallet_listener_url: Option<String>,
|
||||
block_fees: BlockFees,
|
||||
) -> Result<(Output, TxKernel, BlockFees), Error> {
|
||||
return match wallet_listener_url {
|
||||
None => {
|
||||
// Burn it
|
||||
burn_reward(block_fees)
|
||||
}
|
||||
Some(wallet_listener_url) => {
|
||||
let res = create_coinbase(&wallet_listener_url, &block_fees)?;
|
||||
let output = res.output;
|
||||
let kernel = res.kernel;
|
||||
let key_id = res.key_id;
|
||||
let block_fees = BlockFees {
|
||||
key_id,
|
||||
..block_fees
|
||||
};
|
||||
|
||||
debug!("get_coinbase: {:?}", block_fees);
|
||||
Ok((output, kernel, block_fees))
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
/// Call the wallet API to create a coinbase output for the given block_fees.
|
||||
/// Will retry based on default "retry forever with backoff" behavior.
|
||||
fn create_coinbase(dest: &str, block_fees: &BlockFees) -> Result<CbData, Error> {
|
||||
let url = format!("{}/v2/foreign", dest);
|
||||
let req_body = json!({
|
||||
"jsonrpc": "2.0",
|
||||
"method": "build_coinbase",
|
||||
"id": 1,
|
||||
"params": {
|
||||
"block_fees": block_fees
|
||||
}
|
||||
});
|
||||
|
||||
trace!("Sending build_coinbase request: {}", req_body);
|
||||
let req = grin_api::client::create_post_request(url.as_str(), None, &req_body)?;
|
||||
let timeout = grin_api::client::TimeOut::default();
|
||||
let res: String = grin_api::client::send_request(req, timeout).map_err(|e| {
|
||||
let report = format!(
|
||||
"Failed to get coinbase from {}. Is the wallet listening? {:?}",
|
||||
dest, e
|
||||
);
|
||||
error!("{}", report);
|
||||
Error::WalletComm(report)
|
||||
})?;
|
||||
|
||||
let res: Value = serde_json::from_str(&res).unwrap();
|
||||
trace!("Response: {}", res);
|
||||
if res["error"] != json!(null) {
|
||||
let report = format!(
|
||||
"Failed to get coinbase from {}: Error: {}, Message: {}",
|
||||
dest, res["error"]["code"], res["error"]["message"]
|
||||
);
|
||||
error!("{}", report);
|
||||
return Err(Error::WalletComm(report));
|
||||
}
|
||||
|
||||
let cb_data = res["result"]["Ok"].clone();
|
||||
trace!("cb_data: {}", cb_data);
|
||||
let ret_val = match serde_json::from_value::<CbData>(cb_data) {
|
||||
Ok(r) => r,
|
||||
Err(e) => {
|
||||
let report = format!("Couldn't deserialize CbData: {}", e);
|
||||
error!("{}", report);
|
||||
return Err(Error::WalletComm(report));
|
||||
}
|
||||
};
|
||||
|
||||
Ok(ret_val)
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
// Copyright 2023 The Grim Developers
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
mod mine_block;
|
||||
mod stratum;
|
||||
|
||||
mod node;
|
||||
pub use node::Node;
|
||||
|
||||
mod config;
|
||||
pub use config::*;
|
||||
|
||||
mod types;
|
||||
pub use types::*;
|
||||
@@ -0,0 +1,881 @@
|
||||
// Copyright 2023 The Grim Developers
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
use futures::channel::oneshot;
|
||||
use lazy_static::lazy_static;
|
||||
use parking_lot::RwLock;
|
||||
use std::path::{Path, PathBuf};
|
||||
use std::sync::Arc;
|
||||
use std::sync::atomic::{AtomicBool, Ordering};
|
||||
use std::time::Duration;
|
||||
use std::{fs, thread};
|
||||
|
||||
use crate::node::stratum::{StratumServer, StratumStopState};
|
||||
use crate::node::{NodeConfig, NodeError, PeersConfig};
|
||||
use grin_chain::SyncStatus;
|
||||
use grin_core::global;
|
||||
use grin_core::global::ChainTypes;
|
||||
use grin_p2p::Seeding;
|
||||
use grin_p2p::msg::PeerAddrs;
|
||||
use grin_servers::common::types::Error;
|
||||
use grin_servers::{Server, ServerStats, StratumServerConfig, StratumStats};
|
||||
use log::error;
|
||||
|
||||
lazy_static! {
|
||||
/// Static thread-aware state of [`Node`] to be updated from separate thread.
|
||||
static ref NODE_STATE: Arc<Node> = Arc::new(Node::default());
|
||||
}
|
||||
|
||||
/// Provides [`Server`] control, holds current status and statistics.
|
||||
pub struct Node {
|
||||
/// Node [`Server`] statistics information.
|
||||
stats: Arc<RwLock<Option<ServerStats>>>,
|
||||
|
||||
/// [`StratumServer`] statistics information.
|
||||
stratum_stats: Arc<grin_util::RwLock<StratumStats>>,
|
||||
/// Flag to start [`StratumServer`].
|
||||
start_stratum_needed: AtomicBool,
|
||||
/// State to stop [`StratumServer`] from outside.
|
||||
stratum_stop_state: Arc<StratumStopState>,
|
||||
|
||||
/// Indicator if node [`Server`] is starting.
|
||||
starting: AtomicBool,
|
||||
/// Flag to stop the [`Server`] and start it again.
|
||||
restart_needed: AtomicBool,
|
||||
/// Flag to stop the [`Server`].
|
||||
stop_needed: AtomicBool,
|
||||
/// Flag to check if app exit is needed after [`Server`] stop.
|
||||
exit_after_stop: AtomicBool,
|
||||
/// Flag to reset data and restart the [`Server`].
|
||||
reset_data: AtomicBool,
|
||||
/// Flag to change data directory and restart the [`Server`].
|
||||
change_data_dir: AtomicBool,
|
||||
|
||||
/// An error occurred on [`Server`] start.
|
||||
error: Arc<RwLock<Option<Error>>>,
|
||||
}
|
||||
|
||||
impl Default for Node {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
stats: Arc::new(RwLock::new(None)),
|
||||
stratum_stats: Arc::new(grin_util::RwLock::new(StratumStats::default())),
|
||||
stratum_stop_state: Arc::new(StratumStopState::default()),
|
||||
starting: AtomicBool::new(false),
|
||||
restart_needed: AtomicBool::new(false),
|
||||
stop_needed: AtomicBool::new(false),
|
||||
exit_after_stop: AtomicBool::new(false),
|
||||
start_stratum_needed: AtomicBool::new(false),
|
||||
error: Arc::new(RwLock::new(None)),
|
||||
reset_data: AtomicBool::new(false),
|
||||
change_data_dir: AtomicBool::new(false),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Node {
|
||||
/// Delay for thread to update the stats.
|
||||
pub const STATS_UPDATE_DELAY: Duration = Duration::from_millis(1000);
|
||||
|
||||
/// Default Mainnet DNS Seeds
|
||||
pub const MAINNET_DNS_SEEDS: &[&str] = &[
|
||||
"mainnet-seed.grinnode.live", // info@grinnode.live
|
||||
"grincoin.org", // xmpp:aglkm@conversations.im
|
||||
"main.gri.mw", // admin@gri.mw
|
||||
"mainnet.grinffindor.org", // support@grinffindor.org
|
||||
"main-seed.grin.money", // support@grinily.com
|
||||
];
|
||||
|
||||
/// Default Testnet DNS Seeds
|
||||
pub const TESTNET_DNS_SEEDS: &[&str] = &[
|
||||
"testnet.grincoin.org", // xmpp:aglkm@conversations.im
|
||||
"test.gri.mw", // admin@gri.mw
|
||||
"testnet.grinffindor.org", // support@grinffindor.org
|
||||
"test-seed.grin.money", // support@grinily.com
|
||||
];
|
||||
|
||||
/// Stop the [`Server`] and setup exit flag after if needed.
|
||||
pub fn stop(exit_after_stop: bool) {
|
||||
NODE_STATE.stop_needed.store(true, Ordering::Relaxed);
|
||||
NODE_STATE
|
||||
.exit_after_stop
|
||||
.store(exit_after_stop, Ordering::Relaxed);
|
||||
}
|
||||
|
||||
/// Request to start the [`Node`].
|
||||
pub fn start() {
|
||||
if !Self::is_running() {
|
||||
Self::start_server_thread();
|
||||
}
|
||||
}
|
||||
|
||||
/// Request to restart the [`Node`].
|
||||
pub fn restart() {
|
||||
if Self::is_running() {
|
||||
NODE_STATE.restart_needed.store(true, Ordering::Relaxed);
|
||||
} else {
|
||||
Node::start();
|
||||
}
|
||||
}
|
||||
|
||||
/// Request to start [`StratumServer`].
|
||||
pub fn start_stratum() {
|
||||
NODE_STATE
|
||||
.start_stratum_needed
|
||||
.store(true, Ordering::Relaxed);
|
||||
}
|
||||
|
||||
/// Check if [`StratumServer`] is starting.
|
||||
pub fn is_stratum_starting() -> bool {
|
||||
NODE_STATE.start_stratum_needed.load(Ordering::Relaxed)
|
||||
}
|
||||
|
||||
/// Get [`StratumServer`] statistics.
|
||||
pub fn get_stratum_stats() -> StratumStats {
|
||||
NODE_STATE.stratum_stats.read().clone()
|
||||
}
|
||||
|
||||
/// Stop [`StratumServer`].
|
||||
pub fn stop_stratum() {
|
||||
NODE_STATE.stratum_stop_state.stop()
|
||||
}
|
||||
|
||||
/// Check if [`StratumServer`] is stopping.
|
||||
pub fn is_stratum_stopping() -> bool {
|
||||
NODE_STATE.stratum_stop_state.is_stopped()
|
||||
}
|
||||
|
||||
/// Check if [`Node`] is starting.
|
||||
pub fn is_starting() -> bool {
|
||||
NODE_STATE.starting.load(Ordering::Relaxed)
|
||||
}
|
||||
|
||||
/// Check if [`Node`] is running.
|
||||
pub fn is_running() -> bool {
|
||||
Self::get_sync_status().is_some()
|
||||
}
|
||||
|
||||
/// Check if [`Node`] is stopping.
|
||||
pub fn is_stopping() -> bool {
|
||||
NODE_STATE.stop_needed.load(Ordering::Relaxed)
|
||||
}
|
||||
|
||||
/// Check if [`Node`] is restarting.
|
||||
pub fn is_restarting() -> bool {
|
||||
NODE_STATE.restart_needed.load(Ordering::Relaxed) || Self::reset_data_needed()
|
||||
}
|
||||
|
||||
/// Check if reset of [`Server`] peers is needed.
|
||||
fn reset_data_needed() -> bool {
|
||||
NODE_STATE.reset_data.load(Ordering::Relaxed)
|
||||
}
|
||||
|
||||
/// Get node [`Server`] statistics.
|
||||
pub fn get_stats() -> Option<ServerStats> {
|
||||
NODE_STATE.stats.read().clone()
|
||||
}
|
||||
|
||||
/// Check if [`Server`] is not syncing (disabled or just running after synchronization).
|
||||
pub fn not_syncing() -> bool {
|
||||
match Node::get_sync_status() {
|
||||
None => true,
|
||||
Some(ss) => ss == SyncStatus::NoSync,
|
||||
}
|
||||
}
|
||||
|
||||
/// Get synchronization status, empty when [`Server`] is not running.
|
||||
pub fn get_sync_status() -> Option<SyncStatus> {
|
||||
// Return Shutdown status when node is stopping.
|
||||
if Self::is_stopping() {
|
||||
return Some(SyncStatus::Shutdown);
|
||||
}
|
||||
|
||||
// Return Initial status when node is starting or restarting or peers are deleting.
|
||||
if Self::is_starting() || Self::is_restarting() {
|
||||
return Some(SyncStatus::Initial);
|
||||
}
|
||||
|
||||
let stats = Self::get_stats();
|
||||
// Return sync status when server is running (stats are not empty).
|
||||
if stats.is_some() {
|
||||
return Some(stats.as_ref().unwrap().sync_status);
|
||||
}
|
||||
None
|
||||
}
|
||||
|
||||
/// Get [`Server`] error.
|
||||
pub fn get_error() -> Option<NodeError> {
|
||||
let r_err = NODE_STATE.error.read();
|
||||
if r_err.is_some() {
|
||||
let e = r_err.as_ref().unwrap();
|
||||
// Flag setup to show an error to clean up data.
|
||||
let store_err = match e {
|
||||
Error::Store(_) => true,
|
||||
Error::Chain(_) => true,
|
||||
_ => false,
|
||||
};
|
||||
if store_err {
|
||||
return Some(NodeError::Storage);
|
||||
}
|
||||
|
||||
// Flag setup to show P2P or API server error.
|
||||
let p2p_api_err = match e {
|
||||
Error::P2P(_) => Some(NodeError::P2P),
|
||||
Error::API(_) => Some(NodeError::API),
|
||||
_ => None,
|
||||
};
|
||||
if p2p_api_err.is_some() {
|
||||
return p2p_api_err;
|
||||
}
|
||||
|
||||
// Flag setup to show configuration error.
|
||||
let config_err = match e {
|
||||
Error::Configuration(_) => true,
|
||||
_ => false,
|
||||
};
|
||||
return if config_err {
|
||||
Some(NodeError::Configuration)
|
||||
} else {
|
||||
Some(NodeError::Unknown)
|
||||
};
|
||||
}
|
||||
None
|
||||
}
|
||||
|
||||
/// Start the [`Server`] at separate thread to update state with stats and handle statuses.
|
||||
fn start_server_thread() {
|
||||
thread::spawn(move || {
|
||||
NODE_STATE.starting.store(true, Ordering::Relaxed);
|
||||
// Start the server.
|
||||
match start_node_server() {
|
||||
Ok(mut server) => {
|
||||
let mut first_start = true;
|
||||
loop {
|
||||
// Restart server if request or peers clean up is needed
|
||||
if Self::is_restarting() {
|
||||
server.stop();
|
||||
// Wait server after stop.
|
||||
thread::sleep(Duration::from_millis(5000));
|
||||
// Reset data if requested.
|
||||
if Self::reset_data_needed() {
|
||||
Node::reset_data(true);
|
||||
}
|
||||
// Reset stratum stats.
|
||||
{
|
||||
let mut w_stratum_stats = NODE_STATE.stratum_stats.write();
|
||||
*w_stratum_stats = StratumStats::default();
|
||||
}
|
||||
// Create new server.
|
||||
match start_node_server() {
|
||||
Ok(s) => {
|
||||
server = s;
|
||||
NODE_STATE.restart_needed.store(false, Ordering::Relaxed);
|
||||
}
|
||||
Err(e) => {
|
||||
error!("Error starting node: {:?}", e);
|
||||
{
|
||||
let mut w_err = NODE_STATE.error.write();
|
||||
*w_err = Some(e);
|
||||
}
|
||||
// Reset server state.
|
||||
Self::reset_server_state(true);
|
||||
break;
|
||||
}
|
||||
}
|
||||
} else if Self::is_stopping() {
|
||||
// Stop the server.
|
||||
server.stop();
|
||||
// Clean stats and statuses.
|
||||
Self::reset_server_state(false);
|
||||
break;
|
||||
}
|
||||
|
||||
// Start stratum mining server if requested.
|
||||
let stratum_start_requested = Self::is_stratum_starting();
|
||||
if stratum_start_requested {
|
||||
let (s_ip, s_port) = NodeConfig::get_stratum_address();
|
||||
if NodeConfig::is_stratum_port_available(&s_ip, &s_port) {
|
||||
let stratum_config =
|
||||
server.config.stratum_mining_config.clone().unwrap();
|
||||
start_stratum_mining_server(&server, stratum_config);
|
||||
}
|
||||
}
|
||||
|
||||
// Update server stats.
|
||||
if let Ok(stats) = server.get_server_stats() {
|
||||
{
|
||||
let mut w_stats = NODE_STATE.stats.write();
|
||||
*w_stats = Some(stats.clone());
|
||||
}
|
||||
|
||||
if first_start {
|
||||
NODE_STATE.starting.store(false, Ordering::Relaxed);
|
||||
first_start = false;
|
||||
}
|
||||
}
|
||||
|
||||
// Reset stratum server start flag.
|
||||
if stratum_start_requested && NODE_STATE.stratum_stats.read().is_running {
|
||||
NODE_STATE
|
||||
.start_stratum_needed
|
||||
.store(false, Ordering::Relaxed);
|
||||
}
|
||||
|
||||
thread::sleep(Self::STATS_UPDATE_DELAY);
|
||||
}
|
||||
}
|
||||
Err(e) => {
|
||||
error!("Error starting node: {:?}", e);
|
||||
{
|
||||
let mut w_err = NODE_STATE.error.write();
|
||||
*w_err = Some(e);
|
||||
}
|
||||
// Reset server state.
|
||||
Self::reset_server_state(true);
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/// Clean up [`Server`] stats and statuses.
|
||||
fn reset_server_state(has_error: bool) {
|
||||
NODE_STATE.starting.store(false, Ordering::Relaxed);
|
||||
NODE_STATE.restart_needed.store(false, Ordering::Relaxed);
|
||||
NODE_STATE
|
||||
.start_stratum_needed
|
||||
.store(false, Ordering::Relaxed);
|
||||
NODE_STATE.stop_needed.store(false, Ordering::Relaxed);
|
||||
|
||||
// Reset stratum stats.
|
||||
{
|
||||
let mut w_stratum_stats = NODE_STATE.stratum_stats.write();
|
||||
*w_stratum_stats = StratumStats::default();
|
||||
}
|
||||
// Reset server stats.
|
||||
{
|
||||
let mut w_stats = NODE_STATE.stats.write();
|
||||
*w_stats = None;
|
||||
}
|
||||
// Reset an error if needed.
|
||||
if !has_error {
|
||||
let mut w_err = NODE_STATE.error.write();
|
||||
*w_err = None;
|
||||
}
|
||||
}
|
||||
|
||||
/// Change chain data directory.
|
||||
pub fn change_data_dir(path: String) {
|
||||
if Self::data_dir_changing() || NodeConfig::get_chain_data_path() == path {
|
||||
return;
|
||||
}
|
||||
NODE_STATE.change_data_dir.store(true, Ordering::Relaxed);
|
||||
thread::spawn(move || {
|
||||
let running = Node::is_running();
|
||||
if running {
|
||||
Node::stop(false);
|
||||
// Wait node to stop before moving files.
|
||||
while Node::is_running() {
|
||||
thread::sleep(Self::STATS_UPDATE_DELAY);
|
||||
}
|
||||
}
|
||||
let cfg_path = NodeConfig::get_chain_data_path();
|
||||
let old = Path::new(cfg_path.as_str());
|
||||
let new = Path::new(path.as_str());
|
||||
if !old.exists() {
|
||||
NodeConfig::save_chain_data_path(path);
|
||||
} else {
|
||||
fs::create_dir_all(&new).unwrap_or_default();
|
||||
if let Ok(_) = fs::rename(old, new) {
|
||||
NodeConfig::save_chain_data_path(path);
|
||||
} else {
|
||||
fs::remove_dir_all(old).unwrap();
|
||||
NodeConfig::save_chain_data_path(path);
|
||||
}
|
||||
}
|
||||
NODE_STATE.change_data_dir.store(false, Ordering::Relaxed);
|
||||
// Restart node after migration.
|
||||
if running && !Node::is_stopping() {
|
||||
Node::start();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/// Check if chain data directory is changing.
|
||||
pub fn data_dir_changing() -> bool {
|
||||
NODE_STATE.change_data_dir.load(Ordering::Relaxed)
|
||||
}
|
||||
|
||||
/// Clean-up [`Server`] data if server is not running.
|
||||
pub fn clean_up_data() {
|
||||
if Self::is_running() {
|
||||
return;
|
||||
}
|
||||
let config = NodeConfig::node_server_config();
|
||||
let server_config = config.server.clone();
|
||||
let dirs_to_remove: Vec<&str> = vec!["header", "lmdb", "txhashset"];
|
||||
for dir in dirs_to_remove {
|
||||
let mut path = PathBuf::from(&server_config.db_root);
|
||||
path.push(dir);
|
||||
if path.exists() {
|
||||
fs::remove_dir_all(path).unwrap();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Reset [`Server`] data.
|
||||
pub fn reset_data(force: bool) {
|
||||
if force || !Node::is_running() {
|
||||
let config = NodeConfig::node_server_config();
|
||||
let server_config = config.server.clone();
|
||||
// Remove data folder.
|
||||
let data_dir = PathBuf::from(&server_config.db_root);
|
||||
match fs::remove_dir_all(data_dir) {
|
||||
Ok(_) => {}
|
||||
Err(_) => {}
|
||||
}
|
||||
NODE_STATE.reset_data.store(false, Ordering::Relaxed);
|
||||
} else {
|
||||
NODE_STATE.reset_data.store(true, Ordering::Relaxed);
|
||||
}
|
||||
}
|
||||
|
||||
/// Get synchronization status i18n text.
|
||||
pub fn get_sync_status_text() -> String {
|
||||
if Node::data_dir_changing() {
|
||||
return t!("moving_files").into();
|
||||
}
|
||||
|
||||
if Node::is_stopping() {
|
||||
return t!("sync_status.shutdown").into();
|
||||
};
|
||||
if Node::is_starting() {
|
||||
return t!("sync_status.initial").into();
|
||||
};
|
||||
if Node::is_restarting() {
|
||||
return t!("sync_status.node_restarting").into();
|
||||
}
|
||||
|
||||
let sync_status = Self::get_sync_status();
|
||||
|
||||
if sync_status.is_none() {
|
||||
return t!("sync_status.node_down").into();
|
||||
}
|
||||
|
||||
let sync_status = match sync_status.unwrap() {
|
||||
SyncStatus::Initial => t!("sync_status.initial"),
|
||||
SyncStatus::NoSync => t!("sync_status.no_sync"),
|
||||
SyncStatus::AwaitingPeers(_) => t!("sync_status.awaiting_peers"),
|
||||
SyncStatus::HeaderSync {
|
||||
sync_head,
|
||||
highest_height,
|
||||
..
|
||||
} => {
|
||||
if highest_height == 0 {
|
||||
t!("sync_status.header_sync")
|
||||
} else {
|
||||
let percent = sync_head.height * 100 / highest_height;
|
||||
t!("sync_status.header_sync_percent", "percent" => percent)
|
||||
}
|
||||
}
|
||||
SyncStatus::TxHashsetPibd {
|
||||
aborted: _,
|
||||
errored: _,
|
||||
completed_leaves,
|
||||
leaves_required,
|
||||
completed_to_height: _,
|
||||
required_height: _,
|
||||
} => {
|
||||
if completed_leaves == 0 {
|
||||
t!("sync_status.tx_hashset_pibd")
|
||||
} else {
|
||||
let percent = completed_leaves * 100 / leaves_required;
|
||||
t!("sync_status.tx_hashset_pibd_percent", "percent" => percent)
|
||||
}
|
||||
}
|
||||
SyncStatus::TxHashsetDownload(stat) => {
|
||||
if stat.total_size > 0 {
|
||||
let percent = stat.downloaded_size * 100 / stat.total_size;
|
||||
t!("sync_status.tx_hashset_download_percent", "percent" => percent)
|
||||
} else {
|
||||
t!("sync_status.tx_hashset_download")
|
||||
}
|
||||
}
|
||||
SyncStatus::TxHashsetSetup {
|
||||
headers,
|
||||
headers_total,
|
||||
kernel_pos,
|
||||
kernel_pos_total,
|
||||
} => {
|
||||
if headers.is_some() && headers_total.is_some() {
|
||||
let h = headers.unwrap();
|
||||
let ht = headers_total.unwrap();
|
||||
let percent = h * 100 / ht;
|
||||
t!("sync_status.tx_hashset_setup_history", "percent" => percent)
|
||||
} else if kernel_pos.is_some() && kernel_pos_total.is_some() {
|
||||
let k = kernel_pos.unwrap();
|
||||
let kt = kernel_pos_total.unwrap();
|
||||
let percent = k * 100 / kt;
|
||||
t!("sync_status.tx_hashset_setup_position", "percent" => percent)
|
||||
} else {
|
||||
t!("sync_status.tx_hashset_setup")
|
||||
}
|
||||
}
|
||||
SyncStatus::TxHashsetRangeProofsValidation {
|
||||
rproofs,
|
||||
rproofs_total,
|
||||
} => {
|
||||
let r_percent = if rproofs_total > 0 {
|
||||
(rproofs * 100) / rproofs_total
|
||||
} else {
|
||||
0
|
||||
};
|
||||
t!("sync_status.tx_hashset_range_proofs_validation", "percent" => r_percent)
|
||||
}
|
||||
SyncStatus::TxHashsetKernelsValidation {
|
||||
kernels,
|
||||
kernels_total,
|
||||
} => {
|
||||
let k_percent = if kernels_total > 0 {
|
||||
(kernels * 100) / kernels_total
|
||||
} else {
|
||||
0
|
||||
};
|
||||
t!("sync_status.tx_hashset_kernels_validation", "percent" => k_percent)
|
||||
}
|
||||
SyncStatus::TxHashsetSave | SyncStatus::TxHashsetDone => {
|
||||
t!("sync_status.tx_hashset_save")
|
||||
}
|
||||
SyncStatus::BodySync {
|
||||
current_height,
|
||||
highest_height,
|
||||
} => {
|
||||
if highest_height == 0 {
|
||||
t!("sync_status.body_sync")
|
||||
} else {
|
||||
let percent = current_height * 100 / highest_height;
|
||||
t!("sync_status.body_sync_percent", "percent" => percent)
|
||||
}
|
||||
}
|
||||
SyncStatus::Shutdown => t!("sync_status.shutdown"),
|
||||
};
|
||||
sync_status.into()
|
||||
}
|
||||
}
|
||||
|
||||
/// Start the node [`Server`].
|
||||
fn start_node_server() -> Result<Server, Error> {
|
||||
// Setup server config.
|
||||
let mut config = NodeConfig::node_server_config();
|
||||
PeersConfig::load_to_server_config(&mut config);
|
||||
let mut server_config = config.server.clone();
|
||||
|
||||
// DNS seed setup.
|
||||
if NodeConfig::is_default_seeding_type() {
|
||||
server_config.p2p_config.seeding_type = Seeding::List;
|
||||
server_config.p2p_config.seeds = Some(PeerAddrs::default());
|
||||
let is_mainnet = server_config.chain_type == ChainTypes::Mainnet;
|
||||
let seed_list = if is_mainnet {
|
||||
Node::MAINNET_DNS_SEEDS
|
||||
} else {
|
||||
Node::TESTNET_DNS_SEEDS
|
||||
};
|
||||
let seed_port = if is_mainnet { 3414 } else { 13414 };
|
||||
for seed_addr in seed_list {
|
||||
let addr = format!("{}:{}", seed_addr, seed_port);
|
||||
if let Some(p) = PeersConfig::peer_to_addr(addr) {
|
||||
server_config
|
||||
.p2p_config
|
||||
.seeds
|
||||
.as_mut()
|
||||
.unwrap()
|
||||
.peers
|
||||
.push(p)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Fix to avoid too many opened files.
|
||||
server_config.p2p_config.peer_min_preferred_outbound_count =
|
||||
server_config.p2p_config.peer_max_outbound_count;
|
||||
|
||||
// Remove temporary file dir.
|
||||
{
|
||||
let mut tmp_dir = PathBuf::from(&server_config.db_root);
|
||||
tmp_dir = tmp_dir.parent().unwrap().to_path_buf();
|
||||
tmp_dir.push("tmp");
|
||||
if tmp_dir.exists() {
|
||||
match fs::remove_dir_all(tmp_dir) {
|
||||
Ok(_) => {}
|
||||
Err(_) => {}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Initialize our global chain_type, feature flags (NRD kernel support currently),
|
||||
// accept_fee_base, and future_time_limit.
|
||||
// These are read via global and not read from config beyond this point.
|
||||
if !global::GLOBAL_CHAIN_TYPE.is_init() {
|
||||
global::init_global_chain_type(config.server.chain_type);
|
||||
} else {
|
||||
global::set_global_chain_type(config.server.chain_type);
|
||||
global::set_local_chain_type(config.server.chain_type);
|
||||
}
|
||||
|
||||
if !global::GLOBAL_NRD_FEATURE_ENABLED.is_init() {
|
||||
match global::get_chain_type() {
|
||||
ChainTypes::Mainnet => {
|
||||
global::init_global_nrd_enabled(false);
|
||||
}
|
||||
_ => {
|
||||
global::init_global_nrd_enabled(true);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
match global::get_chain_type() {
|
||||
ChainTypes::Mainnet => {
|
||||
global::set_global_nrd_enabled(false);
|
||||
}
|
||||
_ => {
|
||||
global::set_global_nrd_enabled(true);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
let afb = config.server.pool_config.accept_fee_base;
|
||||
if !global::GLOBAL_ACCEPT_FEE_BASE.is_init() {
|
||||
global::init_global_accept_fee_base(afb);
|
||||
} else {
|
||||
global::set_global_accept_fee_base(afb);
|
||||
}
|
||||
|
||||
let future_time_limit = config.server.future_time_limit;
|
||||
if !global::GLOBAL_FUTURE_TIME_LIMIT.is_init() {
|
||||
global::init_global_future_time_limit(future_time_limit);
|
||||
} else {
|
||||
global::set_global_future_time_limit(future_time_limit);
|
||||
}
|
||||
|
||||
// Put flag to start stratum server if autorun is available.
|
||||
if NodeConfig::is_stratum_autorun_enabled() {
|
||||
NODE_STATE
|
||||
.start_stratum_needed
|
||||
.store(true, Ordering::Relaxed);
|
||||
}
|
||||
|
||||
// Reset an error.
|
||||
{
|
||||
let mut w_err = NODE_STATE.error.write();
|
||||
*w_err = None;
|
||||
}
|
||||
|
||||
// Start integrated node server.
|
||||
let api_chan: &'static mut (oneshot::Sender<()>, oneshot::Receiver<()>) =
|
||||
Box::leak(Box::new(oneshot::channel::<()>()));
|
||||
let server_result = Server::new(server_config, None, None, api_chan);
|
||||
server_result
|
||||
}
|
||||
|
||||
/// Start stratum mining server on a separate thread.
|
||||
pub fn start_stratum_mining_server(server: &Server, config: StratumServerConfig) {
|
||||
let proof_size = global::proofsize();
|
||||
let sync_state = server.sync_state.clone();
|
||||
|
||||
let mut stratum_server = StratumServer::new(
|
||||
config,
|
||||
server.chain.clone(),
|
||||
server.tx_pool.clone(),
|
||||
NODE_STATE.stratum_stats.clone(),
|
||||
);
|
||||
let stop_state = NODE_STATE.stratum_stop_state.clone();
|
||||
stop_state.reset();
|
||||
let server_state = stop_state.clone();
|
||||
thread::spawn(move || {
|
||||
stratum_server.run_loop(proof_size, sync_state, stop_state);
|
||||
server_state.reset();
|
||||
// Reset stratum stats.
|
||||
{
|
||||
let mut w_stratum_stats = NODE_STATE.stratum_stats.write();
|
||||
*w_stratum_stats = StratumStats::default();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
#[allow(dead_code)]
|
||||
#[cfg(target_os = "android")]
|
||||
#[allow(non_snake_case)]
|
||||
#[unsafe(no_mangle)]
|
||||
/// Get sync status text for Android notification from [`NODE_STATE`] in Java string format.
|
||||
pub extern "C" fn Java_mw_gri_android_BackgroundService_getSyncStatusText(
|
||||
_env: jni::JNIEnv,
|
||||
_class: jni::objects::JObject,
|
||||
_activity: jni::objects::JObject,
|
||||
) -> jni::sys::jstring {
|
||||
let status_text = Node::get_sync_status_text();
|
||||
let j_text = _env.new_string(status_text);
|
||||
return j_text.unwrap().into_raw();
|
||||
}
|
||||
|
||||
#[allow(dead_code)]
|
||||
#[cfg(target_os = "android")]
|
||||
#[allow(non_snake_case)]
|
||||
#[unsafe(no_mangle)]
|
||||
/// Get sync title for Android notification in Java string format.
|
||||
pub extern "C" fn Java_mw_gri_android_BackgroundService_getSyncTitle(
|
||||
_env: jni::JNIEnv,
|
||||
_class: jni::objects::JObject,
|
||||
_activity: jni::objects::JObject,
|
||||
) -> jni::sys::jstring {
|
||||
let j_text = _env.new_string(t!("network.node"));
|
||||
return j_text.unwrap().into_raw();
|
||||
}
|
||||
|
||||
#[allow(dead_code)]
|
||||
#[cfg(target_os = "android")]
|
||||
#[allow(non_snake_case)]
|
||||
#[unsafe(no_mangle)]
|
||||
/// Get start text for Android notification in Java string format.
|
||||
pub extern "C" fn Java_mw_gri_android_BackgroundService_getStartText(
|
||||
_env: jni::JNIEnv,
|
||||
_class: jni::objects::JObject,
|
||||
_activity: jni::objects::JObject,
|
||||
) -> jni::sys::jstring {
|
||||
let j_text = _env.new_string(t!("network_settings.enable"));
|
||||
return j_text.unwrap().into_raw();
|
||||
}
|
||||
|
||||
#[allow(dead_code)]
|
||||
#[cfg(target_os = "android")]
|
||||
#[allow(non_snake_case)]
|
||||
#[unsafe(no_mangle)]
|
||||
/// Get stop text for Android notification in Java string format.
|
||||
pub extern "C" fn Java_mw_gri_android_BackgroundService_getStopText(
|
||||
_env: jni::JNIEnv,
|
||||
_class: jni::objects::JObject,
|
||||
_activity: jni::objects::JObject,
|
||||
) -> jni::sys::jstring {
|
||||
let j_text = _env.new_string(t!("network_settings.disable"));
|
||||
return j_text.unwrap().into_raw();
|
||||
}
|
||||
|
||||
#[allow(dead_code)]
|
||||
#[cfg(target_os = "android")]
|
||||
#[allow(non_snake_case)]
|
||||
#[unsafe(no_mangle)]
|
||||
/// Get exit text for Android notification in Java string format.
|
||||
pub extern "C" fn Java_mw_gri_android_BackgroundService_getExitText(
|
||||
_env: jni::JNIEnv,
|
||||
_class: jni::objects::JObject,
|
||||
_activity: jni::objects::JObject,
|
||||
) -> jni::sys::jstring {
|
||||
let j_text = _env.new_string(t!("modal_exit.exit"));
|
||||
return j_text.unwrap().into_raw();
|
||||
}
|
||||
|
||||
#[allow(dead_code)]
|
||||
#[cfg(target_os = "android")]
|
||||
#[allow(non_snake_case)]
|
||||
#[unsafe(no_mangle)]
|
||||
/// Check if node launch is possible.
|
||||
pub extern "C" fn Java_mw_gri_android_BackgroundService_canStartNode(
|
||||
_env: jni::JNIEnv,
|
||||
_class: jni::objects::JObject,
|
||||
_activity: jni::objects::JObject,
|
||||
) -> jni::sys::jboolean {
|
||||
let loading = Node::is_stopping() || Node::is_restarting() || Node::is_starting();
|
||||
return (!loading && !Node::is_running()) as jni::sys::jboolean;
|
||||
}
|
||||
|
||||
#[allow(dead_code)]
|
||||
#[cfg(target_os = "android")]
|
||||
#[allow(non_snake_case)]
|
||||
#[unsafe(no_mangle)]
|
||||
/// Check if node stop is possible.
|
||||
pub extern "C" fn Java_mw_gri_android_BackgroundService_canStopNode(
|
||||
_env: jni::JNIEnv,
|
||||
_class: jni::objects::JObject,
|
||||
_activity: jni::objects::JObject,
|
||||
) -> jni::sys::jboolean {
|
||||
let loading = Node::is_stopping() || Node::is_restarting() || Node::is_starting();
|
||||
return (!loading && Node::is_running()) as jni::sys::jboolean;
|
||||
}
|
||||
|
||||
#[allow(dead_code)]
|
||||
#[cfg(target_os = "android")]
|
||||
#[allow(non_snake_case)]
|
||||
#[unsafe(no_mangle)]
|
||||
/// Start node from Android Java code.
|
||||
pub extern "C" fn Java_mw_gri_android_NotificationActionsReceiver_startNode(
|
||||
_env: jni::JNIEnv,
|
||||
_class: jni::objects::JObject,
|
||||
_activity: jni::objects::JObject,
|
||||
) {
|
||||
Node::start();
|
||||
}
|
||||
|
||||
#[allow(dead_code)]
|
||||
#[cfg(target_os = "android")]
|
||||
#[allow(non_snake_case)]
|
||||
#[unsafe(no_mangle)]
|
||||
/// Stop node from Android Java code.
|
||||
pub extern "C" fn Java_mw_gri_android_NotificationActionsReceiver_stopNode(
|
||||
_env: jni::JNIEnv,
|
||||
_class: jni::objects::JObject,
|
||||
_activity: jni::objects::JObject,
|
||||
) {
|
||||
Node::stop(false);
|
||||
}
|
||||
|
||||
#[allow(dead_code)]
|
||||
#[cfg(target_os = "android")]
|
||||
#[allow(non_snake_case)]
|
||||
#[unsafe(no_mangle)]
|
||||
/// Stop node from Android Java code.
|
||||
pub extern "C" fn Java_mw_gri_android_NotificationActionsReceiver_stopNodeToExit(
|
||||
_env: jni::JNIEnv,
|
||||
_class: jni::objects::JObject,
|
||||
_activity: jni::objects::JObject,
|
||||
) {
|
||||
if Node::is_running() {
|
||||
Node::stop(true);
|
||||
} else {
|
||||
NODE_STATE.exit_after_stop.store(true, Ordering::Relaxed);
|
||||
}
|
||||
}
|
||||
|
||||
#[allow(dead_code)]
|
||||
#[cfg(target_os = "android")]
|
||||
#[allow(non_snake_case)]
|
||||
#[unsafe(no_mangle)]
|
||||
/// Check if app exit is needed after node stop to finish Android app at background.
|
||||
pub extern "C" fn Java_mw_gri_android_BackgroundService_exitAppAfterNodeStop(
|
||||
_env: jni::JNIEnv,
|
||||
_class: jni::objects::JObject,
|
||||
_activity: jni::objects::JObject,
|
||||
) -> jni::sys::jboolean {
|
||||
let exit_needed = !Node::is_running() && NODE_STATE.exit_after_stop.load(Ordering::Relaxed);
|
||||
return exit_needed as jni::sys::jboolean;
|
||||
}
|
||||
|
||||
#[allow(dead_code)]
|
||||
#[cfg(target_os = "android")]
|
||||
#[allow(non_snake_case)]
|
||||
#[unsafe(no_mangle)]
|
||||
/// Handle unexpected application termination on Android (removal from recent apps).
|
||||
pub extern "C" fn Java_mw_gri_android_MainActivity_onTermination(
|
||||
_env: jni::JNIEnv,
|
||||
_class: jni::objects::JObject,
|
||||
_activity: jni::objects::JObject,
|
||||
) {
|
||||
Node::stop(false);
|
||||
}
|
||||
+1000
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,28 @@
|
||||
// Copyright 2024 The Grim Developers
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
/// Integrated node error type.
|
||||
#[derive(Clone)]
|
||||
pub enum NodeError {
|
||||
/// Storage issue.
|
||||
Storage,
|
||||
/// P2P server issue.
|
||||
P2P,
|
||||
/// API server issue.
|
||||
API,
|
||||
/// Configuration issue.
|
||||
Configuration,
|
||||
/// Unknown error.
|
||||
Unknown,
|
||||
}
|
||||
Reference in New Issue
Block a user