Skip to content

Commit ecd6a5b

Browse files
committed
multi: use Proof.Bytes method and remove redundant functions
This commit utilizes the new `Proof.Bytes` method, eliminating the need for unnecessary temporary buffer variables. Standalone functions `encodeProof` and `proof.Encode` have also been replaced and removed to reduce redundancy.
1 parent 725eace commit ecd6a5b

File tree

17 files changed

+59
-87
lines changed

17 files changed

+59
-87
lines changed

proof/courier.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1316,14 +1316,14 @@ func (c *UniverseRpcCourier) DeliverProof(ctx context.Context,
13161316
return err
13171317
}
13181318

1319-
var proofBuf bytes.Buffer
1320-
if err := transitionProof.Encode(&proofBuf); err != nil {
1319+
transitionProofBytes, err := transitionProof.Bytes()
1320+
if err != nil {
13211321
return fmt.Errorf("error encoding proof file: %w", err)
13221322
}
13231323

13241324
assetLeaf := unirpc.AssetLeaf{
13251325
Asset: rpcAsset,
1326-
Proof: proofBuf.Bytes(),
1326+
Proof: transitionProofBytes,
13271327
}
13281328

13291329
// Construct universe key.

proof/file.go

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ func NewFile(v Version, proofs ...Proof) (*File, error) {
106106
for idx := range proofs {
107107
proof := proofs[idx]
108108

109-
proofBytes, err := encodeProof(&proof)
109+
proofBytes, err := proof.Bytes()
110110
if err != nil {
111111
return nil, err
112112
}
@@ -380,7 +380,7 @@ func (f *File) AppendProof(proof Proof) error {
380380
prevHash = f.proofs[len(f.proofs)-1].hash
381381
}
382382

383-
proofBytes, err := encodeProof(&proof)
383+
proofBytes, err := proof.Bytes()
384384
if err != nil {
385385
return err
386386
}
@@ -437,7 +437,7 @@ func (f *File) ReplaceProofAt(index uint32, proof Proof) error {
437437
prevHash = f.proofs[index-1].hash
438438
}
439439

440-
proofBytes, err := encodeProof(&proof)
440+
proofBytes, err := proof.Bytes()
441441
if err != nil {
442442
return err
443443
}
@@ -457,16 +457,6 @@ func (f *File) ReplaceProofAt(index uint32, proof Proof) error {
457457
return nil
458458
}
459459

460-
// encodeProof encodes the given proof and returns its raw bytes.
461-
func encodeProof(proof *Proof) ([]byte, error) {
462-
var buf bytes.Buffer
463-
if err := proof.Encode(&buf); err != nil {
464-
return nil, err
465-
}
466-
467-
return buf.Bytes(), nil
468-
}
469-
470460
// hashProof hashes a proof's content together with the previous hash and
471461
// re-uses the given buffer:
472462
//

proof/proof.go

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -476,12 +476,11 @@ func (p *Proof) Bytes() ([]byte, error) {
476476
// NOTE: This is part of the tlv.RecordProducer interface.
477477
func (p *Proof) Record() tlv.Record {
478478
sizeFunc := func() uint64 {
479-
var buf bytes.Buffer
480-
err := p.Encode(&buf)
479+
proofBytes, err := p.Bytes()
481480
if err != nil {
482481
panic(err)
483482
}
484-
return uint64(len(buf.Bytes()))
483+
return uint64(len(proofBytes))
485484
}
486485

487486
// Note that we set the type here as zero, as when used with a
@@ -566,15 +565,6 @@ func SparseDecode(r io.Reader, records ...tlv.Record) error {
566565
return proofStream.Decode(r)
567566
}
568567

569-
// Encode encodes a proof into a byte slice.
570-
func Encode(p *Proof) ([]byte, error) {
571-
var b bytes.Buffer
572-
if err := p.Encode(&b); err != nil {
573-
return nil, err
574-
}
575-
return b.Bytes(), nil
576-
}
577-
578568
// Decode decodes a proof from a byte slice.
579569
func Decode(blob []byte) (*Proof, error) {
580570
var p Proof

proof/proof_test.go

Lines changed: 12 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -297,9 +297,8 @@ func TestProofEncodingGroupKeyRevealV1(t *testing.T) {
297297
require.NoError(t, err)
298298
proof.AdditionalInputs = []File{*file, *file}
299299

300-
var proofBuf bytes.Buffer
301-
require.NoError(t, proof.Encode(&proofBuf))
302-
proofBytes := proofBuf.Bytes()
300+
proofBytes, err := proof.Bytes()
301+
require.NoError(t, err)
303302

304303
var decodedProof Proof
305304
require.NoError(
@@ -324,9 +323,8 @@ func TestProofEncoding(t *testing.T) {
324323
require.NoError(t, err)
325324
proof.AdditionalInputs = []File{*file, *file}
326325

327-
var proofBuf bytes.Buffer
328-
require.NoError(t, proof.Encode(&proofBuf))
329-
proofBytes := proofBuf.Bytes()
326+
proofBytes, err := proof.Bytes()
327+
require.NoError(t, err)
330328

331329
var decodedProof Proof
332330
require.NoError(t, decodedProof.Decode(bytes.NewReader(proofBytes)))
@@ -363,7 +361,7 @@ func TestProofEncoding(t *testing.T) {
363361
require.NoError(t, err)
364362
proof.AdditionalInputs = []File{*file, *file}
365363

366-
proofBuf.Reset()
364+
var proofBuf bytes.Buffer
367365
require.NoError(t, proof.Encode(&proofBuf))
368366
var decodedProof2 Proof
369367
require.NoError(t, decodedProof2.Decode(&proofBuf))
@@ -821,8 +819,7 @@ func TestGenesisProofVerification(t *testing.T) {
821819
)
822820
require.ErrorIs(t, err, tc.expectedErr)
823821

824-
var buf bytes.Buffer
825-
err = genesisProof.Encode(&buf)
822+
genesisProofBytes, err := genesisProof.Bytes()
826823
require.NoError(tt, err)
827824

828825
if tc.expectedErr == nil {
@@ -833,7 +830,7 @@ func TestGenesisProofVerification(t *testing.T) {
833830
t, &genesisProof,
834831
),
835832
Expected: hex.EncodeToString(
836-
buf.Bytes(),
833+
genesisProofBytes,
837834
),
838835
Comment: tc.name,
839836
},
@@ -1233,12 +1230,11 @@ func runBIPTestVector(t *testing.T, testVectors *TestVectors) {
12331230

12341231
p := validCase.Proof.ToProof(tt)
12351232

1236-
var buf bytes.Buffer
1237-
err := p.Encode(&buf)
1233+
proofBytes, err := p.Bytes()
12381234
require.NoError(tt, err)
12391235

12401236
areEqual := validCase.Expected == hex.EncodeToString(
1241-
buf.Bytes(),
1237+
proofBytes,
12421238
)
12431239

12441240
// Make sure the proof in the test vectors doesn't use
@@ -1302,7 +1298,7 @@ func runBIPTestVector(t *testing.T, testVectors *TestVectors) {
13021298
// Make sure we still fail the test.
13031299
require.Equal(
13041300
tt, validCase.Expected,
1305-
hex.EncodeToString(buf.Bytes()),
1301+
hex.EncodeToString(proofBytes),
13061302
)
13071303
}
13081304

@@ -1453,11 +1449,10 @@ func TestProofUnknownOddType(t *testing.T) {
14531449
// The proof should've changed, to make sure the unknown
14541450
// value was taken into account when creating the
14551451
// serialized proof.
1456-
var newBuf bytes.Buffer
1457-
err := parsedProof.Encode(&newBuf)
1452+
parsedProofBytes, err := parsedProof.Bytes()
14581453
require.NoError(t, err)
14591454

1460-
require.NotEqual(t, knownProofBytes, newBuf.Bytes())
1455+
require.NotEqual(t, knownProofBytes, parsedProofBytes)
14611456

14621457
parsedProof.UnknownOddTypes = nil
14631458
require.Equal(t, &knownProof, parsedProof)

rpcserver.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6168,13 +6168,13 @@ func (r *rpcServer) ProveAssetOwnership(ctx context.Context,
61686168

61696169
lastProof.ChallengeWitness = challengeWitness
61706170

6171-
var buf bytes.Buffer
6172-
if err := lastProof.Encode(&buf); err != nil {
6171+
lastProofBytes, err := lastProof.Bytes()
6172+
if err != nil {
61736173
return nil, fmt.Errorf("error encoding proof file: %w", err)
61746174
}
61756175

61766176
return &wrpc.ProveAssetOwnershipResponse{
6177-
ProofWithWitness: buf.Bytes(),
6177+
ProofWithWitness: lastProofBytes,
61786178
}, nil
61796179
}
61806180

tapchannel/auf_leaf_signer_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -271,7 +271,7 @@ func randProof(t *testing.T) proof.Proof {
271271

272272
// Proofs don't Encode everything, so we need to do a quick Encode/
273273
// Decode cycle to make sure we can compare it afterward.
274-
proofBytes, err := proof.Encode(&originalRandProof)
274+
proofBytes, err := originalRandProof.Bytes()
275275
require.NoError(t, err)
276276
p, err := proof.Decode(proofBytes)
277277
require.NoError(t, err)

tapchannel/aux_funding_controller.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -917,7 +917,7 @@ func (f *FundingController) sendInputOwnershipProofs(peerPub btcec.PublicKey,
917917
// With all our proofs assembled, we'll now send each of them to the
918918
// remote peer in series.
919919
for i := range fundingState.inputProofs {
920-
proofBytes, _ := proof.Encode(fundingState.inputProofs[i])
920+
proofBytes, _ := fundingState.inputProofs[i].Bytes()
921921
log.Tracef("Sending input ownership proof to remote party: %x",
922922
proofBytes)
923923

tapchannelmsg/records_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ func TestOpenChannel(t *testing.T) {
6161
// Proofs don't Encode everything, so we need to do a quick Encode/
6262
// Decode cycle to make sure we can compare it afterward.
6363
originalRandProof := randProof(t)
64-
proofBytes, err := proof.Encode(&originalRandProof)
64+
proofBytes, err := originalRandProof.Bytes()
6565
require.NoError(t, err)
6666
randProof, err := proof.Decode(proofBytes)
6767
require.NoError(t, err)
@@ -188,7 +188,7 @@ func TestCommitment(t *testing.T) {
188188
// Proofs don't Encode everything, so we need to do a quick Encode/
189189
// Decode cycle to make sure we can compare it afterward.
190190
originalRandProof := randProof(t)
191-
proofBytes, err := proof.Encode(&originalRandProof)
191+
proofBytes, err := originalRandProof.Bytes()
192192
require.NoError(t, err)
193193
randProof, err := proof.Decode(proofBytes)
194194
require.NoError(t, err)

tapchannelmsg/wire_msgs_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ func TestAssetFundingMsg(t *testing.T) {
4141

4242
// Proofs don't Encode everything, so we need to do a quick Encode/
4343
// Decode cycle to make sure we can compare it afterward.
44-
proofBytes, err := proof.Encode(&originalRandProof)
44+
proofBytes, err := originalRandProof.Bytes()
4545
require.NoError(t, err)
4646
randProof, err := proof.Decode(proofBytes)
4747
require.NoError(t, err)

tapdb/assets_store.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2910,8 +2910,7 @@ func logPendingPassiveAssets(ctx context.Context,
29102910
}
29112911

29122912
// Encode new proof.
2913-
var newProofBuf bytes.Buffer
2914-
err = passiveOut.ProofSuffix.Encode(&newProofBuf)
2913+
proofSuffixBytes, err := passiveOut.ProofSuffix.Bytes()
29152914
if err != nil {
29162915
return fmt.Errorf("unable to encode new passive "+
29172916
"asset proof: %w", err)
@@ -2935,7 +2934,7 @@ func logPendingPassiveAssets(ctx context.Context,
29352934
TransferID: transferID,
29362935
NewAnchorUtxo: newUtxoID,
29372936
NewWitnessStack: newWitnessBuf.Bytes(),
2938-
NewProof: newProofBuf.Bytes(),
2937+
NewProof: proofSuffixBytes,
29392938
PrevOutpoint: prevOutpointBytes,
29402939
ScriptKey: scriptKeyBytes,
29412940
AssetGenesisID: passiveIn.PrevID.ID[:],

0 commit comments

Comments
 (0)