sui_light_client/
object_store.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
// Copyright (c) Mysten Labs, Inc.
// SPDX-License-Identifier: Apache-2.0

use crate::config::Config;
use anyhow::{anyhow, Result};
use async_trait::async_trait;
use object_store::{path::Path, ObjectStore};
use sui_types::full_checkpoint_content::CheckpointData;
use sui_types::messages_checkpoint::CertifiedCheckpointSummary;
use tracing::info;
use url::Url;

pub struct SuiObjectStore {
    store: Box<dyn ObjectStore>,
}

impl SuiObjectStore {
    pub fn new(config: &Config) -> Result<Self> {
        let url = Url::parse(&config.object_store_url)?;
        let (store, _) = object_store::parse_url(&url)?;
        Ok(Self { store })
    }

    pub async fn download_checkpoint_summary(
        &self,
        checkpoint_number: u64,
    ) -> Result<CertifiedCheckpointSummary> {
        let path = Path::from(format!("{}.chk", checkpoint_number));
        let response = self.store.get(&path).await?;
        let bytes = response.bytes().await?;

        let (_, blob) = bcs::from_bytes::<(u8, CheckpointData)>(&bytes)?;

        info!("Downloaded checkpoint summary: {}", checkpoint_number);
        Ok(blob.checkpoint_summary)
    }

    pub async fn get_full_checkpoint(&self, checkpoint_number: u64) -> Result<CheckpointData> {
        let path = Path::from(format!("{}.chk", checkpoint_number));
        info!("Request full checkpoint: {}", path);
        let response = self
            .store
            .get(&path)
            .await
            .map_err(|_| anyhow!("Cannot get full checkpoint from object store"))?;
        let bytes = response.bytes().await?;
        let (_, full_checkpoint) = bcs::from_bytes::<(u8, CheckpointData)>(&bytes)?;
        Ok(full_checkpoint)
    }
}

#[async_trait]
pub trait ObjectStoreExt {
    async fn get_checkpoint_summary(
        &self,
        checkpoint_number: u64,
    ) -> Result<CertifiedCheckpointSummary>;
}

#[async_trait]
impl ObjectStoreExt for SuiObjectStore {
    async fn get_checkpoint_summary(
        &self,
        checkpoint_number: u64,
    ) -> Result<CertifiedCheckpointSummary> {
        self.download_checkpoint_summary(checkpoint_number).await
    }
}

pub async fn download_checkpoint_summary(
    config: &Config,
    checkpoint_number: u64,
) -> Result<CertifiedCheckpointSummary> {
    let store = SuiObjectStore::new(config)?;
    store.get_checkpoint_summary(checkpoint_number).await
}