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

Aligning sidecar with various changes made to nodes binary port #402

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
157 changes: 83 additions & 74 deletions Cargo.lock

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,4 @@ toml = "0.5.8"
tracing = { version = "0", default-features = false }
tracing-subscriber = "0"
serde = { version = "1", default-features = false }
jsonschema = "0.26.2"
jsonschema = "0.26.2"
7 changes: 3 additions & 4 deletions rpc_sidecar/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ mod speculative_exec_server;
pub mod testing;

use anyhow::Error;
use casper_binary_port::{BinaryRequest, BinaryRequestHeader};
use casper_binary_port::{Command, CommandHeader};
use casper_types::bytesrepr::ToBytes;
use casper_types::{bytesrepr, ProtocolVersion};
pub use config::{FieldParseError, NodeClientConfig, RpcConfig, RpcServerConfig};
Expand All @@ -25,7 +25,6 @@ pub use speculative_exec_config::Config as SpeculativeExecConfig;
pub use speculative_exec_server::run as run_speculative_exec_server;
use std::{net::SocketAddr, process::ExitCode, sync::Arc};
use tracing::warn;

/// Minimal casper protocol version supported by this sidecar.
pub const SUPPORTED_PROTOCOL_VERSION: ProtocolVersion = ProtocolVersion::from_parts(2, 0, 0);

Expand Down Expand Up @@ -108,8 +107,8 @@ fn start_listening(address: &SocketAddr) -> anyhow::Result<ServerBuilder<AddrInc
})
}

