-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add key_store table and generate * Implement store/fetch/delete traits * Add SQL key store * Use SQL key store for provider, remove in-memory key store * Use reference to encrypted store to allow multiple consumers * Tidy identity * Add identity table, queries, and unit tests * Persist and retrieve identity from storage inside builder * Tidy up names and types * Add tests and fix bugs * Fix lints * Update xmtp_mls/src/storage/encrypted_store/mod.rs Co-authored-by: Andrew Plaza <[email protected]> * Dont use the word Error in error names * Move identity initialization to identity strategy * Refactor DB stuff into multiple files * Delete models.rs * Cleanup * Add kind to group_messages * Update schema * Update schema * Update schema * Add ciphersuite back in as import --------- Co-authored-by: Richard Hua <[email protected]> Co-authored-by: Richard Hua <[email protected]> Co-authored-by: Andrew Plaza <[email protected]>
- Loading branch information
1 parent
36c2c64
commit 9c0984d
Showing
15 changed files
with
276 additions
and
88 deletions.
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; |
58 changes: 58 additions & 0 deletions
58
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,58 @@ | ||
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 | ||
); | ||
|
||
-- 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 ( | ||
-- Derived via SHA256(CONCAT(decrypted_message_bytes, conversation_id, timestamp)) | ||
"id" BLOB PRIMARY KEY NOT NULL, | ||
"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 | ||
"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. | ||
"payload_hash" BLOB, | ||
-- (Optional) data needed for the post-commit flow. For example, welcome messages | ||
"post_commit_data" BLOB, | ||
FOREIGN KEY (group_id) REFERENCES groups(id) | ||
); | ||
|
||
CREATE INDEX group_intents_group_id_id ON group_intents(group_id, id); |
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,24 @@ | ||
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 payload_hash: Option<Vec<u8>>, | ||
pub post_commit_data: 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; | ||
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.