Skip to content

Use 'params=None' approach #42

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
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 9 additions & 5 deletions bitcoin/messages.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,13 @@ def msg_ser(self, f):
def msg_deser(cls, f, protover=PROTO_VERSION):
raise NotImplementedError

def to_bytes(self):
def to_bytes(self, params=None):
if params is None:
params = bitcoin.params
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"<I", len(body))
Expand All @@ -74,13 +76,15 @@ def from_bytes(cls, b, protover=PROTO_VERSION):
return MsgSerializable.stream_deserialize(f, protover=protover)

@classmethod
def stream_deserialize(cls, f, 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
if recvbuf[:4] != bitcoin.params.MESSAGE_START:
if recvbuf[:4] != params.MESSAGE_START:
raise ValueError("Invalid message start '%s', expected '%s'" %
(b2x(recvbuf[:4]), b2x(bitcoin.params.MESSAGE_START)))
(b2x(recvbuf[:4]), b2x(params.MESSAGE_START)))

# remaining header fields: command, msg length, checksum
command = recvbuf[4:4+12].split(b"\x00", 1)[0]
Expand Down
35 changes: 35 additions & 0 deletions bitcoin/tests/test_params.py
Original file line number Diff line number Diff line change
@@ -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])