Skip to content
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

Mc encoding to tlv #146

Closed
wants to merge 4 commits into from
Closed
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
38 changes: 38 additions & 0 deletions channeldb/migration/lnwire21/msat.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ package lnwire

import (
"fmt"
"io"

"github.com/btcsuite/btcd/btcutil"
"github.com/lightningnetwork/lnd/tlv"
)

const (
Expand Down Expand Up @@ -49,3 +51,39 @@ func (m MilliSatoshi) String() string {
}

// TODO(roasbeef): extend with arithmetic operations?

// Record returns a TLV record that can be used to encode/decode a MilliSatoshi
// to/from a TLV stream.
func (m *MilliSatoshi) Record() tlv.Record {
return tlv.MakeDynamicRecord(
0, m, tlv.SizeBigSize(m), encodeMilliSatoshis,
decodeMilliSatoshis,
)
}
func encodeMilliSatoshis(w io.Writer, val interface{}, buf *[8]byte) error {
if v, ok := val.(*MilliSatoshi); ok {
bigSize := uint64(*v)

return tlv.EBigSize(w, &bigSize, buf)
}

return tlv.NewTypeForEncodingErr(val, "lnwire.MilliSatoshi")
}

func decodeMilliSatoshis(r io.Reader, val interface{}, buf *[8]byte,
l uint64) error {

if v, ok := val.(*MilliSatoshi); ok {
var bigSize uint64
err := tlv.DBigSize(r, &bigSize, buf, l)
if err != nil {
return err
}

*v = MilliSatoshi(bigSize)

return nil
}

return tlv.NewTypeForDecodingErr(val, "lnwire.MilliSatoshi", l, l)
}
37 changes: 37 additions & 0 deletions channeldb/migration/lnwire21/true_boolean.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package lnwire

import (
"io"

"github.com/lightningnetwork/lnd/tlv"
)

// TrueBoolean is a record that indicates true or false using the presence of
// the record. If the record is absent, it indicates false. If it is presence,
// it indicates true.
type TrueBoolean struct{}

// Record returns the tlv record for the boolean entry.
func (b *TrueBoolean) Record() tlv.Record {
return tlv.MakeStaticRecord(
0, b, 0, booleanEncoder, booleanDecoder,
)
}

func booleanEncoder(_ io.Writer, val interface{}, _ *[8]byte) error {
if _, ok := val.(*TrueBoolean); ok {
return nil
}

return tlv.NewTypeForEncodingErr(val, "TrueBoolean")
}

func booleanDecoder(_ io.Reader, val interface{}, _ *[8]byte,
l uint64) error {

if _, ok := val.(*TrueBoolean); ok && (l == 0 || l == 1) {
return nil
}

return tlv.NewTypeForEncodingErr(val, "TrueBoolean")
}
Loading
Loading