Skip to content
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

feat(sdk)!: support errors in mock #2277

Open
wants to merge 55 commits into
base: v1.6-dev-ugly
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 45 commits
Commits
Show all changes
55 commits
Select commit Hold shift + click to select a range
7a77ccf
feat(sdk): provide request execution information
shumkov Oct 21, 2024
b85f52e
test: fix compilation errors
shumkov Oct 21, 2024
6de23ab
refactor: rename `unwrap` to `into_inner`
shumkov Oct 21, 2024
fba6ef8
refactor: rename response to inner
shumkov Oct 21, 2024
8af6776
test: increase timeout to pass the test
shumkov Oct 21, 2024
8e278e3
fix: `into_inner` doesn't need `CanRetry` bound
shumkov Oct 21, 2024
5af8822
fix: errored address is none
shumkov Oct 21, 2024
8269c1f
Merge branch 'v1.4-dev' into feat/sdk/execution_info
shumkov Oct 22, 2024
a9a96a0
chore: don't use empty uri
shumkov Oct 22, 2024
920b50d
docs: fix usage example
shumkov Oct 22, 2024
067db87
refactor: remove unused import
shumkov Oct 22, 2024
c684b3f
chore: update test vectors
shumkov Oct 22, 2024
9da89a8
chore: more test vectors
shumkov Oct 22, 2024
34ffdfb
feat(sdk): retry failed requests on sdk level
lklimek Oct 21, 2024
60d220c
fix(dapi-client): impl VersionedGrpcResponse for ExecutionResponse
lklimek Oct 22, 2024
80d19e0
feat(sdk): fech with retries
lklimek Oct 23, 2024
f78555e
chore(sdk): remove unnecessary changes
lklimek Oct 24, 2024
4343b6e
fix(sdk): correct retry logic
lklimek Oct 24, 2024
50ad84c
feat(sdk): retry impl
lklimek Oct 24, 2024
e4e667a
chore: self-review
lklimek Oct 24, 2024
4bdf0cd
refactor(sdk): mocks accept errors
lklimek Oct 22, 2024
5eac233
chore: testing
lklimek Oct 23, 2024
dfaaa4c
feat(sdk): retry impl
lklimek Oct 24, 2024
ac388a8
chore: apply feedback
lklimek Oct 25, 2024
016450f
chore: remove outdated comment
lklimek Oct 25, 2024
0738f76
chore(sdk): impl<T> From<ExecutionError<T>> for Error
lklimek Oct 25, 2024
438185f
doc(sdk): comment on sync::retry() logic
lklimek Oct 25, 2024
170d5a9
chore: self review
lklimek Oct 25, 2024
e20a29d
refactor: remove unused code
lklimek Oct 25, 2024
c9c6c2f
chore: fmt
lklimek Oct 25, 2024
e5e17a7
chore: doc
lklimek Oct 25, 2024
dace7fa
test: fix retry test
lklimek Oct 25, 2024
eca69f8
refactor(sdk): apply feedback
lklimek Oct 25, 2024
df706f4
test(sdk): add edge case to test_retry
lklimek Oct 25, 2024
1f954d5
fix: use settings in fetch_many
lklimek Oct 25, 2024
034bdb2
test(sdk): regenerate test vectors
lklimek Oct 28, 2024
1bc31a9
refactor: re-export specific types
shumkov Oct 28, 2024
bb21a21
fix(dapi-client): correcrlt serialize ExecutionResult and ExecutionEr…
lklimek Oct 28, 2024
5a497ca
Merge remote-tracking branch 'origin/feat/sdk/execution_info' into fe…
lklimek Oct 28, 2024
723b1c5
chore: fixes after merge
lklimek Oct 28, 2024
098f854
Merge remote-tracking branch 'origin/feat/sdk-retry' into refactor/sd…
lklimek Oct 28, 2024
88dda82
chore: fixes after merge + add InnerInto impl to mocks
lklimek Oct 28, 2024
cebc709
Merge remote-tracking branch 'origin/v1.4-dev' into feat/sdk-retry
lklimek Oct 28, 2024
a0b2427
Merge branch 'feat/sdk-retry' into refactor/sdk-mock-errors
lklimek Oct 28, 2024
4f2bb21
fix(sdk): option not serialized properly (work in progress)
lklimek Oct 28, 2024
100147b
build(deps): optimize mocks feature flag
lklimek Oct 29, 2024
38f073a
refactor(dapi-grpc): no default Mockable impl
lklimek Oct 29, 2024
bd7499d
chore: minor linter fixes
lklimek Oct 29, 2024
98bebee
Merge remote-tracking branch 'origin/v1.4-dev' into refactor/sdk-mock…
lklimek Oct 29, 2024
65cdabd
chore: test vector configs
lklimek Oct 29, 2024
e5f827c
chore: remove unneeded import
lklimek Oct 29, 2024
905955e
chore: typos
lklimek Oct 29, 2024
40963dc
chore: apply review feedback
lklimek Oct 30, 2024
bf07db8
Merge remote-tracking branch 'origin/v1.4-dev' into refactor/sdk-mock…
lklimek Oct 30, 2024
c1f972b
chore: regenerate test vectors
lklimek Oct 30, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

