Navigation Menu

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

txscript: Change makeScriptNum take a length argument #455

Merged
merged 1 commit into from Oct 6, 2015
Merged

txscript: Change makeScriptNum take a length argument #455

merged 1 commit into from Oct 6, 2015

Conversation

dajohi
Copy link
Member

@dajohi dajohi commented Jun 26, 2015

While current existing numeric opcodes are limited to 4 bytes, new
opcodes may need different limits.

This mimics Bitcoin Core commit 99088d60d8a7747c6d1a7fd5d8cd388be1b3e138

// Interpreting data as an integer requires that it is not larger than
// a 32-bit integer.
func makeScriptNum(v []byte, requireMinimal bool, maxScriptNumLen int) (scriptNum, error) {
// Interpreting data requires that it is not larger than
Copy link
Member

Choose a reason for hiding this comment

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

the "as an integer" part should still be retained. Regardless of the number of bytes, it's still being interpreted as an integer.

@davecgh
Copy link
Member

davecgh commented Jun 26, 2015

I think there should be new tests added to deal with the new parameter as well. I see that the existing tests were changed to use the default max size, however, that's only testing the single value.

In particular, I'd add tests for all variants of 5 bytes since that is how it will be used.

@@ -152,11 +156,6 @@ func (n scriptNum) Int32() int32 {
// makeScriptNum interprets the passed serialized bytes as an encoded integer
// and returns the result as a script number.
//
// Since the consensus rules dictate the serialized bytes interpreted as ints
Copy link
Member

Choose a reason for hiding this comment

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

Rather than removing this important information, I'd prefer it simply be modified slightly to reflect the existence of the parameter. Perhaps:

// Since the consensus rules dictate that serialized bytes interpreted as ints
// are only allowed to be in the range determined by a maximum number of bytes,
// on a per opcode basis, an error will be returned when the provided bytes
// would result in a number outside of that range.  In particular, the range for
// the vast majority of opcodes dealing with numeric values are limited to 4
// bytes and therefore will pass that value to this function resulting in an
// allowed range of [-2^31 + 1, 2^31 - 1].

@davecgh
Copy link
Member

davecgh commented Oct 5, 2015

As we discussed on IRC, the comments on makeScriptNum scriptNum.Int32, and type scriptNum int64 need to updated to reflect these changes.

@@ -164,11 +168,16 @@ func (n scriptNum) Int32() int32 {
// [0x7f 0x00 0x00 ...], etc. All forms except [0x7f] will return an error with
// requireMinimal enabled.
//
// The scriptNumLen is the size of the data to be encoded.
Copy link
Member

Choose a reason for hiding this comment

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

This isn't quite right. The number is already encoded when this function is called. Perhaps the following?

// The scriptNumLen is the maximum number of bytes the encoded value can be
// before an ErrStackNumberTooBig is returned.  This effectivley limits the
// range of allowed values.
// WARNING:  Great care should be taken if passing a value larger than
// defaultScriptNumLen, since it could lead to addition and multiplication
// overflows.
//

While current existing numeric opcodes are limited to 4 bytes, new
opcodes may need different limits.

This mimics Bitcoin Core commit 99088d60d8a7747c6d1a7fd5d8cd388be1b3e138
@davecgh
Copy link
Member

davecgh commented Oct 6, 2015

OK

@conformal-deploy conformal-deploy merged commit ce22159 into btcsuite:master Oct 6, 2015
@dajohi dajohi deleted the scriptnum branch October 27, 2015 16:23
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

3 participants