mirror of
https://github.com/fluencelabs/tendermint
synced 2025-06-13 05:11:21 +00:00
Conform to go-merkle SimpleProof.Aunts
This commit is contained in:
@ -173,7 +173,7 @@ func (conR *ConsensusReactor) Receive(chID byte, peer *p2p.Peer, msgBytes []byte
|
|||||||
case *ProposalPOLMessage:
|
case *ProposalPOLMessage:
|
||||||
ps.ApplyProposalPOLMessage(msg)
|
ps.ApplyProposalPOLMessage(msg)
|
||||||
case *BlockPartMessage:
|
case *BlockPartMessage:
|
||||||
ps.SetHasProposalBlockPart(msg.Height, msg.Round, msg.Part.Proof.Index)
|
ps.SetHasProposalBlockPart(msg.Height, msg.Round, msg.Part.Index)
|
||||||
_, err = conR.conS.AddProposalBlockPart(msg.Height, msg.Part)
|
_, err = conR.conS.AddProposalBlockPart(msg.Height, msg.Part)
|
||||||
default:
|
default:
|
||||||
log.Warn(Fmt("Unknown message type %v", reflect.TypeOf(msg)))
|
log.Warn(Fmt("Unknown message type %v", reflect.TypeOf(msg)))
|
||||||
|
@ -24,8 +24,9 @@ var (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type Part struct {
|
type Part struct {
|
||||||
Proof merkle.SimpleProof `json:"proof"`
|
Index int `json:"index"`
|
||||||
Bytes []byte `json:"bytes"`
|
Bytes []byte `json:"bytes"`
|
||||||
|
Proof merkle.SimpleProof `json:"proof"`
|
||||||
|
|
||||||
// Cache
|
// Cache
|
||||||
hash []byte
|
hash []byte
|
||||||
@ -47,12 +48,13 @@ func (part *Part) String() string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (part *Part) StringIndented(indent string) string {
|
func (part *Part) StringIndented(indent string) string {
|
||||||
return fmt.Sprintf(`Part{
|
return fmt.Sprintf(`Part{#%v
|
||||||
%s Proof: %v
|
|
||||||
%s Bytes: %X...
|
%s Bytes: %X...
|
||||||
|
%s Proof: %v
|
||||||
%s}`,
|
%s}`,
|
||||||
indent, part.Proof.StringIndented(indent+" "),
|
part.Index,
|
||||||
indent, Fingerprint(part.Bytes),
|
indent, Fingerprint(part.Bytes),
|
||||||
|
indent, part.Proof.StringIndented(indent+" "),
|
||||||
indent)
|
indent)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -101,6 +103,7 @@ func NewPartSetFromData(data []byte) *PartSet {
|
|||||||
partsBitArray := NewBitArray(total)
|
partsBitArray := NewBitArray(total)
|
||||||
for i := 0; i < total; i++ {
|
for i := 0; i < total; i++ {
|
||||||
part := &Part{
|
part := &Part{
|
||||||
|
Index: i,
|
||||||
Bytes: data[i*partSize : MinInt(len(data), (i+1)*partSize)],
|
Bytes: data[i*partSize : MinInt(len(data), (i+1)*partSize)],
|
||||||
}
|
}
|
||||||
parts[i] = part
|
parts[i] = part
|
||||||
@ -108,13 +111,13 @@ func NewPartSetFromData(data []byte) *PartSet {
|
|||||||
partsBitArray.SetIndex(i, true)
|
partsBitArray.SetIndex(i, true)
|
||||||
}
|
}
|
||||||
// Compute merkle proofs
|
// Compute merkle proofs
|
||||||
proofs := merkle.SimpleProofsFromHashables(parts_)
|
root, proofs := merkle.SimpleProofsFromHashables(parts_)
|
||||||
for i := 0; i < total; i++ {
|
for i := 0; i < total; i++ {
|
||||||
parts[i].Proof = *proofs[i]
|
parts[i].Proof = *proofs[i]
|
||||||
}
|
}
|
||||||
return &PartSet{
|
return &PartSet{
|
||||||
total: total,
|
total: total,
|
||||||
hash: proofs[0].RootHash,
|
hash: root,
|
||||||
parts: parts,
|
parts: parts,
|
||||||
partsBitArray: partsBitArray,
|
partsBitArray: partsBitArray,
|
||||||
count: total,
|
count: total,
|
||||||
@ -190,23 +193,23 @@ func (ps *PartSet) AddPart(part *Part) (bool, error) {
|
|||||||
defer ps.mtx.Unlock()
|
defer ps.mtx.Unlock()
|
||||||
|
|
||||||
// Invalid part index
|
// Invalid part index
|
||||||
if part.Proof.Index >= ps.total {
|
if part.Index >= ps.total {
|
||||||
return false, ErrPartSetUnexpectedIndex
|
return false, ErrPartSetUnexpectedIndex
|
||||||
}
|
}
|
||||||
|
|
||||||
// If part already exists, return false.
|
// If part already exists, return false.
|
||||||
if ps.parts[part.Proof.Index] != nil {
|
if ps.parts[part.Index] != nil {
|
||||||
return false, nil
|
return false, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check hash proof
|
// Check hash proof
|
||||||
if !part.Proof.Verify(part.Hash(), ps.Hash()) {
|
if !part.Proof.Verify(part.Index, ps.total, part.Hash(), ps.Hash()) {
|
||||||
return false, ErrPartSetInvalidProof
|
return false, ErrPartSetInvalidProof
|
||||||
}
|
}
|
||||||
|
|
||||||
// Add part
|
// Add part
|
||||||
ps.parts[part.Proof.Index] = part
|
ps.parts[part.Index] = part
|
||||||
ps.partsBitArray.SetIndex(part.Proof.Index, true)
|
ps.partsBitArray.SetIndex(part.Index, true)
|
||||||
ps.count++
|
ps.count++
|
||||||
return true, nil
|
return true, nil
|
||||||
}
|
}
|
||||||
|
@ -69,7 +69,7 @@ func TestWrongProof(t *testing.T) {
|
|||||||
|
|
||||||
// Test adding a part with wrong trail.
|
// Test adding a part with wrong trail.
|
||||||
part := partSet.GetPart(0)
|
part := partSet.GetPart(0)
|
||||||
part.Proof.InnerHashes[0][0] += byte(0x01)
|
part.Proof.Aunts[0][0] += byte(0x01)
|
||||||
added, err := partSet2.AddPart(part)
|
added, err := partSet2.AddPart(part)
|
||||||
if added || err == nil {
|
if added || err == nil {
|
||||||
t.Errorf("Expected to fail adding a part with bad trail.")
|
t.Errorf("Expected to fail adding a part with bad trail.")
|
||||||
|
Reference in New Issue
Block a user