Skip to content

Commit

Permalink
Updates snapshot test, adds mod docs, moves HexData to its own mod, a…
Browse files Browse the repository at this point in the history
…nd removes the unrelated make_server_error refactor for now
  • Loading branch information
arya2 committed Nov 3, 2022
1 parent dd181c1 commit cf4e937
Show file tree
Hide file tree
Showing 12 changed files with 42 additions and 41 deletions.
13 changes: 0 additions & 13 deletions zebra-rpc/src/errors.rs

This file was deleted.

2 changes: 0 additions & 2 deletions zebra-rpc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
#![doc(html_root_url = "https://doc.zebra.zfnd.org/zebra_rpc")]

pub mod config;
#[cfg(feature = "getblocktemplate-rpcs")]
mod errors;
pub mod methods;
pub mod queue;
pub mod server;
Expand Down
23 changes: 12 additions & 11 deletions zebra-rpc/src/methods/get_block_template_rpcs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,12 @@ use zebra_chain::{
use zebra_consensus::{BlockError, VerifyBlockError, VerifyChainError, VerifyCheckpointError};
use zebra_node_services::mempool;

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,
use crate::methods::{
get_block_template_rpcs::types::{
default_roots::DefaultRoots, get_block_template::GetBlockTemplate, hex_data::HexData,
submit_block, transaction::TransactionTemplate,
},
GetBlockHash, MISSING_BLOCK_ERROR_CODE,
};

pub mod config;
Expand Down Expand Up @@ -99,7 +96,7 @@ pub trait GetBlockTemplateRpc {
#[rpc(name = "submitblock")]
fn submit_block(
&self,
hex_data: submit_block::HexData,
hex_data: HexData,
_options: Option<submit_block::JsonParameters>,
) -> BoxFuture<Result<submit_block::Response>>;
}
Expand Down Expand Up @@ -354,7 +351,7 @@ where

fn submit_block(
&self,
submit_block::HexData(block_bytes): submit_block::HexData,
HexData(block_bytes): HexData,
_options: Option<submit_block::JsonParameters>,
) -> BoxFuture<Result<submit_block::Response>> {
let mut chain_verifier = self.chain_verifier.clone();
Expand All @@ -368,7 +365,11 @@ where
let chain_verifier_response = chain_verifier
.ready()
.await
.map_err(make_server_error)?
.map_err(|error| Error {
code: ErrorCode::ServerError(0),
message: error.to_string(),
data: None,
})?
.call(Arc::new(block))
.await;

Expand Down
1 change: 1 addition & 0 deletions zebra-rpc/src/methods/get_block_template_rpcs/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@
pub(crate) mod default_roots;
pub(crate) mod get_block_template;
pub(crate) mod hex_data;
pub(crate) mod submit_block;
pub(crate) mod transaction;
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
//! Deserializes hex-encoded inputs such as the one required
//! for the `submitblock` RPC method.
/// Deserialize hex-encoded strings to bytes.
#[derive(Debug, PartialEq, Eq, serde::Deserialize)]
pub struct HexData(#[serde(with = "hex")] pub Vec<u8>);
Original file line number Diff line number Diff line change
Expand Up @@ -45,5 +45,3 @@ impl From<ErrorResponse> for Response {
Self::ErrorResponse(error_response)
}
}
#[derive(Debug, PartialEq, Eq, serde::Deserialize)]
pub struct HexData(#[serde(with = "hex")] pub Vec<u8>);
12 changes: 7 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 @@ -15,8 +15,8 @@ use zebra_state::LatestChainTip;
use zebra_test::mock_service::{MockService, PanicAssertion};

use crate::methods::{
get_block_template_rpcs::types::submit_block, GetBlockHash, GetBlockTemplateRpc,
GetBlockTemplateRpcImpl,
get_block_template_rpcs::types::{hex_data::HexData, submit_block},
GetBlockHash, GetBlockTemplateRpc, GetBlockTemplateRpcImpl,
};

pub async fn test_responses<State, ReadState>(
Expand Down Expand Up @@ -103,8 +103,10 @@ pub async fn test_responses<State, ReadState>(

// `submitblock`
let submit_block = get_block_template_rpc
.submit_block(submit_block::HexData("".into()), None)
.await;
.submit_block(HexData("".into()), None)
.await
.expect("unexpected error in submitblock RPC call");

snapshot_rpc_submit_block_invalid(submit_block, &settings);
}

Expand All @@ -128,7 +130,7 @@ fn snapshot_rpc_getblocktemplate(

/// Snapshot `submitblock` response, using `cargo insta` and JSON serialization.
fn snapshot_rpc_submit_block_invalid(
submit_block_response: jsonrpc_core::Result<submit_block::Response>,
submit_block_response: submit_block::Response,
settings: &insta::Settings,
) {
settings.bind(|| {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,4 @@
source: zebra-rpc/src/methods/tests/snapshot/get_block_template_rpcs.rs
expression: submit_block_response
---
{
"Ok": "rejected"
}
"rejected"
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,4 @@
source: zebra-rpc/src/methods/tests/snapshot/get_block_template_rpcs.rs
expression: submit_block_response
---
{
"Ok": "rejected"
}
"rejected"
4 changes: 2 additions & 2 deletions zebra-rpc/src/methods/tests/vectors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -890,7 +890,7 @@ async fn rpc_submitblock_errors() {
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()),
get_block_template_rpcs::types::hex_data::HexData(block_bytes.into()),
None,
)
.await;
Expand All @@ -903,7 +903,7 @@ async fn rpc_submitblock_errors() {

let submit_block_response = get_block_template_rpc
.submit_block(
get_block_template_rpcs::types::submit_block::HexData(
get_block_template_rpcs::types::hex_data::HexData(
zebra_test::vectors::BAD_BLOCK_MAINNET_202_BYTES.to_vec(),
),
None,
Expand Down
2 changes: 2 additions & 0 deletions zebrad/tests/common/get_block_template_rpcs.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
//! Acceptance tests for getblocktemplate RPC methods in Zebra.
use super::*;

pub(crate) mod submit_block;
10 changes: 10 additions & 0 deletions zebrad/tests/common/get_block_template_rpcs/submit_block.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
//! Test submitblock RPC method.
//!
//! This test requires a cached chain state that is partially synchronized close to the
//! network chain tip height, and will finish the sync and update the cached chain state.
//!
//! After finishing the sync, it will get the first 20 blocks in the non-finalized state
//! (past the MAX_BLOCK_REORG_HEIGHT) via getblock rpc calls, get the finalized tip height
//! of the updated cached state, restart zebra without peers, and submit blocks above the
//! finalized tip height.
use std::path::PathBuf;

use color_eyre::eyre::{eyre, Context, Result};
Expand Down

0 comments on commit cf4e937

Please sign in to comment.