Skip to content

Commit

Permalink
updates vectors and snapshot tests, changes hex decoding error in sub…
Browse files Browse the repository at this point in the history
…mit_block method from server error to parse error
  • Loading branch information
arya2 committed Nov 3, 2022
1 parent 0267fb4 commit 2640b3d
Show file tree
Hide file tree
Showing 11 changed files with 72 additions and 60 deletions.
3 changes: 0 additions & 3 deletions zebra-rpc/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,6 @@ getblocktemplate-rpcs = ["zebra-state/getblocktemplate-rpcs", "zebra-node-servic
# Test-only features
proptest-impl = ["proptest", "proptest-derive", "zebra-chain/proptest-impl", "zebra-state/proptest-impl"]

# Test-only features
proptest-impl = ["proptest", "proptest-derive", "zebra-chain/proptest-impl", "zebra-state/proptest-impl"]

[dependencies]
chrono = { version = "0.4.22", default-features = false, features = ["clock", "std"] }
futures = "0.3.25"
Expand Down
9 changes: 9 additions & 0 deletions zebra-rpc/src/errors.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
use jsonrpc_core::types::error::{Error, ErrorCode};

pub(crate) fn make_server_error(message: impl std::fmt::Display) -> Error {
Error {
code: ErrorCode::ServerError(0),
message: message.to_string(),
data: None,
}
}
1 change: 1 addition & 0 deletions zebra-rpc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#![doc(html_root_url = "https://doc.zebra.zfnd.org/zebra_rpc")]

pub mod config;
mod errors;
pub mod methods;
pub mod queue;
pub mod server;
Expand Down
40 changes: 16 additions & 24 deletions zebra-rpc/src/methods/get_block_template_rpcs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,15 @@ use zebra_chain::{
use zebra_consensus::{BlockError, VerifyBlockError, VerifyChainError, VerifyCheckpointError};
use zebra_node_services::mempool;

use crate::methods::{
get_block_template_rpcs::types::{
default_roots::DefaultRoots, get_block_template::GetBlockTemplate, submit_block,
transaction::TransactionTemplate,
use crate::{
errors::make_server_error,
methods::{
get_block_template_rpcs::types::{
default_roots::DefaultRoots, get_block_template::GetBlockTemplate, submit_block,
transaction::TransactionTemplate,
},
GetBlockHash, MISSING_BLOCK_ERROR_CODE,
},
GetBlockHash, MISSING_BLOCK_ERROR_CODE,
};

pub mod config;
Expand Down Expand Up @@ -96,7 +99,7 @@ pub trait GetBlockTemplateRpc {
#[rpc(name = "submitblock")]
fn submit_block(
&self,
hex_data: String,
hex_data: submit_block::HexData,
_options: Option<submit_block::JsonParameters>,
) -> BoxFuture<Result<submit_block::Response>>;
}
Expand Down Expand Up @@ -137,7 +140,7 @@ where
/// Allows efficient access to the best tip of the blockchain.
latest_chain_tip: Tip,

/// The full block verifier, used for submitting blocks.
/// The chain verifier, used for submitting blocks.
chain_verifier: ChainVerifier,
}

Expand Down Expand Up @@ -351,32 +354,21 @@ where

fn submit_block(
&self,
hex_data: String,
submit_block::HexData(block_bytes): submit_block::HexData,
_options: Option<submit_block::JsonParameters>,
) -> BoxFuture<Result<submit_block::Response>> {
let mut chain_verifier = self.chain_verifier.clone();

async move {
let block = hex::decode(hex_data).map_err(|error| Error {
code: ErrorCode::ServerError(0),
message: format!("failed to decode hexdata, error msg: {error}"),
data: None,
})?;

let block: Block = block.zcash_deserialize_into().map_err(|error| Error {
code: ErrorCode::ServerError(0),
message: format!("failed to deserialize into block, error msg: {error}"),
data: None,
})?;
let block: Block = match block_bytes.zcash_deserialize_into() {
Ok(block_bytes) => block_bytes,
Err(_) => return Ok(submit_block::ErrorResponse::Rejected.into()),
};

let chain_verifier_response = chain_verifier
.ready()
.await
.map_err(|error| Error {
code: ErrorCode::ServerError(0),
message: error.to_string(),
data: None,
})?
.map_err(make_server_error)?
.call(Arc::new(block))
.await;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@ use crate::methods::get_block_template_rpcs::GetBlockTemplateRpc;
/// Optional argument `jsonparametersobject` for `submitblock` RPC request
///
/// See notes for [`GetBlockTemplateRpc::submit_block`] method
#[derive(Debug, serde::Serialize, serde::Deserialize)]
#[derive(Debug, serde::Deserialize)]
pub struct JsonParameters {
pub(crate) work_id: Option<String>,
pub(crate) _work_id: Option<String>,
}

/// Response to a `submitblock` RPC request.
///
/// Zebra never returns "duplicate-invalid", because it does not store invalid blocks.
#[derive(Debug, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
#[derive(Debug, PartialEq, Eq, serde::Serialize)]
#[serde(rename_all = "kebab-case")]
pub enum ErrorResponse {
/// Block was already committed to the non-finalized or finalized state
Expand All @@ -31,7 +31,7 @@ pub enum ErrorResponse {
/// Response to a `submitblock` RPC request.
///
/// Zebra never returns "duplicate-invalid", because it does not store invalid blocks.
#[derive(Debug, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
#[derive(Debug, PartialEq, Eq, serde::Serialize)]
#[serde(untagged)]
pub enum Response {
/// Block was not successfully submitted, return error
Expand All @@ -45,3 +45,5 @@ impl From<ErrorResponse> for Response {
Self::ErrorResponse(error_response)
}
}
#[derive(Debug, PartialEq, Eq, serde::Deserialize)]
pub struct HexData(#[serde(with = "hex")] pub Vec<u8>);
10 changes: 5 additions & 5 deletions zebra-rpc/src/methods/tests/snapshot/get_block_template_rpcs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ use crate::methods::{

pub async fn test_responses<State, ReadState>(
network: Network,
mempool: MockService<
mut mempool: MockService<
mempool::Request,
mempool::Response,
PanicAssertion,
Expand Down Expand Up @@ -103,9 +103,9 @@ pub async fn test_responses<State, ReadState>(

// `submitblock`
let submit_block = get_block_template_rpc
.submit_block("".to_string(), None)
.submit_block(submit_block::HexData("".into()), None)
.await;
snapshot_rpc_submit_block_deserialization_error(submit_block, &settings);
snapshot_rpc_submit_block_invalid(submit_block, &settings);
}

/// Snapshot `getblockcount` response, using `cargo insta` and JSON serialization.
Expand All @@ -127,11 +127,11 @@ fn snapshot_rpc_getblocktemplate(
}

/// Snapshot `submitblock` response, using `cargo insta` and JSON serialization.
fn snapshot_rpc_submit_block_deserialization_error(
fn snapshot_rpc_submit_block_invalid(
submit_block_response: jsonrpc_core::Result<submit_block::Response>,
settings: &insta::Settings,
) {
settings.bind(|| {
insta::assert_json_snapshot!("submit_block_deserialization_error", submit_block_response)
insta::assert_json_snapshot!("snapshot_rpc_submit_block_invalid", submit_block_response)
});
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
source: zebra-rpc/src/methods/tests/snapshot/get_block_template_rpcs.rs
expression: submit_block_response
---
{
"Ok": "rejected"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
source: zebra-rpc/src/methods/tests/snapshot/get_block_template_rpcs.rs
expression: submit_block_response
---
{
"Ok": "rejected"
}

This file was deleted.

This file was deleted.

25 changes: 21 additions & 4 deletions zebra-rpc/src/methods/tests/vectors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -887,17 +887,34 @@ async fn rpc_submitblock_errors() {
);

// Try to submit pre-populated blocks and assert that it responds with duplicate.
for (_height, block_bytes) in zebra_test::vectors::CONTINUOUS_MAINNET_BLOCKS.iter() {
let hex_data = hex::encode(block_bytes);

let submit_block_response = get_block_template_rpc.submit_block(hex_data, None).await;
for (_height, &block_bytes) in zebra_test::vectors::CONTINUOUS_MAINNET_BLOCKS.iter() {
let submit_block_response = get_block_template_rpc
.submit_block(
get_block_template_rpcs::types::submit_block::HexData(block_bytes.into()),
None,
)
.await;

assert_eq!(
submit_block_response,
Ok(get_block_template_rpcs::types::submit_block::ErrorResponse::Duplicate.into())
);
}

let submit_block_response = get_block_template_rpc
.submit_block(
get_block_template_rpcs::types::submit_block::HexData(
zebra_test::vectors::BAD_BLOCK_MAINNET_202_BYTES.to_vec(),
),
None,
)
.await;

assert_eq!(
submit_block_response,
Ok(get_block_template_rpcs::types::submit_block::ErrorResponse::Rejected.into())
);

mempool.expect_no_requests().await;

// See zebrad::tests::acceptance::submit_block for success case.
Expand Down

0 comments on commit 2640b3d

Please sign in to comment.