drand_core 0.0.19

A drand client library.
Documentation
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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
use std::{str::FromStr, sync::Mutex};
use thiserror::Error;
#[cfg(feature = "time")]
use time::{OffsetDateTime, format_description::well_known::Rfc3339};
use url::Url;

#[cfg(feature = "time")]
use crate::beacon::RandomnessBeaconTime;
use crate::{
    DrandError, Result,
    beacon::{ApiBeacon, BeaconError, RandomnessBeacon},
    chain::{ChainInfo, ChainOptions},
};

#[derive(Error, Debug)]
pub enum HttpClientError {
    #[error("Chain info is invalid")]
    InvalidChainInfo,
    #[error("Failed to retrieve chain info {message}")]
    FailedToRetrieveChainInfo { message: String },
    #[error("{e}. You might need to add \"https://\" to the provided URL.")]
    NoProtocol { e: url::ParseError },
    #[error(transparent)]
    ParseURL(#[from] url::ParseError),
    #[error(transparent)]
    RequestFailed(#[from] Box<ureq::Error>),
}

/// HTTP Client for drand
/// Queries a specified HTTP endpoint given by `chain`, with specific `options`
/// By default, the client verifies answers, and caches retrieved chain informations
pub struct HttpClient {
    base_url: url::Url,
    options: ChainOptions,
    cached_chain_info: Mutex<Option<ChainInfo>>,
    http_client: ureq::Agent,
}

impl HttpClient {
    pub fn new(base_url: &str, options: Option<ChainOptions>) -> Result<Self> {
        // The most common error is when user forget to add protocol in front of the provided URL string.
        // The error provided by url::Url is rather obscure when that happens.
        let mut url = Url::parse(base_url).map_err(|e| {
            if e == url::ParseError::RelativeUrlWithoutBase {
                Box::new(HttpClientError::NoProtocol { e })
            } else {
                Box::new(HttpClientError::ParseURL(e))
            }
        })?;
        // Ensure base URL ends with a trailing slash.
        // Given it's the base for API calls, it allows for easier joins in other methods.
        if !url.path().ends_with('/') {
            url.set_path(&format!("{}/", url.path()));
        }
        Ok(Self {
            base_url: url,
            options: options.unwrap_or_default(),
            cached_chain_info: Mutex::new(None),
            http_client: ureq::Agent::config_builder()
                .tls_config(
                    ureq::tls::TlsConfig::builder()
                        .root_certs(ureq::tls::RootCerts::PlatformVerifier)
                        .build(),
                )
                .build()
                .new_agent(),
        })
    }

    fn chain_info_no_cache(&self) -> Result<ChainInfo> {
        let mut response = self
            .http_client
            .get(
                self.base_url
                    .join("info")
                    .map_err(|e| -> DrandError { Box::new(HttpClientError::ParseURL(e)).into() })?
                    .as_str(),
            )
            .config()
            .http_status_as_error(false)
            .build()
            .call()
            .map_err(|e| -> DrandError {
                Box::new(HttpClientError::RequestFailed(e.into())).into()
            })?;
        let info = if response.status().as_u16() < 400 {
            response
                .body_mut()
                .read_json::<ChainInfo>()
                .map_err(|_| Box::new(BeaconError::Parsing))?
        } else {
            return Err(Box::new(HttpClientError::FailedToRetrieveChainInfo {
                message: response.body_mut().read_to_string().unwrap_or_default(),
            })
            .into());
        };
        match self.options().verify(&info) {
            true => Ok(info),
            false => Err(Box::new(HttpClientError::InvalidChainInfo).into()),
        }
    }

    fn beacon_url(&self, round: String) -> Result<Url> {
        let mut url = self
            .base_url
            .join(&format!("public/{round}"))
            .map_err(|e| -> DrandError { Box::new(HttpClientError::ParseURL(e)).into() })?;
        if !self.options().is_cache() {
            url.query_pairs_mut()
                .append_key_only(format!("{}", rand::random::<u64>()).as_str());
        }
        Ok(url)
    }

    fn verify_beacon(&self, beacon: RandomnessBeacon, round: String) -> Result<RandomnessBeacon> {
        if !self.options().is_beacon_verification() {
            return Ok(beacon);
        }

        if !beacon.verify(self.chain_info()?)? {
            return Err(Box::new(BeaconError::Validation).into());
        }

        if round == "latest" {
            return Ok(beacon);
        }
        let round: u64 = round
            .parse()
            .map_err(|_| -> DrandError { Box::new(BeaconError::Parsing).into() })?;
        if beacon.round() != round {
            return Err(Box::new(BeaconError::RoundMismatch).into());
        }
        Ok(beacon)
    }

    fn get_with_string(&self, round: String) -> Result<RandomnessBeacon> {
        let beacon = self
            .http_client
            .get(self.beacon_url(round.clone())?.as_str())
            .call()
            .map_err(|e| -> DrandError {
                match e {
                    ureq::Error::StatusCode(404) => Box::new(BeaconError::NotFound).into(),
                    _ => Box::new(HttpClientError::RequestFailed(e.into())).into(),
                }
            })?
            .body_mut()
            .read_json::<ApiBeacon>()
            .map_err(|_| -> DrandError { Box::new(BeaconError::Parsing).into() })?;

        let info = self.chain_info()?;
        let unix_time = info.genesis_time() + beacon.round() * info.period();
        let beacon = RandomnessBeacon::new(beacon, unix_time);

        self.verify_beacon(beacon, round)
    }

    pub fn base_url(&self) -> String {
        self.base_url.to_string()
    }

    pub fn options(&self) -> ChainOptions {
        self.options.clone()
    }

    pub fn chain_info(&self) -> Result<ChainInfo> {
        if self.options().is_cache() {
            let cached = self.cached_chain_info.lock().unwrap().to_owned();
            match cached {
                Some(info) => Ok(info),
                None => {
                    let info = self.chain_info_no_cache()?;
                    *self.cached_chain_info.lock().unwrap() = Some(info.clone());
                    Ok(info)
                }
            }
        } else {
            self.chain_info_no_cache()
        }
    }

    #[cfg(feature = "time")]
    pub fn latest(&self) -> Result<RandomnessBeacon> {
        // it is possible to either use round number 0, latest, or to infer the round number based on the current time
        // to allow for round verification, using inferance seems to be the best approach
        // without verification, latest is used instead
        if self.options().is_beacon_verification() {
            let info = self.chain_info()?;
            let now = OffsetDateTime::now_utc().format(&Rfc3339).unwrap();
            let time =
                RandomnessBeaconTime::parse(&info.into(), &now).expect("time should be valid");
            self.get_with_string(time.round().to_string())
        } else {
            self.get_with_string("latest".to_owned())
        }
    }

    #[cfg(not(feature = "time"))]
    pub fn latest(&self) -> Result<RandomnessBeacon> {
        self.get_with_string("latest".to_owned())
    }

    pub fn get(&self, round_number: u64) -> Result<RandomnessBeacon> {
        self.get_with_string(round_number.to_string())
    }

    pub fn get_by_unix_time(&self, round_unix_time: u64) -> Result<RandomnessBeacon> {
        let info = self.chain_info()?;
        let round = (round_unix_time - info.genesis_time()) / info.period();

        self.get(round)
    }
}

impl crate::chain::ChainClient for HttpClient {
    fn options(&self) -> ChainOptions {
        self.options()
    }

    fn latest(&self) -> Result<RandomnessBeacon> {
        self.latest()
    }

    fn get(&self, round_number: u64) -> Result<RandomnessBeacon> {
        self.get(round_number)
    }

    fn chain_info(&self) -> Result<ChainInfo> {
        self.chain_info()
    }
}

impl TryFrom<&str> for HttpClient {
    type Error = DrandError;

    fn try_from(value: &str) -> std::result::Result<Self, Self::Error> {
        Self::from_str(value)
    }
}

impl FromStr for HttpClient {
    type Err = DrandError;

    fn from_str(s: &str) -> std::result::Result<Self, Self::Err> {
        Self::new(s, None)
    }
}

#[cfg(test)]
mod tests {
    use crate::beacon::{tests::chained_beacon, tests::invalid_beacon, tests::unchained_beacon};
    use crate::chain::{
        ChainOptions, ChainVerification, tests::chained_chain_info, tests::unchained_chain_info,
    };
    #[cfg(feature = "time")]
    use time::Duration;

    use super::*;

    #[test]
    fn client_no_cache_works() {
        let mut server = mockito::Server::new();
        let info_mock = server
            .mock("GET", "/info")
            .match_query(mockito::Matcher::Any)
            .with_status(200)
            .with_header("content-type", "application/json")
            .with_body(serde_json::to_string(&chained_chain_info()).unwrap())
            .expect_at_least(2)
            .create();
        let expected_round = chained_beacon().round();
        let get_mock = server
            .mock("GET", format!("/public/{expected_round}").as_str())
            .match_query(mockito::Matcher::Any)
            .with_status(200)
            .with_header("content-type", "application/json")
            .with_body(serde_json::to_string(&chained_beacon()).unwrap())
            .expect_at_least(2)
            .create();

        // test client without cache
        let no_cache_client = HttpClient::new(
            server.url().as_str(),
            Some(ChainOptions::new(true, false, None)),
        )
        .unwrap();

        // info endpoint
        let info = match no_cache_client.chain_info() {
            Ok(info) => info,
            Err(_err) => panic!("fetch should have succeded"),
        };
        assert_eq!(info, chained_chain_info());
        // do it again to see if it's cached or not
        let _ = no_cache_client.chain_info();
        info_mock.assert();

        // get endpoint
        let beacon = match no_cache_client.get(expected_round) {
            Ok(beacon) => beacon,
            Err(_err) => panic!("fetch should have succeded"),
        };
        assert_eq!(beacon.beacon(), chained_beacon());
        assert_eq!(beacon.time(), 1625431050);
        // do it again to see if it's cached or not
        let _ = no_cache_client.get(expected_round);
        get_mock.assert();
    }

    #[test]
    fn client_cache_works() {
        let mut server = mockito::Server::new();
        let info_mock = server
            .mock("GET", "/info")
            .match_query(mockito::Matcher::Any)
            .with_status(200)
            .with_header("content-type", "application/json")
            .with_body(serde_json::to_string(&chained_chain_info()).unwrap())
            .expect_at_least(1)
            .create();
        let expected_round = chained_beacon().round();
        let get_mock = server
            .mock("GET", format!("/public/{expected_round}").as_str())
            .match_query(mockito::Matcher::Any)
            .with_status(200)
            .with_header("content-type", "application/json")
            .with_body(serde_json::to_string(&chained_beacon()).unwrap())
            .expect_at_least(1)
            .create();

        // test client with cache
        let cache_client = HttpClient::new(
            server.url().as_str(),
            Some(ChainOptions::new(false, true, None)),
        )
        .unwrap();

        // info endpoint
        let info = match cache_client.chain_info() {
            Ok(info) => info,
            Err(_err) => panic!("fetch should have succeded"),
        };
        assert_eq!(info, chained_chain_info());
        // do it again to see if it's cached or not
        let _ = cache_client.chain_info();
        info_mock.assert();

        // get endpoint
        let beacon = match cache_client.get(expected_round) {
            Ok(beacon) => beacon,
            Err(_err) => panic!("fetch should have succeded"),
        };
        assert_eq!(beacon.beacon(), chained_beacon());
        assert_eq!(beacon.time(), 1625431050);
        // do it again to see if it's cached or not
        let _ = cache_client.get(expected_round);
        get_mock.assert();
    }

    // Fakes the chain info genesis time so that the provided beacon is the latest one for the current time
    pub fn chain_info_with_latest(beacon: &ApiBeacon) -> ChainInfo {
        let info = unchained_chain_info();
        let latest_round = beacon.round();
        let period = info.period();
        let genesis_time = OffsetDateTime::now_utc()
            .checked_sub(Duration::seconds((latest_round * period - 1) as i64))
            .unwrap()
            .unix_timestamp();
        serde_json::from_str(&format!(
            r#"{{
            "public_key": "{public_key}",
            "period": {period},
            "genesis_time": {genesis_time},
            "hash": "{hash}",
            "groupHash": "{group_hash}",
            "schemeID": "{scheme_id}",
            "metadata": {{
                "beaconID": "default"
            }}
        }}"#,
            public_key = hex::encode(info.public_key()),
            hash = hex::encode(info.hash()),
            group_hash = hex::encode(info.group_hash()),
            scheme_id = info.scheme_id()
        ))
        .unwrap()
    }

