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
Conversation
PTAL |
PTAL |
rebased, PTAL |
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 { |
There was a problem hiding this comment.
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."
There was a problem hiding this comment.
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.
!PTAL |
Rebased, PTAL |
PTAL |
…e TxOutput witness??
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 { |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this used?
There was a problem hiding this comment.
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 { |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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 { |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
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) { |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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
)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done, thanks.
} | ||
return assetID |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this line reachable?
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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 { |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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).
There was a problem hiding this comment.
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) |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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) |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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...
There was a problem hiding this comment.
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 { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Did this function change?
There was a problem hiding this comment.
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.
Updated, PTAL |
looks like this might need a rebase? |
!PTAL. This PR has gotten too stale, will redo it soon. |
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.