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

encoding/blockchain: Encapsulate "varstrlist" and "extensible string" patterns #376

Merged
merged 3 commits into from
Jan 13, 2017

Conversation

bobg
Copy link
Contributor

@bobg bobg commented Jan 11, 2017

Use the new functions throughout protocol/bc.

@bobg
Copy link
Contributor Author

bobg commented Jan 11, 2017

Carves off part of #349 for easier review.

@bobg
Copy link
Contributor Author

bobg commented Jan 11, 2017

PTAL

@kr
Copy link
Contributor

kr commented Jan 11, 2017

👀

defer bufpool.Put(buf)
t.WriteInputCommitment(buf)
blockchain.WriteVarstr31(w, buf.Bytes())
blockchain.WriteExtensibleString(w, func(w io.Writer) error {
Copy link
Contributor

Choose a reason for hiding this comment

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

Can this be

blockchain.WriteExtensibleString(w, t.WriteInputCommitment)

and same for writeInputWitness below?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes. I had to revise those functions to return errors first, but nice.


// WriteExtensibleString sends the output of the given function,
// together with a varint31 length prefix, to w.
func WriteExtensibleString(w io.Writer, f func(io.Writer) error) (int, error) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Can/should we document that WriteExtensibleString always provides a *bytes.Buffer to f, and that the signature of f is this way mainly so that it matches io.WriterTo? That way, f can safely ignore errors from its Writer (which is guaranteed to be a *bytes.Buffer, which never returns an error from Write). That would let us clean up some (or maybe just one) of the call sites.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Eh, I don't find that suggestion especially compelling. Eliminating two or three error checks in a Go program feels like spitting into the ocean.

@bobg
Copy link
Contributor Author

bobg commented Jan 13, 2017

Revised, PTAL

@kr
Copy link
Contributor

kr commented Jan 13, 2017

LGTM

@iampogo iampogo merged commit 9222796 into main Jan 13, 2017
@iampogo iampogo deleted the varstrlist branch January 13, 2017 20:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants