Skip to content

Commit

Permalink
rest: return with an error when a fragment was rejected by the pool
Browse files Browse the repository at this point in the history
  • Loading branch information
eugene-babichenko committed May 4, 2021
1 parent 76907e8 commit 1a35cce
Show file tree
Hide file tree
Showing 6 changed files with 119 additions and 81 deletions.
88 changes: 48 additions & 40 deletions doc/api/v0.yaml
Expand Up @@ -613,48 +613,15 @@ paths:
'200':
description: Success
content:
text/plain:
application/json:
schema:
description: The information about whether a message was accepted or rejected
type: object
properties:
accepted:
description: IDs of accepted fragments
type: array
items:
type: string
format: '[0-9a-f]+'
rejected:
description: Detailed information about rejected fragments
type: array
items:
type: object
required:
- id
- reason
properties:
id:
description: The ID of a rejected fragment
type: string
format: '[0-9a-f]+'
pool_number:
description: The number of the pool that caused this error
type: integer
reason:
description: |
* `FragmentAlreadyInLog` -- this fragment was already processed by this node.
* `FragmentInvalid` -- this fragment failed validation.
* `PreviousFragmentInvalid` -- one of the previous fragments was rejected and `fail_fast` is enabled.
* `PoolOverflow` -- One of the pools rejected this fragment due to reaching the capacity limit.
type: string
enum:
- FragmentAlreadyInLog
- FragmentInvalid
- PreviousFragmentInvalid
- PoolOverflow
$ref: '#/components/schemas/FragmentsProcessingSummary'
'400':
description: Message is malformed

description: An error occurred when processing this request
content:
application/json:
schema:
$ref: '#/components/schemas/FragmentsProcessingSummary'
/api/v0/network/stats:
get:
description: Fetches network stats
Expand Down Expand Up @@ -2398,3 +2365,44 @@ paths:
'vote_start': { 'epoch': 0, 'slot_id': 0 },
},
]

components:
schemas:
FragmentsProcessingSummary:
description: The information about whether a message was accepted or rejected
type: object
properties:
accepted:
description: IDs of accepted fragments
type: array
items:
type: string
format: '[0-9a-f]+'
rejected:
description: Detailed information about rejected fragments
type: array
items:
type: object
required:
- id
- reason
properties:
id:
description: The ID of a rejected fragment
type: string
format: '[0-9a-f]+'
pool_number:
description: The number of the pool that caused this error
type: integer
reason:
description: |
* `FragmentAlreadyInLog` -- this fragment was already processed by this node.
* `FragmentInvalid` -- this fragment failed validation.
* `PreviousFragmentInvalid` -- one of the previous fragments was rejected and `fail_fast` is enabled.
* `PoolOverflow` -- One of the pools rejected this fragment due to reaching the capacity limit.
type: string
enum:
- FragmentAlreadyInLog
- FragmentInvalid
- PreviousFragmentInvalid
- PoolOverflow
86 changes: 47 additions & 39 deletions doc/api/v1.yaml
Expand Up @@ -43,46 +43,13 @@ paths:
content:
application/json:
schema:
description: The information about whether a message was accepted or rejected
type: object
properties:
accepted:
description: IDs of accepted fragments
type: array
items:
type: string
format: '[0-9a-f]+'
rejected:
description: Detailed information about rejected fragments
type: array
items:
type: object
required:
- id
- reason
properties:
id:
description: The ID of a rejected fragment
type: string
format: '[0-9a-f]+'
pool_number:
description: The number of the pool that caused this error
type: integer
reason:
description: |
* `FragmentAlreadyInLog` -- this fragment was already processed by this node.
* `FragmentInvalid` -- this fragment failed validation.
* `PreviousFragmentInvalid` -- one of the previous fragments was rejected and `fail_fast` is enabled.
* `PoolOverflow` -- One of the pools rejected this fragment due to reaching the capacity limit.
type: string
enum:
- FragmentAlreadyInLog
- FragmentInvalid
- PreviousFragmentInvalid
- PoolOverflow
$ref: '#/components/schemas/FragmentsProcessingSummary'
'400':
description: One of messages is malformed

description: An error occurred when processing this request
content:
application/json:
schema:
$ref: '#/components/schemas/FragmentsProcessingSummary'
/api/v1/fragments/statuses:
get:
description: Get statuses of fragments
Expand Down Expand Up @@ -273,3 +240,44 @@ paths:
},
},
]

