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

Unmarshaling error from response fails with: cannot unmarshal object into Go struct field response.error of type string #5

Open
nkuba opened this issue Nov 15, 2022 · 1 comment · May be fixed by #9

Comments

@nkuba
Copy link

nkuba commented Nov 15, 2022

Responses unmarshaling don't work for errors returned by ElectrumX and Fulcrum servers.

I called the GetBlockHeader function with a block height that doesn't exist.

func (s *Client) GetBlockHeader(ctx context.Context, height uint32, checkpointHeight ...uint32) (*GetBlockHeaderResult, error) {

I observed the following error being returned:

Unmarshal received message failed: json: cannot unmarshal object into Go struct field response.error of type string

The reason for that is that ElectrumX, Fulcrum, and Electrs/Esplora are inconsistent when it comes to the error response format.
Please see the examples (for Bitcoin Testnet servers):

ElectrumX

echo '{"jsonrpc": "2.0", "method": "server.version", "id": 0}' | netcat 49.12.127.114 10068
{"jsonrpc":"2.0","result":["ElectrumX 1.16.0","1.4"],"id":0}

✗ echo '{"jsonrpc": "2.0", "method": "blockchain.block.header", "params": [4294967295], "id": 0}' | netcat 49.12.127.114 10068
{"jsonrpc":"2.0","error":{"code":1,"message":"height 4,294,967,295 out of range"},"id":0}

Fulcrum

echo '{"jsonrpc": "2.0", "method": "server.version", "id": 0}' | netcat 203.132.94.196 51001
{"id":0,"jsonrpc":"2.0","result":["Fulcrum 1.8.2","1.4"]}

✗ echo '{"jsonrpc": "2.0", "method": "blockchain.block.header", "params": [4294967295], "id": 0}' | netcat 203.132.94.196 51001
{"error":{"code":1,"message":"Invalid height"},"id":0,"jsonrpc":"2.0"}

Electrs/Esplora

echo '{"jsonrpc": "2.0", "method": "server.version", "id": 0}' | netcat 35.225.54.191 50001
{"id":0,"jsonrpc":"2.0","result":["electrs-esplora 0.4.1","1.4"]}

✗ echo '{"jsonrpc": "2.0", "method": "blockchain.block.header", "params": [4294967295], "id": 0}' | netcat 35.225.54.191 50001
{"error":"missing header","id":0,"jsonrpc":"2.0"}

The client handles correctly only the response returned by the Electrs/Esplora server, as only this response format can be unmarshaled to the response struct that assumes a string as an error:

type response struct {
ID uint64 `json:"id"`
Method string `json:"method"`
Error string `json:"error"`
}

@nkuba
Copy link
Author

nkuba commented May 16, 2023

Turns out this is a bug in the esplora/electrs (Blockstream/esplora#453), as JSON-RPC 2.0 clearly defines the format for error object.

@nkuba nkuba linked a pull request May 24, 2023 that will close this issue
jpcummins pushed a commit to jpcummins/go-electrum that referenced this issue May 18, 2024
…ders-single

Expose the `SubscribeHeadersSingle` function
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
1 participant