Skip to content

Commit a1e2e2c

Browse files
Copilottzdybal
andcommitted
fix: change proof functions to return values instead of pointers
- Change ProofsFromByteSlices to return []Proof instead of []*Proof - Change ProofFromProto to return Proof instead of *Proof - Change ProofsFromLeafHashes to return []Proof instead of []*Proof - Update all call sites to remove pointer dereferences - Convert RowProof.Proofs field to []merkle.Proof for consistency - Update helper functions to provide pointers where needed for external APIs Co-authored-by: tzdybal <[email protected]>
1 parent 2d1e1f8 commit a1e2e2c

File tree

10 files changed

+40
-36
lines changed

10 files changed

+40
-36
lines changed

consensus/propagation/types/types.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -209,14 +209,18 @@ func (c *CompactBlock) Proofs() ([]*merkle.Proof, error) {
209209
c.proofsCache = make([]*merkle.Proof, 0, len(c.PartsHashes))
210210

211211
root, proofs := merkle.ProofsFromLeafHashes(c.PartsHashes[:total])
212-
c.proofsCache = append(c.proofsCache, proofs...)
212+
for i := range proofs {
213+
c.proofsCache = append(c.proofsCache, &proofs[i])
214+
}
213215

214216
if !bytes.Equal(root, c.Proposal.BlockID.PartSetHeader.Hash) {
215217
return c.proofsCache, fmt.Errorf("incorrect PartsHash: original root")
216218
}
217219

218220
parityRoot, eproofs := merkle.ProofsFromLeafHashes(c.PartsHashes[total:])
219-
c.proofsCache = append(c.proofsCache, eproofs...)
221+
for i := range eproofs {
222+
c.proofsCache = append(c.proofsCache, &eproofs[i])
223+
}
220224

221225
if !bytes.Equal(c.BpHash, parityRoot) {
222226
return c.proofsCache, fmt.Errorf("incorrect PartsHash: parity root")
@@ -483,7 +487,7 @@ func RecoveryPartFromProto(r *protoprop.RecoveryPart) (*RecoveryPart, error) {
483487
Round: r.Round,
484488
Index: r.Index,
485489
Data: r.Data,
486-
Proof: proof,
490+
Proof: &proof,
487491
}
488492
return rp, rp.ValidateBasic()
489493
}

crypto/merkle/proof.go

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,12 @@ type Proof struct {
3232

3333
// ProofsFromByteSlices computes inclusion proof for given items.
3434
// proofs[0] is the proof for items[0].
35-
func ProofsFromByteSlices(items [][]byte) (rootHash []byte, proofs []*Proof) {
35+
func ProofsFromByteSlices(items [][]byte) (rootHash []byte, proofs []Proof) {
3636
trails, rootSPN := trailsFromByteSlices(items)
3737
rootHash = rootSPN.Hash
38-
proofs = make([]*Proof, len(items))
38+
proofs = make([]Proof, len(items))
3939
for i, trail := range trails {
40-
proofs[i] = &Proof{
40+
proofs[i] = Proof{
4141
Total: int64(len(items)),
4242
Index: int64(i),
4343
LeafHash: trail.Hash,
@@ -145,20 +145,20 @@ func (sp *Proof) ToProto() *cmtcrypto.Proof {
145145
return pb
146146
}
147147

148-
func ProofFromProto(pb *cmtcrypto.Proof, optional bool) (*Proof, error) {
148+
func ProofFromProto(pb *cmtcrypto.Proof, optional bool) (Proof, error) {
149149
if pb == nil || (len(pb.LeafHash) == 0 && len(pb.Aunts) == 0) {
150150
if optional {
151-
return nil, nil
151+
return Proof{}, nil
152152
}
153-
return nil, errors.New("nil proof")
153+
return Proof{}, errors.New("nil proof")
154154
}
155155

156-
sp := new(Proof)
157-
158-
sp.Total = pb.Total
159-
sp.Index = pb.Index
160-
sp.LeafHash = pb.LeafHash
161-
sp.Aunts = pb.Aunts
156+
sp := Proof{
157+
Total: pb.Total,
158+
Index: pb.Index,
159+
LeafHash: pb.LeafHash,
160+
Aunts: pb.Aunts,
161+
}
162162

163163
return sp, sp.ValidateBasic()
164164
}
@@ -275,12 +275,12 @@ func trailsFromLeafHashes(leafHashes [][]byte) (trails []*ProofNode, root *Proof
275275
}
276276
}
277277

278-
func ProofsFromLeafHashes(leafHashes [][]byte) (rootHash []byte, proofs []*Proof) {
278+
func ProofsFromLeafHashes(leafHashes [][]byte) (rootHash []byte, proofs []Proof) {
279279
trails, rootNode := trailsFromLeafHashes(leafHashes)
280280
rootHash = rootNode.Hash
281-
proofs = make([]*Proof, len(leafHashes))
281+
proofs = make([]Proof, len(leafHashes))
282282
for i, trail := range trails {
283-
proofs[i] = &Proof{
283+
proofs[i] = Proof{
284284
Total: int64(len(leafHashes)),
285285
Index: int64(i),
286286
LeafHash: trail.Hash,

crypto/merkle/proof_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ func TestProofValidateBasic(t *testing.T) {
164164
[]byte("watermelon"),
165165
[]byte("kiwi"),
166166
})
167-
tc.malleateProof(proofs[0])
167+
tc.malleateProof(&proofs[0])
168168
err := proofs[0].ValidateBasic()
169169
if tc.errStr != "" {
170170
assert.Contains(t, err.Error(), tc.errStr)
@@ -186,15 +186,15 @@ func TestVoteProtobuf(t *testing.T) {
186186
}{
187187
{"empty proof", &Proof{}, false},
188188
{"failure nil", nil, false},
189-
{"success", proofs[0], true},
189+
{"success", &proofs[0], true},
190190
}
191191
for _, tc := range testCases {
192192
pb := tc.v1.ToProto()
193193

194194
v, err := ProofFromProto(pb, false)
195195
if tc.expPass {
196196
require.NoError(t, err)
197-
require.Equal(t, tc.v1, v, tc.testName)
197+
require.Equal(t, *tc.v1, v, tc.testName)
198198
} else {
199199
require.Error(t, err)
200200
}

crypto/merkle/proof_value.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ func ValueOpDecoder(pop cmtcrypto.ProofOp) (ProofOperator, error) {
5151
if err != nil {
5252
return nil, err
5353
}
54-
return NewValueOp(pop.Key, sp), nil
54+
return NewValueOp(pop.Key, &sp), nil
5555
}
5656

5757
func (op ValueOp) ProofOp() cmtcrypto.ProofOp {

rpc/core/blocks.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -490,7 +490,7 @@ func (env *Environment) proveDataRootTuples(tuples []DataRootTuple, height int64
490490
}
491491
_, proofs := merkle.ProofsFromByteSlices(dataRootEncodedTuples)
492492
//nolint:gosec
493-
return proofs[height-int64(tuples[0].height)], nil
493+
return &proofs[height-int64(tuples[0].height)], nil
494494
}
495495

496496
// fetchDataRootTuples takes an end exclusive range of heights and fetches its

types/part_set.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ func PartFromProto(pb *cmtproto.Part) (*Part, error) {
131131
}
132132
part.Index = pb.Index
133133
part.Bytes = pb.Bytes
134-
part.Proof = *proof
134+
part.Proof = proof
135135

136136
return part, part.ValidateBasic()
137137
}
@@ -238,7 +238,7 @@ func NewPartSetFromData(data []byte, partSize uint32) (ops *PartSet, err error)
238238
added, err := ops.AddPart(&Part{
239239
Index: uint32(index),
240240
Bytes: chunk,
241-
Proof: *proofs[index],
241+
Proof: proofs[index],
242242
})
243243
if err != nil {
244244
return nil, err
@@ -308,7 +308,7 @@ func Encode(ops *PartSet, partSize uint32) (*PartSet, int, error) {
308308
added, err := eps.AddPart(&Part{
309309
Index: i,
310310
Bytes: chunks[i],
311-
Proof: *eproofs[i],
311+
Proof: eproofs[i],
312312
})
313313
if err != nil {
314314
return nil, 0, err
@@ -390,7 +390,7 @@ func Decode(ops, eps *PartSet, lastPartLen int) (*PartSet, *PartSet, error) {
390390
added, err := ops.AddPart(&Part{
391391
Index: uint32(i),
392392
Bytes: d,
393-
Proof: *proofs[i],
393+
Proof: proofs[i],
394394
})
395395
if err != nil {
396396
return nil, nil, err
@@ -414,7 +414,7 @@ func Decode(ops, eps *PartSet, lastPartLen int) (*PartSet, *PartSet, error) {
414414
added, err := eps.AddPart(&Part{
415415
Index: uint32(i),
416416
Bytes: data[int(ops.Total())+i],
417-
Proof: *eproofs[i],
417+
Proof: eproofs[i],
418418
})
419419
if err != nil {
420420
return nil, nil, err

types/results.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ func (a ABCIResults) Hash() []byte {
2626
// ProveResult returns a merkle proof of one result from the set
2727
func (a ABCIResults) ProveResult(i int) merkle.Proof {
2828
_, proofs := merkle.ProofsFromByteSlices(a.toByteSlices())
29-
return *proofs[i]
29+
return proofs[i]
3030
}
3131

3232
func (a ABCIResults) toByteSlices() [][]byte {

types/row_proof.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ type RowProof struct {
1616
RowRoots []tmbytes.HexBytes `json:"row_roots"`
1717
// Proofs is a list of Merkle proofs where each proof proves that a row
1818
// exists in a Merkle tree with a given data root.
19-
Proofs []*merkle.Proof `json:"proofs"`
19+
Proofs []merkle.Proof `json:"proofs"`
2020
// StartRow the index of the start row.
2121
// Note: currently, StartRow is not validated as part of the proof verification.
2222
// If this field is used downstream, Validate(root) should be called along with
@@ -66,10 +66,10 @@ func RowProofFromProto(p *tmproto.RowProof) RowProof {
6666
return RowProof{}
6767
}
6868
rowRoots := make([]tmbytes.HexBytes, len(p.RowRoots))
69-
rowProofs := make([]*merkle.Proof, len(p.Proofs))
69+
rowProofs := make([]merkle.Proof, len(p.Proofs))
7070
for i := range p.Proofs {
7171
rowRoots[i] = p.RowRoots[i]
72-
rowProofs[i] = &merkle.Proof{
72+
rowProofs[i] = merkle.Proof{
7373
Total: p.Proofs[i].Total,
7474
Index: p.Proofs[i].Index,
7575
LeafHash: p.Proofs[i].LeafHash,

types/row_proof_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ var incorrectRoot = bytes.Repeat([]byte{0}, 32)
8484
func validRowProof() RowProof {
8585
return RowProof{
8686
RowRoots: tmbytes.FromBytes([]byte{0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x1, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x9d, 0xe6, 0x38, 0x91, 0xc1, 0x6, 0xaf, 0x81, 0x75, 0x5a, 0x36, 0xf5, 0xb2, 0x62, 0x1e, 0xfa, 0xb9, 0xb8, 0x73, 0x87, 0xef, 0xe3, 0x6b, 0x33, 0xd8, 0xbf, 0xc9, 0x87, 0x1b, 0x8d, 0xfa, 0x8a}),
87-
Proofs: []*merkle.Proof{
87+
Proofs: []merkle.Proof{
8888
{
8989
Total: 128,
9090
Index: 0,
@@ -105,7 +105,7 @@ func mismatchedRowRoots() RowProof {
105105

106106
func mismatchedProofs() RowProof {
107107
rp := validRowProof()
108-
rp.Proofs = []*merkle.Proof{}
108+
rp.Proofs = []merkle.Proof{}
109109
return rp
110110
}
111111

types/tx.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ func (txs Txs) Proof(i int) TxProof {
171171
return TxProof{
172172
RootHash: root,
173173
Data: txs[i],
174-
Proof: *proofs[i],
174+
Proof: proofs[i],
175175
}
176176
}
177177

@@ -272,7 +272,7 @@ func TxProofFromProto(pb cmtproto.TxProof) (TxProof, error) {
272272
pbtp := TxProof{
273273
RootHash: pb.RootHash,
274274
Data: pb.Data,
275-
Proof: *pbProof,
275+
Proof: pbProof,
276276
}
277277

278278
return pbtp, nil

0 commit comments

Comments
 (0)