Skip to content

Commit

Permalink
fix: inconsistencies and tests (#876)
Browse files Browse the repository at this point in the history
* fix: wrong error type in unsupported subscription

* initial commit

* add temp files

* enha: add newPendingTransactions tests

* remove temp  files

* chore: lint

* improve conversion

---------

Co-authored-by: Maycon Amaro <[email protected]>
  • Loading branch information
gabriel-aranha-cw and mayconamaroCW authored May 21, 2024
1 parent 6e860a3 commit c4ae172
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 35 deletions.
69 changes: 42 additions & 27 deletions e2e/test/e2e-json-rpc.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
send,
sendAndGetError,
sendExpect,
sendReset,
subscribeAndGetEvent,
subscribeAndGetEventWithContract,
} from "./helpers/rpc";
Expand Down Expand Up @@ -174,13 +175,13 @@ describe("JSON-RPC", () => {
expect(response.result).to.not.be.undefined;
});

//it("Subscribe to newPendingTransactions receives success subscription event", async () => {
// const waitTimeInMilliseconds = 40;
// const response = await subscribeAndGetEvent("newPendingTransactions", waitTimeInMilliseconds);
// expect(response).to.not.be.undefined;
// expect(response.id).to.not.be.undefined;
// expect(response.result).to.not.be.undefined;
//});
it("Subscribe to newPendingTransactions receives success subscription event", async () => {
const waitTimeInMilliseconds = 40;
const response = await subscribeAndGetEvent("newPendingTransactions", waitTimeInMilliseconds);
expect(response).to.not.be.undefined;
expect(response.id).to.not.be.undefined;
expect(response.result).to.not.be.undefined;
});


