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
//! # ssb-db
//!
//! The most basic db that you need to do (legacy) replication on ssb.
//!
//! ## Legacy Replication
//!
//! "Legacy Replication" is how ssb used to do replication before
//! [ebt](https://github.com/dominictarr/epidemic-broadcast-trees)
//!
//! It's simpler than ebt, but uses more bandwidth.
//!
//! To do legacy replication a client calls
//! [createHistoryStream](https://scuttlebot.io/apis/scuttlebot/ssb.html#createhistorystream-source)
//! for each feed it wants to replicate, passing the largest sequence number it knows about.
//!
//! ## SsbDb
//!
//! `ssb-db` defines a trait [SsbDb] that provides all the functionality you should need
//! to make and handle legacy replication requests.
//!
//! ## Architecture
//!
//! [SqliteSsbDb] implements the [SsbDb] trait.
//!
//! The underlying architecture is based on [flume-db](https://github.com/sunrise-choir/flumedb-rs).
//!
//! `ssb-db` stores data in an append only log. It maintains indexes for querying the log in sqlite.
//! The append only log is the source of truth and the indexes are derived from the log. If the
//! indexes break or need to be migrated, the sqlite db can be deleted and rebuilt from the log.
//!
//! ## Validation
//!
//! `ssb-db` **does not validate any messages before appending them.** The caller must validate them first.
//! See [ssb-validate](https://github.com/sunrise-choir/ssb-validate) and [ssb-verify-signatures](https://github.com/sunrise-choir/ssb-verify-signatures).
//!
#[macro_use]
extern crate diesel_migrations;
#[macro_use]
extern crate diesel;

pub use flumedb::flume_view::Sequence as FlumeSequence;

mod db;
pub mod error;
pub mod sqlite_ssb_db;
mod ssb_message;

pub use error::Error;
pub use sqlite_ssb_db::SqliteSsbDb;

use error::Result;
use ssb_multiformats::multihash::Multihash;
use ssb_multiformats::multikey::Multikey;

pub trait SsbDb {
    /// Append a batch of valid ssb messages authored by the `feed_id`.
    fn append_batch<T: 'static + AsRef<[u8]>>(&self, feed_id: &Multikey, messages: &[T]) -> Result<()>;
    /// Get an entry by its ssb message key.
    fn get_entry_by_key(&self, message_key: &Multihash) -> Result<Vec<u8>>;
    /// Get an entry by its sequence key + author.
    fn get_entry_by_seq(&self, feed_id: &Multikey, sequence: i32) -> Result<Option<Vec<u8>>>;
    /// Get the latest sequence number for the given feed.
    fn get_feed_latest_sequence(&self, feed_id: &Multikey) -> Result<Option<i32>>;
    /// Get all the entries for the given `feed_id`, with a sequence larger than `sequence`.
    ///
    /// You may `limit` the maximum number of entries to get.
    ///
    /// You can control whether to `include_keys`, `include_values`, or both.
    ///
    /// If `include_values` and `include_values` are both `false`
    /// `get_entries_newer_than_sequence` will return an `Error`.
    fn get_entries_newer_than_sequence(
        &self,
        feed_id: &Multikey,
        sequence: i32,
        limit: Option<i64>,
        include_keys: bool,
        include_values: bool,
    ) -> Result<Vec<Vec<u8>>>;
    /// You can rebuild the indexes in sqlite db (but not the offset file) if they become
    /// corrupted.
    fn rebuild_indexes(&self) -> Result<()>;
}

#[cfg(test)]
mod tests {
    use crate::ssb_message::{SsbMessage, SsbValue};
    use crate::{SqliteSsbDb, SsbDb};
    use flumedb::offset_log::OffsetLog;
    use ssb_multiformats::multihash::Multihash;
    use ssb_multiformats::multikey::Multikey;

