Skip to content

Commit a4605f7

Browse files
committed
Add Vss KVStore api.
1 parent 62e1322 commit a4605f7

File tree

4 files changed

+119
-0
lines changed

4 files changed

+119
-0
lines changed

rust/api/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ version = "0.1.0"
44
edition = "2021"
55

66
[dependencies]
7+
async-trait = "0.1.77"
78
prost = { version = "0.11.6", default-features = false, features = ["std", "prost-derive"] }
89
bytes = "1.4.0"
910

rust/api/src/error.rs

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
use std::error::Error;
2+
use std::fmt::{Display, Formatter};
3+
use std::io;
4+
/// When there is an error while writing to VSS storage, the response contains a relevant error code.
5+
/// A mapping for VSS server error codes. Refer to [`ErrorResponse`] docs for more
6+
/// information regarding each error code and corresponding use-cases.
7+
///
8+
/// [`ErrorResponse`]: crate::types::ErrorResponse
9+
#[derive(Debug)]
10+
pub enum VssError {
11+
/// Please refer to [`ErrorCode::NoSuchKeyException`].
12+
///
13+
/// [`ErrorCode::NoSuchKeyException`]: crate::types::ErrorCode::NoSuchKeyException
14+
NoSuchKeyError(String),
15+
16+
/// Please refer to [`ErrorCode::InvalidRequestException`].
17+
///
18+
/// [`ErrorCode::InvalidRequestException`]: crate::types::ErrorCode::InvalidRequestException
19+
InvalidRequestError(String),
20+
21+
/// Please refer to [`ErrorCode::ConflictException`].
22+
///
23+
/// [`ErrorCode::ConflictException`]: crate::types::ErrorCode::ConflictException
24+
ConflictError(String),
25+
26+
/// Please refer to [`ErrorCode::AuthException`].
27+
///
28+
/// [`ErrorCode::AuthException`]: crate::types::ErrorCode::AuthException
29+
AuthError(String),
30+
31+
/// Please refer to [`ErrorCode::InternalServerException`].
32+
///
33+
/// [`ErrorCode::InternalServerException`]: crate::types::ErrorCode::InternalServerException
34+
InternalServerError(String),
35+
}
36+
37+
impl Display for VssError {
38+
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
39+
match self {
40+
VssError::NoSuchKeyError(message) => {
41+
write!(f, "Requested key does not exist: {}", message)
42+
},
43+
VssError::InvalidRequestError(message) => {
44+
write!(f, "Request sent to VSS was invalid: {}", message)
45+
},
46+
VssError::ConflictError(message) => {
47+
write!(f, "Version conflict in write operation: {}", message)
48+
},
49+
VssError::AuthError(message) => {
50+
write!(f, "Authentication or Authorization failure: {}", message)
51+
},
52+
VssError::InternalServerError(message) => {
53+
write!(f, "InternalServerError: {}", message)
54+
},
55+
}
56+
}
57+
}
58+
59+
impl Error for VssError {}
60+
61+
impl From<io::Error> for VssError {
62+
fn from(err: io::Error) -> Self {
63+
VssError::InternalServerError(err.to_string())
64+
}
65+
}

rust/api/src/kv_store.rs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
use crate::error::VssError;
2+
use crate::types::{
3+
DeleteObjectRequest, DeleteObjectResponse, GetObjectRequest, GetObjectResponse,
4+
ListKeyVersionsRequest, ListKeyVersionsResponse, PutObjectRequest, PutObjectResponse,
5+
};
6+
use async_trait::async_trait;
7+
8+
pub(crate) const GLOBAL_VERSION_KEY: &str = "global_version";
9+
pub(crate) const INITIAL_RECORD_VERSION: i32 = 1;
10+
11+
/// An interface that must be implemented by every backend implementation of VSS.
12+
#[async_trait]
13+
pub trait KvStore: Send + Sync {
14+
/// Retrieves an object based on the provided request and user token.
15+
async fn get(
16+
&self, user_token: String, request: GetObjectRequest,
17+
) -> Result<GetObjectResponse, VssError>;
18+
19+
/// Stores an object with the provided request and user token.
20+
async fn put(
21+
&self, user_token: String, request: PutObjectRequest,
22+
) -> Result<PutObjectResponse, VssError>;
23+
24+
/// Deletes an object based on the provided request and user token.
25+
async fn delete(
26+
&self, user_token: String, request: DeleteObjectRequest,
27+
) -> Result<DeleteObjectResponse, VssError>;
28+
29+
/// Lists the versions of keys based on the provided request and user token.
30+
async fn list_key_versions(
31+
&self, user_token: String, request: ListKeyVersionsRequest,
32+
) -> Result<ListKeyVersionsResponse, VssError>;
33+
}

rust/api/src/lib.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,21 @@
1+
//! Hosts API contract for VSS.
2+
//!
3+
//! VSS is an open-source project designed to offer a server-side cloud storage solution specifically
4+
//! tailored for noncustodial Lightning supporting mobile wallets. Its primary objective is to
5+
//! simplify the development process for Lightning wallets by providing a secure means to store
6+
//! and manage the essential state required for Lightning Network (LN) operations.
7+
8+
#![deny(rustdoc::broken_intra_doc_links)]
9+
#![deny(rustdoc::private_intra_doc_links)]
10+
#![deny(missing_docs)]
11+
12+
/// Implements the error type ([`error::VssError`]) which is eventually converted to [`ErrorResponse`] and returned to the client.
13+
///
14+
/// [`ErrorResponse`]: types::ErrorResponse
15+
pub mod error;
16+
17+
/// Contains [`kv_store::KvStore`] interface which needs to be implemented by every backend implementation of VSS.
18+
pub mod kv_store;
19+
20+
/// Contains request/response types generated from the API definition of VSS.
121
pub mod types;

0 commit comments

Comments
 (0)