From 880a0baf4f66f47ccf71af36fb396d4a4ac79059 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ceyhun=20=C5=9Een?= Date: Thu, 29 Aug 2024 12:33:10 +0300 Subject: [PATCH 1/5] readme: Update code examples. --- README.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index d1fe7e5..b9ceec5 100644 --- a/README.md +++ b/README.md @@ -60,7 +60,7 @@ fn test() { let address = bitcoin_mock_rpc::spawn_rpc_server(None, None).await.unwrap(); let rpc = - bitcoincore_rpc::Client::new(&address.to_string(), bitcoincore_rpc::Auth::None).unwrap(); + bitcoincore_rpc::Client::new(address, bitcoincore_rpc::Auth::None).unwrap(); // Use `bitcoincore_rpc` as is from now on. No code change is needed. } @@ -80,7 +80,7 @@ fn my_func() { let strct = MyStruct { data: 0x45, // This will connect to Bitcoin RPC. - rpc: bitcoincore_rpc::Client::new(/** parameters here **/), + rpc: bitcoincore_rpc::Client::new("127.0.0.1", bitcoincore_rpc::Auth::None).unwrap(), }; // Do stuff... @@ -91,7 +91,7 @@ fn test() { let strct = MyStruct { data: 0x1F, // This will connect to mock RPC. - rpc: bitcoin_mock_rpc::Client::new(/** parameters here **/), + rpc: bitcoin_mock_rpc::Client::new("db_name", bitcoincore_rpc::Auth::None).unwrap(), }; // Do stuff... From 45d62441c154e69654e720cbc0e363f98052dc07 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ceyhun=20=C5=9Een?= Date: Thu, 29 Aug 2024 12:33:27 +0300 Subject: [PATCH 2/5] docs: Update lib.rs doc. --- src/lib.rs | 116 ++++++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 111 insertions(+), 5 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 62a0930..1622f82 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,11 +1,117 @@ //! # Bitcoin Mock Remote Procedure Call //! -//! This library mocks [bitcoincore-rpc](https://github.com/rust-bitcoin/rust-bitcoincore-rpc) -//! library. This mock takes the advantage of `bitcoincore-rpc` trait interface -//! called `RpcApi`. +//! bitcoin-mock-rpc is an RPC library, that builds on a mock Bitcoin ledger and +//! without a wallet or consenses implementation. It aims to provide an easier +//! way to check your Bitcoin operations without you needing to setup a Bitcoin +//! environment. //! -//! Applications can implement another trait that will switch between this mock -//! and the real RPC interface, for tests and production respectively. +//! This library is built upon +//! [bitcoincore-rpc's](https://github.com/rust-bitcoin/rust-bitcoincore-rpc) +//! `RpcApi` trait interface. +//! +//! ## Interfaces +//! +//! ```text +//! ┌────────────────────┐ +//! │ │ +//! │ User Application │ +//! │ │ +//! └──▲───────────────▲─┘ +//! │ │ +//! │ ┌──────┼───────┐ +//! │ │ │ +//! │ │ RPC Server │ +//! │ │ (Public) │ +//! │ └──────▲───────┘ +//! │ │ +//! │ ┌──────┼────────┐ +//! ┌────────────┴───┐ │ │ +//! │ │ │ RPC Adapter │ +//! │ RpcApi Trait │ │ │ +//! │ Interface ┼───►│ encodes rust │ +//! │ (Public) │ │ structs to │ +//! └───────▲────────┘ │ hex strings │ +//! │ │ │ +//! ┌──────┴──────┐ └───────────────┘ +//! │ │ +//! │ Mock Ledger │ +//! │ │ +//! └─────────────┴ +//! ``` +//! +//! bitcoin-mock-rpc has 2 interfaces: +//! +//! 1. Rpc server: Similar experience as the Bitcoin RPC +//! 2. `RpcApi` trait: No servers but a direct function call to ledger +//! +//! ### RPC Server +//! +//! RPC server can be spawned as long as there are available ports for them. Each +//! server will have an independent blockchain. +//! +//! To run from CLI: +//! +//! ```bash +//! $ cargo run +//! Server started at 127.0.0.1:1024 +//! # ^^^^^^^^^^^^^^ +//! # Use this address in applications +//! $ cargo run -- --help # Prints usage information +//! ``` +//! +//! To run in a Rust application: +//! +//! ```rust +//! #[test] +//! fn test() { +//! // Calling `spawn_rpc_server` in a different test while this test is running +//! // is OK and will spawn another blockchain. If parameters are the same +//! // however, they will operate on the same blockchain. Note: (None, None) +//! // will result to pick random values. +//! let address = bitcoin_mock_rpc::spawn_rpc_server(None, None).await.unwrap(); +//! +//! let rpc = +//! bitcoincore_rpc::Client::new(address, bitcoincore_rpc::Auth::None).unwrap(); +//! +//! // Use `bitcoincore_rpc` as is from now on. No code change is needed. +//! } +//! ``` +//! +//! ### `RpcApi` +//! +//! `RpcApiWrapper` trait can be used to select between real and mock RPC. It is +//! a simple wrapper that allows you to also use methods like `Client::new()`. +//! But it needs changes in your code: +//! +//! ```rust +//! use bitcoin_mock_rpc::RpcApiWrapper; +//! +//! struct MyStruct { +//! data: u32, +//! rpc: R, +//! } +//! +//! fn my_func() { +//! let strct = MyStruct { +//! data: 0x45, +//! // This will connect to Bitcoin RPC. +//! rpc: bitcoincore_rpc::Client::new("127.0.0.1", bitcoincore_rpc::Auth::None).unwrap(), +//! }; +//! +//! // Do stuff... +//! } +//! +//! #[test] +//! fn test() { +//! let strct = MyStruct { +//! data: 0x1F, +//! // This will connect to mock RPC. +//! rpc: bitcoin_mock_rpc::Client::new("db_name", bitcoincore_rpc::Auth::None).unwrap(), +//! }; +//! +//! // Do stuff... +//! } +//! ``` pub mod client; mod ledger; From 5f54f01721445d123e8d59a01860fdbf1dc3d639 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ceyhun=20=C5=9Een?= Date: Thu, 29 Aug 2024 15:08:10 +0300 Subject: [PATCH 3/5] doc: Remove await, update `new` params, remove #[test]. --- README.md | 4 ++-- src/lib.rs | 7 +++---- 2 files changed, 5 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index b9ceec5..50bca2d 100644 --- a/README.md +++ b/README.md @@ -57,10 +57,10 @@ fn test() { // is OK and will spawn another blockchain. If parameters are the same // however, they will operate on the same blockchain. Note: (None, None) // will result to pick random values. - let address = bitcoin_mock_rpc::spawn_rpc_server(None, None).await.unwrap(); + let address = bitcoin_mock_rpc::spawn_rpc_server(None, None).unwrap(); let rpc = - bitcoincore_rpc::Client::new(address, bitcoincore_rpc::Auth::None).unwrap(); + bitcoincore_rpc::Client::new(&address.0.to_string(), bitcoincore_rpc::Auth::None).unwrap(); // Use `bitcoincore_rpc` as is from now on. No code change is needed. } diff --git a/src/lib.rs b/src/lib.rs index 1622f82..6e46bdd 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -62,16 +62,15 @@ //! To run in a Rust application: //! //! ```rust -//! #[test] //! fn test() { //! // Calling `spawn_rpc_server` in a different test while this test is running //! // is OK and will spawn another blockchain. If parameters are the same //! // however, they will operate on the same blockchain. Note: (None, None) //! // will result to pick random values. -//! let address = bitcoin_mock_rpc::spawn_rpc_server(None, None).await.unwrap(); +//! let address = bitcoin_mock_rpc::spawn_rpc_server(None, None).unwrap(); //! //! let rpc = -//! bitcoincore_rpc::Client::new(address, bitcoincore_rpc::Auth::None).unwrap(); +//! bitcoincore_rpc::Client::new(&address.0.to_string(), bitcoincore_rpc::Auth::None).unwrap(); //! //! // Use `bitcoincore_rpc` as is from now on. No code change is needed. //! } @@ -101,7 +100,6 @@ //! // Do stuff... //! } //! -//! #[test] //! fn test() { //! let strct = MyStruct { //! data: 0x1F, @@ -120,3 +118,4 @@ mod utils; // Re-imports. pub use client::*; +pub use rpc::*; From b9381d69a7c403f27c52a3131fd00d129c9e1c35 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ceyhun=20=C5=9Een?= Date: Thu, 5 Sep 2024 15:37:27 +0300 Subject: [PATCH 4/5] lib: Remove old RPC server. --- src/lib.rs | 85 ++++++++++++------------------------------------------ 1 file changed, 18 insertions(+), 67 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 700f9b0..e355f36 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -5,85 +5,36 @@ //! way to check your Bitcoin operations without you needing to setup a Bitcoin //! environment. //! -//! This library mocks [bitcoincore-rpc](https://github.com/rust-bitcoin/rust-bitcoincore-rpc) +//! This library mocks +//! [bitcoincore-rpc](https://github.com/rust-bitcoin/rust-bitcoincore-rpc) //! library. This mock takes advantage of `RpcApi` trait. //! -//! This library is built upon -//! [bitcoincore-rpc's](https://github.com/rust-bitcoin/rust-bitcoincore-rpc) -//! `RpcApi` trait interface. -//! -//! ## Interfaces +//! ## Interface //! //! ```text -//! ┌────────────────────┐ -//! │ │ -//! │ User Application │ -//! │ │ -//! └──▲───────────────▲─┘ -//! │ │ -//! │ ┌──────┼───────┐ -//! │ │ │ -//! │ │ RPC Server │ -//! │ │ (Public) │ -//! │ └──────▲───────┘ -//! │ │ -//! │ ┌──────┼────────┐ -//! ┌────────────┴───┐ │ │ -//! │ │ │ RPC Adapter │ -//! │ RpcApi Trait │ │ │ -//! │ Interface ┼───►│ encodes rust │ -//! │ (Public) │ │ structs to │ -//! └───────▲────────┘ │ hex strings │ -//! │ │ │ -//! ┌──────┴──────┐ └───────────────┘ +//! ┌────────────────────┐ +//! │ │ +//! │ User Application │ +//! │ │ +//! └────────▲───────────┘ +//! │ +//! ┌───────┴────────┐ +//! │ │ +//! │ RpcApi Trait │ +//! │ Interface │ +//! │ │ +//! └───────▲────────┘ +//! │ +//! ┌───────┴─────┐ //! │ │ //! │ Mock Ledger │ //! │ │ //! └─────────────┴ //! ``` //! -//! bitcoin-mock-rpc has 2 interfaces: -//! -//! 1. Rpc server: Similar experience as the Bitcoin RPC -//! 2. `RpcApi` trait: No servers but a direct function call to ledger -//! -//! ### RPC Server -//! -//! RPC server can be spawned as long as there are available ports for them. Each -//! server will have an independent blockchain. -//! -//! To run from CLI: -//! -//! ```bash -//! $ cargo run -//! Server started at 127.0.0.1:1024 -//! # ^^^^^^^^^^^^^^ -//! # Use this address in applications -//! $ cargo run -- --help # Prints usage information -//! ``` -//! -//! To run in a Rust application: -//! -//! ```rust -//! fn test() { -//! // Calling `spawn_rpc_server` in a different test while this test is running -//! // is OK and will spawn another blockchain. If parameters are the same -//! // however, they will operate on the same blockchain. Note: (None, None) -//! // will result to pick random values. -//! let address = bitcoin_mock_rpc::spawn_rpc_server(None, None).unwrap(); -//! -//! let rpc = -//! bitcoincore_rpc::Client::new(&address.0.to_string(), bitcoincore_rpc::Auth::None).unwrap(); -//! -//! // Use `bitcoincore_rpc` as is from now on. No code change is needed. -//! } -//! ``` -//! -//! ### `RpcApi` -//! //! `RpcApiWrapper` trait can be used to select between real and mock RPC. It is //! a simple wrapper that allows you to also use methods like `Client::new()`. -//! But it needs changes in your code: +//! Needs changes in your code: //! //! ```rust //! use bitcoin_mock_rpc::RpcApiWrapper; From cec8fcd061bdb91fe5c279b5e8842bc796dbc65d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ceyhun=20=C5=9Een?= Date: Thu, 5 Sep 2024 15:39:46 +0300 Subject: [PATCH 5/5] rpc: Add warning to crate doc. --- src/rpc/mod.rs | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/rpc/mod.rs b/src/rpc/mod.rs index 9b08051..dd0b232 100644 --- a/src/rpc/mod.rs +++ b/src/rpc/mod.rs @@ -2,6 +2,10 @@ //! //! This crate provides an RPC server that will act like the real Bitcoin RPC //! interface. +//! +//! WARNING: This crate is not actively maintained. And won't work correctly. +//! There is so much work needs to be done in this crate. Use it in your own +//! risk. `--features rpc_server` can be used to enable this crate. use crate::{Client, RpcApiWrapper}; use jsonrpsee::server::middleware::rpc::RpcServiceT;