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

Delegator content /r/undelegated-content/<INSCRIPTION_ID> #3932

Merged
merged 14 commits into from
Oct 26, 2024
96 changes: 54 additions & 42 deletions docs/src/inscriptions/recursion.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,10 @@ Recursion has a number of interesting use-cases:
## Endpoints

<details>
<summary>
<summary>
<code>GET</code>
<code><b>/content/&lt;INSCRIPTION_ID&gt;</b></code>
</summary>
</summary>

### Description
The content of the inscription with `<INSCRIPTION_ID>`
Expand All @@ -50,67 +50,67 @@ curl -s -H "Accept: application/json" \
</details>

<details>
<summary>
<summary>
<code>GET</code>
<code><b>/r/blockhash</b></code>
</summary>
</summary>

### Description
Latest block hash in text.
Latest block hash.

### Example
```bash
curl -s \
http://0.0.0.0:80/r/blockhash
```
```text
```json
"00000000000000000002891b440944e0ce40b37b6ccaa138c280e9edfc319d5d"
```
</details>

<details>
<summary>
<summary>
<code>GET</code>
<code><b>/r/blockhash/&lt;HEIGHT&gt;</b></code>
</summary>
</summary>

### Description
Block hash at given block height in text.
Block hash at given block height as JSON string.

### Example
```bash
curl -s \
http://0.0.0.0:80/r/blockhash/840000
```
```text
```json
"0000000000000000000320283a032748cef8227873ff4872689bf23f1cda83a5"
```
</details>

<details>
<summary>
<summary>
<code>GET</code>
<code><b>/r/blockheight</b></code>
</summary>
</summary>

### Description
Latest block height in text.
Latest block height.

### Example
```bash
curl -s \
http://0.0.0.0:80/r/blockheight
```
```text
```json
866393
```
</details>

<details>
<summary>
<summary>
<code>GET</code>
<code><b>/r/blockinfo/&lt;QUERY&gt;</b></code>
</summary>
</summary>

### Description
Block info. `<QUERY>` may be a block height or block hash.
Expand Down Expand Up @@ -207,29 +207,29 @@ curl -s -H "Accept: application/json" \
</details>

<details>
<summary>
<summary>
<code>GET</code>
<code><b>/r/blocktime</b></code>
</summary>
</summary>

### Description
UNIX time stamp of latest block in text.
UNIX time stamp of latest block.

### Example
```bash
curl -s \
http://0.0.0.0:80/r/blocktime
```
```text
```json
1729362253
```
</details>

<details>
<summary>
<summary>
<code>GET</code>
<code><b>/r/children/&lt;INSCRIPTION_ID&gt;</b></code>
</summary>
</summary>

### Description
The first 100 child inscription ids.
Expand Down Expand Up @@ -350,10 +350,10 @@ curl -s -H "Accept: application/json" \
</details>

<details>
<summary>
<summary>
<code>GET</code>
<code><b>/r/children/&lt;INSCRIPTION_ID&gt;/&lt;PAGE&gt;</b></code>
</summary>
</summary>

### Description
The set of 100 child inscription ids on `<PAGE>`.
Expand Down Expand Up @@ -474,10 +474,10 @@ curl -s -H "Accept: application/json" \
</details>

<details>
<summary>
<summary>
<code>GET</code>
<code><b>/r/children/&lt;INSCRIPTION_ID&gt;/inscriptions</b></code>
</summary>
</summary>

### Description
Details of the first 100 child inscriptions.
Expand Down Expand Up @@ -1796,10 +1796,10 @@ curl -s -H "Accept: application/json" \
</details>

<details>
<summary>
<summary>
<code>GET</code>
<code><b>/r/children/&lt;INSCRIPTION_ID&gt;/inscriptions/&lt;PAGE&gt;</b></code>
</summary>
</summary>

### Description
Details of the set of 100 child inscriptions on &lt;PAGE&gt;.
Expand Down Expand Up @@ -3119,10 +3119,22 @@ curl -s -H "Accept: application/json" \
</details>

<details>
<summary>
<summary>
<code>GET</code>
<code><b>/r/undelegated-content/&lt;INSCRIPTION_ID&gt;</b></code>
</summary>

### Description
Undelegated content of an inscription.

</details>


<details>
<summary>
<code>GET</code>
<code><b>/r/inscription/&lt;INSCRIPTION_ID&gt;</b></code>
</summary>
</summary>

### Description
Information about an inscription.
Expand Down Expand Up @@ -3155,10 +3167,10 @@ curl -s -H "Accept: application/json" \
</details>

<details>
<summary>
<summary>
<code>GET</code>
<code><b>/r/metadata/&lt;INSCRIPTION_ID&gt;</b></code>
</summary>
</summary>

### Description
JSON string containing the hex-encoded CBOR metadata.
Expand All @@ -3174,10 +3186,10 @@ curl -s -H "Accept: application/json" \
</details>

<details>
<summary>
<summary>
<code>GET</code>
<code><b>/r/parents/&lt;INSCRIPTION_ID&gt;</b></code>
</summary>
</summary>