    #[test]
    fn client_beacon_verification_works() {
        // unchained beacon
        let mut valid_server = mockito::Server::new();
        let _info_mock = valid_server
            .mock("GET", "/info")
            .match_query(mockito::Matcher::Any)
            .with_status(200)
            .with_header("content-type", "application/json")
            .with_body(serde_json::to_string(&chain_info_with_latest(&unchained_beacon())).unwrap())
            .expect_at_least(1)
            .create();
        let _latest_mock = valid_server
            .mock("GET", "/public/latest")
            .match_query(mockito::Matcher::Any)
            .with_status(200)
            .with_header("content-type", "application/json")
            .with_body(serde_json::to_string(&unchained_beacon()).unwrap())
            .expect_at_least(1)
            .create();
        let expected_round = unchained_beacon().round();
        let _get_mock = valid_server
            .mock("GET", format!("/public/{expected_round}").as_str())
            .match_query(mockito::Matcher::Any)
            .with_status(200)
            .with_header("content-type", "application/json")
            .with_body(serde_json::to_string(&unchained_beacon()).unwrap())
            .expect_at_least(1)
            .create();

        // test client without cache
        let client = HttpClient::new(
            valid_server.url().as_str(),
            Some(ChainOptions::new(true, false, None)),
        )
        .unwrap();

        // latest endpoint
        let latest = match client.latest() {
            Ok(beacon) => beacon,
            Err(err) => panic!("fetch should have succeded {}", err),
        };
        assert_eq!(latest.beacon(), unchained_beacon());

        // test client with round verification
        let client = HttpClient::new(
            valid_server.url().as_str(),
            Some(ChainOptions::new(true, false, None)),
        )
        .unwrap();

        match client.get(expected_round) {
            Ok(beacon) => assert_eq!(beacon.beacon(), unchained_beacon()),
            Err(err) => panic!("fetch should have succeded {}", err),
        }

        let mut invalid_server = mockito::Server::new();
        let _info_mock = invalid_server
            .mock("GET", "/info")
            .match_query(mockito::Matcher::Any)
            .with_status(200)
            .with_header("content-type", "application/json")
            .with_body(serde_json::to_string(&chained_chain_info()).unwrap())
            .expect_at_least(1)
            .create();
        let expected_round = invalid_beacon().round();
        let _get_mock = invalid_server
            .mock("GET", format!("/public/{expected_round}").as_str())
            .match_query(mockito::Matcher::Any)
            .with_status(200)
            .with_header("content-type", "application/json")
            .with_body(serde_json::to_string(&invalid_beacon()).unwrap())
            .expect_at_least(1)
            .create();

        // test client without cache
        let client = HttpClient::new(
            invalid_server.url().as_str(),
            Some(ChainOptions::new(true, false, None)),
        )
        .unwrap();

        // get endpoint
        match client.get(expected_round) {
            Ok(_beacon) => panic!("Beacon should not validate"),
            Err(_err) => (),
        }
    }

