Skip to content

Commit

Permalink
312: introduce remote container development environment
Browse files Browse the repository at this point in the history
- devcontainer
- tasks.json
- github ci-image workflow
- fmt and fix fmt errors
  • Loading branch information
jac18281828 committed Nov 6, 2023
1 parent 129e3c4 commit c78f0ed
Show file tree
Hide file tree
Showing 12 changed files with 239 additions and 28 deletions.
26 changes: 26 additions & 0 deletions .devcontainer/devcontainer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// For format details, see https://aka.ms/devcontainer.json. For config options, see the
// README at: https://github.com/devcontainers/templates/tree/main/src/docker-existing-dockerfile
{
"name": "Existing Dockerfile",
"build": {
// Sets the run context to one level up instead of the .devcontainer folder.
"context": "..",
// Update the 'dockerFile' property if you aren't using the standard 'Dockerfile' filename.
"dockerfile": "../Dockerfile"
}

// Features to add to the dev container. More info: https://containers.dev/features.
// "features": {},

// Use 'forwardPorts' to make a list of ports inside the container available locally.
// "forwardPorts": [],

// Uncomment the next line to run commands after the container is created.
// "postCreateCommand": "cat /etc/os-release",

// Configure tool-specific properties.
// "customizations": {},

// Uncomment to connect as an existing user other than the container default. More info: https://aka.ms/dev-containers-non-root.
// "remoteUser": "devcontainer"
}
33 changes: 33 additions & 0 deletions .github/workflows/ci-image.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
name: Build Dev Image CI

on:
workflow_dispatch:
pull_request:
branches:
- main
push:
branches:
- "*"

jobs:
build:
runs-on: ubuntu-latest
steps:
-
name: Checkout
uses: actions/checkout@v3
-
name: Set up QEMU
uses: docker/setup-qemu-action@v2
-
name: Set up Docker Buildx
uses: docker/setup-buildx-action@v2

- name: Build and Push
uses: docker/build-push-action@v3
with:
context: .
platforms: linux/amd64
push: false
build-args: |
VERSION=latest
5 changes: 5 additions & 0 deletions .vscode/extensions.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"recommendations": [
"rust-lang.rust-analyzer"
]
}
55 changes: 55 additions & 0 deletions .vscode/tasks.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "2.0.0",
"tasks": [
{
"label": "fmt",
"type": "shell",
"command": "cargo +nightly fmt --check",
"options": {
"cwd": "${workspaceFolder}"
},
"group": {
"kind": "build",
"isDefault": "false"
}
},
{
"label": "build",
"type": "shell",
"command": "cargo build",
"options": {
"cwd": "${workspaceFolder}"
},
"group": {
"kind": "build",
"isDefault": "false"
}
},
{
"label": "check",
"type": "shell",
"command": "cargo check",
"options": {
"cwd": "${workspaceFolder}"
},
"group": {
"kind": "build",
"isDefault": "true"
}
},
{
"label": "test",
"type": "shell",
"command": "cargo test",
"options": {
"cwd": "${workspaceFolder}"
},
"group": {
"kind": "test",
"isDefault": "true"
}
}
]
}
86 changes: 86 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
FROM debian:stable-slim as go-builder
# defined from build kit
# DOCKER_BUILDKIT=1 docker build . -t ...
ARG TARGETARCH

FROM debian:stable-slim as builder
# defined from build kit
# DOCKER_BUILDKIT=1 docker build . -t ...
ARG TARGETARCH

