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

Convert Tendermint Block Response #4922

Closed
wants to merge 28 commits into from
Closed

Convert Tendermint Block Response #4922

wants to merge 28 commits into from

Conversation

RiccardoM
Copy link
Contributor

@RiccardoM RiccardoM commented Aug 19, 2019

Converts a ResultBlock into a CosmosResultBlock having validator addresses represented as Bech32 addresses instead of Hex addresses.

Closes #3544

  • Linked to github-issue with discussion and accepted design OR link to spec that describes this work.
  • Wrote tests
  • Updated relevant documentation (docs/)
  • Added a relevant changelog entry to the Unreleased section in CHANGELOG.md
  • Re-reviewed Files changed in the github PR explorer

For Admin Use:

  • Added appropriate labels to PR (ex. wip, ready-for-review, docs)
  • Reviewers Assigned
  • Squashed all commits, uses message "Merge pull request #XYZ: [title]" (coding standards)

@RiccardoM
Copy link
Contributor Author

Should I write some tests for this? If so, which ones?

client/rpc/block.go Outdated Show resolved Hide resolved
client/rpc/block_converter.go Outdated Show resolved Hide resolved
client/rpc/block_converter.go Outdated Show resolved Hide resolved
client/rpc/block_converter.go Outdated Show resolved Hide resolved
client/rpc/block_converter.go Outdated Show resolved Hide resolved
go.mod Outdated Show resolved Hide resolved
client/rpc/block_converter.go Outdated Show resolved Hide resolved
client/rpc/block.go Outdated Show resolved Hide resolved
@codecov
Copy link

codecov bot commented Aug 19, 2019

Codecov Report

Merging #4922 into master will decrease coverage by 0.57%.
The diff coverage is 98.68%.

@@            Coverage Diff             @@
##           master    #4922      +/-   ##
==========================================
- Coverage   54.79%   54.22%   -0.58%     
==========================================
  Files         307      312       +5     
  Lines       18296    18611     +315     
==========================================
+ Hits        10026    10092      +66     
- Misses       7494     7733     +239     
- Partials      776      786      +10

client/rpc/block.go Outdated Show resolved Hide resolved
go.sum Outdated Show resolved Hide resolved
client/rpc/block_converter.go Show resolved Hide resolved
client/rpc/block_converter.go Show resolved Hide resolved
client/rpc/block_converter.go Show resolved Hide resolved
Copy link
Collaborator

@fedekunze fedekunze left a comment

Choose a reason for hiding this comment

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

Overall LGTM. I'd like to see a simple test to check that the block addresses start with the given prefix. Eg:

require.True(t, strings.HasPrefix(valAddress, "cosmos1"))

client/rpc/block_converter.go Show resolved Hide resolved
client/rpc/block_converter.go Outdated Show resolved Hide resolved
@RiccardoM
Copy link
Contributor Author

RiccardoM commented Oct 16, 2019

I've reverted back the go.sum and go.mod file to the master version.

I've also added a simple test. However, I would like to have a test that you can make sure works properly. For this I would need

  • A default block resposne
  • The mapping between the hash address and its Bech32 representation

I'm also not able to reproduce what @alexanderbez said about the response not having the data inside it

@alexanderbez
Copy link
Contributor

alexanderbez commented Oct 16, 2019

@RiccardoM I recommend you launch a local network and compare block responses pre and post changes. You'll see it's missing a bunch of the fields.

  1. checkout gaia master
  2. point sdk to your local via go mod edit ...
  3. start localnet make clean install localnet-start
  4. query for a block and compare....

@RiccardoM
Copy link
Contributor Author

@alexanderbez thanks for the tip. I've updated the code to fix those errors.
I've also added the overriding of int, int64 and uint64 fields in order to preserve their serialization as strings.

Copy link
Contributor

@alexanderbez alexanderbez left a comment

Choose a reason for hiding this comment

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

Looks pretty close @RiccardoM! Just general feedback on style. Also, starting to feel that it's a shame to introduce so much code just to change a single field type :-/

client/rpc/block_converter.go Outdated Show resolved Hide resolved
client/rpc/block_converter.go Outdated Show resolved Hide resolved
client/rpc/block_converter.go Outdated Show resolved Hide resolved
client/rpc/block_converter.go Outdated Show resolved Hide resolved
client/rpc/block_converter.go Outdated Show resolved Hide resolved
client/rpc/block_converter.go Show resolved Hide resolved
@RiccardoM
Copy link
Contributor Author

@alexanderbez Solved everything. It feels a bit like a shame, but I think it improves a lot the developer UX

@RiccardoM
Copy link
Contributor Author

Ping @alexanderbez the issues should be solved. Can we merge this?

Copy link
Collaborator

@fedekunze fedekunze left a comment

Choose a reason for hiding this comment

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

LGTM. I'd even say we could move the wrapper types into /types to provide an easy access to them for modules such as IBC.

Thoughts @alexanderbez @jackzampolin?

@RiccardoM
Copy link
Contributor Author

RiccardoM commented Nov 7, 2019

Up @alexanderbez, @jackzampolin @fedekunze. Updates on this?

Copy link
Contributor

@alessio alessio left a comment

Choose a reason for hiding this comment

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

Few changes are required

ctypes "github.com/tendermint/tendermint/rpc/core/types"
)

var defaultBlockJson = `{"block_meta":{"block_id":{"hash":"30CD3A9EF2082FF9F2575655E99E37ACC3936DC9E534ADF0BD7436C76258225C","parts":{"total":"1","hash":"463460DA73FA0AE441B6041BF2683AE625CE2D9FD290F4C86CD83D1A7FB9F439"}},"header":{"version":{"block":"10","app":"0"},"chain_id":"test-chain-RoCfX4","height":"16","time":"2019-10-16T09:47:21.054275229Z","num_txs":"0","total_txs":"0","last_block_id":{"hash":"168D140232DA3A59757658E257168B6BCE62024F574CA222C2D449BAB1AE4023","parts":{"total":"1","hash":"4E6D7A1C7DE6942470394C21B132A2D9F89B31908C3B6FA8C2AB96A87553B96E"}},"last_commit_hash":"3483509FCC8048496E9B85E1E4C90A3B6E5944F022FE3C0709096932A010F712","data_hash":"","validators_hash":"61E69204249E5EE6F777C0566040929276A1900CE665BCB11006CABF9A4ED9A3","next_validators_hash":"61E69204249E5EE6F777C0566040929276A1900CE665BCB11006CABF9A4ED9A3","consensus_hash":"048091BC7DDC283F77BFBF91D73C44DA58C3DF8A9CBC867405D8B7F3DAADA22F","app_hash":"070A8213F67F494F7D27A6296C29F0DF62DFA18FD37FB592D9761A44DEE96528","last_results_hash":"","evidence_hash":"","proposer_address":"E8B4AF895B301C3D7108CA93A0B9234E3A2A4B21"}},"block":{"header":{"version":{"block":"10","app":"0"},"chain_id":"test-chain-RoCfX4","height":"16","time":"2019-10-16T09:47:21.054275229Z","num_txs":"0","total_txs":"0","last_block_id":{"hash":"168D140232DA3A59757658E257168B6BCE62024F574CA222C2D449BAB1AE4023","parts":{"total":"1","hash":"4E6D7A1C7DE6942470394C21B132A2D9F89B31908C3B6FA8C2AB96A87553B96E"}},"last_commit_hash":"3483509FCC8048496E9B85E1E4C90A3B6E5944F022FE3C0709096932A010F712","data_hash":"","validators_hash":"61E69204249E5EE6F777C0566040929276A1900CE665BCB11006CABF9A4ED9A3","next_validators_hash":"61E69204249E5EE6F777C0566040929276A1900CE665BCB11006CABF9A4ED9A3","consensus_hash":"048091BC7DDC283F77BFBF91D73C44DA58C3DF8A9CBC867405D8B7F3DAADA22F","app_hash":"070A8213F67F494F7D27A6296C29F0DF62DFA18FD37FB592D9761A44DEE96528","last_results_hash":"","evidence_hash":"","proposer_address":"E8B4AF895B301C3D7108CA93A0B9234E3A2A4B21"},"data":{"txs":null},"evidence":{"evidence":null},"last_commit":{"block_id":{"hash":"168D140232DA3A59757658E257168B6BCE62024F574CA222C2D449BAB1AE4023","parts":{"total":"1","hash":"4E6D7A1C7DE6942470394C21B132A2D9F89B31908C3B6FA8C2AB96A87553B96E"}},"precommits":[{"type":2,"height":"15","round":"0","block_id":{"hash":"168D140232DA3A59757658E257168B6BCE62024F574CA222C2D449BAB1AE4023","parts":{"total":"1","hash":"4E6D7A1C7DE6942470394C21B132A2D9F89B31908C3B6FA8C2AB96A87553B96E"}},"timestamp":"2019-10-16T09:47:21.054275229Z","validator_address":"E8B4AF895B301C3D7108CA93A0B9234E3A2A4B21","validator_index":"0","signature":"eXB1NPmarUC5+2SCUQiWUCoGqtq0nrSn7giMAF/zWbfv3KmueE+1NoZ0CPLJAhBodz8Y8oL8xVy6ElA7J72kBw=="}]}}}`
Copy link
Contributor

Choose a reason for hiding this comment

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

Please avoid using global variables when writing test cases. This is currently used by one test case only

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed

client/rpc/block_converter_test.go Show resolved Hide resolved
client/rpc/block_converter.go Outdated Show resolved Hide resolved
client/rpc/block_converter.go Outdated Show resolved Hide resolved
client/rpc/block_converter.go Outdated Show resolved Hide resolved
client/rpc/block_converter_test.go Outdated Show resolved Hide resolved
// does not respect the JSON stdlib embedding semantics.
func (b Block) MarshalJSON() ([]byte, error) {
type blockJSON Block
return json.Marshal(blockJSON(b))
Copy link
Contributor

Choose a reason for hiding this comment

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

copylocks: call of blockJSON copies lock value: github.com/cosmos/cosmos-sdk/client/rpc.Block contains github.com/tendermint/tendermint/types.Block contains sync.Mutex (from govet)

Copy link
Contributor

Choose a reason for hiding this comment

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

We should probably return a reference to avoid mutex copying.

@fedekunze
Copy link
Collaborator

@alessio @alexanderbez mind reviewing again?

Copy link
Contributor

@alexanderbez alexanderbez left a comment

Choose a reason for hiding this comment

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

Tested this out again. Looks good but I think two fields are still missing.


// test when the block is not nil
var block ctypes.ResultBlock
defaultBlockJSON := `{"block_meta":{"block_id":{"hash":"30CD3A9EF2082FF9F2575655E99E37ACC3936DC9E534ADF0BD7436C76258225C","parts":{"total":"1","hash":"463460DA73FA0AE441B6041BF2683AE625CE2D9FD290F4C86CD83D1A7FB9F439"}},"header":{"version":{"block":"10","app":"0"},"chain_id":"test-chain-RoCfX4","height":"16","time":"2019-10-16T09:47:21.054275229Z","num_txs":"0","total_txs":"0","last_block_id":{"hash":"168D140232DA3A59757658E257168B6BCE62024F574CA222C2D449BAB1AE4023","parts":{"total":"1","hash":"4E6D7A1C7DE6942470394C21B132A2D9F89B31908C3B6FA8C2AB96A87553B96E"}},"last_commit_hash":"3483509FCC8048496E9B85E1E4C90A3B6E5944F022FE3C0709096932A010F712","data_hash":"","validators_hash":"61E69204249E5EE6F777C0566040929276A1900CE665BCB11006CABF9A4ED9A3","next_validators_hash":"61E69204249E5EE6F777C0566040929276A1900CE665BCB11006CABF9A4ED9A3","consensus_hash":"048091BC7DDC283F77BFBF91D73C44DA58C3DF8A9CBC867405D8B7F3DAADA22F","app_hash":"070A8213F67F494F7D27A6296C29F0DF62DFA18FD37FB592D9761A44DEE96528","last_results_hash":"","evidence_hash":"","proposer_address":"E8B4AF895B301C3D7108CA93A0B9234E3A2A4B21"}},"block":{"header":{"version":{"block":"10","app":"0"},"chain_id":"test-chain-RoCfX4","height":"16","time":"2019-10-16T09:47:21.054275229Z","num_txs":"0","total_txs":"0","last_block_id":{"hash":"168D140232DA3A59757658E257168B6BCE62024F574CA222C2D449BAB1AE4023","parts":{"total":"1","hash":"4E6D7A1C7DE6942470394C21B132A2D9F89B31908C3B6FA8C2AB96A87553B96E"}},"last_commit_hash":"3483509FCC8048496E9B85E1E4C90A3B6E5944F022FE3C0709096932A010F712","data_hash":"","validators_hash":"61E69204249E5EE6F777C0566040929276A1900CE665BCB11006CABF9A4ED9A3","next_validators_hash":"61E69204249E5EE6F777C0566040929276A1900CE665BCB11006CABF9A4ED9A3","consensus_hash":"048091BC7DDC283F77BFBF91D73C44DA58C3DF8A9CBC867405D8B7F3DAADA22F","app_hash":"070A8213F67F494F7D27A6296C29F0DF62DFA18FD37FB592D9761A44DEE96528","last_results_hash":"","evidence_hash":"","proposer_address":"E8B4AF895B301C3D7108CA93A0B9234E3A2A4B21"},"data":{"txs":null},"evidence":{"evidence":null},"last_commit":{"block_id":{"hash":"168D140232DA3A59757658E257168B6BCE62024F574CA222C2D449BAB1AE4023","parts":{"total":"1","hash":"4E6D7A1C7DE6942470394C21B132A2D9F89B31908C3B6FA8C2AB96A87553B96E"}},"precommits":[{"type":2,"height":"15","round":"0","block_id":{"hash":"168D140232DA3A59757658E257168B6BCE62024F574CA222C2D449BAB1AE4023","parts":{"total":"1","hash":"4E6D7A1C7DE6942470394C21B132A2D9F89B31908C3B6FA8C2AB96A87553B96E"}},"timestamp":"2019-10-16T09:47:21.054275229Z","validator_address":"E8B4AF895B301C3D7108CA93A0B9234E3A2A4B21","validator_index":"0","signature":"eXB1NPmarUC5+2SCUQiWUCoGqtq0nrSn7giMAF/zWbfv3KmueE+1NoZ0CPLJAhBodz8Y8oL8xVy6ElA7J72kBw=="}]}}}`
Copy link
Contributor

Choose a reason for hiding this comment

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

Can we use block JSON that has all fields populated (i.e. txs and evidence). I submitted a tx (using this branch) and when querying the block, I don't see the tx under block.txs. I bet the same will hold true for block.evidence.

So ideally we have:

// ...

convertedBlock := ConvertBlockResult(&block)
expectedOut := `{...}`

bz, err := cdc.MarshalJSON(convertedBlock)
require.NoError(t, err)
require.Equal(t, expectedOut, string(bz))

// does not respect the JSON stdlib embedding semantics.
func (b Block) MarshalJSON() ([]byte, error) {
type blockJSON Block
return json.Marshal(blockJSON(b))
Copy link
Contributor

Choose a reason for hiding this comment

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

We should probably return a reference to avoid mutex copying.

@stale
Copy link

stale bot commented Jan 1, 2020

This pull request has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@stale stale bot added the stale label Jan 1, 2020
@stale stale bot closed this Jan 5, 2020
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.

Convert Tendermint Block Response
7 participants