2017-11-10 04:44:31 +01:00
|
|
|
package channeldb
|
|
|
|
|
|
|
|
import (
|
2021-02-19 12:03:01 +01:00
|
|
|
"bytes"
|
2017-11-10 04:44:31 +01:00
|
|
|
"encoding/binary"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
2018-06-28 03:06:03 +02:00
|
|
|
"net"
|
2017-11-10 04:44:31 +01:00
|
|
|
|
2022-02-23 14:48:00 +01:00
|
|
|
"github.com/btcsuite/btcd/btcec/v2"
|
|
|
|
"github.com/btcsuite/btcd/btcutil"
|
2018-06-05 03:34:16 +02:00
|
|
|
"github.com/btcsuite/btcd/chaincfg/chainhash"
|
|
|
|
"github.com/btcsuite/btcd/wire"
|
2018-07-31 09:17:17 +02:00
|
|
|
"github.com/lightningnetwork/lnd/keychain"
|
|
|
|
"github.com/lightningnetwork/lnd/lnwire"
|
|
|
|
"github.com/lightningnetwork/lnd/shachain"
|
2022-04-04 22:09:15 +02:00
|
|
|
"github.com/lightningnetwork/lnd/tlv"
|
2017-11-10 04:44:31 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
// writeOutpoint writes an outpoint to the passed writer using the minimal
|
|
|
|
// amount of bytes possible.
|
|
|
|
func writeOutpoint(w io.Writer, o *wire.OutPoint) error {
|
|
|
|
if _, err := w.Write(o.Hash[:]); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := binary.Write(w, byteOrder, o.Index); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// readOutpoint reads an outpoint from the passed reader that was previously
|
|
|
|
// written using the writeOutpoint struct.
|
|
|
|
func readOutpoint(r io.Reader, o *wire.OutPoint) error {
|
|
|
|
if _, err := io.ReadFull(r, o.Hash[:]); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := binary.Read(r, byteOrder, &o.Index); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-06-28 01:56:49 +02:00
|
|
|
// UnknownElementType is an error returned when the codec is unable to encode or
|
|
|
|
// decode a particular type.
|
|
|
|
type UnknownElementType struct {
|
|
|
|
method string
|
|
|
|
element interface{}
|
|
|
|
}
|
|
|
|
|
2019-04-27 02:20:23 +02:00
|
|
|
// NewUnknownElementType creates a new UnknownElementType error from the passed
|
|
|
|
// method name and element.
|
|
|
|
func NewUnknownElementType(method string, el interface{}) UnknownElementType {
|
|
|
|
return UnknownElementType{method: method, element: el}
|
|
|
|
}
|
|
|
|
|
2018-06-28 01:56:49 +02:00
|
|
|
// Error returns the name of the method that encountered the error, as well as
|
|
|
|
// the type that was unsupported.
|
|
|
|
func (e UnknownElementType) Error() string {
|
|
|
|
return fmt.Sprintf("Unknown type in %s: %T", e.method, e.element)
|
|
|
|
}
|
|
|
|
|
2018-06-28 01:29:38 +02:00
|
|
|
// WriteElement is a one-stop shop to write the big endian representation of
|
2017-11-10 04:44:31 +01:00
|
|
|
// any element which is to be serialized for storage on disk. The passed
|
|
|
|
// io.Writer should be backed by an appropriately sized byte slice, or be able
|
|
|
|
// to dynamically expand to accommodate additional data.
|
2018-06-28 01:29:38 +02:00
|
|
|
func WriteElement(w io.Writer, element interface{}) error {
|
2017-11-10 04:44:31 +01:00
|
|
|
switch e := element.(type) {
|
2018-02-18 00:14:07 +01:00
|
|
|
case keychain.KeyDescriptor:
|
|
|
|
if err := binary.Write(w, byteOrder, e.Family); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := binary.Write(w, byteOrder, e.Index); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if e.PubKey != nil {
|
|
|
|
if err := binary.Write(w, byteOrder, true); err != nil {
|
2019-09-12 15:49:11 +02:00
|
|
|
return fmt.Errorf("error writing serialized element: %s", err)
|
2018-02-18 00:14:07 +01:00
|
|
|
}
|
|
|
|
|
2018-06-28 01:29:38 +02:00
|
|
|
return WriteElement(w, e.PubKey)
|
2018-02-18 00:14:07 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return binary.Write(w, byteOrder, false)
|
2017-11-10 04:44:31 +01:00
|
|
|
case ChannelType:
|
2022-04-04 22:09:15 +02:00
|
|
|
var buf [8]byte
|
|
|
|
if err := tlv.WriteVarInt(w, uint64(e), &buf); err != nil {
|
2017-11-10 04:44:31 +01:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
case chainhash.Hash:
|
|
|
|
if _, err := w.Write(e[:]); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
case wire.OutPoint:
|
|
|
|
return writeOutpoint(w, &e)
|
|
|
|
|
|
|
|
case lnwire.ShortChannelID:
|
|
|
|
if err := binary.Write(w, byteOrder, e.ToUint64()); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-05-24 05:48:08 +02:00
|
|
|
case lnwire.ChannelID:
|
|
|
|
if _, err := w.Write(e[:]); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-08-12 14:58:59 +02:00
|
|
|
case int64, uint64:
|
2017-11-10 04:44:31 +01:00
|
|
|
if err := binary.Write(w, byteOrder, e); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
case uint32:
|
|
|
|
if err := binary.Write(w, byteOrder, e); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
case int32:
|
|
|
|
if err := binary.Write(w, byteOrder, e); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
case uint16:
|
|
|
|
if err := binary.Write(w, byteOrder, e); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-05-25 02:04:07 +02:00
|
|
|
case uint8:
|
|
|
|
if err := binary.Write(w, byteOrder, e); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-11-10 04:44:31 +01:00
|
|
|
case bool:
|
|
|
|
if err := binary.Write(w, byteOrder, e); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
case btcutil.Amount:
|
|
|
|
if err := binary.Write(w, byteOrder, uint64(e)); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
case lnwire.MilliSatoshi:
|
|
|
|
if err := binary.Write(w, byteOrder, uint64(e)); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-05-23 20:05:26 +02:00
|
|
|
case *btcec.PrivateKey:
|
|
|
|
b := e.Serialize()
|
|
|
|
if _, err := w.Write(b); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-11-10 04:44:31 +01:00
|
|
|
case *btcec.PublicKey:
|
|
|
|
b := e.SerializeCompressed()
|
|
|
|
if _, err := w.Write(b); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
case shachain.Producer:
|
|
|
|
return e.Encode(w)
|
|
|
|
|
|
|
|
case shachain.Store:
|
|
|
|
return e.Encode(w)
|
|
|
|
|
|
|
|
case *wire.MsgTx:
|
|
|
|
return e.Serialize(w)
|
|
|
|
|
|
|
|
case [32]byte:
|
|
|
|
if _, err := w.Write(e[:]); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
case []byte:
|
|
|
|
if err := wire.WriteVarBytes(w, 0, e); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
case lnwire.Message:
|
2021-02-19 12:03:01 +01:00
|
|
|
var msgBuf bytes.Buffer
|
|
|
|
if _, err := lnwire.WriteMessage(&msgBuf, e, 0); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
msgLen := uint16(len(msgBuf.Bytes()))
|
|
|
|
if err := WriteElements(w, msgLen); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, err := w.Write(msgBuf.Bytes()); err != nil {
|
2017-11-10 04:44:31 +01:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-04-12 13:08:01 +02:00
|
|
|
case ChannelStatus:
|
2022-04-04 22:09:15 +02:00
|
|
|
var buf [8]byte
|
|
|
|
if err := tlv.WriteVarInt(w, uint64(e), &buf); err != nil {
|
2018-04-12 13:08:01 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-11-10 04:44:31 +01:00
|
|
|
case ClosureType:
|
|
|
|
if err := binary.Write(w, byteOrder, e); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-04-12 13:08:01 +02:00
|
|
|
|
2020-06-10 12:34:27 +02:00
|
|
|
case paymentIndexType:
|
|
|
|
if err := binary.Write(w, byteOrder, e); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-11-14 02:09:59 +01:00
|
|
|
case lnwire.FundingFlag:
|
|
|
|
if err := binary.Write(w, byteOrder, e); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-11-10 04:44:31 +01:00
|
|
|
|
2018-06-28 03:06:03 +02:00
|
|
|
case net.Addr:
|
|
|
|
if err := serializeAddr(w, e); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
case []net.Addr:
|
|
|
|
if err := WriteElement(w, uint32(len(e))); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, addr := range e {
|
|
|
|
if err := serializeAddr(w, addr); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-10 04:44:31 +01:00
|
|
|
default:
|
2018-06-28 01:56:49 +02:00
|
|
|
return UnknownElementType{"WriteElement", e}
|
2017-11-10 04:44:31 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-06-28 01:29:38 +02:00
|
|
|
// WriteElements is writes each element in the elements slice to the passed
|
|
|
|
// io.Writer using WriteElement.
|
|
|
|
func WriteElements(w io.Writer, elements ...interface{}) error {
|
2017-11-10 04:44:31 +01:00
|
|
|
for _, element := range elements {
|
2018-06-28 01:29:38 +02:00
|
|
|
err := WriteElement(w, element)
|
2017-11-10 04:44:31 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-06-28 01:29:38 +02:00
|
|
|
// ReadElement is a one-stop utility function to deserialize any datastructure
|
2017-11-10 04:44:31 +01:00
|
|
|
// encoded using the serialization format of the database.
|
2018-06-28 01:29:38 +02:00
|
|
|
func ReadElement(r io.Reader, element interface{}) error {
|
2017-11-10 04:44:31 +01:00
|
|
|
switch e := element.(type) {
|
2018-02-18 00:14:07 +01:00
|
|
|
case *keychain.KeyDescriptor:
|
|
|
|
if err := binary.Read(r, byteOrder, &e.Family); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := binary.Read(r, byteOrder, &e.Index); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
var hasPubKey bool
|
|
|
|
if err := binary.Read(r, byteOrder, &hasPubKey); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if hasPubKey {
|
2018-06-28 01:29:38 +02:00
|
|
|
return ReadElement(r, &e.PubKey)
|
2018-02-18 00:14:07 +01:00
|
|
|
}
|
|
|
|
|
2017-11-10 04:44:31 +01:00
|
|
|
case *ChannelType:
|
2022-04-04 22:09:15 +02:00
|
|
|
var buf [8]byte
|
|
|
|
ctype, err := tlv.ReadVarInt(r, &buf)
|
|
|
|
if err != nil {
|
2017-11-10 04:44:31 +01:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-04-04 22:09:15 +02:00
|
|
|
*e = ChannelType(ctype)
|
|
|
|
|
2017-11-10 04:44:31 +01:00
|
|
|
case *chainhash.Hash:
|
|
|
|
if _, err := io.ReadFull(r, e[:]); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
case *wire.OutPoint:
|
|
|
|
return readOutpoint(r, e)
|
|
|
|
|
|
|
|
case *lnwire.ShortChannelID:
|
|
|
|
var a uint64
|
|
|
|
if err := binary.Read(r, byteOrder, &a); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
*e = lnwire.NewShortChanIDFromInt(a)
|
|
|
|
|
2019-05-24 05:48:08 +02:00
|
|
|
case *lnwire.ChannelID:
|
|
|
|
if _, err := io.ReadFull(r, e[:]); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-08-12 14:58:59 +02:00
|
|
|
case *int64, *uint64:
|
2017-11-10 04:44:31 +01:00
|
|
|
if err := binary.Read(r, byteOrder, e); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
case *uint32:
|
|
|
|
if err := binary.Read(r, byteOrder, e); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
case *int32:
|
|
|
|
if err := binary.Read(r, byteOrder, e); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
case *uint16:
|
|
|
|
if err := binary.Read(r, byteOrder, e); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-05-25 02:04:07 +02:00
|
|
|
case *uint8:
|
|
|
|
if err := binary.Read(r, byteOrder, e); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-11-10 04:44:31 +01:00
|
|
|
case *bool:
|
|
|
|
if err := binary.Read(r, byteOrder, e); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
case *btcutil.Amount:
|
|
|
|
var a uint64
|
|
|
|
if err := binary.Read(r, byteOrder, &a); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
*e = btcutil.Amount(a)
|
|
|
|
|
|
|
|
case *lnwire.MilliSatoshi:
|
|
|
|
var a uint64
|
|
|
|
if err := binary.Read(r, byteOrder, &a); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
*e = lnwire.MilliSatoshi(a)
|
|
|
|
|
2019-05-23 20:05:26 +02:00
|
|
|
case **btcec.PrivateKey:
|
|
|
|
var b [btcec.PrivKeyBytesLen]byte
|
|
|
|
if _, err := io.ReadFull(r, b[:]); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-02-23 14:48:00 +01:00
|
|
|
priv, _ := btcec.PrivKeyFromBytes(b[:])
|
2019-05-23 20:05:26 +02:00
|
|
|
*e = priv
|
|
|
|
|
2017-11-10 04:44:31 +01:00
|
|
|
case **btcec.PublicKey:
|
|
|
|
var b [btcec.PubKeyBytesLenCompressed]byte
|
|
|
|
if _, err := io.ReadFull(r, b[:]); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-02-23 14:48:00 +01:00
|
|
|
pubKey, err := btcec.ParsePubKey(b[:])
|
2017-11-10 04:44:31 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
*e = pubKey
|
|
|
|
|
|
|
|
case *shachain.Producer:
|
|
|
|
var root [32]byte
|
|
|
|
if _, err := io.ReadFull(r, root[:]); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO(roasbeef): remove
|
|
|
|
producer, err := shachain.NewRevocationProducerFromBytes(root[:])
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
*e = producer
|
|
|
|
|
|
|
|
case *shachain.Store:
|
|
|
|
store, err := shachain.NewRevocationStoreFromBytes(r)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
*e = store
|
|
|
|
|
|
|
|
case **wire.MsgTx:
|
|
|
|
tx := wire.NewMsgTx(2)
|
|
|
|
if err := tx.Deserialize(r); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
*e = tx
|
|
|
|
|
|
|
|
case *[32]byte:
|
|
|
|
if _, err := io.ReadFull(r, e[:]); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
case *[]byte:
|
|
|
|
bytes, err := wire.ReadVarBytes(r, 0, 66000, "[]byte")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
*e = bytes
|
|
|
|
|
|
|
|
case *lnwire.Message:
|
2021-02-19 12:03:01 +01:00
|
|
|
var msgLen uint16
|
|
|
|
if err := ReadElement(r, &msgLen); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
msgReader := io.LimitReader(r, int64(msgLen))
|
|
|
|
msg, err := lnwire.ReadMessage(msgReader, 0)
|
2017-11-10 04:44:31 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
*e = msg
|
|
|
|
|
2018-04-12 13:08:01 +02:00
|
|
|
case *ChannelStatus:
|
2022-04-04 22:09:15 +02:00
|
|
|
var buf [8]byte
|
|
|
|
status, err := tlv.ReadVarInt(r, &buf)
|
|
|
|
if err != nil {
|
2018-04-12 13:08:01 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-04-04 22:09:15 +02:00
|
|
|
*e = ChannelStatus(status)
|
|
|
|
|
2017-11-10 04:44:31 +01:00
|
|
|
case *ClosureType:
|
|
|
|
if err := binary.Read(r, byteOrder, e); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-04-12 13:08:01 +02:00
|
|
|
|
2020-06-10 12:34:27 +02:00
|
|
|
case *paymentIndexType:
|
|
|
|
if err := binary.Read(r, byteOrder, e); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-11-14 02:09:59 +01:00
|
|
|
case *lnwire.FundingFlag:
|
|
|
|
if err := binary.Read(r, byteOrder, e); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-11-10 04:44:31 +01:00
|
|
|
|
2018-06-28 03:06:03 +02:00
|
|
|
case *net.Addr:
|
|
|
|
addr, err := deserializeAddr(r)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
*e = addr
|
|
|
|
|
|
|
|
case *[]net.Addr:
|
|
|
|
var numAddrs uint32
|
|
|
|
if err := ReadElement(r, &numAddrs); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
*e = make([]net.Addr, numAddrs)
|
|
|
|
for i := uint32(0); i < numAddrs; i++ {
|
|
|
|
addr, err := deserializeAddr(r)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
(*e)[i] = addr
|
|
|
|
}
|
|
|
|
|
2017-11-10 04:44:31 +01:00
|
|
|
default:
|
2018-06-28 01:56:49 +02:00
|
|
|
return UnknownElementType{"ReadElement", e}
|
2017-11-10 04:44:31 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-06-28 01:29:38 +02:00
|
|
|
// ReadElements deserializes a variable number of elements into the passed
|
|
|
|
// io.Reader, with each element being deserialized according to the ReadElement
|
2017-11-10 04:44:31 +01:00
|
|
|
// function.
|
2018-06-28 01:29:38 +02:00
|
|
|
func ReadElements(r io.Reader, elements ...interface{}) error {
|
2017-11-10 04:44:31 +01:00
|
|
|
for _, element := range elements {
|
2018-06-28 01:29:38 +02:00
|
|
|
err := ReadElement(r, element)
|
2017-11-10 04:44:31 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|