From a3ff9f1cb1dee74dc3b441be7f76b082df229384 Mon Sep 17 00:00:00 2001 From: David Hill Date: Thu, 9 Jul 2015 11:50:03 -0400 Subject: [PATCH] Implmement BlockHeader BtcEncode/BtcDecode. At the current time, there is no difference between the wire encoding at protocol version 0 and the stable long-term storage format. These methods are simply for consistency with the other types. --- wire/blockheader.go | 16 ++++++++++++++++ wire/blockheader_test.go | 24 +++++++++++++++++++----- wire/internal_test.go | 6 ------ 3 files changed, 35 insertions(+), 11 deletions(-) diff --git a/wire/blockheader.go b/wire/blockheader.go index 4e8577f0..2eefa5dc 100644 --- a/wire/blockheader.go +++ b/wire/blockheader.go @@ -95,6 +95,22 @@ func (h *BlockHeader) BlockSha() chainhash.Hash { return chainhash.HashFuncH(buf.Bytes()) } +// BtcDecode decodes r using the bitcoin protocol encoding into the receiver. +// This is part of the Message interface implementation. +// See Deserialize for decoding block headers stored to disk, such as in a +// database, as opposed to decoding block headers from the wire. +func (h *BlockHeader) BtcDecode(r io.Reader, pver uint32) error { + return readBlockHeader(r, pver, h) +} + +// BtcEncode encodes the receiver to w using the bitcoin protocol encoding. +// This is part of the Message interface implementation. +// See Serialize for encoding block headers to be stored to disk, such as in a +// database, as opposed to encoding block headers for the wire. +func (h *BlockHeader) BtcEncode(w io.Writer, pver uint32) error { + return writeBlockHeader(w, pver, h) +} + // Deserialize decodes a block header from r into the receiver using a format // that is suitable for long-term storage such as a database while respecting // the Version field. diff --git a/wire/blockheader_test.go b/wire/blockheader_test.go index 7476c9b0..601532fc 100644 --- a/wire/blockheader_test.go +++ b/wire/blockheader_test.go @@ -113,6 +113,7 @@ func TestBlockHeader(t *testing.T) { // protocol versions. func TestBlockHeaderWire(t *testing.T) { nonce := uint32(123123) // 0x1e0f3 + pver := uint32(70001) /*bh := dcrwire.NewBlockHeader( &hash, @@ -216,14 +217,15 @@ func TestBlockHeaderWire(t *testing.T) { continue } - b, err := wire.TstBytesBlockHeader(test.in) + buf.Reset() + err = test.in.BtcEncode(&buf, pver) if err != nil { - t.Errorf("writeBlockHeader #%d error %v", i, err) + t.Errorf("BtcEncode #%d error %v", i, err) continue } - if !bytes.Equal(b, test.buf) { - t.Errorf("writeBlockHeader #%d\n got: %s want: %s", i, - spew.Sdump(b), spew.Sdump(test.buf)) + if !bytes.Equal(buf.Bytes(), test.buf) { + t.Errorf("BtcEncode #%d\n got: %s want: %s", i, + spew.Sdump(buf.Bytes()), spew.Sdump(test.buf)) continue } @@ -240,6 +242,18 @@ func TestBlockHeaderWire(t *testing.T) { spew.Sdump(&bh), spew.Sdump(test.out)) continue } + + rbuf = bytes.NewReader(test.buf) + err = bh.BtcDecode(rbuf, pver) + if err != nil { + t.Errorf("BtcDecode #%d error %v", i, err) + continue + } + if !reflect.DeepEqual(&bh, test.out) { + t.Errorf("BtcDecode #%d\n got: %s want: %s", i, + spew.Sdump(&bh), spew.Sdump(test.out)) + continue + } } } diff --git a/wire/internal_test.go b/wire/internal_test.go index 3a65a689..0987fc7b 100644 --- a/wire/internal_test.go +++ b/wire/internal_test.go @@ -117,9 +117,3 @@ func TstReadBlockHeader(r io.Reader, pver uint32, bh *BlockHeader) error { func TstWriteBlockHeader(w io.Writer, pver uint32, bh *BlockHeader) error { return writeBlockHeader(w, pver, bh) } - -// TstWriteBlockHeader makes the internal writeBlockHeader function available to -// the test package. -func TstBytesBlockHeader(bh *BlockHeader) ([]byte, error) { - return bh.Bytes() -}