-
Notifications
You must be signed in to change notification settings - Fork 49
feat(sdk)!: support errors in mock #2277
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
Changes from all commits
7a77ccf
b85f52e
6de23ab
fba6ef8
8af6776
8e278e3
5af8822
8269c1f
a9a96a0
920b50d
067db87
c684b3f
9da89a8
34ffdfb
60d220c
80d19e0
f78555e
4343b6e
50ad84c
e4e667a
4bdf0cd
5eac233
dfaaa4c
ac388a8
016450f
0738f76
438185f
170d5a9
e20a29d
c9c6c2f
e5e17a7
dace7fa
eca69f8
df706f4
1f954d5
034bdb2
1bc31a9
bb21a21
5a497ca
723b1c5
098f854
88dda82
cebc709
a0b2427
4f2bb21
100147b
38f073a
bd7499d
98bebee
65cdabd
e5f827c
905955e
40963dc
bf07db8
c1f972b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -257,32 +257,83 @@ impl Expectations { | |
|
|
||
| impl<R: Mockable> Mockable for ExecutionResponse<R> { | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We need to decided, either we keep mockable implementation in their modules, like address, or in mock, like execution types. I would say they should be in their modules otherwise the mock module will be a mess eventually.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. To be refactored in separate PR. |
||
| fn mock_serialize(&self) -> Option<Vec<u8>> { | ||
| R::mock_serialize(&self.inner) | ||
| // We encode data as vec![inner, address, retries] where each od them is serialized to bytes | ||
| let ser: Vec<Vec<u8>> = vec![ | ||
| self.inner | ||
| .mock_serialize() | ||
| .expect("unable to serialize ExecutionResponse inner"), | ||
| self.address | ||
| .mock_serialize() | ||
| .expect("unable to serialize ExecutionResponse address"), | ||
| (self.retries as u64) | ||
| .mock_serialize() | ||
| .expect("unable to serialize ExecutionResponse retries"), | ||
| ]; | ||
|
|
||
| ser.mock_serialize() | ||
| } | ||
|
|
||
| fn mock_deserialize(data: &[u8]) -> Option<Self> { | ||
| // TODO: We need serialize retries and address too | ||
| R::mock_deserialize(data).map(|inner| ExecutionResponse { | ||
| inner, | ||
| retries: 0, | ||
| address: "http://127.0.0.1:9000" | ||
| .parse() | ||
| .expect("failed to parse address"), | ||
| let deser: Vec<Vec<u8>> = | ||
| Mockable::mock_deserialize(data).expect("unable to deserialize ExecutionResponse"); | ||
|
|
||
| let [inner, address, retries] = &deser[0..] else { | ||
| // panics intentionally, as this is just for mocking, and we know these types can be mocked | ||
| // because they were serialized somehow :) | ||
| panic!( | ||
| "invalid ExecutionResponse data: expected 3 elements, got {}", | ||
| deser.len() | ||
| ); | ||
| }; | ||
lklimek marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| Some(Self { | ||
| inner: Mockable::mock_deserialize(inner) | ||
| .expect("unable to deserialize ExecutionResponse inner"), | ||
| address: Mockable::mock_deserialize(address) | ||
| .expect("unable to deserialize ExecutionResponse address"), | ||
| retries: Mockable::mock_deserialize(retries) | ||
| .expect("unable to deserialize ExecutionResponse retries"), | ||
lklimek marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| }) | ||
| } | ||
| } | ||
|
|
||
| impl<E: Mockable> Mockable for ExecutionError<E> { | ||
| fn mock_serialize(&self) -> Option<Vec<u8>> { | ||
| E::mock_serialize(&self.inner) | ||
| // We encode data as vec![inner, address, retries] where each od them is serialized to bytes | ||
| let ser: Vec<Vec<u8>> = vec![ | ||
| self.inner | ||
| .mock_serialize() | ||
| .expect("unable to serialize ExecutionError inner"), | ||
| self.address | ||
| .mock_serialize() | ||
| .expect("unable to serialize ExecutionError address"), | ||
| self.retries | ||
| .mock_serialize() | ||
| .expect("unable to serialize ExecutionError retries"), | ||
| ]; | ||
|
|
||
| ser.mock_serialize() | ||
| } | ||
|
|
||
| fn mock_deserialize(data: &[u8]) -> Option<Self> { | ||
| // TODO: We need serialize retries and address too | ||
| E::mock_deserialize(data).map(|inner| ExecutionError { | ||
| inner, | ||
| retries: 0, | ||
| address: None, | ||
| let deser: Vec<Vec<u8>> = | ||
| Mockable::mock_deserialize(data).expect("unable to deserialize ExecutionError"); | ||
|
|
||
| let [inner, address, retries] = &deser[0..] else { | ||
| // panics intentionally, as this is just for mocking, and we know these types can be mocked because they were | ||
| // serialized before | ||
| panic!( | ||
| "invalid ExecutionError data: expected 3 elements, got {}", | ||
| deser.len() | ||
| ); | ||
| }; | ||
| Some(Self { | ||
| inner: Mockable::mock_deserialize(inner) | ||
| .expect("unable to deserialize ExecutionError inner"), | ||
| address: Mockable::mock_deserialize(address) | ||
| .expect("unable to deserialize ExecutionError address"), | ||
| retries: Mockable::mock_deserialize(retries) | ||
| .expect("unable to deserialize ExecutionError retries"), | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,6 +2,7 @@ use dpp::ProtocolError; | |
|
|
||
| /// Errors | ||
| #[derive(Debug, thiserror::Error)] | ||
| #[cfg_attr(feature = "mocks", derive(bincode::Encode, bincode::Decode))] | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We use serde in dapi client
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's all under |
||
| #[allow(missing_docs)] | ||
| pub enum Error { | ||
| /// Not initialized | ||
|
|
@@ -86,6 +87,7 @@ pub enum Error { | |
|
|
||
| /// Errors returned by the context provider | ||
| #[derive(Debug, thiserror::Error)] | ||
| #[cfg_attr(feature = "mocks", derive(bincode::Encode, bincode::Decode))] | ||
| pub enum ContextProviderError { | ||
| /// Generic Context provider error | ||
| #[error("Context provider error: {0}")] | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.