Skip to content

Commit 3c58c07

Browse files
jharveybguggero
authored andcommitted
itest: add AltLeaves in PSBT ping-pong tests
1 parent 36e0d12 commit 3c58c07

File tree

2 files changed

+86
-6
lines changed

2 files changed

+86
-6
lines changed

itest/assertions.go

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -521,6 +521,59 @@ func AssertAssetProofs(t *testing.T, tapClient taprpc.TaprootAssetsClient,
521521
return exportResp.RawProofFile
522522
}
523523

524+
// AssertProofAltLeaves makes sure that, for a given asset, the latest proof
525+
// commits to an expected set of altLeaves.
526+
func AssertProofAltLeaves(t *testing.T, tapClient taprpc.TaprootAssetsClient,
527+
a *taprpc.Asset, leafMap map[string][]*asset.Asset) {
528+
529+
t.Helper()
530+
ctxb := context.Background()
531+
ctxt, cancel := context.WithTimeout(ctxb, defaultWaitTimeout)
532+
defer cancel()
533+
534+
// Fetch the latest proof for the given asset.
535+
scriptKey := a.ScriptKey
536+
proofReq := taprpc.ExportProofRequest{
537+
AssetId: a.AssetGenesis.AssetId,
538+
ScriptKey: scriptKey,
539+
}
540+
exportResp, err := tapClient.ExportProof(ctxt, &proofReq)
541+
require.NoError(t, err)
542+
543+
decodeReq := taprpc.DecodeProofRequest{
544+
RawProof: exportResp.RawProofFile,
545+
}
546+
decodeResp, err := tapClient.DecodeProof(ctxt, &decodeReq)
547+
require.NoError(t, err)
548+
549+
// Check if we expect the asset to be anchored alongside alt leaves or
550+
// not. E.x. a passive asset created inside the freighter will not be
551+
// anchored with any alt leaves.
552+
altLeavesBytes := decodeResp.DecodedProof.AltLeaves
553+
expectedAltLeaves, ok := leafMap[string(scriptKey)]
554+
emptyAltLeaves := len(altLeavesBytes) == 0
555+
556+
require.Equal(t, ok, !emptyAltLeaves)
557+
if emptyAltLeaves {
558+
return
559+
}
560+
561+
// If we have altLeaves, decode them and check that they match the
562+
// expected leaves.
563+
var (
564+
r = bytes.NewReader(altLeavesBytes)
565+
l = uint64(len(altLeavesBytes))
566+
scratch [8]byte
567+
val []asset.AltLeaf[asset.Asset]
568+
)
569+
570+
require.NoError(t, asset.AltLeavesDecoder(r, &val, &scratch, l))
571+
asset.CompareAltLeaves(t, asset.ToAltLeaves(expectedAltLeaves), val)
572+
t.Logf("matching alt leaves for: %v, %x, %x: %d leaves",
573+
a.ChainAnchor.AnchorOutpoint, a.AssetGenesis.AssetId,
574+
a.ScriptKey, len(val))
575+
}
576+
524577
// AssertMintingProofs make sure the asset minting proofs contain all the
525578
// correct reveal information.
526579
func AssertMintingProofs(t *testing.T, tapd *tapdHarness,

itest/psbt_test.go

Lines changed: 33 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -608,12 +608,19 @@ func runPsbtInteractiveFullValueSendTest(ctxt context.Context, t *harnessTest,
608608
receiverScriptKey, receiverAnchorIntKeyDesc := DeriveKeys(
609609
t.t, receiver,
610610
)
611+
receiverScriptKeyBytes := receiverScriptKey.PubKey.
612+
SerializeCompressed()
611613

612614
vPkt := tappsbt.ForInteractiveSend(
613615
id, fullAmt, receiverScriptKey, 0, 0, 0,
614-
receiverAnchorIntKeyDesc, asset.V0,
615-
chainParams,
616+
receiverAnchorIntKeyDesc, asset.V0, chainParams,
616617
)
618+
altLeaves := asset.RandAltLeaves(t.t, true)
619+
leafMap := map[string][]*asset.Asset{
620+
string(receiverScriptKeyBytes): altLeaves,
621+
}
622+
err := vPkt.Outputs[0].SetAltLeaves(altLeaves)
623+
require.NoError(t.t, err)
617624

618625
// Next, we'll attempt to complete a transfer with PSBTs from
619626
// our sender node to our receiver, using the full amount.
@@ -644,8 +651,8 @@ func runPsbtInteractiveFullValueSendTest(ctxt context.Context, t *harnessTest,
644651
// This is an interactive transfer, so we do need to manually
645652
// send the proof from the sender to the receiver.
646653
_ = sendProof(
647-
t, sender, receiver, sendResp,
648-
receiverScriptKey.PubKey.SerializeCompressed(), genInfo,
654+
t, sender, receiver, sendResp, receiverScriptKeyBytes,
655+
genInfo,
649656
)
650657

651658
senderAssets, err := sender.ListAssets(
@@ -674,6 +681,12 @@ func runPsbtInteractiveFullValueSendTest(ctxt context.Context, t *harnessTest,
674681
t.t, receivedAssets, genInfo.Name, genInfo.MetaHash,
675682
AssetAmountCheck(fullAmt),
676683
)
684+
685+
// Check that the altLeaves from the receiver's vPacket were set
686+
// by the sender correctly.
687+
for _, asset := range receiverAssets.Assets {
688+
AssertProofAltLeaves(t.t, receiver, asset, leafMap)
689+
}
677690
}
678691

679692
// Finally, make sure we can still send out the passive asset.
@@ -822,11 +835,19 @@ func runPsbtInteractiveSplitSendTest(ctxt context.Context, t *harnessTest,
822835
receiverScriptKey, receiverAnchorIntKeyDesc := DeriveKeys(
823836
t.t, receiver,
824837
)
838+
receiverScriptKeyBytes := receiverScriptKey.PubKey.
839+
SerializeCompressed()
825840

826841
vPkt := tappsbt.ForInteractiveSend(
827842
id, sendAmt, receiverScriptKey, 0, 0, 0,
828843
receiverAnchorIntKeyDesc, asset.V0, chainParams,
829844
)
845+
altLeaves := asset.RandAltLeaves(t.t, true)
846+
leafMap := map[string][]*asset.Asset{
847+
string(receiverScriptKeyBytes): altLeaves,
848+
}
849+
err := vPkt.Outputs[0].SetAltLeaves(altLeaves)
850+
require.NoError(t.t, err)
830851

831852
// Next, we'll attempt to complete a transfer with PSBTs from
832853
// our sender node to our receiver, using the partial amount.
@@ -857,8 +878,8 @@ func runPsbtInteractiveSplitSendTest(ctxt context.Context, t *harnessTest,
857878
// This is an interactive transfer, so we do need to manually
858879
// send the proof from the sender to the receiver.
859880
_ = sendProof(
860-
t, sender, receiver, sendResp,
861-
receiverScriptKey.PubKey.SerializeCompressed(), genInfo,
881+
t, sender, receiver, sendResp, receiverScriptKeyBytes,
882+
genInfo,
862883
)
863884

864885
senderAssets, err := sender.ListAssets(
@@ -890,6 +911,12 @@ func runPsbtInteractiveSplitSendTest(ctxt context.Context, t *harnessTest,
890911
)
891912
require.NoError(t.t, err)
892913
require.Len(t.t, receiverAssets.Assets, numReceiverAssets)
914+
915+
// Check that the altLeaves from the receiver's vPacket were set
916+
// by the sender correctly.
917+
for _, asset := range receiverAssets.Assets {
918+
AssertProofAltLeaves(t.t, receiver, asset, leafMap)
919+
}
893920
}
894921

895922
// Finally, make sure we can still send out the passive asset.

0 commit comments

Comments
 (0)