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: add/group numeric related opcode tests. #1353

Merged
merged 1 commit into from
Jul 30, 2018

Conversation

dnldd
Copy link
Member

@dnldd dnldd commented Jul 18, 2018

This is work towards #1331

@davecgh davecgh added this to the 1.3.0 milestone Jul 20, 2018
@dnldd dnldd changed the title [wip] txscript: add/group numeric related opcode tests. txscript: add/group numeric related opcode tests. Jul 23, 2018
["2147483647", "1ADD 1", "NONE", "OK"],
["-2147483647", "1ADD 1", "NONE", "OK"],
["2147483648 1ADD", "1", "NONE", "ERR_OUT_OF_RANGE", "1ADD must fail with stack item >4 bytes"],
["-2147483648", "1ADD 1", "NONE", "ERR_OUT_OF_RANGE", "1ADD must fail with stack item >4 bytes"],
Copy link
Member

Choose a reason for hiding this comment

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

Identical comment.


["2MUL test coverage"],
["2 2MUL", "4 EQUAL", "NONE", "ERR_EVAL_FALSE", "2MUL is a NOP"],
["2", "2MUL 1", "NONE", "OK", "2MUL is NOP"],
Copy link
Member

Choose a reason for hiding this comment

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

Not cleanstack.


["2DIV test coverage"],
["2 2DIV", "1 EQUAL", "NONE", "ERR_EVAL_FALSE", "2DIV is a NOP"],
["2", "2DIV 1", "NONE", "OK", "2DIV is NOP"],
Copy link
Member

Choose a reason for hiding this comment

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

Not cleanstack.

["-2147483647", "1ADD 1", "NONE", "OK"],
["2147483648 1ADD", "1", "NONE", "ERR_OUT_OF_RANGE", "1ADD must fail with stack item >4 bytes"],
["2147483647", "1ADD 1SUB 1", "NONE", "ERR_OUT_OF_RANGE", "We cannot do math on 5-byte integers, even if the result is 4-bytes"],
["0x7a{6}", "1ADD 1", "NONE", "ERR_INVALID_STACK_OPERATION", "1ADD must fail with a non-numeric stack item"],
Copy link
Member

@davecgh davecgh Jul 28, 2018

Choose a reason for hiding this comment

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

This, and all of the tests like it, aren't actually testing what the comment says. 0x7a is not a data push operation, rather it is OP_ROLL, so the failure is because there are no inputs to OP_ROLL.

Since numbers are just raw bytes popped from the stack and interpreted as numeric according to the script number rules, it's not actually possible to test non-numeric inputs since they are all effectively numeric. The edge conditions, such as max 4-bytes, which the above test already handles, are the relevant tests.

Consequently, this test, and all similar, should be removed.

["111 1SUB", "110 EQUAL", "NONE", "OK"],
["-1 1SUB", "-2 EQUAL", "NONE", "OK", "1SUB must decrement the provided numeric stack item by one"],
["2147483648 1SUB", "1", "NONE", "ERR_OUT_OF_RANGE", "1SUB must fail with stack item >4 bytes"],
["0x7a{6}", "1SUB 1", "NONE", "ERR_INVALID_STACK_OPERATION", "1SUB must fail with a non-numeric stack item"],
Copy link
Member

Choose a reason for hiding this comment

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

Another case of OP_ROLL.

["2", "NEGATE -2 EQUAL", "NONE", "OK"],
["-5", "NEGATE 5 EQUAL", "NONE", "OK", "NEGATE must replace the numeric stack item with its negative equivalent"],
["2147483648", "NEGATE -2147483648 EQUAL", "NONE", "ERR_OUT_OF_RANGE", "NEGATE must fail with stack item >4 bytes"],
["0x7a{6}", "NEGATE 1", "NONE", "ERR_INVALID_STACK_OPERATION", "NEGATE must fail with a non-numeric stack item"],
Copy link
Member

Choose a reason for hiding this comment

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

Another case of OP_ROLL.

