Skip to content

Commit 0c224cc

Browse files
committed
tapsend: copy sort to before creating virtual packets
This fixes a bug where the order of the inputs in a virtual packet wasn't deterministic, because we only sorted the proofs _after_ already assigning them as inputs to the packet.
1 parent 29229f6 commit 0c224cc

File tree

2 files changed

+28
-20
lines changed

2 files changed

+28
-20
lines changed

tapsend/allocation.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -504,6 +504,14 @@ func DistributeCoins(inputs []*proof.Proof, allocations []*Allocation,
504504
},
505505
)
506506

507+
// Before creating the virtual packet, sort the proofs by
508+
// amount (in reverse order) then by script key. This ensures
509+
// deterministic ordering before assigning them to the virtual
510+
// packet inputs.
511+
slices.SortFunc(proofsByID, func(i, j *proof.Proof) int {
512+
return AssetSortForInputs(i.Asset, j.Asset)
513+
})
514+
507515
pkt, err := tappsbt.FromProofs(
508516
proofsByID, chainParams, vPktVersion,
509517
)

tapsend/allocation_test.go

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -410,8 +410,8 @@ func TestDistributeCoins(t *testing.T) {
410410
vPktVersion: tappsbt.V1,
411411
expectedInputs: map[asset.ID][]asset.ScriptKey{
412412
assetID2.ID(): {
413-
assetID2Tranche1.ScriptKey,
414413
assetID2Tranche2.ScriptKey,
414+
assetID2Tranche1.ScriptKey,
415415
},
416416
},
417417
expectedOutputs: map[asset.ID][]*tappsbt.VOutput{
@@ -452,8 +452,8 @@ func TestDistributeCoins(t *testing.T) {
452452
},
453453
expectedInputs: map[asset.ID][]asset.ScriptKey{
454454
assetID2.ID(): {
455-
assetID2Tranche1.ScriptKey,
456455
assetID2Tranche2.ScriptKey,
456+
assetID2Tranche1.ScriptKey,
457457
},
458458
},
459459
expectedOutputs: map[asset.ID][]*tappsbt.VOutput{
@@ -613,19 +613,19 @@ func TestDistributeCoins(t *testing.T) {
613613
},
614614
expectedInputs: map[asset.ID][]asset.ScriptKey{
615615
assetID1.ID(): {
616-
assetID1Tranche1.ScriptKey,
617-
assetID1Tranche2.ScriptKey,
618616
assetID1Tranche3.ScriptKey,
617+
assetID1Tranche2.ScriptKey,
618+
assetID1Tranche1.ScriptKey,
619619
},
620620
assetID2.ID(): {
621-
assetID2Tranche1.ScriptKey,
622-
assetID2Tranche2.ScriptKey,
623621
assetID2Tranche3.ScriptKey,
622+
assetID2Tranche2.ScriptKey,
623+
assetID2Tranche1.ScriptKey,
624624
},
625625
assetID3.ID(): {
626-
assetID3Tranche1.ScriptKey,
627-
assetID3Tranche2.ScriptKey,
628626
assetID3Tranche3.ScriptKey,
627+
assetID3Tranche2.ScriptKey,
628+
assetID3Tranche1.ScriptKey,
629629
},
630630
},
631631
expectedOutputs: map[asset.ID][]*tappsbt.VOutput{
@@ -689,19 +689,19 @@ func TestDistributeCoins(t *testing.T) {
689689
},
690690
expectedInputs: map[asset.ID][]asset.ScriptKey{
691691
assetID1.ID(): {
692-
assetID1Tranche1.ScriptKey,
693-
assetID1Tranche2.ScriptKey,
694692
assetID1Tranche3.ScriptKey,
693+
assetID1Tranche2.ScriptKey,
694+
assetID1Tranche1.ScriptKey,
695695
},
696696
assetID2.ID(): {
697-
assetID2Tranche1.ScriptKey,
698-
assetID2Tranche2.ScriptKey,
699697
assetID2Tranche3.ScriptKey,
698+
assetID2Tranche2.ScriptKey,
699+
assetID2Tranche1.ScriptKey,
700700
},
701701
assetID3.ID(): {
702-
assetID3Tranche1.ScriptKey,
703-
assetID3Tranche2.ScriptKey,
704702
assetID3Tranche3.ScriptKey,
703+
assetID3Tranche2.ScriptKey,
704+
assetID3Tranche1.ScriptKey,
705705
},
706706
},
707707
expectedOutputs: map[asset.ID][]*tappsbt.VOutput{
@@ -770,19 +770,19 @@ func TestDistributeCoins(t *testing.T) {
770770
},
771771
expectedInputs: map[asset.ID][]asset.ScriptKey{
772772
assetID1.ID(): {
773-
assetID1Tranche1.ScriptKey,
774-
assetID1Tranche2.ScriptKey,
775773
assetID1Tranche3.ScriptKey,
774+
assetID1Tranche2.ScriptKey,
775+
assetID1Tranche1.ScriptKey,
776776
},
777777
assetID2.ID(): {
778-
assetID2Tranche1.ScriptKey,
779-
assetID2Tranche2.ScriptKey,
780778
assetID2Tranche3.ScriptKey,
779+
assetID2Tranche2.ScriptKey,
780+
assetID2Tranche1.ScriptKey,
781781
},
782782
assetID3.ID(): {
783-
assetID3Tranche1.ScriptKey,
784-
assetID3Tranche2.ScriptKey,
785783
assetID3Tranche3.ScriptKey,
784+
assetID3Tranche2.ScriptKey,
785+
assetID3Tranche1.ScriptKey,
786786
},
787787
},
788788
expectedOutputs: map[asset.ID][]*tappsbt.VOutput{

0 commit comments

Comments
 (0)