-
Notifications
You must be signed in to change notification settings - Fork 30
tests(sighash): add sighash bitmask tests [part 2/7] #912
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
Open
glevco
wants to merge
3
commits into
feat/sighash/bitmask
Choose a base branch
from
tests/sighash/bitmask
base: feat/sighash/bitmask
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Empty file.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
# Copyright 2023 Hathor Labs | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
from hathor.transaction.scripts import P2PKH, Opcode | ||
from hathor.transaction.scripts.sighash import InputsOutputsLimit, SighashBitmask | ||
|
||
|
||
def test_create_input_data_simple() -> None: | ||
pub_key = b'my_pub_key' | ||
signature = b'my_signature' | ||
data = P2PKH.create_input_data(public_key_bytes=pub_key, signature=signature) | ||
|
||
assert data == bytes([ | ||
len(signature), | ||
*signature, | ||
len(pub_key), | ||
*pub_key | ||
]) | ||
|
||
|
||
def test_create_input_data_with_sighash_bitmask() -> None: | ||
pub_key = b'my_pub_key' | ||
signature = b'my_signature' | ||
inputs_bitmask = 0b111 | ||
outputs_bitmask = 0b101 | ||
sighash = SighashBitmask(inputs=inputs_bitmask, outputs=outputs_bitmask) | ||
data = P2PKH.create_input_data(public_key_bytes=pub_key, signature=signature, sighash=sighash) | ||
|
||
assert data == bytes([ | ||
1, | ||
inputs_bitmask, | ||
1, | ||
outputs_bitmask, | ||
Opcode.OP_SIGHASH_BITMASK, | ||
len(signature), | ||
*signature, | ||
len(pub_key), | ||
*pub_key | ||
]) | ||
|
||
|
||
def test_create_input_data_with_inputs_outputs_limit() -> None: | ||
pub_key = b'my_pub_key' | ||
signature = b'my_signature' | ||
max_inputs = 2 | ||
max_outputs = 3 | ||
limit = InputsOutputsLimit(max_inputs=max_inputs, max_outputs=max_outputs) | ||
data = P2PKH.create_input_data(public_key_bytes=pub_key, signature=signature, inputs_outputs_limit=limit) | ||
|
||
assert data == bytes([ | ||
1, | ||
max_inputs, | ||
1, | ||
max_outputs, | ||
Opcode.OP_MAX_INPUTS_OUTPUTS, | ||
len(signature), | ||
*signature, | ||
len(pub_key), | ||
*pub_key | ||
]) | ||
|
||
|
||
def test_create_input_data_with_sighash_bitmask_and_inputs_outputs_limit() -> None: | ||
pub_key = b'my_pub_key' | ||
signature = b'my_signature' | ||
inputs_bitmask = 0b111 | ||
outputs_bitmask = 0b101 | ||
max_inputs = 2 | ||
max_outputs = 3 | ||
sighash = SighashBitmask(inputs=inputs_bitmask, outputs=outputs_bitmask) | ||
limit = InputsOutputsLimit(max_inputs=max_inputs, max_outputs=max_outputs) | ||
data = P2PKH.create_input_data( | ||
public_key_bytes=pub_key, | ||
signature=signature, | ||
sighash=sighash, | ||
inputs_outputs_limit=limit | ||
) | ||
|
||
assert data == bytes([ | ||
1, | ||
inputs_bitmask, | ||
1, | ||
outputs_bitmask, | ||
Opcode.OP_SIGHASH_BITMASK, | ||
1, | ||
max_inputs, | ||
1, | ||
max_outputs, | ||
Opcode.OP_MAX_INPUTS_OUTPUTS, | ||
len(signature), | ||
*signature, | ||
len(pub_key), | ||
*pub_key | ||
]) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
# Copyright 2023 Hathor Labs | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
import hashlib | ||
from unittest.mock import Mock | ||
|
||
import pytest | ||
|
||
from hathor.conf.settings import HathorSettings | ||
from hathor.transaction import Transaction, TxInput, TxOutput | ||
from hathor.transaction.exceptions import ScriptError | ||
from hathor.transaction.scripts.script_context import ScriptContext | ||
from hathor.transaction.scripts.sighash import SighashAll, SighashBitmask | ||
|
||
|
||
@pytest.mark.parametrize(['max_num_outputs'], [(99,), (255,)]) | ||
def test_defaults(max_num_outputs: int) -> None: | ||
settings = Mock(spec_set=HathorSettings) | ||
settings.MAX_NUM_OUTPUTS = max_num_outputs | ||
|
||
context = ScriptContext(settings=settings, stack=Mock(), logs=[], extras=Mock()) | ||
|
||
tx = Transaction( | ||
inputs=[ | ||
TxInput(tx_id=b'tx1', index=0, data=b''), | ||
TxInput(tx_id=b'tx2', index=1, data=b''), | ||
], | ||
outputs=[ | ||
TxOutput(value=11, script=b''), | ||
TxOutput(value=22, script=b''), | ||
] | ||
) | ||
|
||
assert context._sighash == SighashAll() | ||
assert context.get_tx_sighash_data(tx) == tx.get_sighash_all_data() | ||
assert context.get_selected_outputs() == set(range(max_num_outputs)) | ||
|
||
|
||
def test_set_sighash() -> None: | ||
context = ScriptContext(settings=Mock(), stack=Mock(), logs=[], extras=Mock()) | ||
|
||
sighash = SighashBitmask(inputs=0b111, outputs=0b101) | ||
context.set_sighash(sighash) | ||
assert context._sighash == sighash | ||
|
||
with pytest.raises(ScriptError): | ||
context.set_sighash(sighash) | ||
|
||
|
||
@pytest.mark.parametrize( | ||
['outputs_bitmask', 'selected_outputs'], | ||
[ | ||
(0b00, set()), | ||
(0b01, {0}), | ||
(0b10, {1}), | ||
(0b11, {0, 1}), | ||
] | ||
) | ||
def test_sighash_bitmask(outputs_bitmask: int, selected_outputs: set[int]) -> None: | ||
settings = Mock() | ||
settings.MAX_NUM_INPUTS = 88 | ||
settings.MAX_NUM_OUTPUTS = 99 | ||
|
||
context = ScriptContext(settings=settings, stack=Mock(), logs=[], extras=Mock()) | ||
tx = Transaction( | ||
inputs=[ | ||
TxInput(tx_id=b'tx1', index=0, data=b''), | ||
TxInput(tx_id=b'tx2', index=1, data=b''), | ||
], | ||
outputs=[ | ||
TxOutput(value=11, script=b''), | ||
TxOutput(value=22, script=b''), | ||
] | ||
) | ||
|
||
sighash_bitmask = SighashBitmask(inputs=0b11, outputs=outputs_bitmask) | ||
context.set_sighash(sighash_bitmask) | ||
|
||
data = tx.get_custom_sighash_data(sighash_bitmask) | ||
assert context.get_tx_sighash_data(tx) == hashlib.sha256(data).digest() | ||
|
||
with pytest.raises(ScriptError) as e: | ||
context.set_sighash(Mock()) | ||
|
||
assert str(e.value) == 'Cannot modify sighash after it is already set.' | ||
assert context.get_selected_outputs() == selected_outputs |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Should we add one more bit?
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 testing
0b111
, for example? That would raise an exception, as the test tx only has 2 outputs. This error is checked intest_sighash.py
.