Skip to content

Commit 3350118

Browse files
committed
Use 'params=None' approach for params parameter
in to_bytes() and stream_deaerialize()
1 parent 8f43b2e commit 3350118

File tree

2 files changed

+50
-3
lines changed

2 files changed

+50
-3
lines changed

bitcoin/messages.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
from bitcoin.core import *
3434
from bitcoin.core.serialize import *
3535
from bitcoin.net import *
36-
from bitcoin import MainParams
36+
import bitcoin
3737

3838
MSG_TX = 1
3939
MSG_BLOCK = 2
@@ -51,7 +51,9 @@ def msg_ser(self, f):
5151
def msg_deser(cls, f, protover=PROTO_VERSION):
5252
raise NotImplementedError
5353

54-
def to_bytes(self, params=MainParams()):
54+
def to_bytes(self, params=None):
55+
if params is None:
56+
params = bitcoin.params
5557
f = _BytesIO()
5658
self.msg_ser(f)
5759
body = f.getvalue()
@@ -74,7 +76,9 @@ def from_bytes(cls, b, protover=PROTO_VERSION):
7476
return MsgSerializable.stream_deserialize(f, protover=protover)
7577

7678
@classmethod
77-
def stream_deserialize(cls, f, params=MainParams(), protover=PROTO_VERSION):
79+
def stream_deserialize(cls, f, params=None, protover=PROTO_VERSION):
80+
if params is None:
81+
params = bitcoin.params
7882
recvbuf = ser_read(f, 4 + 12 + 4 + 4)
7983

8084
# check magic

bitcoin/tests/test_params.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# Copyright (C) 2013-2014 The python-bitcoinlib developers
2+
#
3+
# This file is part of python-bitcoinlib.
4+
#
5+
# It is subject to the license terms in the LICENSE file found in the top-level
6+
# directory of this distribution.
7+
#
8+
# No part of python-bitcoinlib, including this file, may be copied, modified,
9+
# propagated, or distributed except according to the terms contained in the
10+
# LICENSE file.
11+
12+
import unittest
13+
import bitcoin
14+
15+
from bitcoin.messages import msg_addr
16+
17+
class Test_params(unittest.TestCase):
18+
def tearDown(self):
19+
bitcoin.SelectParams('mainnet')
20+
21+
def test_mainnet_magic_byte(self):
22+
bitcoin.SelectParams('mainnet')
23+
self.assertEquals(bitcoin.MainParams().MESSAGE_START, msg_addr().to_bytes()[0:4])
24+
25+
def test_testnet_magic_byte(self):
26+
bitcoin.SelectParams('testnet')
27+
self.assertEquals(bitcoin.TestNetParams().MESSAGE_START, msg_addr().to_bytes()[0:4])
28+
29+
def test_mainnet_params_magic_byte(self):
30+
bitcoin.SelectParams('testnet')
31+
self.assertEquals(bitcoin.MainParams().MESSAGE_START, msg_addr().to_bytes(params=bitcoin.MainParams())[0:4])
32+
33+
def test_testnet_params_magic_byte(self):
34+
bitcoin.SelectParams('mainnet')
35+
self.assertEquals(bitcoin.TestNetParams().MESSAGE_START, msg_addr().to_bytes(params=bitcoin.TestNetParams())[0:4])
36+
37+
38+
39+
40+
41+
42+
43+

0 commit comments

Comments
 (0)