Skip to content

Commit 47f748c

Browse files
Copilottzdybal
andcommitted
fix: address review comments - change more proof types to values
Co-authored-by: tzdybal <[email protected]>
1 parent a1e2e2c commit 47f748c

File tree

7 files changed

+37
-33
lines changed

7 files changed

+37
-33
lines changed

consensus/propagation/commitment.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -136,16 +136,16 @@ func extractHashes(blocks ...*types.PartSet) [][]byte {
136136
return partHashes
137137
}
138138

139-
func extractProofs(blocks ...*types.PartSet) []*merkle.Proof {
139+
func extractProofs(blocks ...*types.PartSet) []merkle.Proof {
140140
total := uint32(0)
141141
for _, block := range blocks {
142142
total += block.Total()
143143
}
144144

145-
proofs := make([]*merkle.Proof, 0, total) // Preallocate capacity
145+
proofs := make([]merkle.Proof, 0, total) // Preallocate capacity
146146
for _, block := range blocks {
147147
for i := uint32(0); i < block.Total(); i++ {
148-
proofs = append(proofs, &block.GetPart(int(i)).Proof)
148+
proofs = append(proofs, block.GetPart(int(i)).Proof)
149149
}
150150
}
151151
return proofs
@@ -275,7 +275,7 @@ func (blockProp *Reactor) recoverPartsFromMempool(cb *proptypes.CompactBlock) {
275275
if partSet.HasPart(int(p.Index)) {
276276
continue
277277
}
278-
p.Proof = *proofs[p.Index]
278+
p.Proof = proofs[p.Index]
279279

280280
added, err := partSet.AddOriginalPart(p)
281281
if err != nil {

consensus/propagation/have_wants.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -467,14 +467,14 @@ func (blockProp *Reactor) handleRecoveryPart(peer p2p.ID, part *proptypes.Recove
467467
// sent during catchup.
468468
proof := cb.GetProof(part.Index)
469469
if proof == nil {
470-
if part.Proof == nil {
470+
if len(part.Proof.LeafHash) == 0 {
471471
blockProp.Logger.Error("proof not found", "peer", peer, "height", part.Height, "round", part.Round, "part", part.Index)
472472
return
473473
}
474474
if len(part.Proof.LeafHash) != tmhash.Size {
475475
return
476476
}
477-
proof = part.Proof
477+
proof = &part.Proof
478478
}
479479

480480
added, err := parts.AddPart(part, *proof)
@@ -588,7 +588,7 @@ func (blockProp *Reactor) handleRecoveryPart(peer p2p.ID, part *proptypes.Recove
588588
return
589589
}
590590

591-
go blockProp.clearWants(part, *proof)
591+
go blockProp.clearWants(part, part.Proof)
592592
}
593593

594594
// clearWants checks the wantState to see if any peers want the given part, if

consensus/propagation/reactor_test.go

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

644644
// testCompactBlock returns a test compact block with the corresponding orignal part set,
645645
// parity partset, and proofs.
646-
func testCompactBlock(t *testing.T, height int64, round int32) (*proptypes.CompactBlock, *types.PartSet, *types.PartSet, []*merkle.Proof) {
646+
func testCompactBlock(t *testing.T, height int64, round int32) (*proptypes.CompactBlock, *types.PartSet, *types.PartSet, []merkle.Proof) {
647647
ps, err := types.NewPartSetFromData(cmtrand.Bytes(1000), types.BlockPartSizeBytes)
648648
require.NoError(t, err)
649649
pse, lastLen, err := types.Encode(ps, types.BlockPartSizeBytes)

consensus/propagation/types/types.go

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ type CompactBlock struct {
7575
mtx sync.Mutex
7676
// proofsCache is local storage from generated proofs from the PartsHashes.
7777
// It must not be included in any serialization.
78-
proofsCache []*merkle.Proof
78+
proofsCache []merkle.Proof
7979
}
8080

8181
// SignBytes returns the compact block commitment data that
@@ -192,7 +192,7 @@ func (c *CompactBlock) ToProto() *protoprop.CompactBlock {
192192
// thrown if the proofs are generated and the resulting hashes don't match those
193193
// in the compact block. This method should be called upon first receiving a
194194
// compact block.
195-
func (c *CompactBlock) Proofs() ([]*merkle.Proof, error) {
195+
func (c *CompactBlock) Proofs() ([]merkle.Proof, error) {
196196
c.mtx.Lock()
197197
defer c.mtx.Unlock()
198198

@@ -206,11 +206,11 @@ func (c *CompactBlock) Proofs() ([]*merkle.Proof, error) {
206206
return nil, errors.New("invalid number of partset hashes")
207207
}
208208

209-
c.proofsCache = make([]*merkle.Proof, 0, len(c.PartsHashes))
209+
c.proofsCache = make([]merkle.Proof, 0, len(c.PartsHashes))
210210

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

216216
if !bytes.Equal(root, c.Proposal.BlockID.PartSetHeader.Hash) {
@@ -219,7 +219,7 @@ func (c *CompactBlock) Proofs() ([]*merkle.Proof, error) {
219219

220220
parityRoot, eproofs := merkle.ProofsFromLeafHashes(c.PartsHashes[total:])
221221
for i := range eproofs {
222-
c.proofsCache = append(c.proofsCache, &eproofs[i])
222+
c.proofsCache = append(c.proofsCache, eproofs[i])
223223
}
224224

225225
if !bytes.Equal(c.BpHash, parityRoot) {
@@ -233,12 +233,12 @@ func (c *CompactBlock) GetProof(i uint32) *merkle.Proof {
233233
c.mtx.Lock()
234234
defer c.mtx.Unlock()
235235
if i < uint32(len(c.proofsCache)) {
236-
return c.proofsCache[i]
236+
return &c.proofsCache[i]
237237
}
238238
return nil
239239
}
240240

241-
func (c *CompactBlock) SetProofCache(proofs []*merkle.Proof) {
241+
func (c *CompactBlock) SetProofCache(proofs []merkle.Proof) {
242242
c.mtx.Lock()
243243
defer c.mtx.Unlock()
244244
c.proofsCache = proofs
@@ -449,7 +449,7 @@ type RecoveryPart struct {
449449
Round int32
450450
Index uint32
451451
Data []byte
452-
Proof *merkle.Proof
452+
Proof merkle.Proof
453453
}
454454

455455
func (p *RecoveryPart) ValidateBasic() error {
@@ -462,14 +462,12 @@ func (p *RecoveryPart) ValidateBasic() error {
462462
if len(p.Data) == 0 {
463463
return errors.New("RecoveryPart: Data cannot be nil or empty")
464464
}
465-
if p.Proof != nil {
466-
if err := p.Proof.ValidateBasic(); err != nil {
467-
return fmt.Errorf("RecoveryPart: invalid proof: %w", err)
468-
}
469-
hash := merkle.LeafHash(p.Data)
470-
if !bytes.Equal(hash, p.Proof.LeafHash) {
471-
return errors.New("RecoveryPart: invalid proof leaf hash")
472-
}
465+
if err := p.Proof.ValidateBasic(); err != nil {
466+
return fmt.Errorf("RecoveryPart: invalid proof: %w", err)
467+
}
468+
hash := merkle.LeafHash(p.Data)
469+
if !bytes.Equal(hash, p.Proof.LeafHash) {
470+
return errors.New("RecoveryPart: invalid proof leaf hash")
473471
}
474472
return nil
475473
}
@@ -487,7 +485,7 @@ func RecoveryPartFromProto(r *protoprop.RecoveryPart) (*RecoveryPart, error) {
487485
Round: r.Round,
488486
Index: r.Index,
489487
Data: r.Data,
490-
Proof: &proof,
488+
Proof: proof,
491489
}
492490
return rp, rp.ValidateBasic()
493491
}

consensus/propagation/types/types_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -791,7 +791,7 @@ func TestRecoveryPart_ValidateBasic(t *testing.T) {
791791
Round: 1,
792792
Index: 2,
793793
Data: []byte("valid-data"),
794-
Proof: &merkle.Proof{
794+
Proof: merkle.Proof{
795795
LeafHash: merkle.LeafHash([]byte("valid-data")),
796796
},
797797
},
@@ -844,7 +844,7 @@ func TestRecoveryPart_ValidateBasic(t *testing.T) {
844844
Round: 1,
845845
Index: 0,
846846
Data: []byte("data"),
847-
Proof: &merkle.Proof{},
847+
Proof: merkle.Proof{},
848848
},
849849
expectError: "RecoveryPart: invalid proof",
850850
},
@@ -855,7 +855,7 @@ func TestRecoveryPart_ValidateBasic(t *testing.T) {
855855
Round: 1,
856856
Index: 0,
857857
Data: []byte("data"),
858-
Proof: &merkle.Proof{
858+
Proof: merkle.Proof{
859859
LeafHash: merkle.LeafHash([]byte("invalid")),
860860
},
861861
},

crypto/merkle/proof_test.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -189,8 +189,14 @@ func TestVoteProtobuf(t *testing.T) {
189189
{"success", &proofs[0], true},
190190
}
191191
for _, tc := range testCases {
192+
if tc.v1 == nil {
193+
// Test nil case
194+
_, err := ProofFromProto(nil, false)
195+
require.Error(t, err)
196+
continue
197+
}
198+
192199
pb := tc.v1.ToProto()
193-
194200
v, err := ProofFromProto(pb, false)
195201
if tc.expPass {
196202
require.NoError(t, err)

rpc/core/blocks.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -476,21 +476,21 @@ func (env *Environment) validateDataRootInclusionProofRequest(height uint64, sta
476476
}
477477

478478
// proveDataRootTuples returns the merkle inclusion proof for a height.
479-
func (env *Environment) proveDataRootTuples(tuples []DataRootTuple, height int64) (*merkle.Proof, error) {
479+
func (env *Environment) proveDataRootTuples(tuples []DataRootTuple, height int64) (merkle.Proof, error) {
480480
dataRootEncodedTuples := make([][]byte, 0, len(tuples))
481481
for _, tuple := range tuples {
482482
encodedTuple, err := EncodeDataRootTuple(
483483
tuple.height,
484484
tuple.dataRoot,
485485
)
486486
if err != nil {
487-
return nil, err
487+
return merkle.Proof{}, err
488488
}
489489
dataRootEncodedTuples = append(dataRootEncodedTuples, encodedTuple)
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
@@ -544,5 +544,5 @@ func (env *Environment) GenerateDataRootInclusionProof(height int64, start, end
544544
if err != nil {
545545
return nil, err
546546
}
547-
return proof, nil
547+
return &proof, nil
548548
}

0 commit comments

Comments
 (0)