it("Subscribe to unsupported receives error subscription event", async () => {
Expand All @@ -191,7 +192,7 @@ describe("JSON-RPC", () => {
expect(response.error).to.not.be.undefined;
expect(response.error.code).to.not.be.undefined;
expect(response.error.code).to.be.a('number');
// expect(response.error.code).eq(-32602);
expect(response.error.code).eq(-32602);
});

it("Validate newHeads event", async () => {
Expand All @@ -203,28 +204,31 @@ describe("JSON-RPC", () => {
expect(params).to.have.property('subscription').that.is.a('string');
expect(params).to.have.property('result').that.is.an('object');

// const result = params.result;
// expect(result).to.have.property('number').that.is.a('string');
// expect(result).to.have.property('hash').that.is.a('string');
// expect(result).to.have.property('transactionsRoot').that.is.a('string');
// expect(result).to.have.property('gasUsed').that.is.a('string');
// expect(result).to.have.property('gasLimit').that.is.a('string');
// expect(result).to.have.property('bloom').that.is.a('string');
// expect(result).to.have.property('timestamp').that.is.a('string');
// expect(result).to.have.property('parentHash').that.is.a('string');
// expect(result).to.have.property('author').that.is.a('string');
// expect(result).to.have.property('extraData').that.is.a('string');
// expect(result).to.have.property('miner').that.is.a('string');
// expect(result).to.have.property('difficulty').that.is.a('string');
// expect(result).to.have.property('receiptsRoot').that.is.a('string');
// expect(result).to.have.property('uncleHash').that.is.a('string');
// expect(result).to.have.property('size').that.is.a('string');
// expect(result).to.have.property('stateRoot').that.is.a('string');
// expect(result).to.have.property('totalDifficulty').that.is.a('string');
// expect(result).to.have.property('nonce').that.is.a('string');
const result = params.result;
expect(result).to.have.property('hash').that.is.a('string');
expect(result).to.have.property('parentHash').that.is.a('string');
expect(result).to.have.property('sha3Uncles').that.is.a('string');
expect(result).to.have.property('miner').that.is.a('string');
expect(result).to.have.property('stateRoot').that.is.a('string');
expect(result).to.have.property('transactionsRoot').that.is.a('string');
expect(result).to.have.property('receiptsRoot').that.is.a('string');
expect(result).to.have.property('number').that.is.a('string');
expect(result).to.have.property('gasUsed').that.is.a('string');
expect(result).to.have.property('extraData').that.is.a('string');
expect(result).to.have.property('logsBloom').that.is.a('string');
expect(result).to.have.property('timestamp').that.is.a('string');
expect(result).to.have.property('difficulty').that.is.a('string');
expect(result).to.have.property('totalDifficulty').that.is.a('string');
expect(result).to.have.property('uncles').that.is.an('array');
expect(result).to.have.property('transactions').that.is.an('array');
expect(result).to.have.property('size')
expect(result).to.have.property('mixHash')
expect(result).to.have.property('nonce').that.is.a('string');
expect(result).to.have.property('baseFeePerGas').that.is.a('string');
});

it("Validate logs event", async () => {
await sendReset();
const waitTimeInMilliseconds = 40;
const response = await subscribeAndGetEventWithContract("logs", waitTimeInMilliseconds, 2);
expect(response).to.not.be.undefined;
Expand All @@ -244,6 +248,17 @@ describe("JSON-RPC", () => {
expect(result).to.have.property('logIndex').that.is.a('string');
expect(result).to.have.property('removed').that.is.a('boolean');
});

it("Validate newPendingTransactions event", async () => {
await sendReset();
const waitTimeInMilliseconds = 40;
const response = await subscribeAndGetEventWithContract("newPendingTransactions", waitTimeInMilliseconds, 2);
expect(response).to.not.be.undefined;

const params = response.params;
expect(params).to.have.property('subscription').that.is.a('string');
expect(params).to.have.property('result').that.is.an('string');
});
});
});
});
3 changes: 2 additions & 1 deletion src/eth/primitives/block_header.rs
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,7 @@ impl TryFrom<&ExternalBlock> for BlockHeader {

impl From<BlockHeader> for SubscriptionMessage {
fn from(value: BlockHeader) -> Self {
Self::from_json(&value).unwrap()
let ethers_block: EthersBlock<()> = EthersBlock::from(value);
Self::from_json(&ethers_block).unwrap()
}
}
2 changes: 1 addition & 1 deletion src/eth/rpc/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,6 @@ use rpc_parser::next_rpc_param;
use rpc_parser::next_rpc_param_or_default;
use rpc_parser::parse_rpc_rlp;
use rpc_parser::rpc_internal_error;
use rpc_parser::rpc_parsing_error;
use rpc_parser::rpc_invalid_params_error;
pub use rpc_server::serve_rpc;
pub use rpc_subscriptions::RpcSubscriptions;
8 changes: 4 additions & 4 deletions src/eth/rpc/rpc_parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
use anyhow::anyhow;
use jsonrpsee::types::error::INTERNAL_ERROR_CODE;
use jsonrpsee::types::error::INTERNAL_ERROR_MSG;
use jsonrpsee::types::error::PARSE_ERROR_CODE;
use jsonrpsee::types::error::PARSE_ERROR_MSG;
use jsonrpsee::types::error::INVALID_PARAMS_CODE;
use jsonrpsee::types::error::INVALID_PARAMS_MSG;
use jsonrpsee::types::ErrorObjectOwned;
use jsonrpsee::types::ParamsSequence;
use rlp::Decodable;
Expand Down Expand Up @@ -44,8 +44,8 @@ pub fn parse_rpc_rlp<T: Decodable>(value: &[u8]) -> anyhow::Result<T> {
}

/// Creates an RPC parsing error response.
pub fn rpc_parsing_error<S: serde::Serialize>(message: S) -> ErrorObjectOwned {
ErrorObjectOwned::owned(PARSE_ERROR_CODE, PARSE_ERROR_MSG, Some(message))
pub fn rpc_invalid_params_error<S: serde::Serialize>(message: S) -> ErrorObjectOwned {
ErrorObjectOwned::owned(INVALID_PARAMS_CODE, INVALID_PARAMS_MSG, Some(message))
}

/// Creates an RPC internal error response.
Expand Down
6 changes: 4 additions & 2 deletions src/eth/rpc/rpc_server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ use crate::eth::rpc::next_rpc_param;
use crate::eth::rpc::next_rpc_param_or_default;
use crate::eth::rpc::parse_rpc_rlp;
use crate::eth::rpc::rpc_internal_error;
use crate::eth::rpc::rpc_parsing_error;
use crate::eth::rpc::rpc_invalid_params_error;
use crate::eth::rpc::RpcContext;
use crate::eth::rpc::RpcError;
use crate::eth::rpc::RpcMiddleware;
Expand Down Expand Up @@ -419,7 +419,9 @@ async fn eth_subscribe(params: Params<'_>, pending: PendingSubscriptionSink, ctx
// unsupported
kind => {
tracing::warn!(%kind, "unsupported subscription kind");
pending.reject(rpc_parsing_error(format!("unsupported subscription kind: {}", kind))).await;
pending
.reject(rpc_invalid_params_error(format!("unsupported subscription kind: {}", kind)))
.await;
}
};
Ok(())
Expand Down

0 comments on commit c4ae172

Please sign in to comment.