-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathservice.rs
123 lines (112 loc) · 3.73 KB
/
service.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
// Copyright 2018-2019 Kodebox, Inc.
// This file is part of CodeChain.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
use crate::client::{Client, ClientConfig};
use crate::error::Error;
use crate::miner::Miner;
use crate::{genesis::Genesis, ConsensusEngine};
use cio::{IoContext, IoHandler, IoHandlerResult, IoService};
use coordinator::Coordinator;
use ctimer::TimerApi;
use ctypes::{BlockHash, BlockId};
use kvdb::KeyValueDB;
use primitives::Bytes;
use std::sync::Arc;
/// Client service setup.
pub struct ClientService {
_io_service: IoService<ClientIoMessage>,
client: Arc<Client>,
}
impl ClientService {
pub fn start(
config: &ClientConfig,
engine: Arc<dyn ConsensusEngine>,
genesis: &Genesis,
db: Arc<dyn KeyValueDB>,
miner: Arc<Miner>,
coordinator: Arc<Coordinator>,
reseal_timer: TimerApi,
) -> Result<ClientService, Error> {
let io_service = IoService::<ClientIoMessage>::start("Client")?;
let client = Client::try_new(
config,
Arc::clone(&engine),
&genesis,
db,
miner,
coordinator,
io_service.channel(),
reseal_timer,
)?;
let client_io = Arc::new(ClientIoHandler {
client: client.clone(),
});
io_service.register_handler(client_io)?;
engine.register_client(Arc::downgrade(&client) as _);
Ok(ClientService {
_io_service: io_service,
client,
})
}
pub fn client(&self) -> Arc<Client> {
Arc::clone(&self.client)
}
}
/// Message type for external and internal events
#[derive(Clone, PartialEq, Eq, Debug)]
pub enum ClientIoMessage {
/// A block is ready
BlockVerified,
/// A header is ready
HeaderVerified,
/// New transaction RLPs are ready to be imported
NewTransactions(Vec<Bytes>),
/// Block generation is required
NewBlockRequired {
parent_block: BlockId,
allow_empty_block: bool,
},
/// Update the best block by the given hash
UpdateBestAsCommitted(BlockHash),
}
/// IO interface for the Client handler
struct ClientIoHandler {
client: Arc<Client>,
}
impl IoHandler<ClientIoMessage> for ClientIoHandler {
fn message(&self, _io: &IoContext<ClientIoMessage>, net_message: ClientIoMessage) -> IoHandlerResult<()> {
match net_message {
ClientIoMessage::BlockVerified => {
self.client.import_verified_blocks();
}
ClientIoMessage::HeaderVerified => {
self.client.import_verified_headers();
}
ClientIoMessage::NewTransactions(transactions) => {
self.client.import_queued_transactions(&transactions);
}
ClientIoMessage::NewBlockRequired {
parent_block,
allow_empty_block,
} => {
self.client.update_sealing(parent_block, allow_empty_block);
}
ClientIoMessage::UpdateBestAsCommitted(block_hash) => {
self.client.update_best_as_committed(block_hash);
}
}
Ok(())
}
}