From 8f43b2e91d963f581baadd2f249400f5259fcfca Mon Sep 17 00:00:00 2001 From: Florian Schmaus Date: Sat, 3 Jan 2015 12:25:07 +0100 Subject: [PATCH 1/2] Revert "Fixing bug where to_bytes clobbers testnet params." This reverts commit 9b00e4b475d1ccf41b7584148ea783b1e24f3df0. --- bitcoin/messages.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/bitcoin/messages.py b/bitcoin/messages.py index 59bd55cc..cc289d07 100644 --- a/bitcoin/messages.py +++ b/bitcoin/messages.py @@ -33,7 +33,7 @@ from bitcoin.core import * from bitcoin.core.serialize import * from bitcoin.net import * -import bitcoin +from bitcoin import MainParams MSG_TX = 1 MSG_BLOCK = 2 @@ -51,11 +51,11 @@ def msg_ser(self, f): def msg_deser(cls, f, protover=PROTO_VERSION): raise NotImplementedError - def to_bytes(self): + def to_bytes(self, params=MainParams()): f = _BytesIO() self.msg_ser(f) body = f.getvalue() - res = bitcoin.params.MESSAGE_START + res = params.MESSAGE_START res += self.command res += b"\x00" * (12 - len(self.command)) res += struct.pack(b" Date: Sat, 3 Jan 2015 13:02:07 +0100 Subject: [PATCH 2/2] Use 'params=None' approach for params parameter in to_bytes() and stream_deaerialize() --- bitcoin/messages.py | 10 +++++++--- bitcoin/tests/test_params.py | 35 +++++++++++++++++++++++++++++++++++ 2 files changed, 42 insertions(+), 3 deletions(-) create mode 100644 bitcoin/tests/test_params.py diff --git a/bitcoin/messages.py b/bitcoin/messages.py index cc289d07..b295e71f 100644 --- a/bitcoin/messages.py +++ b/bitcoin/messages.py @@ -33,7 +33,7 @@ from bitcoin.core import * from bitcoin.core.serialize import * from bitcoin.net import * -from bitcoin import MainParams +import bitcoin MSG_TX = 1 MSG_BLOCK = 2 @@ -51,7 +51,9 @@ def msg_ser(self, f): def msg_deser(cls, f, protover=PROTO_VERSION): raise NotImplementedError - def to_bytes(self, params=MainParams()): + def to_bytes(self, params=None): + if params is None: + params = bitcoin.params f = _BytesIO() self.msg_ser(f) body = f.getvalue() @@ -74,7 +76,9 @@ def from_bytes(cls, b, protover=PROTO_VERSION): return MsgSerializable.stream_deserialize(f, protover=protover) @classmethod - def stream_deserialize(cls, f, params=MainParams(), protover=PROTO_VERSION): + def stream_deserialize(cls, f, params=None, protover=PROTO_VERSION): + if params is None: + params = bitcoin.params recvbuf = ser_read(f, 4 + 12 + 4 + 4) # check magic diff --git a/bitcoin/tests/test_params.py b/bitcoin/tests/test_params.py new file mode 100644 index 00000000..83b90e1c --- /dev/null +++ b/bitcoin/tests/test_params.py @@ -0,0 +1,35 @@ +# Copyright (C) 2013-2014 The python-bitcoinlib developers +# +# This file is part of python-bitcoinlib. +# +# It is subject to the license terms in the LICENSE file found in the top-level +# directory of this distribution. +# +# No part of python-bitcoinlib, including this file, may be copied, modified, +# propagated, or distributed except according to the terms contained in the +# LICENSE file. + +import unittest +import bitcoin + +from bitcoin.messages import msg_addr + +class Test_params(unittest.TestCase): + def tearDown(self): + bitcoin.SelectParams('mainnet') + + def test_mainnet_magic_byte(self): + bitcoin.SelectParams('mainnet') + self.assertEquals(bitcoin.MainParams().MESSAGE_START, msg_addr().to_bytes()[0:4]) + + def test_testnet_magic_byte(self): + bitcoin.SelectParams('testnet') + self.assertEquals(bitcoin.TestNetParams().MESSAGE_START, msg_addr().to_bytes()[0:4]) + + def test_mainnet_params_magic_byte(self): + bitcoin.SelectParams('testnet') + self.assertEquals(bitcoin.MainParams().MESSAGE_START, msg_addr().to_bytes(params=bitcoin.MainParams())[0:4]) + + def test_testnet_params_magic_byte(self): + bitcoin.SelectParams('mainnet') + self.assertEquals(bitcoin.TestNetParams().MESSAGE_START, msg_addr().to_bytes(params=bitcoin.TestNetParams())[0:4])