RUN export DEBIAN_FRONTEND=noninteractive && \
apt update && \
apt install -y -q --no-install-recommends \
git curl gnupg2 build-essential \
linux-headers-${TARGETARCH} libc6-dev \
openssl libssl-dev pkg-config \
ca-certificates apt-transport-https \
python3 && \
apt clean && \
rm -rf /var/lib/apt/lists/*

RUN useradd --create-home -s /bin/bash xmtp
RUN usermod -a -G sudo xmtp
RUN echo '%xmtp ALL=(ALL) NOPASSWD:ALL' >> /etc/sudoers

WORKDIR /rustup
## Rust
ADD https://sh.rustup.rs /rustup/rustup.sh
RUN chmod 755 /rustup/rustup.sh

ENV USER=xmtp
USER xmtp
RUN /rustup/rustup.sh -y --default-toolchain stable --profile minimal

ENV PATH=$PATH:~xmtp/.cargo/bin

FROM debian:stable-slim
ARG TARGETARCH

RUN export DEBIAN_FRONTEND=noninteractive && \
apt update && \
apt install -y -q --no-install-recommends \
ca-certificates apt-transport-https \
sudo ripgrep procps build-essential \
python3 python3-pip python3-dev git && \
apt clean && \
rm -rf /var/lib/apt/lists/*

RUN echo "building platform $(uname -m)"

RUN useradd --create-home -s /bin/bash xmtp
RUN usermod -a -G sudo xmtp
RUN echo '%sudo ALL=(ALL) NOPASSWD:ALL' >> /etc/sudoers

## Rust
COPY --chown=xmtp:xmtp --from=builder /home/xmtp/.cargo /home/xmtp/.cargo
COPY --chown=xmtp:xmtp --from=builder /home/xmtp/.rustup /home/xmtp/.rustup

USER xmtp

# fmt coming from nightly
RUN ~xmtp/.cargo/bin/rustup toolchain install nightly
RUN ~xmtp/.cargo/bin/rustup component add rustfmt --toolchain nightly

WORKDIR /workspaces/libxmtp
COPY --chown=xmtp:xmtp . .

ENV PATH=~xmtp/.cargo/bin:$PATH
ENV USER=xmtp

RUN ~xmtp/.cargo/bin/cargo check
RUN ~xmtp/.cargo/bin/cargo +nightly --version
RUN ~xmtp/.cargo/bin/cargo +nightly fmt --check
# some tests are setup as integration tests 👀 xmtp_mls
RUN for crate in xmtp xmtp_api_grpc xmtp_api_grpc_gateway xmtp_cryptography xmtp_proto xmtp_v2; do cd ${crate}; ~xmtp/.cargo/bin/cargo test; done

LABEL org.label-schema.build-date=$BUILD_DATE \
org.label-schema.name="rustdev" \
org.label-schema.description="Rust Development Container" \
org.label-schema.url="https://github.com/xmtp/libxmtp" \
org.label-schema.vcs-ref=$VCS_REF \
org.label-schema.vcs-url="[email protected]:xmtp/libxmtp.git" \
org.label-schema.vendor="xmtp" \
org.label-schema.version=$VERSION \
org.label-schema.schema-version="1.0" \
org.opencontainers.image.description="Rust Development Container"
1 change: 0 additions & 1 deletion xmtp/src/storage/encrypted_store/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
//! if there are any outstanding database migrations and perform them as needed. When updating the
//! table definitions `schema.rs` must also be updated. To generate the correct schemas you can run
//! `diesel print-schema` or use `cargo run update-schema` which will update the files for you.
//!
pub mod models;
pub mod schema;
Expand Down
3 changes: 1 addition & 2 deletions xmtp_mls/src/storage/encrypted_store/group.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@ use diesel::{
};

use super::schema::groups;
use crate::impl_fetch;
use crate::impl_store;
use crate::{impl_fetch, impl_store};

/// The Group ID type.
pub type ID = Vec<u8>;
Expand Down
46 changes: 29 additions & 17 deletions xmtp_mls/src/storage/encrypted_store/group_intent.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
use super::schema::group_intents::dsl;
use super::{group, schema::group_intents};
use super::{DbConnection, EncryptedMessageStore};
use crate::storage::StorageError;
use crate::{impl_fetch, impl_store};

use diesel::prelude::*;
use diesel::{
backend::Backend,
deserialize::{self, FromSql, FromSqlRow},
expression::AsExpression,
prelude::*,
serialize::{self, IsNull, Output, ToSql},
sql_types::Integer,
sqlite::Sqlite,
};

use super::{
group,
schema::{group_intents, group_intents::dsl},
DbConnection, EncryptedMessageStore,
};
use crate::{impl_fetch, impl_store, storage::StorageError};

pub type ID = i32;

#[repr(i32)]
Expand Down Expand Up @@ -98,7 +99,8 @@ impl EncryptedMessageStore {
Ok(query.load::<StoredGroupIntent>(conn)?)
}

// Set the intent with the given ID to `Published` and set the payload hash. Optionally add `post_commit_data`
// Set the intent with the given ID to `Published` and set the payload hash. Optionally add
// `post_commit_data`
pub fn set_group_intent_published(
&self,
conn: &mut DbConnection,
Expand All @@ -108,7 +110,8 @@ impl EncryptedMessageStore {
) -> Result<(), StorageError> {
let res = diesel::update(dsl::group_intents)
.filter(dsl::id.eq(intent_id))
// State machine requires that the only valid state transition to Published is from ToPublish
// State machine requires that the only valid state transition to Published is from
// ToPublish
.filter(dsl::state.eq(IntentState::ToPublish))
.set((
dsl::state.eq(IntentState::Published),
Expand All @@ -132,7 +135,8 @@ impl EncryptedMessageStore {
) -> Result<(), StorageError> {
let res = diesel::update(dsl::group_intents)
.filter(dsl::id.eq(intent_id))
// State machine requires that the only valid state transition to Committed is from Published
// State machine requires that the only valid state transition to Committed is from
// Published
.filter(dsl::state.eq(IntentState::Published))
.set(dsl::state.eq(IntentState::Committed))
.execute(conn)?;
Expand All @@ -144,15 +148,17 @@ impl EncryptedMessageStore {
}
}

// Set the intent with the given ID to `ToPublish`. Wipe any values for `payload_hash` and `post_commit_data`
// Set the intent with the given ID to `ToPublish`. Wipe any values for `payload_hash` and
// `post_commit_data`
pub fn set_group_intent_to_publish(
&self,
conn: &mut DbConnection,
intent_id: ID,
) -> Result<(), StorageError> {
let res = diesel::update(dsl::group_intents)
.filter(dsl::id.eq(intent_id))
// State machine requires that the only valid state transition to ToPublish is from Published
// State machine requires that the only valid state transition to ToPublish is from
// Published
.filter(dsl::state.eq(IntentState::Published))
.set((
dsl::state.eq(IntentState::ToPublish),
Expand All @@ -169,7 +175,8 @@ impl EncryptedMessageStore {
}
}

// Simple lookup of intents by payload hash, meant to be used when processing messages off the network
// Simple lookup of intents by payload hash, meant to be used when processing messages off the
// network
pub fn find_group_intent_by_payload_hash(
&self,
conn: &mut DbConnection,
Expand Down Expand Up @@ -236,17 +243,22 @@ where
#[cfg(test)]
mod tests {
use super::*;
use crate::storage::encrypted_store::group::{GroupMembershipState, StoredGroup};
use crate::storage::encrypted_store::tests::{rand_vec, with_store};
use crate::{Fetch, Store};
use crate::{
storage::encrypted_store::{
group::{GroupMembershipState, StoredGroup},
tests::{rand_vec, with_store},
},
Fetch, Store,
};

fn insert_group(conn: &mut DbConnection, group_id: Vec<u8>) {
let group = StoredGroup::new(group_id, 100, GroupMembershipState::Allowed);
group.store(conn).unwrap();
}

impl NewGroupIntent {
// Real group intents must always start as ToPublish. But for tests we allow forcing the state
// Real group intents must always start as ToPublish. But for tests we allow forcing the
// state
pub fn new_test(
kind: IntentKind,
group_id: Vec<u8>,
Expand Down
3 changes: 1 addition & 2 deletions xmtp_mls/src/storage/encrypted_store/group_message.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
use diesel::prelude::*;

use super::schema::group_messages;
use crate::impl_fetch;
use crate::impl_store;
use crate::{impl_fetch, impl_store};

#[derive(Insertable, Identifiable, Queryable, Debug, Clone)]
#[diesel(table_name = group_messages)]
Expand Down
1 change: 0 additions & 1 deletion xmtp_mls/src/storage/encrypted_store/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
//! if there are any outstanding database migrations and perform them as needed. When updating the
//! table definitions `schema.rs` must also be updated. To generate the correct schemas you can run
//! `diesel print-schema` or use `cargo run update-schema` which will update the files for you.
//!
pub mod group;
pub mod group_intent;
Expand Down
3 changes: 1 addition & 2 deletions xmtp_mls/src/storage/encrypted_store/topic_refresh_state.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
use diesel::prelude::*;

use super::schema::topic_refresh_state;
use crate::impl_fetch;
use crate::impl_store;
use crate::{impl_fetch, impl_store};

#[derive(Insertable, Identifiable, Queryable, Debug, Clone)]
#[diesel(table_name = topic_refresh_state)]
Expand Down
5 changes: 2 additions & 3 deletions xmtp_v2/src/signature.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,8 @@ impl traits::SignatureVerifier<EcdsaSignature> for PublicKey {
verifying_key
.verify_digest(digest, &signature)
.map_err(|e| e.to_string())
}
// The idea for unsupported types is to uncomment this catch-all
// _ => Err("Unsupported signature type for k256 public key".to_string()),
} /* The idea for unsupported types is to uncomment this catch-all
* _ => Err("Unsupported signature type for k256 public key".to_string()), */
}
}
}

0 comments on commit c78f0ed

Please sign in to comment.