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

bitcoindevkit / bdk / 10348217438

12 Aug 2024 08:10AM UTC coverage: 81.794% (-0.02%) from 81.813%
10348217438

Pull #1535

github

web-flow
Merge 2c0bc45ec into 98c49592d
Pull Request #1535: test(electrum): Test sync in reorg and no-reorg situations

19 of 25 new or added lines in 1 file covered. (76.0%)

1 existing line in 1 file now uncovered.

10908 of 13336 relevant lines covered (81.79%)

12995.15 hits per line

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

46.03
/crates/chain/src/persist.rs
1
use core::{
2
    future::Future,
3
    ops::{Deref, DerefMut},
4
    pin::Pin,
5
};
6

7
use alloc::boxed::Box;
8

9
use crate::Merge;
10

11
/// Represents a type that contains staged changes.
12
pub trait Staged {
13
    /// Type for staged changes.
14
    type ChangeSet: Merge;
15

16
    /// Get mutable reference of staged changes.
17
    fn staged(&mut self) -> &mut Self::ChangeSet;
18
}
19

20
/// Trait that persists the type with `Db`.
21
///
22
/// Methods of this trait should not be called directly.
23
pub trait PersistWith<Db>: Staged + Sized {
24
    /// Parameters for [`PersistWith::create`].
25
    type CreateParams;
26
    /// Parameters for [`PersistWith::load`].
27
    type LoadParams;
28
    /// Error type of [`PersistWith::create`].
29
    type CreateError;
30
    /// Error type of [`PersistWith::load`].
31
    type LoadError;
32
    /// Error type of [`PersistWith::persist`].
33
    type PersistError;
34

35
    /// Initialize the `Db` and create `Self`.
36
    fn create(db: &mut Db, params: Self::CreateParams) -> Result<Self, Self::CreateError>;
37

38
    /// Initialize the `Db` and load a previously-persisted `Self`.
39
    fn load(db: &mut Db, params: Self::LoadParams) -> Result<Option<Self>, Self::LoadError>;
40

41
    /// Persist changes to the `Db`.
42
    fn persist(
43
        db: &mut Db,
44
        changeset: &<Self as Staged>::ChangeSet,
45
    ) -> Result<(), Self::PersistError>;
46
}
47

48
type FutureResult<'a, T, E> = Pin<Box<dyn Future<Output = Result<T, E>> + Send + 'a>>;
49

50
/// Trait that persists the type with an async `Db`.
51
pub trait PersistAsyncWith<Db>: Staged + Sized {
52
    /// Parameters for [`PersistAsyncWith::create`].
53
    type CreateParams;
54
    /// Parameters for [`PersistAsyncWith::load`].
55
    type LoadParams;
56
    /// Error type of [`PersistAsyncWith::create`].
57
    type CreateError;
58
    /// Error type of [`PersistAsyncWith::load`].
59
    type LoadError;
60
    /// Error type of [`PersistAsyncWith::persist`].
61
    type PersistError;
62

63
    /// Initialize the `Db` and create `Self`.
64
    fn create(db: &mut Db, params: Self::CreateParams) -> FutureResult<Self, Self::CreateError>;
65

66
    /// Initialize the `Db` and load a previously-persisted `Self`.
67
    fn load(db: &mut Db, params: Self::LoadParams) -> FutureResult<Option<Self>, Self::LoadError>;
68

69
    /// Persist changes to the `Db`.
70
    fn persist<'a>(
71
        db: &'a mut Db,
72
        changeset: &'a <Self as Staged>::ChangeSet,
73
    ) -> FutureResult<'a, (), Self::PersistError>;
74
}
75

76
/// Represents a persisted `T`.
77
#[derive(Debug, PartialEq, Eq, PartialOrd, Ord)]
78
pub struct Persisted<T> {
79
    inner: T,
80
}
81

82
impl<T> Persisted<T> {
83
    /// Create a new persisted `T`.
84
    pub fn create<Db>(db: &mut Db, params: T::CreateParams) -> Result<Self, T::CreateError>
4✔
85
    where
4✔
86
        T: PersistWith<Db>,
4✔
87
    {
4✔
88
        T::create(db, params).map(|inner| Self { inner })
4✔
89
    }
4✔
90

91
    /// Create a new persisted `T` with async `Db`.
92
    pub async fn create_async<Db>(
×
93
        db: &mut Db,
×
94
        params: T::CreateParams,
×
95
    ) -> Result<Self, T::CreateError>
×
96
    where
×
97
        T: PersistAsyncWith<Db>,
×
98
    {
×
99
        T::create(db, params).await.map(|inner| Self { inner })
×
100
    }
×
101

102
    /// Construct a persisted `T` from `Db`.
103
    pub fn load<Db>(db: &mut Db, params: T::LoadParams) -> Result<Option<Self>, T::LoadError>
10✔
104
    where
10✔
105
        T: PersistWith<Db>,
10✔
106
    {
10✔
107
        Ok(T::load(db, params)?.map(|inner| Self { inner }))
10✔
108
    }
10✔
109

110
    /// Construct a persisted `T` from an async `Db`.
111
    pub async fn load_async<Db>(
×
112
        db: &mut Db,
×
113
        params: T::LoadParams,
×
114
    ) -> Result<Option<Self>, T::LoadError>
×
115
    where
×
116
        T: PersistAsyncWith<Db>,
×
117
    {
×
118
        Ok(T::load(db, params).await?.map(|inner| Self { inner }))
×
119
    }
×
120

121
    /// Persist staged changes of `T` into `Db`.
122
    ///
123
    /// If the database errors, the staged changes will not be cleared.
124
    pub fn persist<Db>(&mut self, db: &mut Db) -> Result<bool, T::PersistError>
2✔
125
    where
2✔
126
        T: PersistWith<Db>,
2✔
127
    {
2✔
128
        let stage = T::staged(&mut self.inner);
2✔
129
        if stage.is_empty() {
2✔
130
            return Ok(false);
×
131
        }
2✔
132
        T::persist(db, &*stage)?;
2✔
133
        stage.take();
2✔
134
        Ok(true)
2✔
135
    }
2✔
136

137
    /// Persist staged changes of `T` into an async `Db`.
138
    ///
139
    /// If the database errors, the staged changes will not be cleared.
140
    pub async fn persist_async<'a, Db>(
×
141
        &'a mut self,
×
142
        db: &'a mut Db,
×
143
    ) -> Result<bool, T::PersistError>
×
144
    where
×
145
        T: PersistAsyncWith<Db>,
×
146
    {
×
147
        let stage = T::staged(&mut self.inner);
×
148
        if stage.is_empty() {
×
149
            return Ok(false);
×
150
        }
×
151
        T::persist(db, &*stage).await?;
×
152
        stage.take();
×
153
        Ok(true)
×
154
    }
×
155
}
156

157
impl<T> Deref for Persisted<T> {
158
    type Target = T;
159

160
    fn deref(&self) -> &Self::Target {
12✔
161
        &self.inner
12✔
162
    }
12✔
163
}
164

165
impl<T> DerefMut for Persisted<T> {
166
    fn deref_mut(&mut self) -> &mut Self::Target {
2✔
167
        &mut self.inner
2✔
168
    }
2✔
169
}
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

© 2025 Coveralls, Inc