    #[test]
    fn client_chain_verification_works() {
        // unchained beacon
        let mut valid_server = mockito::Server::new();
        let _info_mock = valid_server
            .mock("GET", "/info")
            .match_query(mockito::Matcher::Any)
            .with_status(200)
            .with_header("content-type", "application/json")
            .with_body(serde_json::to_string(&unchained_chain_info()).unwrap())
            .expect_at_least(1)
            .create();
        let _latest_mock = valid_server
            .mock("GET", "/public/latest")
            .match_query(mockito::Matcher::Any)
            .with_status(200)
            .with_header("content-type", "application/json")
            .with_body(serde_json::to_string(&unchained_beacon()).unwrap())
            .expect_at_least(1)
            .create();
        let expected_round = chained_beacon().round();
        let _get_mock = valid_server
            .mock("GET", format!("/public/{expected_round}").as_str())
            .match_query(mockito::Matcher::Any)
            .with_status(200)
            .with_header("content-type", "application/json")
            .with_body(serde_json::to_string(&unchained_beacon()).unwrap())
            .expect_at_least(1)
            .create();
        let invalid_round = expected_round + 1;
        let _get_mock = valid_server
            .mock("GET", format!("/public/{invalid_round}").as_str())
            .match_query(mockito::Matcher::Any)
            .with_status(200)
            .with_header("content-type", "application/json")
            .with_body(serde_json::to_string(&unchained_beacon()).unwrap())
            .expect_at_least(1)
            .create();

        // test client without cache
        let unchained_info = unchained_chain_info();
        let unchained_client = HttpClient::new(
            valid_server.url().as_str(),
            Some(ChainOptions::new(
                true,
                false,
                Some(ChainVerification::new(
                    Some(unchained_info.hash()),
                    Some(unchained_info.public_key()),
                )),
            )),
        )
        .unwrap();

        // get endpoint
        let beacon = match unchained_client.get(expected_round) {
            Ok(beacon) => beacon,
            Err(err) => panic!("fetch should have succeded {}", err),
        };
        assert_eq!(beacon.beacon(), unchained_beacon());
        assert_eq!(beacon.time(), 1654677099);

        // test with not the correct hash
        let chained_info = chained_chain_info();
        let invalid_client = HttpClient::new(
            valid_server.url().as_str(),
            Some(ChainOptions::new(
                true,
                false,
                Some(ChainVerification::new(Some(chained_info.hash()), None)),
            )),
        )
        .unwrap();

        match invalid_client.get(expected_round) {
            Ok(_beacon) => panic!("Beacon should not validate"),
            Err(_err) => (),
        };
        // test with not the correct public_key
        let chained_info = chained_chain_info();
        let invalid_client = HttpClient::new(
            valid_server.url().as_str(),
            Some(ChainOptions::new(
                true,
                false,
                Some(ChainVerification::new(
                    None,
                    Some(chained_info.public_key()),
                )),
            )),
        )
        .unwrap();

        match invalid_client.get(expected_round) {
            Ok(_beacon) => panic!("Beacon should not validate"),
            Err(_err) => (),
        };

        // test with incorrect round
        let chained_info = chained_chain_info();
        let invalid_client = HttpClient::new(
            valid_server.url().as_str(),
            Some(ChainOptions::new(
                true,
                false,
                Some(ChainVerification::new(
                    Some(chained_info.hash()),
                    Some(chained_info.public_key()),
                )),
            )),
        )
        .unwrap();

        match invalid_client.get(invalid_round) {
            Ok(_beacon) => panic!("Beacon should not validate"),
            Err(_err) => (),
        };
    }
}