• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

tari-project / tari / 16123384529

07 Jul 2025 05:11PM UTC coverage: 64.327% (-7.6%) from 71.89%
16123384529

push

github

web-flow
chore: new release v4.9.0-pre.0 (#7289)

Description
---
new release esmeralda

77151 of 119935 relevant lines covered (64.33%)

227108.34 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/base_layer/core/src/base_node/service/initializer.rs
1
// Copyright 2019. The Tari Project
2
//
3
// Redistribution and use in source and binary forms, with or without modification, are permitted provided that the
4
// following conditions are met:
5
//
6
// 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following
7
// disclaimer.
8
//
9
// 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the
10
// following disclaimer in the documentation and/or other materials provided with the distribution.
11
//
12
// 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote
13
// products derived from this software without specific prior written permission.
14
//
15
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
16
// INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
17
// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
18
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
19
// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
20
// WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
21
// USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
22

23
use std::{convert::TryFrom, sync::Arc, time::Duration};
24

25
use futures::{future, Stream, StreamExt};
26
use log::*;
27
use tari_comms::connectivity::ConnectivityRequester;
28
use tari_comms_dht::Dht;
29
use tari_p2p::{
30
    comms_connector::{PeerMessage, SubscriptionFactory},
31
    domain_message::DomainMessage,
32
    services::utils::map_decode,
33
    tari_message::TariMessageType,
34
};
35
use tari_service_framework::{
36
    async_trait,
37
    reply_channel,
38
    ServiceInitializationError,
39
    ServiceInitializer,
40
    ServiceInitializerContext,
41
};
42
use thiserror::Error;
43
use tokio::sync::{broadcast, mpsc};
44

45
use crate::{
46
    base_node::{
47
        comms_interface::{InboundNodeCommsHandlers, LocalNodeCommsInterface, OutboundNodeCommsInterface},
48
        service::service::{BaseNodeService, BaseNodeStreams},
49
        BaseNodeStateMachineConfig,
50
        StateMachineHandle,
51
    },
52
    blocks::NewBlock,
53
    chain_storage::{async_db::AsyncBlockchainDb, BlockchainBackend},
54
    consensus::ConsensusManager,
55
    mempool::Mempool,
56
    proof_of_work::randomx_factory::RandomXFactory,
57
    proto as shared_protos,
58
    proto::base_node as proto,
59
};
60

61
const LOG_TARGET: &str = "c::bn::service::initializer";
62
const SUBSCRIPTION_LABEL: &str = "Base Node";
63

64
/// Initializer for the Base Node service handle and service future.
65
pub struct BaseNodeServiceInitializer<T> {
66
    inbound_message_subscription_factory: Arc<SubscriptionFactory>,
67
    blockchain_db: AsyncBlockchainDb<T>,
68
    mempool: Mempool,
69
    consensus_manager: ConsensusManager,
70
    service_request_timeout: Duration,
71
    randomx_factory: RandomXFactory,
72
    base_node_config: BaseNodeStateMachineConfig,
73
}
74

75
impl<T> BaseNodeServiceInitializer<T>
76
where T: BlockchainBackend
77
{
78
    /// Create a new BaseNodeServiceInitializer from the inbound message subscriber.
79
    pub fn new(
×
80
        inbound_message_subscription_factory: Arc<SubscriptionFactory>,
×
81
        blockchain_db: AsyncBlockchainDb<T>,
×
82
        mempool: Mempool,
×
83
        consensus_manager: ConsensusManager,
×
84
        service_request_timeout: Duration,
×
85
        randomx_factory: RandomXFactory,
×
86
        base_node_config: BaseNodeStateMachineConfig,
×
87
    ) -> Self {
×
88
        Self {
×
89
            inbound_message_subscription_factory,
×
90
            blockchain_db,
×
91
            mempool,
×
92
            consensus_manager,
×
93
            service_request_timeout,
×
94
            randomx_factory,
×
95
            base_node_config,
×
96
        }
×
97
    }
×
98

99
    /// Get a stream for inbound Base Node request messages
100
    fn inbound_request_stream(
×
101
        &self,
×
102
    ) -> impl Stream<Item = DomainMessage<Result<proto::BaseNodeServiceRequest, prost::DecodeError>>> {
×
103
        self.inbound_message_subscription_factory
×
104
            .get_subscription(TariMessageType::BaseNodeRequest, SUBSCRIPTION_LABEL)
×
105
            .map(map_decode::<proto::BaseNodeServiceRequest>)
×
106
    }
×
107

108
    /// Get a stream for inbound Base Node response messages
109
    fn inbound_response_stream(
×
110
        &self,
×
111
    ) -> impl Stream<Item = DomainMessage<Result<proto::BaseNodeServiceResponse, prost::DecodeError>>> {
×
112
        self.inbound_message_subscription_factory
×
113
            .get_subscription(TariMessageType::BaseNodeResponse, SUBSCRIPTION_LABEL)
×
114
            .map(map_decode::<proto::BaseNodeServiceResponse>)
×
115
    }
×
116

117
    /// Create a stream of 'New Block` messages
118
    fn inbound_block_stream(&self) -> impl Stream<Item = DomainMessage<Result<NewBlock, ExtractBlockError>>> {
×
119
        self.inbound_message_subscription_factory
×
120
            .get_subscription(TariMessageType::NewBlock, SUBSCRIPTION_LABEL)
×
121
            .map(extract_block)
×
122
    }
×
123
}
124

125
#[derive(Error, Debug)]
126
pub enum ExtractBlockError {
127
    #[error("Could not decode inbound block message. {0}")]
128
    DecodeError(#[from] prost::DecodeError),
129
    #[error("Inbound block message was ill-formed. {0}")]
130
    MalformedMessage(String),
131
}
132

133
fn extract_block(msg: Arc<PeerMessage>) -> DomainMessage<Result<NewBlock, ExtractBlockError>> {
×
134
    let new_block = match msg.decode_message::<shared_protos::core::NewBlock>() {
×
135
        Ok(block) => block,
×
136
        Err(e) => {
×
137
            return DomainMessage {
×
138
                source_peer: msg.source_peer.clone(),
×
139
                dht_header: msg.dht_header.clone(),
×
140
                authenticated_origin: msg.authenticated_origin.clone(),
×
141
                inner: Err(e.into()),
×
142
            }
×
143
        },
144
    };
145
    let block = NewBlock::try_from(new_block).map_err(ExtractBlockError::MalformedMessage);
×
146
    DomainMessage {
×
147
        source_peer: msg.source_peer.clone(),
×
148
        dht_header: msg.dht_header.clone(),
×
149
        authenticated_origin: msg.authenticated_origin.clone(),
×
150
        inner: block,
×
151
    }
×
152
}
×
153

154
#[async_trait]
155
impl<T> ServiceInitializer for BaseNodeServiceInitializer<T>
156
where T: BlockchainBackend + 'static
157
{
158
    async fn initialize(&mut self, context: ServiceInitializerContext) -> Result<(), ServiceInitializationError> {
×
159
        trace!(target: LOG_TARGET, "Initializing Base Node Service");
×
160
        // Create streams for receiving Base Node requests and response messages from comms
161
        let inbound_request_stream = self.inbound_request_stream();
×
162
        let inbound_response_stream = self.inbound_response_stream();
×
163
        let inbound_block_stream = self.inbound_block_stream();
×
164
        // Connect InboundNodeCommsInterface and OutboundNodeCommsInterface to BaseNodeService
×
165
        let (outbound_request_sender_service, outbound_request_stream) = reply_channel::unbounded();
×
166
        let (outbound_block_sender_service, outbound_block_stream) = mpsc::unbounded_channel();
×
167
        let (local_request_sender_service, local_request_stream) = reply_channel::unbounded();
×
168
        let (local_block_sender_service, local_block_stream) = reply_channel::unbounded();
×
169
        let outbound_nci =
×
170
            OutboundNodeCommsInterface::new(outbound_request_sender_service, outbound_block_sender_service);
×
171
        let (block_event_sender, _) = broadcast::channel(50);
×
172
        let local_nci = LocalNodeCommsInterface::new(
×
173
            local_request_sender_service,
×
174
            local_block_sender_service,
×
175
            block_event_sender.clone(),
×
176
        );
×
177

×
178
        // Register handle to OutboundNodeCommsInterface before waiting for handles to be ready
×
179
        context.register_handle(outbound_nci.clone());
×
180
        context.register_handle(local_nci);
×
181

×
182
        let service_request_timeout = self.service_request_timeout;
×
183
        let blockchain_db = self.blockchain_db.clone();
×
184
        let mempool = self.mempool.clone();
×
185
        let consensus_manager = self.consensus_manager.clone();
×
186
        let randomx_factory = self.randomx_factory.clone();
×
187
        let config = self.base_node_config.clone();
×
188

×
189
        context.spawn_when_ready(move |handles| async move {
×
190
            let dht = handles.expect_handle::<Dht>();
×
191
            let connectivity = handles.expect_handle::<ConnectivityRequester>();
×
192
            let outbound_message_service = dht.outbound_requester();
×
193

×
194
            let state_machine = handles.expect_handle::<StateMachineHandle>();
×
195

×
196
            let inbound_nch = InboundNodeCommsHandlers::new(
×
197
                block_event_sender,
×
198
                blockchain_db,
×
199
                mempool,
×
200
                consensus_manager,
×
201
                outbound_nci.clone(),
×
202
                connectivity.clone(),
×
203
                randomx_factory,
×
204
            );
×
205

×
206
            let streams = BaseNodeStreams {
×
207
                outbound_request_stream,
×
208
                outbound_block_stream,
×
209
                inbound_request_stream,
×
210
                inbound_response_stream,
×
211
                inbound_block_stream,
×
212
                local_request_stream,
×
213
                local_block_stream,
×
214
            };
×
215
            let service = BaseNodeService::new(
×
216
                outbound_message_service,
×
217
                inbound_nch,
×
218
                service_request_timeout,
×
219
                state_machine,
×
220
                connectivity,
×
221
                config,
×
222
            )
×
223
            .start(streams);
×
224
            futures::pin_mut!(service);
×
225
            future::select(service, handles.get_shutdown_signal()).await;
×
226
            info!(target: LOG_TARGET, "Base Node Service shutdown");
×
227
        });
×
228

×
229
        debug!(target: LOG_TARGET, "Base Node Service initialized");
×
230
        Ok(())
×
231
    }
×
232
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc