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

chainhash: JSON Unmarshal hash from appropriate string. #1952

Merged
merged 2 commits into from
Mar 21, 2023

Conversation

LindenWang01
Copy link
Contributor

This commit(1d6e578) occurred a problem:
"error": "json: cannot unmarshal string into Go struct field OutPoint.TxIn.PreviousOutPoint.Hash of type chainhash.Hash"

adds a UnmarshalJSON() method to chainhash.Hash to fix it.

@coveralls
Copy link

coveralls commented Feb 22, 2023

Pull Request Test Coverage Report for Build 4258913166

  • 0 of 0 changed or added relevant lines in 0 files are covered.
  • 3 unchanged lines in 1 file lost coverage.
  • Overall coverage increased (+0.006%) to 55.281%

Files with Coverage Reduction New Missed Lines %
connmgr/connmanager.go 3 86.27%
Totals Coverage Status
Change from base Build 4163994991: 0.006%
Covered Lines: 26641
Relevant Lines: 48192

💛 - Coveralls

@kcalvinalvin
Copy link
Collaborator

How about a simple test in chaincfg/chainhash/hash_test.go to catch these bugs in the future?

Maybe something like:

func TestHashJsonMarshal(t *testing.T) {
        tests := []struct {
                hash Hash
                err  error
        }{
                // Genesis hash.
                {
                        mainNetGenesisHash,
                        nil,
                },
        }

        unexpectedErrStr := "TestHashJsonMarshal #%d got an unexpected error: %v"
        unexpectedResultStr := "TestHashJsonMarshal #%d got: %v want: %v"
        for i, test := range tests {
                bytes, err := test.hash.MarshalJSON()
                if err != nil {
                        t.Errorf(unexpectedErrStr, i, err)
                        continue
                }

                got := new(Hash)
                err = json.Unmarshal(bytes, got)
                if err != nil {
                        t.Errorf(unexpectedErrStr, i, err)
                        continue
                }

                if !test.hash.IsEqual(got) {
                        t.Errorf(unexpectedResultStr, i, got, &test.hash)
                        continue
                }
        }
}

@Roasbeef
Copy link
Member

Approved the CI run.

Copy link
Member

@jcvernaleo jcvernaleo left a comment

Choose a reason for hiding this comment

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

OK

@kcalvinalvin
Copy link
Collaborator

kcalvinalvin commented Mar 21, 2023

I see no reason for the holdup for this PR. Anything else to address? @jcvernaleo @Roasbeef

@jcvernaleo
Copy link
Member

@kcalvinalvin I have no idea why I approved but didn't merge. I must have gotten distracted or something. That delay's on me, sorry about that. Merging now.

@jcvernaleo jcvernaleo merged commit e0e4916 into btcsuite:master Mar 21, 2023
@chappjc
Copy link
Contributor

chappjc commented Apr 25, 2023

MarshalJSON was in chaincfg/chainhash/v1.0.2. If possible, it would be good to tag a v1.0.3 with the matching UnmarshalJSON method added in this PR.

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.

7 participants