["16 ABS", "16 EQUAL", "NONE", "OK"],
["-16 ABS", "16 EQUAL", "NONE", "OK", "ABS must replace the numeric stack item with its absolute equivalent"],
["2147483648 ABS", "2147483648 EQUAL", "NONE", "ERR_OUT_OF_RANGE", "ABS must fail with stack item >4 bytes"],
["0x7a{6}", "ABS 1", "NONE", "ERR_INVALID_STACK_OPERATION", "ABS must fail with a non-numeric stack item"],
Copy link
Member

Choose a reason for hiding this comment

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

Another case of OP_ROLL.

["111 0NOTEQUAL", "1 EQUAL", "NONE", "OK"],
["-111 0NOTEQUAL", "1 EQUAL", "NONE", "OK"],
["2147483648", "0NOTEQUAL 0 EQUAL", "NONE", "ERR_OUT_OF_RANGE", "0NOTEQUAL must fail with stack item >4 bytes"],
["0x7a{6} 0NOTEQUAL", "0 EQUAL", "NONE", "ERR_INVALID_STACK_OPERATION", "0NOTEQUAL must fail with a non-numeric stack item"],
Copy link
Member

Choose a reason for hiding this comment

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

Another case of OP_ROLL.

["2147483648 0 ADD", "NOP", "NONE", "ERR_OUT_OF_RANGE", "Arithmetic operands must be in range [-2^31...2^31] "],
["-2147483648 0 ADD", "NOP", "NONE", "ERR_OUT_OF_RANGE", "Arithmetic operands must be in range [-2^31...2^31] "],
["2147483648 1", "ADD 2147483649 EQUAL", "NONE", "ERR_OUT_OF_RANGE", "ADD must fail with stack item >4 bytes"],
["0x7a{6} 1", "ADD 0 EQUAL", "NONE", "ERR_INVALID_STACK_OPERATION", "ADD must fail with a non-numeric stack item"],
Copy link
Member

Choose a reason for hiding this comment

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

Another case of OP_ROLL.

["111 1 ADD 12 SUB", "100 EQUAL", "NONE", "OK"],
["-1 1 SUB", "-2 EQUAL", "NONE", "OK"],
["2147483648 1", "SUB 2147483647 EQUAL", "NONE", "ERR_OUT_OF_RANGE", "SUB must fail with stack item >4 bytes"],
["0x7a{6} 1", "SUB 0 EQUAL", "NONE", "ERR_INVALID_STACK_OPERATION", "SUB must fail with a non-numeric stack item"],
Copy link
Member

Choose a reason for hiding this comment

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

Another case of OP_ROLL.

["254 1 MUL", "254 EQUAL", "NONE", "OK"],
["-12 12 MUL", "-144 EQUAL", "NONE", "OK"],
["2147483648 2", "MUL", "NONE", "ERR_OUT_OF_RANGE", "MUL must fail with stack item >4 bytes"],
["0x7a{6} 1", "MUL 0 EQUAL", "NONE", "ERR_INVALID_STACK_OPERATION", "MUL must fail with a non-numeric stack item"],
Copy link
Member

Choose a reason for hiding this comment

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

Another case of OP_ROLL.

["ABS test coverage"],
["NOP", "ABS 1", "NONE", "ERR_INVALID_STACK_OPERATION", "ABS requires an item on the stack and NOP must not be treated as one"],
["", "ABS 0 EQUAL", "NONE", "ERR_INVALID_STACK_OPERATION", "ABS requires an item on the stack, stack has no items"],
["ABS", "0 EQUAL", "NONE", "ERR_INVALID_STACK_OPERATION", "ABS requires an item on the stack, stack has no items"],
Copy link
Member

Choose a reason for hiding this comment

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

Comment is not unique

["0 NOT", "NOP", "NONE", "OK"],
["1 NOT", "0 EQUAL", "NONE", "OK", "NOT must invert the boolean value of a numeric stack item"],
["11 NOT", "0 EQUAL", "NONE", "OK"],
["-11 NOT", "0 EQUAL", "NONE", "OK"],
Copy link
Member

Choose a reason for hiding this comment

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

Needs comment since added in this PR.

