diff --git a/wire/batchproof.go b/wire/batchproof.go index 9b640625..a7a6aee8 100644 --- a/wire/batchproof.go +++ b/wire/batchproof.go @@ -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 diff --git a/wire/udata.go b/wire/udata.go index 173cc354..d5a59c9a 100644 --- a/wire/udata.go +++ b/wire/udata.go @@ -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