Skip to content

Commit 6c2eb13

Browse files
committed
asset+tappsbt: fix generic type weirdness
By changing the generic signature of the AltLeaf type we can get rid of some of the weirdness and helper methods surrounding the type.
1 parent cdcba13 commit 6c2eb13

File tree

8 files changed

+44
-48
lines changed

8 files changed

+44
-48
lines changed

asset/asset.go

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2302,7 +2302,7 @@ type ChainAsset struct {
23022302
type AltLeaf[T any] interface {
23032303
// Copyable asserts that the target type of this interface satisfies
23042304
// the Copyable interface.
2305-
fn.Copyable[T]
2305+
fn.Copyable[*T]
23062306

23072307
// ValidateAltLeaf ensures that an AltLeaf is valid.
23082308
ValidateAltLeaf() error
@@ -2336,23 +2336,13 @@ func NewAltLeaf(key ScriptKey, keyVersion ScriptVersion,
23362336
}, nil
23372337
}
23382338

2339-
// InnerAltLeaf returns the inner value of an AltLeaf, as its concrete type.
2340-
func InnerAltLeaf[T AltLeaf[T]](a AltLeaf[T]) T {
2341-
return a.(T)
2342-
}
2343-
2344-
// CopyAltLeaf performs a deep copy of an AltLeaf.
2345-
func CopyAltLeaf[T AltLeaf[T]](a AltLeaf[T]) AltLeaf[T] {
2346-
return a.Copy()
2347-
}
2348-
23492339
// CopyAltLeaves performs a deep copy of an AltLeaf slice.
2350-
func CopyAltLeaves[T AltLeaf[T]](a []AltLeaf[T]) []AltLeaf[T] {
2340+
func CopyAltLeaves(a []AltLeaf[Asset]) []AltLeaf[Asset] {
23512341
if len(a) == 0 {
23522342
return nil
23532343
}
23542344

2355-
return fn.Map(a, CopyAltLeaf[T])
2345+
return ToAltLeaves(fn.CopyAll(FromAltLeaves(a)))
23562346
}
23572347

23582348
// ValidateAltLeaf checks that an Asset is a valid AltLeaf. An Asset used as an
@@ -2435,8 +2425,20 @@ func (a *Asset) DecodeAltLeaf(r io.Reader) error {
24352425
return a.Decode(r)
24362426
}
24372427

2438-
// AltLeafAsset is an AltLeaf backed by an Asset object.
2439-
type AltLeafAsset = AltLeaf[*Asset]
2440-
24412428
// Ensure Asset implements the AltLeaf interface.
2442-
var _ AltLeaf[*Asset] = (*Asset)(nil)
2429+
var _ AltLeaf[Asset] = (*Asset)(nil)
2430+
2431+
// ToAltLeaves casts []Asset to []AltLeafAsset, without checking that the assets
2432+
// are valid AltLeaves.
2433+
func ToAltLeaves(leaves []*Asset) []AltLeaf[Asset] {
2434+
return fn.Map(leaves, func(l *Asset) AltLeaf[Asset] {
2435+
return l
2436+
})
2437+
}
2438+
2439+
// FromAltLeaves casts []AltLeafAsset to []Asset, which is always safe.
2440+
func FromAltLeaves(leaves []AltLeaf[Asset]) []*Asset {
2441+
return fn.Map(leaves, func(l AltLeaf[Asset]) *Asset {
2442+
return l.(*Asset)
2443+
})
2444+
}

asset/encoding.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -809,7 +809,7 @@ func DecodeTapLeaf(leafData []byte) (*txscript.TapLeaf, error) {
809809
}
810810

811811
func AltLeavesEncoder(w io.Writer, val any, buf *[8]byte) error {
812-
if t, ok := val.(*[]AltLeaf[*Asset]); ok {
812+
if t, ok := val.(*[]AltLeaf[Asset]); ok {
813813
// If the AltLeaves slice is empty, we will still encode its
814814
// length here (as 0). Callers should avoid encoding empty
815815
// AltLeaves slices.
@@ -855,7 +855,7 @@ func AltLeavesDecoder(r io.Reader, val any, buf *[8]byte, l uint64) error {
855855
return tlv.ErrRecordTooLarge
856856
}
857857

858-
if typ, ok := val.(*[]AltLeaf[*Asset]); ok {
858+
if typ, ok := val.(*[]AltLeaf[Asset]); ok {
859859
// Each alt leaf is at least 42 bytes, which limits the total
860860
// number of aux leaves. So we don't need to enforce a strict
861861
// limit here.
@@ -864,7 +864,7 @@ func AltLeavesDecoder(r io.Reader, val any, buf *[8]byte, l uint64) error {
864864
return err
865865
}
866866

867-
leaves := make([]AltLeaf[*Asset], 0, numItems)
867+
leaves := make([]AltLeaf[Asset], 0, numItems)
868868
leafKeys := make(map[SerializedKey]struct{})
869869
for i := uint64(0); i < numItems; i++ {
870870
var streamBytes []byte
@@ -890,7 +890,7 @@ func AltLeavesDecoder(r io.Reader, val any, buf *[8]byte, l uint64) error {
890890
}
891891

892892
leafKeys[leafKey] = struct{}{}
893-
leaves = append(leaves, AltLeaf[*Asset](&leaf))
893+
leaves = append(leaves, AltLeaf[Asset](&leaf))
894894
}
895895

896896
*typ = leaves

asset/mock.go

Lines changed: 3 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ import (
1414
"github.com/btcsuite/btcd/txscript"
1515
"github.com/btcsuite/btcd/wire"
1616
"github.com/lightninglabs/lndclient"
17-
"github.com/lightninglabs/taproot-assets/fn"
1817
"github.com/lightninglabs/taproot-assets/internal/test"
1918
"github.com/lightninglabs/taproot-assets/mssmt"
2019
"github.com/lightningnetwork/lnd/input"
@@ -705,25 +704,12 @@ func RandAltLeaves(t testing.TB, nonZero bool) []*Asset {
705704
return altLeaves
706705
}
707706

708-
// ToAltLeaves casts []Asset to []AltLeafAsset, without checking that the assets
709-
// are valid AltLeaves.
710-
func ToAltLeaves(leaves []*Asset) []AltLeafAsset {
711-
return fn.Map(leaves, func(l *Asset) AltLeafAsset {
712-
return AltLeafAsset(l)
713-
})
714-
}
715-
716-
// FromAltLeaves casts []AltLeafAsset to []Asset, which is always safe.
717-
func FromAltLeaves(leaves []AltLeafAsset) []*Asset {
718-
return fn.Map(leaves, InnerAltLeaf[*Asset])
719-
}
720-
721707
// CompareAltLeaves compares two slices of AltLeafAssets for equality.
722-
func CompareAltLeaves(t *testing.T, a, b []AltLeafAsset) {
708+
func CompareAltLeaves(t *testing.T, a, b []AltLeaf[Asset]) {
723709
require.Equal(t, len(a), len(b))
724710

725-
aInner := fn.Map(a, InnerAltLeaf[*Asset])
726-
bInner := fn.Map(b, InnerAltLeaf[*Asset])
711+
aInner := FromAltLeaves(a)
712+
bInner := FromAltLeaves(b)
727713

728714
slices.SortStableFunc(aInner, SortFunc)
729715
slices.SortStableFunc(bInner, SortFunc)

tappsbt/decode.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -366,7 +366,7 @@ func proofDecoder(p **proof.Proof) decoderFunc {
366366
}
367367

368368
// altLeavesDecoder returns a decoder function that can handle nil alt leaves.
369-
func altLeavesDecoder(a *[]asset.AltLeafAsset) decoderFunc {
369+
func altLeavesDecoder(a *[]asset.AltLeaf[asset.Asset]) decoderFunc {
370370
return func(key, byteVal []byte) error {
371371
if len(byteVal) == 0 {
372372
return nil

tappsbt/decode_test.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -251,7 +251,9 @@ func TestEncodingDecoding(t *testing.T) {
251251
require.NoError(t, err)
252252

253253
secondLeaf.ScriptKey = asset.NewScriptKey(leafKeyCopy)
254-
altLeaves := []asset.AltLeafAsset{firstLeaf, secondLeaf}
254+
altLeaves := []asset.AltLeaf[asset.Asset]{
255+
firstLeaf, secondLeaf,
256+
}
255257

256258
pkt.Inputs[0].AltLeaves = asset.CopyAltLeaves(altLeaves)
257259
pkt.Outputs[0].AltLeaves = asset.CopyAltLeaves(
@@ -270,7 +272,9 @@ func TestEncodingDecoding(t *testing.T) {
270272
pkt := RandPacket(t, true, true)
271273

272274
numLeaves := 2000
273-
altLeaves := make([]asset.AltLeafAsset, numLeaves)
275+
altLeaves := make(
276+
[]asset.AltLeaf[asset.Asset], numLeaves,
277+
)
274278
for idx := range numLeaves {
275279
altLeaves[idx] = asset.RandAltLeaf(t)
276280
}

tappsbt/encode.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -385,7 +385,7 @@ func proofEncoder(p *proof.Proof) encoderFunc {
385385

386386
// altLeavesEncoder is an encoder that does nothing if the given alt leaf slice
387387
// is nil or empty.
388-
func altLeavesEncoder(a []asset.AltLeafAsset) encoderFunc {
388+
func altLeavesEncoder(a []asset.AltLeaf[asset.Asset]) encoderFunc {
389389
if len(a) == 0 {
390390
return func([]byte) ([]*customPsbtField, error) {
391391
return nil, nil

tappsbt/interface.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -396,7 +396,7 @@ type VInput struct {
396396
// will be inserted in the input anchor Tap commitment. These
397397
// data-carrying leaves are used for a purpose distinct from
398398
// representing individual Taproot Assets.
399-
AltLeaves []asset.AltLeafAsset
399+
AltLeaves []asset.AltLeaf[asset.Asset]
400400
}
401401

402402
// Copy creates a deep copy of the VInput.
@@ -596,7 +596,7 @@ type VOutput struct {
596596
// will be inserted in the output anchor Tap commitment. These
597597
// data-carrying leaves are used for a purpose distinct from
598598
// representing individual Taproot Assets.
599-
AltLeaves []asset.AltLeafAsset
599+
AltLeaves []asset.AltLeaf[asset.Asset]
600600
}
601601

602602
// Copy creates a deep copy of the VOutput.

tappsbt/mock.go

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -317,7 +317,7 @@ func NewTestFromVInput(t testing.TB, i *VInput) *TestVInput {
317317

318318
ti.AltLeaves = make([]*asset.TestAsset, 0, len(i.AltLeaves))
319319
for idx := range i.AltLeaves {
320-
leaf := asset.InnerAltLeaf(i.AltLeaves[idx])
320+
leaf := i.AltLeaves[idx].(*asset.Asset)
321321
ti.AltLeaves = append(
322322
ti.AltLeaves, asset.NewTestFromAsset(t, leaf),
323323
)
@@ -380,7 +380,9 @@ func (ti *TestVInput) ToVInput(t testing.TB) *VInput {
380380
}
381381

382382
if len(ti.AltLeaves) > 0 {
383-
vi.AltLeaves = make([]asset.AltLeafAsset, len(ti.AltLeaves))
383+
vi.AltLeaves = make(
384+
[]asset.AltLeaf[asset.Asset], len(ti.AltLeaves),
385+
)
384386
for idx, leaf := range ti.AltLeaves {
385387
vi.AltLeaves[idx] = leaf.ToAsset(t)
386388
}
@@ -634,7 +636,7 @@ func NewTestFromVOutput(t testing.TB, v *VOutput,
634636

635637
vo.AltLeaves = make([]*asset.TestAsset, 0, len(vo.AltLeaves))
636638
for idx := range v.AltLeaves {
637-
leaf := asset.InnerAltLeaf(v.AltLeaves[idx])
639+
leaf := v.AltLeaves[idx].(*asset.Asset)
638640
vo.AltLeaves = append(
639641
vo.AltLeaves, asset.NewTestFromAsset(t, leaf),
640642
)
@@ -759,7 +761,9 @@ func (to *TestVOutput) ToVOutput(t testing.TB) *VOutput {
759761
}
760762

761763
if len(to.AltLeaves) > 0 {
762-
v.AltLeaves = make([]asset.AltLeafAsset, len(to.AltLeaves))
764+
v.AltLeaves = make(
765+
[]asset.AltLeaf[asset.Asset], len(to.AltLeaves),
766+
)
763767
for idx, leaf := range to.AltLeaves {
764768
v.AltLeaves[idx] = leaf.ToAsset(t)
765769
}

0 commit comments

Comments
 (0)