components:
schemas:
FragmentsProcessingSummary:
description: The information about whether a message was accepted or rejected
type: object
properties:
accepted:
description: IDs of accepted fragments
type: array
items:
type: string
format: '[0-9a-f]+'
rejected:
description: Detailed information about rejected fragments
type: array
items:
type: object
required:
- id
- reason
properties:
id:
description: The ID of a rejected fragment
type: string
format: '[0-9a-f]+'
pool_number:
description: The number of the pool that caused this error
type: integer
reason:
description: |
* `FragmentAlreadyInLog` -- this fragment was already processed by this node.
* `FragmentInvalid` -- this fragment failed validation.
* `PreviousFragmentInvalid` -- one of the previous fragments was rejected and `fail_fast` is enabled.
* `PoolOverflow` -- One of the pools rejected this fragment due to reaching the capacity limit.
type: string
enum:
- FragmentAlreadyInLog
- FragmentInvalid
- PreviousFragmentInvalid
- PoolOverflow
9 changes: 8 additions & 1 deletion jormungandr/src/rest/v0/logic.rs
Expand Up @@ -77,6 +77,8 @@ pub enum Error {
InvalidTopic,
#[error(transparent)]
Hex(#[from] hex::FromHexError),
#[error("Could not process fragment")]
Fragment(FragmentsProcessingSummary),
}

fn parse_account_id(id_hex: &str) -> Result<Identifier, Error> {
Expand Down Expand Up @@ -143,7 +145,12 @@ pub async fn post_message(
reply_handle,
};
context.try_full()?.transaction_task.clone().try_send(msg)?;
reply_future.await.map_err(Into::into)
let reply = reply_future.await?;
if reply.rejected.is_empty() {
Ok(reply)
} else {
Err(Error::Fragment(reply))
}
}

pub async fn get_tip(context: &Context) -> Result<String, Error> {
Expand Down
4 changes: 4 additions & 0 deletions jormungandr/src/rest/v0/mod.rs
Expand Up @@ -270,6 +270,10 @@ async fn handle_rejection(err: Rejection) -> Result<impl Reply, Rejection> {
logic::Error::PublicKey(_) | logic::Error::Hash(_) | logic::Error::Hex(_) => {
(err.to_string(), StatusCode::BAD_REQUEST)
}
logic::Error::Fragment(summary) => (
serde_json::to_string(&summary).unwrap(),
StatusCode::BAD_REQUEST,
),
err => (
display_internal_server_error(err),
StatusCode::INTERNAL_SERVER_ERROR,
Expand Down
9 changes: 8 additions & 1 deletion jormungandr/src/rest/v1/logic.rs
Expand Up @@ -36,6 +36,8 @@ pub enum Error {
Storage(#[from] StorageError),
#[error(transparent)]
Hex(#[from] hex::FromHexError),
#[error("Could not process all fragments")]
Fragments(FragmentsProcessingSummary),
}

pub async fn get_fragment_statuses<'a>(
Expand Down Expand Up @@ -84,7 +86,12 @@ pub async fn post_fragments(
reply_handle,
};
msgbox.try_send(msg)?;
reply_future.await.map_err(Into::into)
let reply = reply_future.await?;
if reply.rejected.is_empty() {
Ok(reply)
} else {
Err(Error::Fragments(reply))
}
}

pub async fn get_fragment_logs(context: &Context) -> Result<Vec<FragmentLog>, Error> {
Expand Down
4 changes: 4 additions & 0 deletions jormungandr/src/rest/v1/mod.rs
Expand Up @@ -49,6 +49,10 @@ async fn handle_rejection(err: Rejection) -> Result<impl Reply, Rejection> {
logic::Error::PublicKey(_) | logic::Error::Hash(_) | logic::Error::Hex(_) => {
(err.to_string(), StatusCode::BAD_REQUEST)
}
logic::Error::Fragments(summary) => (
serde_json::to_string(&summary).unwrap(),
StatusCode::BAD_REQUEST,
),
err => (
display_internal_server_error(err),
StatusCode::INTERNAL_SERVER_ERROR,
Expand Down

0 comments on commit 1a35cce

Please sign in to comment.