79 changes: 77 additions & 2 deletions packages/dapi-grpc/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,12 +77,37 @@ where
impl<T: Mockable> Mockable for Option<T> {
#[cfg(feature = "mocks")]
fn mock_serialize(&self) -> Option<Vec<u8>> {
self.as_ref().and_then(|value| value.mock_serialize())
let data = match self {
None => vec![0],
Some(value) => {
let mut data = vec![1]; // we return None if value does not support serialization
let mut serialized = value.mock_serialize()?;
data.append(&mut serialized);

data
}
};

Some(data)
}

#[cfg(feature = "mocks")]
fn mock_deserialize(data: &[u8]) -> Option<Self> {
T::mock_deserialize(data).map(Some)
if data.is_empty() {
panic!("empty data");
}

match data[0] {
0 => Some(None),
// Mind double Some - first says mock_deserialize is implemented, second is deserialized value
1 => Some(Some(
T::mock_deserialize(&data[1..]).expect("unable to deserialize Option<T>"),
)),
_ => panic!(
"unsupported first byte for Option<T>::mock_deserialize: {:x}",
data[0]
),
}
lklimek marked this conversation as resolved.
Show resolved Hide resolved
}
}

Expand All @@ -97,6 +122,29 @@ impl Mockable for Vec<u8> {
serde_json::from_slice(data).ok()
}
}

impl<T: Mockable> Mockable for Vec<T> {
#[cfg(feature = "mocks")]
fn mock_serialize(&self) -> Option<Vec<u8>> {
let data: Vec<Vec<u8>> = self
.iter()
.map(|d| d.mock_serialize())
.collect::<Option<Vec<Vec<u8>>>>()?;

Some(serde_json::to_vec(&data).expect("unable to serialize Vec<T>"))
}

#[cfg(feature = "mocks")]
fn mock_deserialize(data: &[u8]) -> Option<Self> {
let data: Vec<Vec<u8>> =
serde_json::from_slice(data).expect("unable to deserialize Vec<T>");

data.into_iter()
.map(|d| T::mock_deserialize(&d))
.collect::<Option<Vec<T>>>()
}
}
lklimek marked this conversation as resolved.
Show resolved Hide resolved

