-
Notifications
You must be signed in to change notification settings - Fork 33
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add database models #289
Merged
Merged
Add database models #289
Changes from 22 commits
Commits
Show all changes
28 commits
Select commit
Hold shift + click to select a range
db813ba
Add key_store table and generate
richardhuaaa f9c6ec8
Implement store/fetch/delete traits
richardhuaaa 257d5b6
Add SQL key store
richardhuaaa 6af1384
Use SQL key store for provider, remove in-memory key store
richardhuaaa a404c20
Use reference to encrypted store to allow multiple consumers
richardhuaaa acd42b8
Tidy identity
richardhuaaa 0a53a11
Add identity table, queries, and unit tests
richardhuaaa 4fb1059
Persist and retrieve identity from storage inside builder
richardhuaaa 598b5ea
Tidy up names and types
richardhuaaa c8780f0
Merge branch 'main' into rich/sqlite-store
neekolas 091afdb
Add tests and fix bugs
richardhuaaa 18c6aa4
Fix lints
richardhuaaa dae1c74
Update xmtp_mls/src/storage/encrypted_store/mod.rs
richardhuaaa 77aa94b
Dont use the word Error in error names
richardhuaaa 5278d13
Move identity initialization to identity strategy
richardhuaaa a6c5095
Merge remote-tracking branch 'origin/main' into rich/sqlite-store
richardhuaaa 7000fb4
Merge branch 'rich/sqlite-store' of github.com:xmtp/libxmtp into rich…
neekolas afea4b3
Refactor DB stuff into multiple files
neekolas 671754d
Delete models.rs
neekolas 0582dda
Merge branch 'main' into nmolnar/add-db-models
neekolas 787e392
Cleanup
neekolas b570521
Add kind to group_messages
neekolas ad25444
Update schema
neekolas 24fd63a
Update schema
neekolas b1797ae
Update schema
neekolas 78f274e
Merge branch 'main' into nmolnar/add-db-models
neekolas 390f60f
Add ciphersuite back in as import
neekolas caab44a
Merge branch 'main' into nmolnar/add-db-models
neekolas File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
7 changes: 4 additions & 3 deletions
7
xmtp/migrations/2023-06-30-063813_create_users_and_conversations/up.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
9 changes: 9 additions & 0 deletions
9
xmtp_mls/migrations/2023-10-29-205333_state_machine_init/down.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
DROP TABLE IF EXISTS groups; | ||
|
||
DROP TABLE IF EXISTS group_messages; | ||
|
||
DROP TABLE IF EXISTS topic_refresh_state; | ||
|
||
DROP TABLE IF EXISTS group_intents; | ||
|
||
DROP TABLE IF EXISTS outbound_welcome_messages; |
72 changes: 72 additions & 0 deletions
72
xmtp_mls/migrations/2023-10-29-205333_state_machine_init/up.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
CREATE TABLE groups ( | ||
-- Random ID generated by group creator | ||
"id" BLOB PRIMARY KEY NOT NULL, | ||
-- Based on the timestamp of the welcome message | ||
"created_at_ns" BIGINT NOT NULL, | ||
-- Enum of GROUP_MEMBERSHIP_STATE | ||
"membership_state" INT NOT NULL | ||
neekolas marked this conversation as resolved.
Show resolved
Hide resolved
|
||
); | ||
|
||
-- Allow for efficient sorting of groups | ||
CREATE INDEX groups_created_at_idx ON groups(created_at_ns); | ||
|
||
CREATE INDEX groups_membership_state ON groups(membership_state); | ||
|
||
-- Successfully processed messages meant to be returned to the user | ||
CREATE TABLE group_messages ( | ||
"id" BLOB PRIMARY KEY NOT NULL, | ||
-- Derived via SHA256(CONCAT(decrypted_message_bytes, conversation_id, timestamp)) | ||
richardhuaaa marked this conversation as resolved.
Show resolved
Hide resolved
|
||
"group_id" BLOB NOT NULL, | ||
-- Message contents after decryption | ||
"decrypted_message_bytes" BLOB NOT NULL, | ||
-- Based on the timestamp of the message | ||
"sent_at_ns" BIGINT NOT NULL, | ||
-- Enum GROUP_MESSAGE_KIND | ||
"kind" INT NOT NULL, | ||
-- Could remove this if we added a table mapping installation_ids to wallet addresses | ||
richardhuaaa marked this conversation as resolved.
Show resolved
Hide resolved
|
||
"sender_installation_id" BLOB NOT NULL, | ||
"sender_wallet_address" TEXT NOT NULL, | ||
FOREIGN KEY (group_id) REFERENCES groups(id) | ||
); | ||
|
||
CREATE INDEX group_messages_group_id_sort_idx ON group_messages(group_id, sent_at_ns); | ||
|
||
-- Used to keep track of the last seen message timestamp in a topic | ||
CREATE TABLE topic_refresh_state ( | ||
"topic" TEXT PRIMARY KEY NOT NULL, | ||
"last_message_timestamp_ns" BIGINT NOT NULL | ||
); | ||
|
||
-- This table is required to retry messages that do not send successfully due to epoch conflicts | ||
CREATE TABLE group_intents ( | ||
-- Serial ID auto-generated by the DB | ||
"id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, | ||
-- Enum INTENT_KIND | ||
"kind" INT NOT NULL, | ||
"group_id" BLOB NOT NULL, | ||
-- Some sort of serializable blob that can be used to re-try the message if the first attempt failed due to conflict | ||
"data" BLOB NOT NULL, | ||
-- INTENT_STATE, | ||
"state" INT NOT NULL, | ||
-- The hash of the encrypted, concrete, form of the message if it was published. | ||
neekolas marked this conversation as resolved.
Show resolved
Hide resolved
|
||
"message_hash" BLOB, | ||
FOREIGN KEY (group_id) REFERENCES groups(id) | ||
); | ||
|
||
CREATE INDEX group_intents_group_id_id ON group_intents(group_id, id); | ||
|
||
CREATE TABLE outbound_welcome_messages ( | ||
neekolas marked this conversation as resolved.
Show resolved
Hide resolved
|
||
-- Derived via SHA256(CONCAT(group_id, welcome_message, installation_id)) | ||
"id" BLOB PRIMARY KEY NOT NULL, | ||
-- OUTBOUND_WELCOME_STATE | ||
"state" INT NOT NULL, | ||
"installation_id" BLOB NOT NULL, | ||
-- The hash of the commit message which created this welcome | ||
"commit_hash" BLOB NOT NULL, | ||
-- The group this welcome belongs to | ||
"group_id" BLOB NOT NULL, | ||
"welcome_message" BLOB NOT NULL, | ||
FOREIGN KEY (group_id) REFERENCES groups(id) | ||
); | ||
|
||
CREATE INDEX outbound_welcome_messages_commit_hash ON outbound_welcome_messages(commit_hash, state); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
use super::schema::groups; | ||
use diesel::prelude::*; | ||
|
||
#[derive(Insertable, Identifiable, Queryable, Debug, Clone)] | ||
#[diesel(table_name = groups)] | ||
#[diesel(primary_key(id))] | ||
pub struct StoredGroup { | ||
pub id: Vec<u8>, | ||
pub created_at_ns: i64, | ||
pub membership_state: i32, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
use super::schema::group_intents; | ||
use diesel::prelude::*; | ||
|
||
#[derive(Queryable, Identifiable, Debug, Clone)] | ||
#[diesel(table_name = group_intents)] | ||
#[diesel(primary_key(id))] | ||
pub struct StoredGroupIntent { | ||
pub id: i32, | ||
pub kind: i32, | ||
pub state: i32, | ||
pub group_id: Vec<u8>, | ||
pub data: Vec<u8>, | ||
pub message_hash: Option<Vec<u8>>, | ||
} | ||
|
||
#[derive(Insertable, Debug, Clone)] | ||
#[diesel(table_name = group_intents)] | ||
pub struct NewGroupIntent { | ||
pub kind: i32, | ||
pub state: i32, | ||
pub group_id: Vec<u8>, | ||
pub data: Vec<u8>, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
use super::schema::group_messages; | ||
use diesel::prelude::*; | ||
|
||
#[derive(Insertable, Identifiable, Queryable, Debug, Clone)] | ||
#[diesel(table_name = group_messages)] | ||
#[diesel(primary_key(id))] | ||
pub struct StoredGroupMessage { | ||
pub id: Vec<u8>, | ||
pub group_id: Vec<u8>, | ||
pub decrypted_message_bytes: Vec<u8>, | ||
pub sent_at_ns: i64, | ||
pub sender_installation_id: Vec<u8>, | ||
pub sender_wallet_address: String, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
use super::DbConnection; | ||
neekolas marked this conversation as resolved.
Show resolved
Hide resolved
|
||
use super::{schema::openmls_key_store, StorageError}; | ||
use crate::{Delete, Fetch, Store}; | ||
use diesel::prelude::*; | ||
|
||
#[derive(Insertable, Queryable, Debug, Clone)] | ||
#[diesel(table_name = openmls_key_store)] | ||
#[diesel(primary_key(key_bytes))] | ||
pub struct StoredKeyStoreEntry { | ||
pub key_bytes: Vec<u8>, | ||
pub value_bytes: Vec<u8>, | ||
} | ||
|
||
impl Store<DbConnection> for StoredKeyStoreEntry { | ||
fn store(&self, into: &mut DbConnection) -> Result<(), StorageError> { | ||
diesel::insert_into(openmls_key_store::table) | ||
.values(self) | ||
.execute(into)?; | ||
|
||
Ok(()) | ||
} | ||
} | ||
|
||
impl Fetch<StoredKeyStoreEntry> for DbConnection { | ||
type Key = Vec<u8>; | ||
fn fetch(&mut self, key: Vec<u8>) -> Result<Option<StoredKeyStoreEntry>, StorageError> where { | ||
use super::schema::openmls_key_store::dsl::*; | ||
Ok(openmls_key_store.find(key).first(self).optional()?) | ||
} | ||
} | ||
|
||
impl Delete<StoredKeyStoreEntry> for DbConnection { | ||
type Key = Vec<u8>; | ||
fn delete(&mut self, key: Vec<u8>) -> Result<usize, StorageError> where { | ||
use super::schema::openmls_key_store::dsl::*; | ||
Ok(diesel::delete(openmls_key_store.filter(key_bytes.eq(key))).execute(self)?) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
are we re-using some of the tables from XMTP?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This schema below is all we have a need for at the moment. Because OpenMLS has its own database structure with the KeyValue store, some of the tables from the old
xmtp
schema simply aren't necessary.