["1 0 1", "WITHIN NOT", "NONE", "OK"],
["0 -2147483647 2147483647", "WITHIN", "NONE", "OK"],
["-1 -100 100", "WITHIN", "NONE", "OK"],
["11 -100 100", "WITHIN", "NONE", "OK"],
["-2147483647 -100 100", "WITHIN NOT", "NONE", "OK"],
["2147483647 -100 100", "WITHIN NOT", "NONE", "OK"],
["1 0 2147483648", "WITHIN", "NONE", "ERR_OUT_OF_RANGE", "WITHIN must fail with stack item >4 bytes"],

["Test every numeric-accepting opcode for correct handling of the numeric minimal encoding rule"],
Copy link
Member

Choose a reason for hiding this comment

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

This comment isn't correct since the section does not actually do that. It does it for most of them, but it doesn't have every opcode. Either the missing opcodes should be added here, or all of these should be split into the per-opcode tests. I don't really have a strong preference, but I do suspect that, until all of the tests are split for all opcodes, adding the missing tests here will make sense to avoid having some dangling tests.

["-1 -1 ADD", "-2 EQUAL", "NONE", "OK"],
["1", "ADD", "NONE", "ERR_INVALID_STACK_OPERATION"],
["2147483648 0 ADD", "NOP", "NONE", "ERR_OUT_OF_RANGE", "Arithmetic operands must be in range [-2^31...2^31] "],
["-2147483648 0 ADD", "NOP", "NONE", "ERR_OUT_OF_RANGE", "Arithmetic operands must be in range [-2^31...2^31] "],
Copy link
Member

Choose a reason for hiding this comment

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

Identical comments.

["1", "ADD", "NONE", "ERR_INVALID_STACK_OPERATION"],
["2147483648 0 ADD", "NOP", "NONE", "ERR_OUT_OF_RANGE", "Arithmetic operands must be in range [-2^31...2^31] "],
["-2147483648 0 ADD", "NOP", "NONE", "ERR_OUT_OF_RANGE", "Arithmetic operands must be in range [-2^31...2^31] "],
["2147483648 1", "ADD 2147483649 EQUAL", "NONE", "ERR_OUT_OF_RANGE", "ADD must fail with stack item >4 bytes"],
Copy link
Member

Choose a reason for hiding this comment

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

Need another test to ensure the other operand being >4 bytes is rejected as well.

Also, the comment for this one should probably say ADD must fail with second operand >4 bytes whereas the other missing test would be ADD must fail with first operand >4 bytes.

["4 4", "LESSTHANOREQUAL", "NONE", "OK"],
["10 11", "LESSTHANOREQUAL", "NONE", "OK"],
["-11 11", "LESSTHANOREQUAL", "NONE", "OK"],
["-11 -10", "LESSTHANOREQUAL", "NONE", "OK"],
["11 10", "GREATERTHANOREQUAL", "NONE", "OK"],
["1 2147483648", "LESSTHANOREQUAL", "NONE", "ERR_OUT_OF_RANGE", "LESSTHANOREQUAL must fail with stack item >4 bytes"],
Copy link
Member

Choose a reason for hiding this comment

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

Same here regarding needing another test for the other operand being >4 bytes and comment.

["4 4", "GREATERTHANOREQUAL", "NONE", "OK"],
["10 11", "GREATERTHANOREQUAL NOT", "NONE", "OK"],
["-11 11", "GREATERTHANOREQUAL NOT", "NONE", "OK"],
["-11 -10", "GREATERTHANOREQUAL NOT", "NONE", "OK"],
["1 0 MIN", "0 NUMEQUAL", "NONE", "OK"],
["1 2147483648", "GREATERTHANOREQUAL", "NONE", "ERR_OUT_OF_RANGE", "GREATERTHANOREQUAL must fail with stack item >4 bytes"],
Copy link
Member

Choose a reason for hiding this comment

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

Same here regarding needing another test for the other operand being >4 bytes and comment.

["0 1 MIN", "0 NUMEQUAL", "NONE", "OK"],
["-1 0 MIN", "-1 NUMEQUAL", "NONE", "OK"],
["0 -2147483647 MIN", "-2147483647 NUMEQUAL", "NONE", "OK"],
["2147483647 0 MAX", "2147483647 NUMEQUAL", "NONE", "OK"],
["1 2147483648", "MIN", "NONE", "ERR_OUT_OF_RANGE", "MIN must fail with stack >4 bytes"],
Copy link
Member

