ordinals / ord

👁‍🗨 Rare and exotic sats
https://ordinals.com
Creative Commons Zero v1.0 Universal
3.85k stars 1.38k forks source link

JSON-RPC error: transport error: unexpected HTTP code: 404 #2707

Open easeev opened 11 months ago

easeev commented 11 months ago
❯ RUST_LOG=debug ./target/release/ord --chain testnet --bitcoin-rpc-user user --bitcoin-rpc-pass pass --rpc-url nd-111-222-333.p2pify.com:80 server
[2023-11-21T06:55:38Z INFO  ord::options] Connecting to Bitcoin Core at nd-111-222-333.p2pify.com:80/wallet/ord
[2023-11-21T06:55:38Z DEBUG bitcoincore_rpc] JSON-RPC request: getblockchaininfo []
[2023-11-21T06:55:38Z DEBUG bitcoincore_rpc] JSON-RPC failed parsing reply of getblockchaininfo: JsonRpc(Transport(HttpErrorCode(404)))
error: JSON-RPC error: transport error: unexpected HTTP code: 404

Whereas

❯ ./src/bitcoin-cli -rpcconnect=nd-111-222-333.p2pify.com -rpcport=80 -rpcuser=user -rpcpassword=pass getblockchaininfo
{
  "chain": "test",
  "blocks": 2539577,
  "headers": 2539577,
  ...
}

And

❯ curl --data-binary '{"jsonrpc": "1.0", "id": "1", "method": "getblockchaininfo", "params": []}' -H 'content-type: text/plain;' http://user:pass@nd-111-222-333.p2pify.com/wallet/ord
{"result":{"chain":"test","blocks":2539577,"headers":2539577, ... },"error":null,"id":"1"}

After looking into the traffic it becomes apparent that ord is resolving the hostname to IP and then querying it instead of making a query using the provided hostname

CleanShot 2023-11-21 at 15 08 46@2x

Looks like something that should be fixed

raphjaph commented 11 months ago

Is the issue that the IP address is changing or is the recipient (server hosting the bitcoin node) doing some dispatch using the host header?

easeev commented 11 months ago

Is the issue that the IP address is changing or is the recipient (server hosting the bitcoin node) doing some dispatch using the host header?

Both. IP can change at any time as it's dynamically provisioned, as well as routing is done via hostname instead of IP

raphjaph commented 11 months ago

Looked into it and the reason for this is because bitcoincore_rpc crate we are using uses a minimal dependency http transport. It does allow providing a custom transport through a public trait Transport, we plan do upgrade to reqwest soon anyways so that might help (#2716). We would have to do something like this:

struct Reqwest {
    client: reqwest::blocking::Client,
    url: String,
}

impl Transport for Reqwest {
    /// Sends an RPC request over the transport.
    fn send_request(&self, request: Request) -> Result<Response, Error> {
        let body = serde_json::to_vec(&request)?;

        let response = self.client.post(&self.url).body(body).send().unwrap();

        Ok(response.json().unwrap())
    }

    /// Sends a batch of RPC requests over the transport.
    fn send_batch(&self, _: &[Request]) -> Result<Vec<Response>, Error> {
        todo!()
    }

    /// Formats the target of this transport. I.e. the URL/socket/...
    fn fmt_target(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
        todo!()
    }
}