multi: fix linter errors

This commit is contained in:
Olaoluwa Osuntokun
2023-08-08 22:22:12 -07:00
parent d3e4bca772
commit a2b277cf1d
20 changed files with 187 additions and 144 deletions

View File

@@ -22,48 +22,6 @@ const (
secondLevelHtlcTweakCtrlBlockType tlv.Type = 2
)
// tlvDecoder is an interface to support more generic decoding of TLV records.
type tlvDecoder interface {
// DecodeRecords returns a slice of TLV records that should be decoded.
DecodeRecords() []tlv.Record
// Decode decodes the given reader into the target struct.
Decode(r io.Reader) error
}
// makeAndDecodeStream takes a tlvDecoder and decodes the records encoded in
// the stream into it.
//
// TODO(roasbeef): use elsewhere
func makeAndDecodeStream(r io.Reader, d tlvDecoder) error {
stream, err := tlv.NewStream(d.DecodeRecords()...)
if err != nil {
return err
}
return stream.Decode(r)
}
// tlvEncoder is an interface to support more generic encoding of TLV records.
type tlvEncoder interface {
// EncodeRecords returns a slice of TLV records that should be encoded.
EncodeRecords() []tlv.Record
// Encode encodes the target struct into the given writer.
Encode(w io.Writer) error
}
// makeAnEncodeStream takes a tlvEncoder and encodes the records into the
// target writer as a TLV stream.
func makeAndEncodeStream[E tlvEncoder](w io.Writer, e E) error {
stream, err := tlv.NewStream(e.EncodeRecords()...)
if err != nil {
return err
}
return stream.Encode(w)
}
// taprootBriefcase is a supplemental storage struct that contains all the
// information we need to sweep taproot outputs.
type taprootBriefcase struct {
@@ -107,6 +65,7 @@ func (t *taprootBriefcase) Encode(w io.Writer) error {
if err != nil {
return err
}
return stream.Encode(w)
}
@@ -116,13 +75,15 @@ func (t *taprootBriefcase) Decode(r io.Reader) error {
if err != nil {
return err
}
return stream.Decode(r)
}
// resolverCtrlBlocks is a map of resolver IDs to their corresponding control block.
// resolverCtrlBlocks is a map of resolver IDs to their corresponding control
// block.
type resolverCtrlBlocks map[resolverID][]byte
// newResolverCtrlBlockss returns a new instance of the resolverCtrlBlocks
// newResolverCtrlBlockss returns a new instance of the resolverCtrlBlocks.
func newResolverCtrlBlocks() resolverCtrlBlocks {
return make(resolverCtrlBlocks)
}
@@ -170,32 +131,34 @@ func (r *resolverCtrlBlocks) Encode(w io.Writer) error {
}
// Decode decodes the given reader into the target struct.
func (t *resolverCtrlBlocks) Decode(r io.Reader) error {
func (r *resolverCtrlBlocks) Decode(reader io.Reader) error {
var buf [8]byte
numBlocks, err := tlv.ReadVarInt(r, &buf)
numBlocks, err := tlv.ReadVarInt(reader, &buf)
if err != nil {
return err
}
for i := uint64(0); i < numBlocks; i++ {
var id resolverID
if _, err := io.ReadFull(r, id[:]); err != nil {
if _, err := io.ReadFull(reader, id[:]); err != nil {
return err
}
var ctrlBlock []byte
if err := varBytesDecoder(r, &ctrlBlock, &buf, 0); err != nil {
err := varBytesDecoder(reader, &ctrlBlock, &buf, 0)
if err != nil {
return err
}
(*t)[id] = ctrlBlock
(*r)[id] = ctrlBlock
}
return nil
}
// resolverCtrlBlocksEncoder is a custom TLV encoder for the resolverCtrlBlocks
// resolverCtrlBlocksEncoder is a custom TLV encoder for the
// resolverCtrlBlocks.
func resolverCtrlBlocksEncoder(w io.Writer, val any, buf *[8]byte) error {
if typ, ok := val.(*resolverCtrlBlocks); ok {
return (*typ).Encode(w)
@@ -204,7 +167,7 @@ func resolverCtrlBlocksEncoder(w io.Writer, val any, buf *[8]byte) error {
return tlv.NewTypeForEncodingErr(val, "resolverCtrlBlocks")
}
// rsolverCtrlBlocksDecoder is a custom TLV decoder for the resolverCtrlBlocks
// rsolverCtrlBlocksDecoder is a custom TLV decoder for the resolverCtrlBlocks.
func resolverCtrlBlocksDecoder(r io.Reader, val any, buf *[8]byte,
l uint64) error {
@@ -218,6 +181,7 @@ func resolverCtrlBlocksDecoder(r io.Reader, val any, buf *[8]byte,
}
*typ = resolverBlocks
return nil
}
@@ -315,6 +279,7 @@ func ctrlBlockDecoder(r io.Reader, val any, buf *[8]byte, l uint64) error {
}
*typ = &ctrlBlocks
return nil
}
@@ -333,8 +298,10 @@ func newCtrlBlocksRecord(blks **ctrlBlocks) tlv.Record {
if err := ctrlBlockEncoder(&b, blks, &buf); err != nil {
panic(err)
}
return uint64(len(b.Bytes()))
}
return tlv.MakeDynamicRecord(
taprootCtrlBlockType, blks, recordSize, ctrlBlockEncoder,
ctrlBlockDecoder,
@@ -549,7 +516,8 @@ func (h *htlcTapTweaks) Decode(reader io.Reader) error {
// tapTweaks stores the set of taptweaks needed to perform keyspends for the
// commitment outputs.
type tapTweaks struct {
// AnchorTweak is the tweak used to derive the key used to spend the anchor output.
// AnchorTweak is the tweak used to derive the key used to spend the
// anchor output.
AnchorTweak []byte
// BreachedHtlcTweaks stores the set of tweaks needed to sweep the
@@ -590,6 +558,7 @@ func tapTweaksDecoder(r io.Reader, val any, buf *[8]byte, l uint64) error {
}
*typ = &tapTweaks
return nil
}
@@ -607,8 +576,10 @@ func newTapTweaksRecord(tweaks **tapTweaks) tlv.Record {
if err := tapTweaksEncoder(&b, tweaks, &buf); err != nil {
panic(err)
}
return uint64(len(b.Bytes()))
}
return tlv.MakeDynamicRecord(
taprootTapTweakType, tweaks, recordSize, tapTweaksEncoder,
tapTweaksDecoder,