Choose a reason for hiding this comment

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

Same here regarding needing another test for the other operand being >4 bytes and comment.

["0 100 MAX", "100 NUMEQUAL", "NONE", "OK"],
["-100 0 MAX", "0 NUMEQUAL", "NONE", "OK"],
["0 -2147483647 MAX", "0 NUMEQUAL", "NONE", "OK"],
["0 0 1", "WITHIN", "NONE", "OK"],
["1 2147483648", "MAX", "NONE", "ERR_OUT_OF_RANGE", "MAX must fail with stack item >4 bytes"],
Copy link
Member

Choose a reason for hiding this comment

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

Same here regarding needing another test for the other operand being >4 bytes and comment.

["1 0 1", "WITHIN NOT", "NONE", "OK"],
["0 -2147483647 2147483647", "WITHIN", "NONE", "OK"],
["-1 -100 100", "WITHIN", "NONE", "OK"],
["11 -100 100", "WITHIN", "NONE", "OK"],
["-2147483647 -100 100", "WITHIN NOT", "NONE", "OK"],
["2147483647 -100 100", "WITHIN NOT", "NONE", "OK"],
["1 0 2147483648", "WITHIN", "NONE", "ERR_OUT_OF_RANGE", "WITHIN must fail with stack item >4 bytes"],
Copy link
Member

Choose a reason for hiding this comment

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

Same here regarding needing another test for the other operands being >4 bytes and comment. Note that there are 3 here instead of 2 as all of the others up to this point.

["1", "ADD", "NONE", "ERR_INVALID_STACK_OPERATION"],
["2147483648 0 ADD", "NOP", "NONE", "ERR_OUT_OF_RANGE", "Arithmetic operands must be in range [-2^31...2^31] (positive numeric case)"],
["-2147483648 0 ADD", "NOP", "NONE", "ERR_OUT_OF_RANGE", "Arithmetic operands must be in range [-2^31...2^31] (negative numerica case)"],
["2147483648 1", "ADD 2147483649 EQUAL", "NONE", "ERR_OUT_OF_RANGE", "ADD must fail with first operand >4 bytes"],
Copy link
Member

Choose a reason for hiding this comment

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

Comment is incorrect. The first operand to the opcode is the second one in the script because they are popped in reverse order.

Same is true with all of these.

@@ -1256,8 +1351,9 @@
["6 0", "DIV TRUE", "NONE", "ERR_DIVIDE_BY_ZERO", "DIV must fail with zero divisor"],
["2147483648 1073741824", "DIV TRUE", "NONE", "ERR_OUT_OF_RANGE", "DIV must fail with dividend >4 bytes"],
["1 2147483648", "DIV TRUE", "NONE", "ERR_OUT_OF_RANGE", "DIV must fail with divisor >4 bytes"],
["2147483648 1", "DIV TRUE", "NONE", "ERR_OUT_OF_RANGE", "DIV must fail with dividend >4 bytes"],
Copy link
Member

Choose a reason for hiding this comment

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

Already tested 2 lines above. Not needed.

@@ -1272,8 +1368,9 @@
["6 0", "MOD TRUE", "NONE", "ERR_DIVIDE_BY_ZERO", "MOD must fail with zero divisor"],
["2147483648 1073741824", "MOD TRUE", "NONE", "ERR_OUT_OF_RANGE", "MOD must fail with dividend >4 bytes"],
["1 2147483648", "MOD TRUE", "NONE", "ERR_OUT_OF_RANGE", "MOD must fail with divisor >4 bytes"],
["2147483648 1", "MOD TRUE", "NONE", "ERR_OUT_OF_RANGE", "MOD must fail with dividend >4 bytes"],
Copy link
Member

Choose a reason for hiding this comment

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

Already tested 2 lines above. Not needed.

@davecgh davecgh merged commit 1ce290d into decred:master Jul 30, 2018
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.

2 participants