Skip to content

Commit 930f84d

Browse files
committed
wire: don't init unless if the count is bigger than 0
For reflect.DeepEqual, we nil and len() == 0 is not equal. To account for this, keep slices nil when we can.
1 parent 88ceb1b commit 930f84d

File tree

2 files changed

+30
-14
lines changed

2 files changed

+30
-14
lines changed

wire/batchproof.go

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -103,30 +103,41 @@ func BatchProofDeserialize(r io.Reader) (*utreexo.Proof, error) {
103103
return nil, err
104104
}
105105

106-
targets := make([]uint64, targetCount)
107-
for i := range targets {
108-
target, err := ReadVarInt(r, 0)
109-
if err != nil {
110-
return nil, err
111-
}
106+
proof := new(utreexo.Proof)
112107

113-
targets[i] = target
108+
if targetCount > 0 {
109+
targets := make([]uint64, 0, targetCount)
110+
for i := 0; i < int(targetCount); i++ {
111+
target, err := ReadVarInt(r, 0)
112+
if err != nil {
113+
return nil, err
114+
}
115+
116+
targets = append(targets, target)
117+
}
118+
proof.Targets = targets
114119
}
115120

116121
proofCount, err := ReadVarInt(r, 0)
117122
if err != nil {
118123
return nil, err
119124
}
125+
if proofCount == 0 {
126+
return proof, nil
127+
}
120128

121-
proofs := make([]utreexo.Hash, proofCount)
122-
for i := range proofs {
123-
_, err = io.ReadFull(r, proofs[i][:])
129+
proofs := make([]utreexo.Hash, 0, proofCount)
130+
for i := 0; i < int(proofCount); i++ {
131+
var hash utreexo.Hash
132+
_, err = io.ReadFull(r, hash[:])
124133
if err != nil {
125134
return nil, err
126135
}
136+
proofs = append(proofs, hash)
127137
}
138+
proof.Proof = proofs
128139

129-
return &utreexo.Proof{Targets: targets, Proof: proofs}, nil
140+
return proof, nil
130141
}
131142

132143
// BatchProofToString converts a batchproof into a human-readable string. Note

wire/udata.go

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -111,16 +111,21 @@ func (ud *UData) Deserialize(r io.Reader) error {
111111
if err != nil {
112112
return err
113113
}
114+
if udCount == 0 {
115+
return nil
116+
}
114117

115-
ud.LeafDatas = make([]LeafData, udCount)
116-
for i := range ud.LeafDatas {
117-
err = ud.LeafDatas[i].Deserialize(r)
118+
ud.LeafDatas = make([]LeafData, 0, udCount)
119+
for i := 0; i < int(udCount); i++ {
120+
ld := LeafData{}
121+
err = ld.Deserialize(r)
118122
if err != nil {
119123
str := fmt.Sprintf("targetCount:%d, Stxos[%d], err:%s\n",
120124
len(ud.AccProof.Targets), i, err.Error())
121125
returnErr := messageError("Deserialize stxos", str)
122126
return returnErr
123127
}
128+
ud.LeafDatas = append(ud.LeafDatas, ld)
124129
}
125130

126131
return nil

0 commit comments

Comments
 (0)