cuprated/rpc/request/
blockchain_manager.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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
//! Functions for [`BlockchainManagerRequest`] & [`BlockchainManagerResponse`].

use anyhow::Error;
use monero_serai::block::Block;
use tower::{Service, ServiceExt};

use cuprate_helper::cast::{u64_to_usize, usize_to_u64};
use cuprate_p2p_core::{types::ConnectionId, NetworkZone};
use cuprate_pruning::PruningSeed;
use cuprate_rpc_types::misc::Span;
use cuprate_types::{AddAuxPow, AuxPow, HardFork};

use crate::rpc::{
    constants::FIELD_NOT_SUPPORTED,
    handler::{BlockchainManagerHandle, BlockchainManagerRequest, BlockchainManagerResponse},
};

/// [`BlockchainManagerRequest::PopBlocks`]
pub(crate) async fn pop_blocks(
    blockchain_manager: &mut BlockchainManagerHandle,
    amount: u64,
) -> Result<u64, Error> {
    let BlockchainManagerResponse::PopBlocks { new_height } = blockchain_manager
        .ready()
        .await?
        .call(BlockchainManagerRequest::PopBlocks {
            amount: u64_to_usize(amount),
        })
        .await?
    else {
        unreachable!();
    };

    Ok(usize_to_u64(new_height))
}

/// [`BlockchainManagerRequest::Prune`]
pub(crate) async fn prune(
    blockchain_manager: &mut BlockchainManagerHandle,
) -> Result<PruningSeed, Error> {
    let BlockchainManagerResponse::Prune(seed) = blockchain_manager
        .ready()
        .await?
        .call(BlockchainManagerRequest::Prune)
        .await?
    else {
        unreachable!();
    };

    Ok(seed)
}

/// [`BlockchainManagerRequest::Pruned`]
pub(crate) async fn pruned(
    blockchain_manager: &mut BlockchainManagerHandle,
) -> Result<bool, Error> {
    let BlockchainManagerResponse::Pruned(pruned) = blockchain_manager
        .ready()
        .await?
        .call(BlockchainManagerRequest::Pruned)
        .await?
    else {
        unreachable!();
    };

    Ok(pruned)
}

/// [`BlockchainManagerRequest::RelayBlock`]
pub(crate) async fn relay_block(
    blockchain_manager: &mut BlockchainManagerHandle,
    block: Block,
) -> Result<(), Error> {
    let BlockchainManagerResponse::Ok = blockchain_manager
        .ready()
        .await?
        .call(BlockchainManagerRequest::RelayBlock(block))
        .await?
    else {
        unreachable!();
    };

    Ok(())
}

/// [`BlockchainManagerRequest::Syncing`]
pub(crate) async fn syncing(
    blockchain_manager: &mut BlockchainManagerHandle,
) -> Result<bool, Error> {
    let BlockchainManagerResponse::Syncing(syncing) = blockchain_manager
        .ready()
        .await?
        .call(BlockchainManagerRequest::Syncing)
        .await?
    else {
        unreachable!();
    };

    Ok(syncing)
}

/// [`BlockchainManagerRequest::Synced`]
pub(crate) async fn synced(
    blockchain_manager: &mut BlockchainManagerHandle,
) -> Result<bool, Error> {
    let BlockchainManagerResponse::Synced(syncing) = blockchain_manager
        .ready()
        .await?
        .call(BlockchainManagerRequest::Synced)
        .await?
    else {
        unreachable!();
    };

    Ok(syncing)
}

/// [`BlockchainManagerRequest::Target`]
pub(crate) async fn target(
    blockchain_manager: &mut BlockchainManagerHandle,
) -> Result<std::time::Duration, Error> {
    let BlockchainManagerResponse::Target(target) = blockchain_manager
        .ready()
        .await?
        .call(BlockchainManagerRequest::Target)
        .await?
    else {
        unreachable!();
    };

    Ok(target)
}

/// [`BlockchainManagerRequest::TargetHeight`]
pub(crate) async fn target_height(
    blockchain_manager: &mut BlockchainManagerHandle,
) -> Result<u64, Error> {
    let BlockchainManagerResponse::TargetHeight { height } = blockchain_manager
        .ready()
        .await?
        .call(BlockchainManagerRequest::TargetHeight)
        .await?
    else {
        unreachable!();
    };

    Ok(usize_to_u64(height))
}

/// [`BlockchainManagerRequest::GenerateBlocks`]
pub(crate) async fn generate_blocks(
    blockchain_manager: &mut BlockchainManagerHandle,
    amount_of_blocks: u64,
    prev_block: [u8; 32],
    starting_nonce: u32,
    wallet_address: String,
) -> Result<(Vec<[u8; 32]>, u64), Error> {
    let BlockchainManagerResponse::GenerateBlocks { blocks, height } = blockchain_manager
        .ready()
        .await?
        .call(BlockchainManagerRequest::GenerateBlocks {
            amount_of_blocks,
            prev_block,
            starting_nonce,
            wallet_address,
        })
        .await?
    else {
        unreachable!();
    };

    Ok((blocks, usize_to_u64(height)))
}

// [`BlockchainManagerRequest::Spans`]
pub(crate) async fn spans<Z: NetworkZone>(
    blockchain_manager: &mut BlockchainManagerHandle,
) -> Result<Vec<Span>, Error> {
    // let BlockchainManagerResponse::Spans(vec) = blockchain_manager
    //     .ready()
    //     .await?
    //     .call(BlockchainManagerRequest::Spans)
    //     .await?
    // else {
    //     unreachable!();
    // };

    let vec: Vec<cuprate_p2p_core::types::Span<Z::Addr>> = todo!();

    // FIXME: impl this map somewhere instead of inline.
    let vec = vec
        .into_iter()
        .map(|span| Span {
            connection_id: String::from(ConnectionId::DEFAULT_STR),
            nblocks: span.nblocks,
            rate: span.rate,
            remote_address: span.remote_address.to_string(),
            size: span.size,
            speed: span.speed,
            start_block_height: span.start_block_height,
        })
        .collect();

    Ok(vec)
}

/// [`BlockchainManagerRequest::NextNeededPruningSeed`]
pub(crate) async fn next_needed_pruning_seed(
    blockchain_manager: &mut BlockchainManagerHandle,
) -> Result<PruningSeed, Error> {
    let BlockchainManagerResponse::NextNeededPruningSeed(seed) = blockchain_manager
        .ready()
        .await?
        .call(BlockchainManagerRequest::NextNeededPruningSeed)
        .await?
    else {
        unreachable!();
    };

    Ok(seed)
}