    #[test]
    fn get_entry_by_key_works() {
        let key_str = "%/v5mCnV/kmnVtnF3zXtD4tbzoEQo4kRq/0d/bgxP1WI=.sha256";
        let key = Multihash::from_legacy(key_str.as_bytes()).unwrap().0;

        let db_path = "/tmp/test_get_entry_by_key.sqlite3";
        let db = SqliteSsbDb::new(db_path, "./test_vecs/piet.offset");
        db.update_indexes_from_offset_file().unwrap();

        let res = db.get_entry_by_key(&key);
        let entry = res.unwrap();
        let value: serde_json::Value = serde_json::from_slice(&entry).unwrap();

        let actual_key_str: &str = value["key"].as_str().unwrap();

        assert_eq!(actual_key_str, key_str);
        std::fs::remove_file(&db_path).unwrap();
    }
    #[test]
    fn get_feed_latest_sequence_works() {
        let expected_seq = 6006;
        let author_str = "@U5GvOKP/YUza9k53DSXxT0mk3PIrnyAmessvNfZl5E0=.ed25519";
        let author = Multikey::from_legacy(author_str.as_bytes()).unwrap().0;

        let db_path = "/tmp/test_get_latest_seq.sqlite3";
        let db = SqliteSsbDb::new(db_path, "./test_vecs/piet.offset");
        db.update_indexes_from_offset_file().unwrap();

        let res = db.get_feed_latest_sequence(&author);
        let seq = res.unwrap();

        assert_eq!(seq.unwrap(), expected_seq);
        std::fs::remove_file(&db_path).unwrap();
    }
    #[test]
    fn get_entries_kv_newer_than_sequence_works() {
        let author_str = "@U5GvOKP/YUza9k53DSXxT0mk3PIrnyAmessvNfZl5E0=.ed25519";
        let author = Multikey::from_legacy(author_str.as_bytes()).unwrap().0;

        let db_path = "/tmp/test_get_entries_kv.sqlite3";
        let db = SqliteSsbDb::new(db_path, "./test_vecs/piet.offset");
        db.update_indexes_from_offset_file().unwrap();

        let res = db
            .get_entries_newer_than_sequence(&author, 6000, None, true, true)
            .unwrap()
            .iter()
            .flat_map(|entry| serde_json::from_slice::<SsbMessage>(&entry))
            .collect::<Vec<_>>();

        assert_eq!(res.len(), 6);

        std::fs::remove_file(&db_path).unwrap();
    }
    #[test]
    fn get_entries_newer_than_sequence_works_with_limit() {
        let author_str = "@U5GvOKP/YUza9k53DSXxT0mk3PIrnyAmessvNfZl5E0=.ed25519";
        let author = Multikey::from_legacy(author_str.as_bytes()).unwrap().0;

        let db_path = "/tmp/test_get_entries_kv_limit.sqlite3";
        let db = SqliteSsbDb::new(db_path, "./test_vecs/piet.offset");
        db.update_indexes_from_offset_file().unwrap();

        let res = db
            .get_entries_newer_than_sequence(&author, 6000, Some(2), true, true)
            .unwrap()
            .iter()
            .flat_map(|entry| serde_json::from_slice::<SsbMessage>(&entry))
            .collect::<Vec<_>>();

        assert_eq!(res.len(), 2);

        std::fs::remove_file(&db_path).unwrap();
    }
    #[test]
    fn get_entries_k_newer_than_sequence_works() {
        let author_str = "@U5GvOKP/YUza9k53DSXxT0mk3PIrnyAmessvNfZl5E0=.ed25519";
        let author = Multikey::from_legacy(author_str.as_bytes()).unwrap().0;

        let db_path = "/tmp/test_get_entries_k.sqlite3";
        let db = SqliteSsbDb::new(db_path, "./test_vecs/piet.offset");
        db.update_indexes_from_offset_file().unwrap();

        let res = db
            .get_entries_newer_than_sequence(&author, 6000, None, true, false)
            .unwrap()
            .iter()
            .flat_map(|entry| Multihash::from_legacy(entry))
            .map(|(key, _)| key)
            .collect::<Vec<_>>();

        assert_eq!(res.len(), 6);

        std::fs::remove_file(&db_path).unwrap();
    }
    #[test]
    fn get_entries_v_newer_than_sequence_works() {
        //check message is valid
        let author_str = "@U5GvOKP/YUza9k53DSXxT0mk3PIrnyAmessvNfZl5E0=.ed25519";
        let author = Multikey::from_legacy(author_str.as_bytes()).unwrap().0;

        let db_path = "/tmp/test_get_entries_v.sqlite3";
        let db = SqliteSsbDb::new(db_path, "./test_vecs/piet.offset");
        db.update_indexes_from_offset_file().unwrap();

        let res = db
            .get_entries_newer_than_sequence(&author, 6000, None, false, true)
            .unwrap()
            .iter()
            .flat_map(|entry| serde_json::from_slice::<SsbValue>(entry))
            .collect::<Vec<_>>();

        assert_eq!(res.len(), 6);

        std::fs::remove_file(&db_path).unwrap();
    }
    #[test]
    fn get_entries_no_kv_newer_than_sequence_errors() {
        //check message is valid
        let author_str = "@U5GvOKP/YUza9k53DSXxT0mk3PIrnyAmessvNfZl5E0=.ed25519";
        let author = Multikey::from_legacy(author_str.as_bytes()).unwrap().0;

        let db_path = "/tmp/test_get_entries_no_kv.sqlite3";
        let db = SqliteSsbDb::new(db_path, "./test_vecs/piet.offset");
        db.update_indexes_from_offset_file().unwrap();

        let res = db.get_entries_newer_than_sequence(&author, 6000, None, false, false);

        assert!(res.is_err());

        std::fs::remove_file(&db_path).unwrap();
    }
    #[test]
    fn append_batch_works() {
        let author_str = "@U5GvOKP/YUza9k53DSXxT0mk3PIrnyAmessvNfZl5E0=.ed25519";
        let author = Multikey::from_legacy(author_str.as_bytes()).unwrap().0;
        let offset_log_path = "./test_vecs/piet.offset";
        let log = OffsetLog::<u32>::new(&offset_log_path).unwrap();

        let entries = log.iter().map(|entry| entry.data).collect::<Vec<_>>();

        let db_path = "/tmp/test_append_batch.sqlite3";
        let offset_path = "/tmp/test_append_batch.offset";
        let db = SqliteSsbDb::new(db_path, offset_path);

        let res = db.append_batch(&author, &entries.as_slice());
        assert!(res.is_ok());
        std::fs::remove_file(&db_path).unwrap();
        std::fs::remove_file(&offset_path).unwrap();
    }
    #[test]
    fn rebuild_indexes_works() {
        let expected_seq = 6006;

        let author_str = "@U5GvOKP/YUza9k53DSXxT0mk3PIrnyAmessvNfZl5E0=.ed25519";
        let author = Multikey::from_legacy(author_str.as_bytes()).unwrap().0;

        let db_path = "/tmp/test_rebuild_indexes.sqlite3";
        let db = SqliteSsbDb::new(db_path, "./test_vecs/piet.offset");
        db.update_indexes_from_offset_file().unwrap();

        let res = db.rebuild_indexes();

        assert!(res.is_ok());

        let res = db.get_feed_latest_sequence(&author);
        let seq = res.unwrap();

        assert_eq!(seq.unwrap(), expected_seq);

        std::fs::remove_file(&db_path).unwrap();
    }
}