### Description
The first 100 parent inscription ids.
Expand All @@ -3199,10 +3211,10 @@ curl -s -H "Accept: application/json" \
</details>

<details>
<summary>
<summary>
<code>GET</code>
<code><b>/r/parents/&lt;INSCRIPTION_ID&gt;/&lt;PAGE&gt;</b></code>
</summary>
</summary>

### Description
The set of 100 parent inscription ids on `<PAGE>`.
Expand All @@ -3222,10 +3234,10 @@ curl -s -H "Accept: application/json" \
</details>

<details>
<summary>
<summary>
<code>GET</code>
<code><b>/r/sat/&lt;SAT_NUMBER&gt;</b></code>
</summary>
</summary>

### Description
The first 100 inscription ids on a sat. Requires index with `--index-sats` flag.
Expand All @@ -3250,10 +3262,10 @@ curl -s -H "Accept: application/json" \
</details>

<details>
<summary>
<summary>
<code>GET</code>
<code><b>/r/sat/&lt;SAT_NUMBER&gt;/&lt;PAGE&gt;</b></code>
</summary>
</summary>

### Description
The set of 100 inscription ids on `<PAGE>`. Requires index with `--index-sats` flag.
Expand Down Expand Up @@ -3285,10 +3297,10 @@ curl -s -H "Accept: application/json" \
</details>

<details>
<summary>
<summary>
<code>GET</code>
<code><b>/r/sat/&lt;SAT_NUMBER&gt;/at/&lt;INDEX&gt;</b></code>
</summary>
</summary>

### Description
The inscription id at `<INDEX>` of all inscriptions on a sat. `<INDEX>` may be a negative number to index from the back. `0` being the first and `-1` being the most recent for example. Requires index with `--index-sats` flag.
Expand Down
103 changes: 103 additions & 0 deletions src/subcommand/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,10 @@ impl Server {
"/r/children/:inscription_id/inscriptions/:page",
get(Self::child_inscriptions_recursive_paginated),
)
.route(
"/r/undelegated-content/:inscription_id",
get(Self::undelegated_content),
)
.route("/r/metadata/:inscription_id", get(Self::metadata))
.route("/r/parents/:inscription_id", get(Self::parents_recursive))
.route(
Expand Down Expand Up @@ -1467,6 +1471,30 @@ impl Server {
})
}

async fn undelegated_content(
Extension(index): Extension<Arc<Index>>,
Extension(settings): Extension<Arc<Settings>>,
Extension(server_config): Extension<Arc<ServerConfig>>,
Path(inscription_id): Path<InscriptionId>,
accept_encoding: AcceptEncoding,
) -> ServerResult {
task::block_in_place(|| {
if settings.is_hidden(inscription_id) {
return Ok(PreviewUnknownHtml.into_response());
}

let inscription = index
.get_inscription_by_id(inscription_id)?
.ok_or_not_found(|| format!("inscription {inscription_id}"))?;

Ok(
Self::content_response(inscription, accept_encoding, &server_config)?
.ok_or_not_found(|| format!("inscription {inscription_id} content"))?
.into_response(),
)
})
}

fn content_response(
inscription: Inscription,
accept_encoding: AcceptEncoding,
Expand Down Expand Up @@ -6576,6 +6604,81 @@ next
);
}

#[test]
fn undelegated_content() {
let server = TestServer::builder().chain(Chain::Regtest).build();

server.mine_blocks(1);

let delegate = Inscription {
content_type: Some("text/plain".into()),
body: Some("foo".into()),
..default()
};

let delegate_txid = server.core.broadcast_tx(TransactionTemplate {
inputs: &[(1, 0, 0, delegate.to_witness())],
..default()
});

let delegate_id = InscriptionId {
txid: delegate_txid,
index: 0,
};

server.mine_blocks(1);

let inscription = Inscription {
content_type: Some("text/plain".into()),
body: Some("bar".into()),
delegate: Some(delegate_id.value()),
..default()
};

let txid = server.core.broadcast_tx(TransactionTemplate {
inputs: &[(2, 0, 0, inscription.to_witness())],
..default()
});

server.mine_blocks(1);

let id = InscriptionId { txid, index: 0 };

server.assert_response(
format!("/r/undelegated-content/{id}"),
StatusCode::OK,
"bar",
);

server.assert_response(format!("/content/{id}"), StatusCode::OK, "foo");

// Test normal inscription without delegate
let normal_inscription = Inscription {
content_type: Some("text/plain".into()),
body: Some("baz".into()),
..default()
};

let normal_txid = server.core.broadcast_tx(TransactionTemplate {
inputs: &[(3, 0, 0, normal_inscription.to_witness())],
..default()
});

server.mine_blocks(1);

let normal_id = InscriptionId {
txid: normal_txid,
index: 0,
};

server.assert_response(
format!("/r/undelegated-content/{normal_id}"),
StatusCode::OK,
"baz",
);
server.assert_response(format!("/content/{normal_id}"), StatusCode::OK, "baz");
}

#[test]
fn content_proxy() {
let server = TestServer::builder().chain(Chain::Regtest).build();
Expand Down
Loading