Skip to main content

snarkos_node/
traits.rs

1// Copyright (c) 2019-2026 Provable Inc.
2// This file is part of the snarkOS library.
3
4// Licensed under the Apache License, Version 2.0 (the "License");
5// you may not use this file except in compliance with the License.
6// You may obtain a copy of the License at:
7
8// http://www.apache.org/licenses/LICENSE-2.0
9
10// Unless required by applicable law or agreed to in writing, software
11// distributed under the License is distributed on an "AS IS" BASIS,
12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13// See the License for the specific language governing permissions and
14// limitations under the License.
15
16use snarkos_node_network::{NodeType, PeerPoolHandling};
17use snarkos_node_router::Routing;
18use snarkos_utilities::SignalHandler;
19use snarkvm::prelude::{Address, Network, PrivateKey, ViewKey};
20
21use std::time::Duration;
22use tokio::time::sleep;
23
24#[async_trait]
25pub trait NodeInterface<N: Network>: Routing<N> {
26    /// Returns the node type.
27    fn node_type(&self) -> NodeType {
28        self.router().node_type()
29    }
30
31    /// Returns the account private key of the node.
32    fn private_key(&self) -> &PrivateKey<N> {
33        self.router().private_key()
34    }
35
36    /// Returns the account view key of the node.
37    fn view_key(&self) -> &ViewKey<N> {
38        self.router().view_key()
39    }
40
41    /// Returns the account address of the node.
42    fn address(&self) -> Address<N> {
43        self.router().address()
44    }
45
46    /// Returns `true` if the node is in development mode.
47    fn is_dev(&self) -> bool {
48        self.router().is_dev()
49    }
50
51    /// Blocks until a shutdown signal was received or manual shutdown was triggered.
52    async fn wait_for_signals(&self, handler: &SignalHandler) {
53        handler.wait_for_signals().await;
54
55        // If the node is already initialized, then shut it down.
56        self.shut_down().await;
57
58        // Allow a bit of time for the tasks to wind down.
59        sleep(Duration::from_secs(1)).await;
60
61        // Check if there are any stragglers left.
62        if let Some(handle) = &handler.handle {
63            let live_tasks = handle.metrics().num_alive_tasks();
64            if live_tasks != 0 {
65                error!("There are still {live_tasks} live tasks");
66            }
67        }
68    }
69
70    /// Shuts down the node.
71    async fn shut_down(&self);
72}