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
//! Database corruption manager.
use std::{io, sync::Arc};

use super::{batch::BoxedBatch, iterator::BoxedIterator, BoxedDatabase};
use crate::subnet::rpc::{errors, utils};

use tokio::sync::Mutex;

/// Database wrapper which blocks further calls to the database at first sign of corruption.
///
/// ref. <https://pkg.go.dev/github.com/ava-labs/avalanchego/database/corruptabledb#Database>
#[derive(Clone)]
pub struct Database {
    db: BoxedDatabase,
    /// Stores a corrupted error if observed.
    corrupted: Arc<Mutex<utils::Errors>>,
}

impl Database {
    pub fn new(db: BoxedDatabase) -> BoxedDatabase {
        Box::new(Self {
            db,
            corrupted: Arc::new(Mutex::new(utils::Errors::new())),
        })
    }
}

#[tonic::async_trait]
impl crate::subnet::rpc::database::KeyValueReaderWriterDeleter for Database {
    /// Attempts to return if the database has a key with the provided value.
    async fn has(&self, key: &[u8]) -> io::Result<bool> {
        let mut corrupted = self.corrupted.lock().await;
        let _ = corrupted.err()?;

        let db = &self.db;
        let has = db.has(key).await.map_err(|err| {
            if errors::is_corruptible(&err) {
                corrupted.add(&io::Error::new(
                    io::ErrorKind::Other,
                    format!("closed to avoid possible corruption, init error: {err}"),
                ));
            }
            err
        })?;

        Ok(has)
    }

    /// Attempts to return the value that was mapped to the key that was provided.
    async fn get(&self, key: &[u8]) -> io::Result<Vec<u8>> {
        let mut corrupted = self.corrupted.lock().await;
        let _ = corrupted.err()?;

        let db = &self.db;
        let value = db.get(key).await.map_err(|err| {
            if errors::is_corruptible(&err) {
                corrupted.add(&io::Error::new(
                    io::ErrorKind::Other,
                    format!("closed to avoid possible corruption, init error: {err}"),
                ));
            }
            err
        })?;

        Ok(value)
    }

    /// Attempts to set the value this key maps to.
    async fn put(&mut self, key: &[u8], value: &[u8]) -> io::Result<()> {
        let mut corrupted = self.corrupted.lock().await;
        let _ = corrupted.err()?;

        let db = &mut self.db;
        db.put(key, value).await.map_err(|err| {
            if errors::is_corruptible(&err) {
                corrupted.add(&io::Error::new(
                    io::ErrorKind::Other,
                    format!("closed to avoid possible corruption, init error: {err}"),
                ));
            }
            err
        })?;

        Ok(())
    }

    /// Attempts to remove any mapping from the key.
    async fn delete(&mut self, key: &[u8]) -> io::Result<()> {
        let mut corrupted = self.corrupted.lock().await;
        let _ = corrupted.err()?;

        let db = &mut self.db;
        db.delete(key).await.map_err(|err| {
            if errors::is_corruptible(&err) {
                corrupted.add(&io::Error::new(
                    io::ErrorKind::Other,
                    format!("closed to avoid possible corruption, init error: {err}"),
                ));
            }
            err
        })?;

        Ok(())
    }
}

#[tonic::async_trait]
impl crate::subnet::rpc::database::Closer for Database {
    /// Attempts to close the database.
    async fn close(&self) -> io::Result<()> {
        let mut corrupted = self.corrupted.lock().await;
        let _ = corrupted.err()?;

        let db = &self.db;
        db.close().await.map_err(|err| {
            if errors::is_corruptible(&err) {
                corrupted.add(&io::Error::new(
                    io::ErrorKind::Other,
                    format!("closed to avoid possible corruption, init error: {err}"),
                ));
            }
            err
        })?;

        Ok(())
    }
}

#[tonic::async_trait]
impl crate::subnet::rpc::health::Checkable for Database {
    /// Checks if the database has been closed.
    async fn health_check(&self) -> io::Result<Vec<u8>> {
        let mut corrupted = self.corrupted.lock().await;
        let _ = corrupted.err()?;

        let db = &self.db;
        let check = db.health_check().await.map_err(|err| {
            if errors::is_corruptible(&err) {
                corrupted.add(&io::Error::new(
                    io::ErrorKind::Other,
                    format!("closed to avoid possible corruption, init error: {err}"),
                ));
            }
            err
        })?;

        Ok(check)
    }
}

#[tonic::async_trait]
impl crate::subnet::rpc::database::iterator::Iteratee for Database {
    /// Implements the [`crate::subnet::rpc::database::iterator::Iteratee`] trait.
    async fn new_iterator(&self) -> io::Result<BoxedIterator> {
        self.new_iterator_with_start_and_prefix(&[], &[]).await
    }

    /// Implements the [`crate::subnet::rpc::database::iterator::Iteratee`] trait.
    async fn new_iterator_with_start(&self, start: &[u8]) -> io::Result<BoxedIterator> {
        self.new_iterator_with_start_and_prefix(start, &[]).await
    }

    /// Implements the [`crate::subnet::rpc::database::iterator::Iteratee`] trait.
    async fn new_iterator_with_prefix(&self, prefix: &[u8]) -> io::Result<BoxedIterator> {
        self.new_iterator_with_start_and_prefix(&[], prefix).await
    }

    /// Implements the [`crate::subnet::rpc::database::iterator::Iteratee`] trait.
    async fn new_iterator_with_start_and_prefix(
        &self,
        start: &[u8],
        prefix: &[u8],
    ) -> io::Result<BoxedIterator> {
        let corrupted = self.corrupted.lock().await;
        let _ = corrupted.err()?;

        self.db
            .new_iterator_with_start_and_prefix(start, prefix)
            .await
    }
}

#[tonic::async_trait]
impl crate::subnet::rpc::database::batch::Batcher for Database {
    /// Implements the [`crate::subnet::rpc::database::batch::Batcher`] trait.
    async fn new_batch(&self) -> io::Result<BoxedBatch> {
        let db = &self.db;
        let mut corrupted = self.corrupted.lock().await;

        let batch = db.new_batch().await.map_err(|err| {
            if errors::is_corruptible(&err) {
                corrupted.add(&io::Error::new(
                    io::ErrorKind::Other,
                    format!("closed to avoid possible corruption, init error: {err}"),
                ));
            }
            err
        })?;

        Ok(batch)
    }
}

impl crate::subnet::rpc::database::Database for Database {}