fn encode_request(req: &BinaryRequest, id: u16) -> Result<Vec<u8>, bytesrepr::Error> {
let header = BinaryRequestHeader::new(SUPPORTED_PROTOCOL_VERSION, req.tag(), id);
fn encode_request(req: &Command, id: u16) -> Result<Vec<u8>, bytesrepr::Error> {
let header = CommandHeader::new(req.tag(), id);
let mut bytes = Vec::with_capacity(header.serialized_length() + req.serialized_length());
header.write_bytes(&mut bytes)?;
req.write_bytes(&mut bytes)?;
Expand Down
293 changes: 113 additions & 180 deletions rpc_sidecar/src/node_client.rs

Large diffs are not rendered by default.

48 changes: 18 additions & 30 deletions rpc_sidecar/src/rpcs/account.rs
Original file line number Diff line number Diff line change
Expand Up @@ -148,12 +148,12 @@ impl RpcWithParams for PutTransaction {
#[cfg(test)]
mod tests {
use casper_binary_port::{
BinaryRequest, BinaryResponse, BinaryResponseAndRequest, ErrorCode as BinaryPortErrorCode,
BinaryResponse, BinaryResponseAndRequest, Command, ErrorCode as BinaryPortErrorCode,
};
use casper_types::testing::TestRng;
use casper_types::{bytesrepr::Bytes, testing::TestRng};
use pretty_assertions::assert_eq;

use crate::{rpcs::ErrorCode, SUPPORTED_PROTOCOL_VERSION};
use crate::rpcs::ErrorCode;

use super::*;

Expand All @@ -165,16 +165,13 @@ mod tests {
impl NodeClient for ClientMock {
async fn send_request(
&self,
req: BinaryRequest,
req: Command,
) -> Result<BinaryResponseAndRequest, ClientError> {
match req {
BinaryRequest::TryAcceptTransaction { .. } => {
Ok(BinaryResponseAndRequest::new(
BinaryResponse::new_empty(SUPPORTED_PROTOCOL_VERSION),
&[],
0,
))
}
Command::TryAcceptTransaction { .. } => Ok(BinaryResponseAndRequest::new(
BinaryResponse::new_empty(),
Bytes::from(vec![]),
)),
_ => unimplemented!(),
}
}
Expand Down Expand Up @@ -207,16 +204,13 @@ mod tests {
impl NodeClient for ClientMock {
async fn send_request(
&self,
req: BinaryRequest,
req: Command,
) -> Result<BinaryResponseAndRequest, ClientError> {
match req {
BinaryRequest::TryAcceptTransaction { .. } => {
Ok(BinaryResponseAndRequest::new(
BinaryResponse::new_empty(SUPPORTED_PROTOCOL_VERSION),
&[],
0,
))
}
Command::TryAcceptTransaction { .. } => Ok(BinaryResponseAndRequest::new(
BinaryResponse::new_empty(),
Bytes::from(vec![]),
)),
_ => unimplemented!(),
}
}
Expand Down Expand Up @@ -249,19 +243,13 @@ mod tests {
impl NodeClient for ClientMock {
async fn send_request(
&self,
req: BinaryRequest,
req: Command,
) -> Result<BinaryResponseAndRequest, ClientError> {
match req {
BinaryRequest::TryAcceptTransaction { .. } => {
Ok(BinaryResponseAndRequest::new(
BinaryResponse::new_error(
BinaryPortErrorCode::InvalidTransactionBodyHash,
SUPPORTED_PROTOCOL_VERSION,
),
&[],
0,
))
}
Command::TryAcceptTransaction { .. } => Ok(BinaryResponseAndRequest::new(
BinaryResponse::new_error(BinaryPortErrorCode::InvalidTransactionBodyHash),
Bytes::from(vec![]),
)),
_ => unimplemented!(),
}
}
Expand Down
58 changes: 22 additions & 36 deletions rpc_sidecar/src/rpcs/chain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -402,13 +402,13 @@ async fn get_era_summary_by_block(
mod tests {
use std::convert::TryFrom;

use crate::{rpcs::test_utils::BinaryPortMock, ClientError, SUPPORTED_PROTOCOL_VERSION};
use crate::{rpcs::test_utils::BinaryPortMock, ClientError};
use casper_binary_port::{
BinaryRequest, BinaryResponse, BinaryResponseAndRequest, GetRequest,
GlobalStateEntityQualifier, GlobalStateQueryResult, InformationRequest,
InformationRequestTag, RecordId,
BinaryResponse, BinaryResponseAndRequest, Command, GetRequest, GlobalStateEntityQualifier,
GlobalStateQueryResult, InformationRequest, InformationRequestTag, RecordId,
};
use casper_types::{
bytesrepr::Bytes,
system::auction::{DelegatorKind, EraInfo, SeigniorageAllocation},
testing::TestRng,
AsymmetricType, Block, BlockSignaturesV1, BlockSignaturesV2, BlockWithSignatures,
Expand Down Expand Up @@ -731,39 +731,33 @@ mod tests {
impl NodeClient for ValidBlockMock {
async fn send_request(
&self,
req: BinaryRequest,
req: Command,
) -> Result<BinaryResponseAndRequest, ClientError> {
match req {
BinaryRequest::Get(GetRequest::Information { info_type_tag, .. })
Command::Get(GetRequest::Information { info_type_tag, .. })
if InformationRequestTag::try_from(info_type_tag)
== Ok(InformationRequestTag::BlockWithSignatures) =>
{
Ok(BinaryResponseAndRequest::new(
BinaryResponse::from_value(self.block.clone(), SUPPORTED_PROTOCOL_VERSION),
&[],
0,
BinaryResponse::from_value(self.block.clone()),
Bytes::from(vec![]),
))
}
BinaryRequest::Get(GetRequest::Information { info_type_tag, .. })
Command::Get(GetRequest::Information { info_type_tag, .. })
if InformationRequestTag::try_from(info_type_tag)
== Ok(InformationRequestTag::BlockHeader) =>
{
Ok(BinaryResponseAndRequest::new(
BinaryResponse::from_value(
self.block.block().clone_header(),
SUPPORTED_PROTOCOL_VERSION,
),
&[],
0,
BinaryResponse::from_value(self.block.block().clone_header()),
Bytes::from(vec![]),
))
}
BinaryRequest::Get(GetRequest::Record {
Command::Get(GetRequest::Record {
record_type_tag, ..
}) if RecordId::try_from(record_type_tag) == Ok(RecordId::Transfer) => {
Ok(BinaryResponseAndRequest::new_legacy_test_response(
RecordId::Transfer,
&self.transfers,
SUPPORTED_PROTOCOL_VERSION,
))
}
req => unimplemented!("unexpected request: {:?}", req),
Expand All @@ -780,27 +774,23 @@ mod tests {
impl NodeClient for ValidEraSummaryMock {
async fn send_request(
&self,
req: BinaryRequest,
req: Command,
) -> Result<BinaryResponseAndRequest, ClientError> {
let expected_tag = if self.expect_no_block_identifier {
InformationRequestTag::LatestSwitchBlockHeader
} else {
InformationRequestTag::BlockHeader
};
match req {
BinaryRequest::Get(GetRequest::Information { info_type_tag, .. })
Command::Get(GetRequest::Information { info_type_tag, .. })
if InformationRequestTag::try_from(info_type_tag) == Ok(expected_tag) =>
{
Ok(BinaryResponseAndRequest::new(
BinaryResponse::from_value(
self.block.clone_header(),
SUPPORTED_PROTOCOL_VERSION,
),
&[],
0,
BinaryResponse::from_value(self.block.clone_header()),
Bytes::from(vec![]),
))
}
BinaryRequest::Get(GetRequest::State(req))
Command::Get(GetRequest::State(req))
if matches!(
req.clone().destructure().1,
GlobalStateEntityQualifier::Item {
Expand All @@ -810,15 +800,11 @@ mod tests {
) =>
{
Ok(BinaryResponseAndRequest::new(
BinaryResponse::from_value(
GlobalStateQueryResult::new(
StoredValue::EraInfo(EraInfo::new()),
vec![],
),
SUPPORTED_PROTOCOL_VERSION,
),
&[],
0,
BinaryResponse::from_value(GlobalStateQueryResult::new(
StoredValue::EraInfo(EraInfo::new()),
vec![],
)),
Bytes::from(vec![]),
))
}
req => unimplemented!("unexpected request: {:?}", req),
Expand Down
24 changes: 11 additions & 13 deletions rpc_sidecar/src/rpcs/info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -625,13 +625,13 @@ fn version_string() -> String {
mod tests {
use std::convert::TryFrom;

use crate::{rpcs::ErrorCode, ClientError, SUPPORTED_PROTOCOL_VERSION};
use crate::{rpcs::ErrorCode, ClientError};
use casper_binary_port::{
BinaryRequest, BinaryResponse, BinaryResponseAndRequest, GetRequest, InformationRequest,
BinaryResponse, BinaryResponseAndRequest, Command, GetRequest, InformationRequest,
InformationRequestTag, RewardResponse, TransactionWithExecutionInfo,
};
use casper_types::{
bytesrepr::{FromBytes, ToBytes},
bytesrepr::{Bytes, FromBytes, ToBytes},
testing::TestRng,
BlockHash, TransactionV1,
};
Expand Down Expand Up @@ -874,10 +874,10 @@ mod tests {
impl NodeClient for ValidTransactionMock {
async fn send_request(
&self,
req: BinaryRequest,
req: Command,
) -> Result<BinaryResponseAndRequest, ClientError> {
match req {
BinaryRequest::Get(GetRequest::Information { info_type_tag, key })
Command::Get(GetRequest::Information { info_type_tag, key })
if InformationRequestTag::try_from(info_type_tag)
== Ok(InformationRequestTag::Transaction) =>
{
Expand All @@ -895,9 +895,8 @@ mod tests {
TransactionWithExecutionInfo::from_bytes(&self.transaction_bytes)
.expect("should deserialize transaction");
Ok(BinaryResponseAndRequest::new(
BinaryResponse::from_value(transaction, SUPPORTED_PROTOCOL_VERSION),
&[],
0,
BinaryResponse::from_value(transaction),
Bytes::from(vec![]),
))
}
req => unimplemented!("unexpected request: {:?}", req),
Expand All @@ -916,10 +915,10 @@ mod tests {
impl NodeClient for RewardMock {
async fn send_request(
&self,
req: BinaryRequest,
req: Command,
) -> Result<BinaryResponseAndRequest, ClientError> {
match req {
BinaryRequest::Get(GetRequest::Information { info_type_tag, .. })
Command::Get(GetRequest::Information { info_type_tag, .. })
if InformationRequestTag::try_from(info_type_tag)
== Ok(InformationRequestTag::Reward) =>
{
Expand All @@ -930,9 +929,8 @@ mod tests {
self.switch_block_hash,
);
Ok(BinaryResponseAndRequest::new(
BinaryResponse::from_value(resp, SUPPORTED_PROTOCOL_VERSION),
&[],
0,
BinaryResponse::from_value(resp),
Bytes::from(vec![]),
))
}
req => unimplemented!("unexpected request: {:?}", req),
Expand Down
28 changes: 10 additions & 18 deletions rpc_sidecar/src/rpcs/speculative_exec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -170,13 +170,13 @@ mod tests {
use std::convert::TryFrom;

use casper_binary_port::{
BinaryRequest, BinaryResponse, BinaryResponseAndRequest, GetRequest, InformationRequestTag,
BinaryResponse, BinaryResponseAndRequest, Command, GetRequest, InformationRequestTag,
SpeculativeExecutionResult,
};
use casper_types::testing::TestRng;
use casper_types::{bytesrepr::Bytes, testing::TestRng};
use pretty_assertions::assert_eq;

use crate::{ClientError, SUPPORTED_PROTOCOL_VERSION};
use crate::ClientError;

use super::*;

Expand Down Expand Up @@ -234,29 +234,21 @@ mod tests {
impl NodeClient for ValidSpecExecMock {
async fn send_request(
&self,
req: BinaryRequest,
req: Command,
) -> Result<BinaryResponseAndRequest, ClientError> {
match req {
BinaryRequest::Get(GetRequest::Information { info_type_tag, .. })
Command::Get(GetRequest::Information { info_type_tag, .. })
if InformationRequestTag::try_from(info_type_tag)
== Ok(InformationRequestTag::BlockHeader) =>
{
Ok(BinaryResponseAndRequest::new(
BinaryResponse::from_value(
self.execution_result.clone(),
SUPPORTED_PROTOCOL_VERSION,
),
&[],
0,
BinaryResponse::from_value(self.execution_result.clone()),
Bytes::from(vec![]),
))
}
BinaryRequest::TrySpeculativeExec { .. } => Ok(BinaryResponseAndRequest::new(
BinaryResponse::from_value(
self.execution_result.clone(),
SUPPORTED_PROTOCOL_VERSION,
),
&[],
0,
Command::TrySpeculativeExec { .. } => Ok(BinaryResponseAndRequest::new(
BinaryResponse::from_value(self.execution_result.clone()),
Bytes::from(vec![]),
)),
req => unimplemented!("unexpected request: {:?}", req),
}
Expand Down
Loading
Loading