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

Add CScriptNum decode python implementation in functional suite #14816

Merged
merged 1 commit into from Dec 4, 2018
Merged
Changes from all commits
Commits
File filter...
Filter file types
Jump to…
Jump to file or symbol
Failed to load files and symbols.

Always

Just for now

@@ -25,7 +25,7 @@
assert_raises_rpc_error,
bytes_to_hex_str as b2x,
)

from test_framework.script import CScriptNum

def assert_template(node, block, expect, rehash=True):
if rehash:
@@ -65,11 +65,19 @@ def assert_submitblock(block, result_str_1, result_str_2=None):
assert 'proposal' in tmpl['capabilities']
assert 'coinbasetxn' not in tmpl

coinbase_tx = create_coinbase(height=int(tmpl["height"]) + 1)
next_height = int(tmpl["height"])
coinbase_tx = create_coinbase(height=next_height)
# sequence numbers must not be max for nLockTime to have effect
coinbase_tx.vin[0].nSequence = 2 ** 32 - 2
coinbase_tx.rehash()

# round-trip the encoded bip34 block height commitment
assert_equal(CScriptNum.decode(coinbase_tx.vin[0].scriptSig), next_height)
# round-trip negative and multi-byte CScriptNums to catch python regression
assert_equal(CScriptNum.decode(CScriptNum.encode(CScriptNum(1500))), 1500)
assert_equal(CScriptNum.decode(CScriptNum.encode(CScriptNum(-1500))), -1500)
assert_equal(CScriptNum.decode(CScriptNum.encode(CScriptNum(-1))), -1)

block = CBlock()
block.nVersion = tmpl["version"]
block.hashPrevBlock = int(tmpl["previousblockhash"], 16)
@@ -385,6 +385,22 @@ def encode(obj):
r[-1] |= 0x80
return bytes([len(r)]) + r

@staticmethod
def decode(vch):
result = 0
# We assume valid push_size and minimal encoding
value = vch[1:]
if len(value) == 0:

This comment has been minimized.

Copy link
@practicalswift

practicalswift Nov 28, 2018

Member

If @promag:s suggestions is not implemented: Use if not value: which is more idiomatic

This comment has been minimized.

Copy link
@instagibbs

instagibbs Nov 28, 2018

Author Member

fwiw I wouldn't know hot to read that suggestion properly, sorry

This comment has been minimized.

Copy link
@practicalswift

practicalswift Nov 28, 2018

Member

Yes leave it as is - it was just a nit :-)

I just meant to say that if sequence: and if not sequence: are very common Python idioms for checking non-emptiness and emptiness of sequences. Perhaps this is not the ideal case for my suggestion since it is not obvious from the variable name that value is a sequence :-)

return result
for i, byte in enumerate(value):
result |= int(byte) << 8*i
if value[-1] >= 0x80:
# Mask for all but the highest result bit
num_mask = (2**(len(value)*8) - 1) >> 1
result &= num_mask
result *= -1
return result


class CScript(bytes):
"""Serialized script
ProTip! Use n and p to navigate between commits in a pull request.
You can’t perform that action at this time.