Skip to content

Commit

Permalink
wire: don't init unless if the count is bigger than 0
Browse files Browse the repository at this point in the history
For reflect.DeepEqual, we nil and len() == 0 is not equal. To account
for this, keep slices nil when we can.
  • Loading branch information
kcalvinalvin committed Oct 23, 2024
1 parent b9813a5 commit 2a20551
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 14 deletions.
33 changes: 22 additions & 11 deletions wire/batchproof.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,30 +103,41 @@ func BatchProofDeserialize(r io.Reader) (*utreexo.Proof, error) {
return nil, err
}

targets := make([]uint64, targetCount)
for i := range targets {
target, err := ReadVarInt(r, 0)
if err != nil {
return nil, err
}
proof := new(utreexo.Proof)

targets[i] = target
if targetCount > 0 {
targets := make([]uint64, 0, targetCount)
for i := 0; i < int(targetCount); i++ {
target, err := ReadVarInt(r, 0)
if err != nil {
return nil, err
}

targets = append(targets, target)
}
proof.Targets = targets
}

proofCount, err := ReadVarInt(r, 0)
if err != nil {
return nil, err
}
if proofCount == 0 {
return proof, nil
}

proofs := make([]utreexo.Hash, proofCount)
for i := range proofs {
_, err = io.ReadFull(r, proofs[i][:])
proofs := make([]utreexo.Hash, 0, proofCount)
for i := 0; i < int(proofCount); i++ {
var hash utreexo.Hash
_, err = io.ReadFull(r, hash[:])
if err != nil {
return nil, err
}
proofs = append(proofs, hash)
}
proof.Proof = proofs

return &utreexo.Proof{Targets: targets, Proof: proofs}, nil
return proof, nil
}

// BatchProofToString converts a batchproof into a human-readable string. Note
Expand Down
11 changes: 8 additions & 3 deletions wire/udata.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,16 +111,21 @@ func (ud *UData) Deserialize(r io.Reader) error {
if err != nil {
return err
}
if udCount == 0 {
return nil
}

ud.LeafDatas = make([]LeafData, udCount)
for i := range ud.LeafDatas {
err = ud.LeafDatas[i].Deserialize(r)
ud.LeafDatas = make([]LeafData, 0, udCount)
for i := 0; i < int(udCount); i++ {
ld := LeafData{}
err = ld.Deserialize(r)
if err != nil {
str := fmt.Sprintf("targetCount:%d, Stxos[%d], err:%s\n",
len(ud.AccProof.Targets), i, err.Error())
returnErr := messageError("Deserialize stxos", str)
return returnErr
}
ud.LeafDatas = append(ud.LeafDatas, ld)
}

return nil
Expand Down

0 comments on commit 2a20551

Please sign in to comment.