From 2dad447476b04b39c92b7eb74119b875a8ed6d0d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Halil=20Samed=20=C3=87=C4=B1ld=C4=B1r?= Date: Sun, 22 Dec 2024 14:55:01 +0300 Subject: [PATCH 1/7] added `/r/txhex/` for tx-hex recursion --- docs/src/inscriptions/recursion.md | 22 ++++++++++++++++++++++ src/index.rs | 4 ++++ src/subcommand/server.rs | 28 ++++++++++++++++++++++++++++ 3 files changed, 54 insertions(+) diff --git a/docs/src/inscriptions/recursion.md b/docs/src/inscriptions/recursion.md index abb151bc9c..fa42578e45 100644 --- a/docs/src/inscriptions/recursion.md +++ b/docs/src/inscriptions/recursion.md @@ -244,6 +244,28 @@ curl -s \ ``` +
+ + GET + /r/txhex/<TRANSACTION_ID> + + +### Description + +Raw transaction hex for ``. + +### Example + +```bash +curl -s -H "Accept: application/json" \ + http://0.0.0.0:80/r/txhex/60bcf821240064a9c55225c4f01711b0ebbcab39aa3fafeefe4299ab158536fa +``` + +```json +"0100000000010183572872dcb32bee57003d53c2b8dbb5bc5819ff6478052599911f7778d1c7bd0000000000fdffffff011027000000000000225120e41e0cba05c6ac797cf543ff9a6c619a91a53813e59146d1e32ea89747b111a603407aa50d93d6fc01265fd52d3edc93af4e009ccc1a704ce1b5cb8ede1412a5df31eba587d080b3dc903ceb9002ed9d921aad323fd44d7b4dc2a1ad2ea12d4360424d20c7a3a38df198a4fcde7d5dac5819ed19ff4d25bb893c9511f8e1f51d59326effac0063036f7264010118746578742f706c61696e3b636861727365743d7574662d3800077072696d65730a6821c1c7a3a38df198a4fcde7d5dac5819ed19ff4d25bb893c9511f8e1f51d59326eff00000000" +``` +
+
GET diff --git a/src/index.rs b/src/index.rs index b5e035b668..f2e82437e3 100644 --- a/src/index.rs +++ b/src/index.rs @@ -1610,6 +1610,10 @@ impl Index { self.client.get_raw_transaction(&txid, None).into_option() } + pub fn get_transaction_hex(&self, txid: Txid) -> Result> { + self.client.get_raw_transaction_hex(&txid, None).into_option() + } + pub fn find(&self, sat: Sat) -> Result> { let sat = sat.0; let rtx = self.begin_read()?; diff --git a/src/subcommand/server.rs b/src/subcommand/server.rs index cdbaf763b2..5a995a2370 100644 --- a/src/subcommand/server.rs +++ b/src/subcommand/server.rs @@ -263,6 +263,7 @@ impl Server { "/r/parents/:inscription_id/:page", get(Self::parents_recursive_paginated), ) + .route("/r/txhex/:txid", get(Self::get_raw_transaction_hex)) .route("/r/sat/:sat_number", get(Self::sat_inscriptions)) .route( "/r/sat/:sat_number/:page", @@ -1088,6 +1089,19 @@ impl Server { }) } + async fn get_raw_transaction_hex( + Extension(index): Extension>, + Path(txid): Path, + ) -> ServerResult> { + task::block_in_place(|| { + let raw_transaction_hex = index + .get_transaction_hex(txid)? + .ok_or_not_found(|| format!("transaction {txid}"))?; + + Ok(Json(raw_transaction_hex)) + }) + } + async fn decode( Extension(index): Extension>, Path(txid): Path, @@ -4351,6 +4365,20 @@ mod tests { ); } + #[test] + fn raw_transaction_hex() { + let test_server = TestServer::new(); + + let coinbase_tx = test_server.mine_blocks(1)[0].txdata[0].clone(); + let txid = coinbase_tx.compute_txid(); + + test_server.assert_response( + format!("/r/txhex/{txid}"), + StatusCode::OK, + "\"02000000010000000000000000000000000000000000000000000000000000000000000000ffffffff0151ffffffff0100f2052a01000000225120be7cbbe9ca06a7d7b2a17c6b4ff4b85b362cbcd7ee1970daa66dfaa834df59a000000000\"" + ); + } + #[test] fn detect_unrecoverable_reorg() { let test_server = TestServer::new(); From 00dcdf63449439209d888941bd079f87605f1b00 Mon Sep 17 00:00:00 2001 From: raphjaph Date: Mon, 27 Jan 2025 21:11:30 +0100 Subject: [PATCH 2/7] Rename to /r/tx/ --- docs/src/guides/api.md | 2 +- docs/src/inscriptions/recursion.md | 6 +++--- src/index.rs | 5 ++++- src/subcommand/server.rs | 18 +++++++++--------- 4 files changed, 17 insertions(+), 14 deletions(-) diff --git a/docs/src/guides/api.md b/docs/src/guides/api.md index f53f77afc0..dc6c336931 100644 --- a/docs/src/guides/api.md +++ b/docs/src/guides/api.md @@ -5129,4 +5129,4 @@ curl -s -H "Accept: application/json" \ See [Recursion](../inscriptions/recursion.md) for an explanation of these. -{{#include ../inscriptions/recursion.md:35:3429}} +{{#include ../inscriptions/recursion.md:35:3480}} diff --git a/docs/src/inscriptions/recursion.md b/docs/src/inscriptions/recursion.md index 616ecdea7e..98fe7cb70e 100644 --- a/docs/src/inscriptions/recursion.md +++ b/docs/src/inscriptions/recursion.md @@ -247,18 +247,18 @@ curl -s \
GET - /r/txhex/<TRANSACTION_ID> + /r/tx/<TRANSACTION_ID> ### Description -Raw transaction hex for ``. +Raw transaction for `` in segwit BIP144 hex encoding. ### Example ```bash curl -s -H "Accept: application/json" \ - http://0.0.0.0:80/r/txhex/60bcf821240064a9c55225c4f01711b0ebbcab39aa3fafeefe4299ab158536fa + http://0.0.0.0:80/r/tx/60bcf821240064a9c55225c4f01711b0ebbcab39aa3fafeefe4299ab158536fa ``` ```json diff --git a/src/index.rs b/src/index.rs index 0618bd2234..1b66559cbd 100644 --- a/src/index.rs +++ b/src/index.rs @@ -1616,7 +1616,10 @@ impl Index { } pub fn get_transaction_hex(&self, txid: Txid) -> Result> { - self.client.get_raw_transaction_hex(&txid, None).into_option() + self + .client + .get_raw_transaction_hex(&txid, None) + .into_option() } pub fn find(&self, sat: Sat) -> Result> { diff --git a/src/subcommand/server.rs b/src/subcommand/server.rs index 82c2b5a828..17d40a62b1 100644 --- a/src/subcommand/server.rs +++ b/src/subcommand/server.rs @@ -263,7 +263,7 @@ impl Server { "/r/parents/{inscription_id}/{page}", get(Self::parents_recursive_paginated), ) - .route("/r/txhex/:txid", get(Self::get_raw_transaction_hex)) + .route("/r/tx/{txid}", get(Self::get_transaction_hex)) .route("/r/sat/{sat_number}", get(Self::sat_inscriptions)) .route( "/r/sat/{sat_number}/at/{index}", @@ -1196,16 +1196,16 @@ impl Server { }) } - async fn get_raw_transaction_hex( + async fn get_transaction_hex( Extension(index): Extension>, Path(txid): Path, ) -> ServerResult> { task::block_in_place(|| { - let raw_transaction_hex = index - .get_transaction_hex(txid)? - .ok_or_not_found(|| format!("transaction {txid}"))?; - - Ok(Json(raw_transaction_hex)) + Ok(Json( + index + .get_transaction_hex(txid)? + .ok_or_not_found(|| format!("transaction {txid}"))?, + )) }) } @@ -4534,14 +4534,14 @@ mod tests { } #[test] - fn raw_transaction_hex() { + fn recursive_transaction_hex_endpoint() { let test_server = TestServer::new(); let coinbase_tx = test_server.mine_blocks(1)[0].txdata[0].clone(); let txid = coinbase_tx.compute_txid(); test_server.assert_response( - format!("/r/txhex/{txid}"), + format!("/r/tx/{txid}"), StatusCode::OK, "\"02000000010000000000000000000000000000000000000000000000000000000000000000ffffffff0151ffffffff0100f2052a01000000225120be7cbbe9ca06a7d7b2a17c6b4ff4b85b362cbcd7ee1970daa66dfaa834df59a000000000\"" ); From f3a0636cb0b93a96cdfbf9ab9c49fdf99667ca6c Mon Sep 17 00:00:00 2001 From: raphjaph Date: Mon, 27 Jan 2025 21:17:38 +0100 Subject: [PATCH 3/7] Amend --- docs/src/inscriptions/recursion.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/src/inscriptions/recursion.md b/docs/src/inscriptions/recursion.md index 98fe7cb70e..0298efee8f 100644 --- a/docs/src/inscriptions/recursion.md +++ b/docs/src/inscriptions/recursion.md @@ -252,7 +252,7 @@ curl -s \ ### Description -Raw transaction for `` in segwit BIP144 hex encoding. +Get hex-encoded transaction with ``. ### Example From 9519da6d3a58a936c2e4722057b1c97e3c7d4bb9 Mon Sep 17 00:00:00 2001 From: raphjaph Date: Mon, 27 Jan 2025 21:21:07 +0100 Subject: [PATCH 4/7] Amend --- docs/src/inscriptions/recursion.md | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/docs/src/inscriptions/recursion.md b/docs/src/inscriptions/recursion.md index 0298efee8f..7e5fa4a655 100644 --- a/docs/src/inscriptions/recursion.md +++ b/docs/src/inscriptions/recursion.md @@ -252,7 +252,10 @@ curl -s \ ### Description -Get hex-encoded transaction with ``. +Get hex-encoded transaction with ``. In the event of a future +change to Bitcoin that changes transaction serialization in a +backwards-incompatible fashion, such as SegWit, this endpoint is guaranteed to +remain backwards compatible. ### Example From 1196033d0cbe2148b06b6ee846c074156c247a08 Mon Sep 17 00:00:00 2001 From: raphjaph Date: Mon, 27 Jan 2025 21:30:31 +0100 Subject: [PATCH 5/7] Return genesis hex as well --- docs/src/guides/api.md | 2 +- docs/src/inscriptions/recursion.md | 50 +++++++++++++++--------------- src/index.rs | 8 ++++- src/subcommand/server.rs | 6 ++-- 4 files changed, 36 insertions(+), 30 deletions(-) diff --git a/docs/src/guides/api.md b/docs/src/guides/api.md index dc6c336931..f5dda88ce4 100644 --- a/docs/src/guides/api.md +++ b/docs/src/guides/api.md @@ -5129,4 +5129,4 @@ curl -s -H "Accept: application/json" \ See [Recursion](../inscriptions/recursion.md) for an explanation of these. -{{#include ../inscriptions/recursion.md:35:3480}} +{{#include ../inscriptions/recursion.md:35:3483}} diff --git a/docs/src/inscriptions/recursion.md b/docs/src/inscriptions/recursion.md index 7e5fa4a655..c7b43de5ab 100644 --- a/docs/src/inscriptions/recursion.md +++ b/docs/src/inscriptions/recursion.md @@ -244,31 +244,6 @@ curl -s \ ```
-
- - GET - /r/tx/<TRANSACTION_ID> - - -### Description - -Get hex-encoded transaction with ``. In the event of a future -change to Bitcoin that changes transaction serialization in a -backwards-incompatible fashion, such as SegWit, this endpoint is guaranteed to -remain backwards compatible. - -### Example - -```bash -curl -s -H "Accept: application/json" \ - http://0.0.0.0:80/r/tx/60bcf821240064a9c55225c4f01711b0ebbcab39aa3fafeefe4299ab158536fa -``` - -```json -"0100000000010183572872dcb32bee57003d53c2b8dbb5bc5819ff6478052599911f7778d1c7bd0000000000fdffffff011027000000000000225120e41e0cba05c6ac797cf543ff9a6c619a91a53813e59146d1e32ea89747b111a603407aa50d93d6fc01265fd52d3edc93af4e009ccc1a704ce1b5cb8ede1412a5df31eba587d080b3dc903ceb9002ed9d921aad323fd44d7b4dc2a1ad2ea12d4360424d20c7a3a38df198a4fcde7d5dac5819ed19ff4d25bb893c9511f8e1f51d59326effac0063036f7264010118746578742f706c61696e3b636861727365743d7574662d3800077072696d65730a6821c1c7a3a38df198a4fcde7d5dac5819ed19ff4d25bb893c9511f8e1f51d59326eff00000000" -``` -
-
GET @@ -3424,6 +3399,31 @@ Hello, world!
+
+ + GET + /r/tx/<TRANSACTION_ID> + + +### Description + +Get hex-encoded transaction with ``. In the event of a future +change to Bitcoin that changes transaction serialization in a +backwards-incompatible fashion, such as SegWit, this endpoint is guaranteed to +remain backwards compatible. + +### Example + +```bash +curl -s -H "Accept: application/json" \ + http://0.0.0.0:80/r/tx/60bcf821240064a9c55225c4f01711b0ebbcab39aa3fafeefe4299ab158536fa +``` + +```json +"0100000000010183572872dcb32bee57003d53c2b8dbb5bc5819ff6478052599911f7778d1c7bd0000000000fdffffff011027000000000000225120e41e0cba05c6ac797cf543ff9a6c619a91a53813e59146d1e32ea89747b111a603407aa50d93d6fc01265fd52d3edc93af4e009ccc1a704ce1b5cb8ede1412a5df31eba587d080b3dc903ceb9002ed9d921aad323fd44d7b4dc2a1ad2ea12d4360424d20c7a3a38df198a4fcde7d5dac5819ed19ff4d25bb893c9511f8e1f51d59326effac0063036f7264010118746578742f706c61696e3b636861727365743d7574662d3800077072696d65730a6821c1c7a3a38df198a4fcde7d5dac5819ed19ff4d25bb893c9511f8e1f51d59326eff00000000" +``` +
+
GET diff --git a/src/index.rs b/src/index.rs index 1b66559cbd..55541a8198 100644 --- a/src/index.rs +++ b/src/index.rs @@ -1615,7 +1615,13 @@ impl Index { self.client.get_raw_transaction(&txid, None).into_option() } - pub fn get_transaction_hex(&self, txid: Txid) -> Result> { + pub fn get_transaction_hex_recursive(&self, txid: Txid) -> Result> { + if txid == self.genesis_block_coinbase_txid { + return Ok(Some(consensus::encode::serialize_hex( + &self.genesis_block_coinbase_transaction, + ))); + } + self .client .get_raw_transaction_hex(&txid, None) diff --git a/src/subcommand/server.rs b/src/subcommand/server.rs index 17d40a62b1..2582e7beb4 100644 --- a/src/subcommand/server.rs +++ b/src/subcommand/server.rs @@ -263,7 +263,7 @@ impl Server { "/r/parents/{inscription_id}/{page}", get(Self::parents_recursive_paginated), ) - .route("/r/tx/{txid}", get(Self::get_transaction_hex)) + .route("/r/tx/{txid}", get(Self::get_transaction_hex_recursive)) .route("/r/sat/{sat_number}", get(Self::sat_inscriptions)) .route( "/r/sat/{sat_number}/at/{index}", @@ -1196,14 +1196,14 @@ impl Server { }) } - async fn get_transaction_hex( + async fn get_transaction_hex_recursive( Extension(index): Extension>, Path(txid): Path, ) -> ServerResult> { task::block_in_place(|| { Ok(Json( index - .get_transaction_hex(txid)? + .get_transaction_hex_recursive(txid)? .ok_or_not_found(|| format!("transaction {txid}"))?, )) }) From f98dbc42c743e82d787b6ba19ece7619ae329d6d Mon Sep 17 00:00:00 2001 From: raphjaph Date: Mon, 27 Jan 2025 21:36:38 +0100 Subject: [PATCH 6/7] Add genesis transaction test --- docs/src/inscriptions/recursion.md | 3 +-- src/subcommand/server.rs | 13 +++++++++++++ 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/docs/src/inscriptions/recursion.md b/docs/src/inscriptions/recursion.md index c7b43de5ab..8b8c6dd080 100644 --- a/docs/src/inscriptions/recursion.md +++ b/docs/src/inscriptions/recursion.md @@ -3415,8 +3415,7 @@ remain backwards compatible. ### Example ```bash -curl -s -H "Accept: application/json" \ - http://0.0.0.0:80/r/tx/60bcf821240064a9c55225c4f01711b0ebbcab39aa3fafeefe4299ab158536fa +curl -s http://0.0.0.0:80/r/tx/60bcf821240064a9c55225c4f01711b0ebbcab39aa3fafeefe4299ab158536fa ``` ```json diff --git a/src/subcommand/server.rs b/src/subcommand/server.rs index 2582e7beb4..f7e2fdd893 100644 --- a/src/subcommand/server.rs +++ b/src/subcommand/server.rs @@ -4547,6 +4547,19 @@ mod tests { ); } + #[test] + fn recursive_transaction_hex_endpoint_for_genesis_transaction() { + let test_server = TestServer::new(); + + test_server.mine_blocks(1); + + test_server.assert_response( + format!("/r/tx/4a5e1e4baab89f3a32518a88c31bc87f618f76673e2cc77ab2127b7afdeda33b"), + StatusCode::OK, + "\"01000000010000000000000000000000000000000000000000000000000000000000000000ffffffff4d04ffff001d0104455468652054696d65732030332f4a616e2f32303039204368616e63656c6c6f72206f6e206272696e6b206f66207365636f6e64206261696c6f757420666f722062616e6b73ffffffff0100f2052a01000000434104678afdb0fe5548271967f1a67130b7105cd6a828e03909a67962e0ea1f61deb649f6bc3f4cef38c4f35504e51ec112de5c384df7ba0b8d578a4c702b6bf11d5fac00000000\"" + ); + } + #[test] fn detect_unrecoverable_reorg() { let test_server = TestServer::new(); From c31cb2033dd6b6a3149d29ba3edf2f83ceb18484 Mon Sep 17 00:00:00 2001 From: raphjaph Date: Mon, 27 Jan 2025 21:37:38 +0100 Subject: [PATCH 7/7] Amend --- src/subcommand/server.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/subcommand/server.rs b/src/subcommand/server.rs index f7e2fdd893..97da99e408 100644 --- a/src/subcommand/server.rs +++ b/src/subcommand/server.rs @@ -4554,7 +4554,7 @@ mod tests { test_server.mine_blocks(1); test_server.assert_response( - format!("/r/tx/4a5e1e4baab89f3a32518a88c31bc87f618f76673e2cc77ab2127b7afdeda33b"), + "/r/tx/4a5e1e4baab89f3a32518a88c31bc87f618f76673e2cc77ab2127b7afdeda33b", StatusCode::OK, "\"01000000010000000000000000000000000000000000000000000000000000000000000000ffffffff4d04ffff001d0104455468652054696d65732030332f4a616e2f32303039204368616e63656c6c6f72206f6e206272696e6b206f66207365636f6e64206261696c6f757420666f722062616e6b73ffffffff0100f2052a01000000434104678afdb0fe5548271967f1a67130b7105cd6a828e03909a67962e0ea1f61deb649f6bc3f4cef38c4f35504e51ec112de5c384df7ba0b8d578a4c702b6bf11d5fac00000000\"" );