#[cfg(feature = "mocks")]
#[derive(serde::Serialize, serde::Deserialize)]
struct MockableStatus {
Expand Down Expand Up @@ -128,3 +176,30 @@ impl Mockable for crate::tonic::Status {
/// This will return `None` on serialization,
/// effectively disabling mocking of streaming responses.
impl<T: Mockable> Mockable for Streaming<T> {}

/// Mocking of primitive types - just serialize them as little-endian bytes.
///
/// This is useful for mocking of messages that contain primitive types.
macro_rules! mockable_number {
($($t:ty),*) => {
$(
impl Mockable for $t {
#[cfg(feature = "mocks")]
fn mock_serialize(&self) -> Option<Vec<u8>> {
(*self).to_le_bytes().to_vec().mock_serialize()
}

#[cfg(feature = "mocks")]
fn mock_deserialize(data: &[u8]) -> Option<Self> {
let data: Vec<u8> = Mockable::mock_deserialize(data)?;
Some(Self::from_le_bytes(
data.try_into().expect("invalid serialized data"),
))
}
}
)*
};
}

// No `u8` as it would cause conflict between Vec<u8> and Vec<T: Mockable> impls.
mockable_number!(usize, u16, u32, u64, u128, isize, i8, i16, i32, i64, i128);
lklimek marked this conversation as resolved.
Show resolved Hide resolved
3 changes: 1 addition & 2 deletions packages/rs-dapi-client/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ edition = "2021"

[features]
default = ["mocks", "offline-testing"]
tokio-sleep = ["backon/tokio-sleep"]
mocks = [
"dep:sha2",
"dep:hex",
Expand All @@ -20,7 +19,7 @@ dump = ["mocks"]
offline-testing = []

[dependencies]
backon = { version = "1.2"}
backon = { version = "1.2", features = ["tokio-sleep"] }
lklimek marked this conversation as resolved.
Show resolved Hide resolved
dapi-grpc = { path = "../dapi-grpc", features = [
"core",
"platform",
Expand Down
13 changes: 13 additions & 0 deletions packages/rs-dapi-client/src/address_list.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
//! Subsystem to manage DAPI nodes.

use chrono::Utc;
use dapi_grpc::mock::Mockable;
use dapi_grpc::tonic::codegen::http;
use dapi_grpc::tonic::transport::Uri;
use rand::{rngs::SmallRng, seq::IteratorRandom, SeedableRng};
Expand Down Expand Up @@ -59,6 +60,18 @@ impl From<Uri> for Address {
}
}

impl Mockable for Address {
#[cfg(feature = "mocks")]
fn mock_serialize(&self) -> Option<Vec<u8>> {
Some(serde_json::to_vec(self).expect("unable to serialize Address"))
}

#[cfg(feature = "mocks")]
fn mock_deserialize(data: &[u8]) -> Option<Self> {
Some(serde_json::from_slice(data).expect("unable to deserialize Address"))
}
}
lklimek marked this conversation as resolved.
Show resolved Hide resolved

impl Address {
/// Ban the [Address] so it won't be available through [AddressList::get_live_address] for some time.
fn ban(&mut self, base_ban_period: &Duration) {
Expand Down
96 changes: 90 additions & 6 deletions packages/rs-dapi-client/src/executor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,24 @@ pub trait DapiRequestExecutor {
<R::Client as TransportClient>::Error: Mockable;
}

/// Unwrap wrapped types
pub trait IntoInner<T> {
/// Unwrap the inner type.
///
/// This function returns inner type, dropping additional context information.
/// It is lossy operation, so it should be used with caution.
lklimek marked this conversation as resolved.
Show resolved Hide resolved
fn into_inner(self) -> T;
lklimek marked this conversation as resolved.
Show resolved Hide resolved
}

/// Convert inner type without loosing additional context information of the wrapper.
pub trait InnerInto<T> {
/// Convert inner type without loosing additional context information of the wrapper.
fn inner_into(self) -> T;
}

/// Error happened during request execution.
#[derive(Debug, Clone, thiserror::Error, Eq, PartialEq)]
#[cfg_attr(feature = "mocks", derive(serde::Serialize, serde::Deserialize))]
lklimek marked this conversation as resolved.
Show resolved Hide resolved
#[error("{inner}")]
pub struct ExecutionError<E> {
/// The cause of error
Expand All @@ -31,10 +47,27 @@ pub struct ExecutionError<E> {
pub address: Option<Address>,
}

impl<E> ExecutionError<E> {
impl<F, T> InnerInto<ExecutionError<T>> for ExecutionError<F>
where
F: Into<T>,
{
/// Convert inner error type without loosing retries and address
fn inner_into(self) -> ExecutionError<T> {
ExecutionError {
inner: self.inner.into(),
retries: self.retries,
address: self.address,
}
}
}

impl<E, I> IntoInner<I> for ExecutionError<E>
where
E: Into<I>,
{
/// Unwrap the error cause
pub fn into_inner(self) -> E {
self.inner
fn into_inner(self) -> I {
self.inner.into()
}
}

Expand All @@ -46,6 +79,7 @@ impl<E: CanRetry> CanRetry for ExecutionError<E> {

/// Request execution response.
#[derive(Debug, Clone, Eq, PartialEq)]
#[cfg_attr(feature = "mocks", derive(serde::Serialize, serde::Deserialize))]
pub struct ExecutionResponse<R> {
/// The response from the request
pub inner: R,
Expand All @@ -55,12 +89,62 @@ pub struct ExecutionResponse<R> {
pub address: Address,
}

impl<R> ExecutionResponse<R> {
#[cfg(feature = "mocks")]
impl<R: Default> Default for ExecutionResponse<R> {
fn default() -> Self {
Self {
retries: Default::default(),
address: "http://127.0.0.1".parse().expect("create mock address"),
inner: Default::default(),
}
}
}
lklimek marked this conversation as resolved.
Show resolved Hide resolved

impl<R, I> IntoInner<I> for ExecutionResponse<R>
where
R: Into<I>,
{
/// Unwrap the response
pub fn into_inner(self) -> R {
self.inner
fn into_inner(self) -> I {
self.inner.into()
}
}

impl<F, T> InnerInto<ExecutionResponse<T>> for ExecutionResponse<F>
where
F: Into<T>,
{
/// Convert inner response type without loosing retries and address
fn inner_into(self) -> ExecutionResponse<T> {
ExecutionResponse {
inner: self.inner.into(),
retries: self.retries,
address: self.address,
}
}
}

/// Result of request execution
pub type ExecutionResult<R, E> = Result<ExecutionResponse<R>, ExecutionError<E>>;

impl<R, E> IntoInner<Result<R, E>> for ExecutionResult<R, E> {
fn into_inner(self) -> Result<R, E> {
match self {
Ok(response) => Ok(response.into_inner()),
Err(error) => Err(error.into_inner()),
}
}
}

impl<F, FE, T, TE> InnerInto<ExecutionResult<T, TE>> for ExecutionResult<F, FE>
where
F: Into<T>,
FE: Into<TE>,
{
fn inner_into(self) -> ExecutionResult<T, TE> {
match self {
Ok(response) => Ok(response.inner_into()),
Err(error) => Err(error.inner_into()),
}
}
}
4 changes: 3 additions & 1 deletion packages/rs-dapi-client/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ pub use dapi_client::{DapiClient, DapiClientError};
use dapi_grpc::mock::Mockable;
#[cfg(feature = "dump")]
pub use dump::DumpData;
pub use executor::{DapiRequestExecutor, ExecutionError, ExecutionResponse, ExecutionResult};
pub use executor::{
DapiRequestExecutor, ExecutionError, ExecutionResponse, ExecutionResult, InnerInto, IntoInner,
};
use futures::{future::BoxFuture, FutureExt};
pub use request_settings::RequestSettings;

Expand Down
Loading