Skip to content

rpc: fix hash encoding in JSON parameters - #6813

Merged
creachadair merged 7 commits into
masterfrom
encoding-inconsistency
Sep 9, 2021
Merged

creachadair merged 7 commits into
masterfrom
encoding-inconsistency

Conversation

@creachadair

Copy link
Copy Markdown

The responses from node RPCs encode hash values as hexadecimal strings. In
some cases, however, hashes received as JSON parameters were being decoded as
byte buffers.

This resulted in the confusing situation that a hash reported by one request
(e.g., broadcast_tx_commit) could not be passed as a parameter to another
(e.g., tx) via JSON, without translating the hex-encoded output hash into the
base64 encoding used by JSON for opaque bytes.

Addresses #6802.

Note to reviewers: These changes satisfy the existing tests, but the existing test
suite verifies only that requests round-trip. We do not currently have any tests to
verify encoding expectations.

@codecov

codecov Bot commented Aug 10, 2021 •

Copy link
Copy Markdown

Codecov Report

Merging #6813 (447c0a8) into master (dc0e04d) will decrease coverage by 0.15%.
The diff coverage is 0.00%.

❗ Current head 447c0a8 differs from pull request most recent head f451339. Consider uploading reports for the commit f451339 to get more accurate results

@@            Coverage Diff             @@
##           master    #6813      +/-   ##
==========================================
- Coverage   62.74%   62.58%   -0.16%     
==========================================
  Files         310      310              
  Lines       40815    40823       +8     
==========================================
- Hits        25608    25550      -58     
- Misses      13388    13450      +62     
- Partials     1819     1823       +4     
Impacted Files Coverage Δ
rpc/client/mock/client.go 18.03% <0.00%> (ø)
rpc/core/blocks.go 26.50% <0.00%> (-0.66%) ⬇️
rpc/core/tx.go 0.00% <0.00%> (ø)
internal/statesync/dispatcher.go 74.69% <0.00%> (-5.56%) ⬇️
internal/consensus/reactor.go 67.28% <0.00%> (-4.35%) ⬇️
internal/statesync/block_queue.go 92.36% <0.00%> (-2.78%) ⬇️
internal/consensus/peer_state.go 79.69% <0.00%> (-2.47%) ⬇️
privval/signer_listener_endpoint.go 88.52% <0.00%> (-2.46%) ⬇️
internal/blocksync/v0/pool.go 80.09% <0.00%> (-1.75%) ⬇️
libs/pubsub/pubsub.go 78.86% <0.00%> (-1.51%) ⬇️
... and 12 more

@creachadair
creachadair force-pushed the encoding-inconsistency branch from 8803fb5 to cf221dc Compare August 10, 2021 19:32
@creachadair
creachadair marked this pull request as ready for review August 10, 2021 21:36
@williambanfield

Copy link
Copy Markdown
Contributor

The code looks great. If you're able to get this running, would you mind posting a quick example of the RPC call?

@tac0turtle tac0turtle left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

utACK.

Changlog and upgrading.md entries would be good.

Also any docs that mention base64 should be changed

@creachadair

creachadair commented Aug 11, 2021 •

Copy link
Copy Markdown
Author

The code looks great. If you're able to get this running, would you mind posting a quick example of the RPC call?

Repro:

git clone https://github.com/tendermint/tendermint 
pushd tendermint
git checkout -b repro origin/encoding-inconsistency
make build

# -- Terminal A
./build/tendermint --home repro.tendermint init validator
./build/tendermint --home repro.tendermint start --proxy-app=kvstore --consensus.create-empty-blocks=false
# N.B. Disabling empty blocks isn't necessary for repro, it just makes the logs less spammy.

# -- Terminal B
curl -s 'http://localhost:26657/broadcast_tx_commit?tx="hash-me=maybe"'           
## output: https://git.io/JRP5L
##     "hash": "BD83A7167763FEB5D0B137A3591C74A45E9BC87A47D7E500A8D933199A08AE27"

curl -s 'http://localhost:26657/tx?hash=0xBD83A7167763FEB5D0B137A3591C74A45E9BC87A47D7E500A8D933199A08AE27'
## output: https://git.io/JRP5o

curl -s -X POST \
  -H "Content-Type: application/json" \
  http://localhost:26657 \
  -d '{"jsonrpc": "2.0", "id": 1000, "method": "tx", "params": {"hash": "BD83A7167763FEB5D0B137A3591C74A45E9BC87A47D7E500A8D933199A08AE27", "prove": false}}'
## output: https://git.io/JRPdi

This is essentially the same example as in #6802, but the "hash" parameter in the JSON call is now correctly interpreted as hex rather than base64.

@creachadair

Copy link
Copy Markdown
Author

Also any docs that mention base64 should be changed

The only documentation that explicitly mentions base64 is for transaction data, and that is still essentially correct. I did add a commit (2a344ca) to clarify the encoding rules a bit, and I think that's worth doing anyway, but it doesn't actually address the change here.

@creachadair

Copy link
Copy Markdown
Author

I do not think the e2e test failure is related to this change. Specifically, the failure seems to change even if I re-run without updating the PR. But I'm not sure if there's a better way to be sure than that.

@tac0turtle

Copy link
Copy Markdown
Contributor

I do not think the e2e test failure is related to this change. Specifically, the failure seems to change even if I re-run without updating the PR. But I'm not sure if there's a better way to be sure than that.

to me it looks like it is.

		hash := tx.Hash()
		waitTime := 20 * time.Second

		require.Eventuallyf(t, func() bool {
			txResp, err := client.Tx(ctx, hash, false)
			return err == nil && bytes.Equal(txResp.Tx, tx)
		}, waitTime, time.Second,
			"submitted tx %X wasn't committed after %v", hash, waitTime,
		)

here the encoding is wrong on the query.

@creachadair

Copy link
Copy Markdown
Author

here the encoding is wrong on the query.

Aha, thank you, that is very helpful. I'm surprised this didn't get reported as an error elsewhere in the log, though. Anyway, that's a clue I can use to find the case that needs fixed.

@creachadair
creachadair force-pushed the encoding-inconsistency branch 2 times, most recently from 137c1c0 to af0ac3e Compare September 9, 2021 02:16
@creachadair
creachadair force-pushed the encoding-inconsistency branch from 424bdc6 to 87ed3e5 Compare September 9, 2021 02:29
@creachadair

Copy link
Copy Markdown
Author

I finally figured out why this was working locally but not in the end-to-end tests: I hadn't updated all the client implementations and the e2e plumbing tickled at least one of the ones I'd missed. Applying the change to all of them averts the inconsistency. Or at least I think I got them all this time. 🤪

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants