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

protocol/bc: reorganize and refactor #349

Closed
wants to merge 10 commits into from
Closed

protocol/bc: reorganize and refactor #349

wants to merge 10 commits into from

Conversation

bobg
Copy link
Contributor

@bobg bobg commented Dec 20, 2016

Reorganize and refactor TxData, TxInput, TxOutput, and related types in anticipation of future changes. Fix serialization errors and update test vectors.

Closes #330.
Closes #335.

@bobg
Copy link
Contributor Author

bobg commented Dec 20, 2016

PTAL

@bobg
Copy link
Contributor Author

bobg commented Jan 10, 2017

PTAL

@bobg
Copy link
Contributor Author

bobg commented Jan 10, 2017

rebased, PTAL

@kr
Copy link
Contributor

kr commented Jan 10, 2017

It would be a lot easier to review this if the refactoring could be done separately from any behavior changes.

Unrelated thought, if you're looking for opportunities to pick off smaller pieces: how about a PR that just introduces the Read- and WriteVarstrList functions and uses them were applicable?

"chain/errors"
)

type AssetWitness struct {
Copy link
Contributor

Choose a reason for hiding this comment

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

For the most part, this PR seems to be moving our Go types to more closely match the datatypes described in the spec, but AssetWitness is an exception. What is it?

It would be good to have godoc, at least for newly-introduced types. The ones that do map 1-1 can be short, like "Type Foo represents a Foo as defined in Chain Protocol. See https://chain.com/docs/protocol/specifications/data."

Copy link
Contributor Author

Choose a reason for hiding this comment

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

IssuanceWitness got turned into AssetWitness somewhere between brain and keyboard, and stuck unnoticed somehow. Now fixed. Also added some Godoc.

@bobg
Copy link
Contributor Author

bobg commented Jan 11, 2017

!PTAL
See #376 first

@bobg
Copy link
Contributor Author

bobg commented Jan 13, 2017

Rebased, PTAL

@bobg bobg added the PTAL label Jan 13, 2017
@bobg
Copy link
Contributor Author

bobg commented Jan 17, 2017

PTAL

@tessr
Copy link
Contributor

tessr commented Jan 17, 2017

👮‍♀️ Hello, it's me, the PR size police!

Things that could get pulled out of this PR and into their own PRs:

  1. Improving error messages (https://github.com/chain/chain/pull/349/files#diff-60836afb202f0dcd9b8af17f1788a860R86, https://github.com/chain/chain/pull/349/files#diff-016bd9f722295a39e81867b58707e4c4L117)
  2. Moving sighasher out of transaction.go and into its own file (https://github.com/chain/chain/pull/349/files#diff-016bd9f722295a39e81867b58707e4c4L260)
  3. Moving AssetAmount out of transaction.go and into asset.go (https://github.com/chain/chain/pull/349/files#diff-016bd9f722295a39e81867b58707e4c4L361)
  4. Moving OutputCommitment out of TxOutput and into its own file (https://github.com/chain/chain/pull/349/files#diff-9ae5ef8a42069d22d4a6863875362c74L22), and the removal of (TxOutput).WriteCommitment (https://github.com/chain/chain/pull/349/files#diff-9ae5ef8a42069d22d4a6863875362c74L104)
  5. (TxInput).WitnessHash and subsequently CalcMerkleRoot returning an error (https://github.com/chain/chain/pull/349/files#diff-601ce85b9d920fdbec9f8d7d0fd5950bR301 and https://github.com/chain/chain/pull/349/files#diff-9dec076f454f1bf45901947d56f2612aR17, respectively)
  6. SpendInput and IssuanceInput getting moved into their own files (https://github.com/chain/chain/pull/349/files#diff-601ce85b9d920fdbec9f8d7d0fd5950bL23).

I think once some of these changes go in on their own, it will be easier to review any other smaller and nitty-grittier changes. I can open PRs for any of them.

iampogo pushed a commit that referenced this pull request Jan 17, 2017
iampogo pushed a commit that referenced this pull request Jan 17, 2017
iampogo pushed a commit that referenced this pull request Jan 17, 2017
iampogo pushed a commit that referenced this pull request Jan 18, 2017
Part of #349. 

Note that this simply splits out `OutputCommitment` without making some of
the other changes to the output commitment that are included in 349.

Closes #397
iampogo pushed a commit that referenced this pull request Jan 18, 2017
This trickles down to `CalcMerkleRoot` as well. Part of #349. 

Closes #398
iampogo pushed a commit that referenced this pull request Jan 18, 2017
Part of #349. In 349, there are other changes to both `IssuanceInput` and
`SpendInput`, but I don't make those changes here.

Closes #399
@bobg
Copy link
Contributor Author

bobg commented Jan 19, 2017

Rebased: +585,-428

PTAL

@@ -96,7 +96,7 @@ func ParseHash(s string) (h Hash, err error) {
return h, errors.Wrap(err, "decode hex")
}

func writeFastHash(w io.Writer, d []byte) error {
func WriteFastHash(w io.Writer, d []byte) error {
Copy link
Contributor

Choose a reason for hiding this comment

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

Does this need to be exported?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Nope, fixed, thanks.

}
return o
}

func (t *TxInput) InitialBlock() (blockID Hash, ok bool) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Is this used?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Nope, removed, thanks.

return nil, false
}

func (t *TxInput) VMVer() uint64 {
Copy link
Contributor

Choose a reason for hiding this comment

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

Ditto here--is this used?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Nope, removed, thanks.

}

func (iw *IssuanceWitness) AssetDefinitionHash() (defhash Hash) {
if len(iw.AssetDefinition) == 0 {
Copy link
Contributor

Choose a reason for hiding this comment

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

Is this new if clause the reason that the merkle tree tests are changing?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

No, this is just an optimization. The test vectors are changing because of the fixes to #330 and #335.

Copy link
Contributor

Choose a reason for hiding this comment

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

Hmm, I know we said we'd try to just merge this without teasing it apart more, but do you think it would be easy to pull those fixes out of this PR, and make this PR purely refactoring?


// readCommitment reads a spend input commitment AFTER the leading
// type byte has been consumed.
func (si *SpendInput) readCommitment(r io.Reader, txVersion, assetVersion uint64) (err error) {
Copy link
Contributor

Choose a reason for hiding this comment

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

IMO it would be nice if SpendInput and IssuanceInput did the asset version check in the same way--right now, (IssuanceInput).readCommitment assumes that the asset version has been checked before it gets called, and (SpendInput).readCommitment does not

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Do you mean you wish they had the same calling signature, maybe defined in an interface? The thing about that is, IssuanceInput commitments do not need the asset version right now, but SpendInput commitments do. We'd have to add unused parameters to the IssuanceInput version of readCommitment, which seems wrong in this case.

Would it be better if the two readCommitment functions had different names?

Copy link
Contributor

Choose a reason for hiding this comment

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

Perhaps a comment at the call site would be enough--I'm basically looking for ways to reduce cognitive load while reading this code.

return hash, err
}

// does not write the enclosing extensible string
Copy link
Contributor

@tessr tessr Jan 23, 2017

Choose a reason for hiding this comment

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

might also add to this comment explaining why it's nil for now (same goes for readWitness)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done, thanks.

}
return assetID
Copy link
Contributor

Choose a reason for hiding this comment

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

Is this line reachable?

Copy link
Contributor

Choose a reason for hiding this comment

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

Well, I guess it is, if we get an input that's not an issuance or a spend. But what is assetID in this case?

Copy link
Contributor

Choose a reason for hiding this comment

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

Never mind, I just realized the return values are named. I'm still waking up today.


func (t *TxInput) ControlProgram() []byte {
Copy link
Contributor

Choose a reason for hiding this comment

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

Did (TxInput).ControlProgram() or (TxInput).IssuanceProgram() change?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

You mean, with respect to main? No, they just got shuffled around a bit (sorry).

Copy link
Contributor

Choose a reason for hiding this comment

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

It's cool--github seems to be struggling a bit with the diff, just wanted to make sure I wasn't missing anything.

return si.ControlProgram
if serflags&SerWitness != 0 {
_, err = blockchain.WriteExtensibleString(w, func(w io.Writer) error {
return t.writeInputWitness(w)
Copy link
Contributor

Choose a reason for hiding this comment

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

can t.WriteInputWitness be used as the param to WriteExtensibleString directly?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yep, done, thanks.

})
return err
}
return fmt.Errorf("unknown input type %T", t.TypedInput)
Copy link
Contributor

Choose a reason for hiding this comment

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

It seems like we sometimes return an error when we come across an unknown input type (like here, or in writeInputWitness), and sometimes we return a zero value (in AssetAmount or AssetID)--do we have a rule of thumb for this?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Not a conscious one, but your question made me think about the implicit one I've been using, which I think is: if the function doesn't already return an error value, don't add one for this purpose (which is the case for e.g. accessors); but if it does, then use it.

Not commenting on whether or not that's the right guideline to be using...

Copy link
Contributor

Choose a reason for hiding this comment

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

I feel like it's fine to say that accessors can return a zero value 👍

return ew.Written(), ew.Err()
}

func (tx *TxData) writeTo(w io.Writer, serflags byte) error {
Copy link
Contributor

Choose a reason for hiding this comment

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

Did this function change?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

With respect to main? Only in terms of comments and error wrapping - and the version on main is better. Updated.

@bobg
Copy link
Contributor Author

bobg commented Jan 23, 2017

Updated, PTAL

@bobg bobg changed the title protocol/bc: reorganize and refactor; also fix serialization errors protocol/bc: reorganize and refactor Jan 24, 2017
@jbowens
Copy link
Contributor

jbowens commented Jan 26, 2017

looks like this might need a rebase?

@bobg bobg removed the PTAL label Jan 29, 2017
@bobg
Copy link
Contributor Author

bobg commented Jan 29, 2017

!PTAL. This PR has gotten too stale, will redo it soon.

@bobg bobg closed this Feb 13, 2017
@bobg bobg deleted the txtypes branch February 13, 2017 19:46
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.

None yet

4 participants