lnw+ba+utxon+cdb: reverts OutPoint and TxOut encoding

This commit is contained in:
Conner Fromknecht authored and Olaoluwa Osuntokun committed 2017-08-25 17:56:50 -07:00
1 parent 8698085e35
commit 6ffe33f01a
8 files changed
+148 -148

No files matched your search

+38 -2
View File
@@ -4,11 +4,13 @@ import (
"encoding/binary"
"fmt"
"io"
"math"
"net"
"github.com/go-errors/errors"
"github.com/roasbeef/btcd/btcec"
"github.com/roasbeef/btcd/chaincfg/chainhash"
"github.com/roasbeef/btcd/wire"
"github.com/roasbeef/btcutil"
)
@@ -187,7 +189,21 @@ func writeElement(w io.Writer, element interface{}) error {
}
case wire.OutPoint:
if err := WriteOutPoint(w, &e); err != nil {
var h [32]byte
copy(h[:], e.Hash[:])
if _, err := w.Write(h[:]); err != nil {
return err
}
if e.Index > math.MaxUint16 {
return fmt.Errorf("index for outpoint (%v) is "+
"greater than max index of %v", e.Index,
math.MaxUint16)
}
var idx [2]byte
binary.BigEndian.PutUint16(idx[:], uint16(e.Index))
if _, err := w.Write(idx[:]); err != nil {
return err
}
@@ -463,10 +479,30 @@ func readElement(r io.Reader, element interface{}) error {
}
*e = pkScript
case *wire.OutPoint:
if err := ReadOutPoint(r, e); err != nil {
var h [32]byte
if _, err = io.ReadFull(r, h[:]); err != nil {
return err
}
hash, err := chainhash.NewHash(h[:])
if err != nil {
return err
}
var idxBytes [2]byte
_, err = io.ReadFull(r, idxBytes[:])
if err != nil {
return err
}
index := binary.BigEndian.Uint16(idxBytes[:])
*e = wire.OutPoint{
Hash: *hash,
Index: uint32(index),
}
case *FailCode:
if err := readElement(r, (*uint16)(e)); err != nil {
return err
}
case *ChannelID:
if _, err := io.ReadFull(r, e[:]); err != nil {
return err
-59
View File
@@ -1,59 +0,0 @@
package lnwire
import (
"encoding/binary"
"fmt"
"io"
"math"
"github.com/roasbeef/btcd/chaincfg/chainhash"
"github.com/roasbeef/btcd/wire"
)
// WriteOutPoint serializes a wire.OutPoint struct into the passed io.Writer
// stream.
func WriteOutPoint(w io.Writer, o *wire.OutPoint) error {
if _, err := w.Write(o.Hash[:chainhash.HashSize]); err != nil {
return err
}
if o.Index > math.MaxUint16 {
return fmt.Errorf("index for outpoint (%v) is "+
"greater than max index of %v", o.Index, math.MaxUint16)
}
var idx [2]byte
binary.BigEndian.PutUint16(idx[:], uint16(o.Index))
if _, err := w.Write(idx[:]); err != nil {
return err
}
return nil
}
// ReadOutPoint deserializes a wire.OutPoint struct from the passed io.Reader
// stream.
func ReadOutPoint(r io.Reader, o *wire.OutPoint) error {
var h [chainhash.HashSize]byte
if _, err := io.ReadFull(r, h[:]); err != nil {
return err
}
hash, err := chainhash.NewHash(h[:])
if err != nil {
return err
}
var idxBytes [2]byte
_, err = io.ReadFull(r, idxBytes[:])
if err != nil {
return err
}
index := binary.BigEndian.Uint16(idxBytes[:])
*o = wire.OutPoint{
Hash: *hash,
Index: uint32(index),
}
return nil
}
-40
View File
@@ -1,40 +0,0 @@
package lnwire
import (
"bytes"
"reflect"
"testing"
"github.com/roasbeef/btcd/chaincfg/chainhash"
"github.com/roasbeef/btcd/wire"
)
func TestOutpointSerialization(t *testing.T) {
outpoint := wire.OutPoint{
Hash: [chainhash.HashSize]byte{
0x51, 0xb6, 0x37, 0xd8, 0xfc, 0xd2, 0xc6, 0xda,
0x48, 0x59, 0xe6, 0x96, 0x31, 0x13, 0xa1, 0x17,
0x2d, 0xe7, 0x93, 0xe4, 0xb7, 0x25, 0xb8, 0x4d,
0x1f, 0xb, 0x4c, 0xf9, 0x9e, 0xc5, 0x8c, 0xe9,
},
Index: 9,
}
var buf bytes.Buffer
if err := WriteOutPoint(&buf, &outpoint); err != nil {
t.Fatalf("unable to serialize outpoint: %v", err)
}
var deserializedOutpoint wire.OutPoint
if err := ReadOutPoint(&buf, &deserializedOutpoint); err != nil {
t.Fatalf("unable to deserialize outpoint: %v", err)
}
if !reflect.DeepEqual(outpoint, deserializedOutpoint) {
t.Fatalf("original and deserialized outpoints are different:\n"+
"original : %+v\n"+
"deserialized : %+v\n",
outpoint, deserializedOutpoint)
}
}