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.
This commit is contained in:
David Hill 2015-07-09 11:50:03 -04:00 committed by Dave Collins
parent 458329060c
commit a3ff9f1cb1
3 changed files with 35 additions and 11 deletions

View File

@ -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.

View File

@ -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
}
}
}

View File

@ -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()
}