Update types package, fix proof's aunt handling

This commit is contained in:
ValarDragon
2018-08-30 22:43:49 -07:00
parent 4b14e17d61
commit 1302db7dac
10 changed files with 41 additions and 45 deletions
+5 -8
View File
@@ -419,11 +419,11 @@ func (commit *Commit) Hash() cmn.HexBytes {
return nil
}
if commit.hash == nil {
bs := make([]merkle.Hasher, len(commit.Precommits))
bzs := make([][]byte, len(commit.Precommits))
for i, precommit := range commit.Precommits {
bs[i] = aminoHasher(precommit)
bzs[i] = cdc.MustMarshalBinaryBare(precommit)
}
commit.hash = merkle.SimpleHashFromHashers(bs)
commit.hash = merkle.SimpleHashFromByteSlices(bzs)
}
return commit.hash
}
@@ -638,11 +638,8 @@ type hasher struct {
func (h hasher) Hash() []byte {
hasher := tmhash.New()
if h.item != nil && !cmn.IsTypedNil(h.item) && !cmn.IsEmpty(h.item) {
bz, err := cdc.MarshalBinaryBare(h.item)
if err != nil {
panic(err)
}
_, err = hasher.Write(bz)
bz := cdc.MustMarshalBinaryBare(h.item)
_, err := hasher.Write(bz)
if err != nil {
panic(err)
}
+3 -3
View File
@@ -88,7 +88,7 @@ func NewPartSetFromData(data []byte, partSize int) *PartSet {
// divide data into 4kb parts.
total := (len(data) + partSize - 1) / partSize
parts := make([]*Part, total)
parts_ := make([]merkle.Hasher, total)
parts_ := make([][]byte, total)
partsBitArray := cmn.NewBitArray(total)
for i := 0; i < total; i++ {
part := &Part{
@@ -96,11 +96,11 @@ func NewPartSetFromData(data []byte, partSize int) *PartSet {
Bytes: data[i*partSize : cmn.MinInt(len(data), (i+1)*partSize)],
}
parts[i] = part
parts_[i] = part
parts_[i] = part.Bytes
partsBitArray.SetIndex(i, true)
}
// Compute merkle proofs
root, proofs := merkle.SimpleProofsFromHashers(parts_)
root, proofs := merkle.SimpleProofsFromByteSlices(parts_)
for i := 0; i < total; i++ {
parts[i].Proof = *proofs[i]
}
+6 -6
View File
@@ -54,20 +54,20 @@ func (a ABCIResults) Bytes() []byte {
func (a ABCIResults) Hash() []byte {
// NOTE: we copy the impl of the merkle tree for txs -
// we should be consistent and either do it for both or not.
return merkle.SimpleHashFromHashers(a.toHashers())
return merkle.SimpleHashFromByteSlices(a.toByteSlices())
}
// ProveResult returns a merkle proof of one result from the set
func (a ABCIResults) ProveResult(i int) merkle.SimpleProof {
_, proofs := merkle.SimpleProofsFromHashers(a.toHashers())
_, proofs := merkle.SimpleProofsFromByteSlices(a.toByteSlices())
return *proofs[i]
}
func (a ABCIResults) toHashers() []merkle.Hasher {
func (a ABCIResults) toByteSlices() [][]byte {
l := len(a)
hashers := make([]merkle.Hasher, l)
byteslices := make([][]byte, l)
for i := 0; i < l; i++ {
hashers[i] = a[i]
byteslices[i] = cdc.MustMarshalBinaryBare(a[i])
}
return hashers
return byteslices
}
+3 -3
View File
@@ -70,11 +70,11 @@ func (txs Txs) IndexByHash(hash []byte) int {
// TODO: optimize this!
func (txs Txs) Proof(i int) TxProof {
l := len(txs)
hashers := make([]merkle.Hasher, l)
bzs := make([][]byte, l)
for i := 0; i < l; i++ {
hashers[i] = txs[i]
bzs[i] = txs[i]
}
root, proofs := merkle.SimpleProofsFromHashers(hashers)
root, proofs := merkle.SimpleProofsFromByteSlices(bzs)
return TxProof{
Index: i,
+14
View File
@@ -82,6 +82,20 @@ func (v *Validator) Hash() []byte {
})
}
// hashBytes computes the bytes to be hashed of a validator with a given voting power.
// It excludes the Accum value, which changes with every round.
func (v *Validator) hashBytes() []byte {
return cdc.MustMarshalBinaryBare(struct {
Address Address
PubKey crypto.PubKey
VotingPower int64
}{
v.Address,
v.PubKey,
v.VotingPower,
})
}
//----------------------------------------
// RandValidator
+3 -3
View File
@@ -176,11 +176,11 @@ func (vals *ValidatorSet) Hash() []byte {
if len(vals.Validators) == 0 {
return nil
}
hashers := make([]merkle.Hasher, len(vals.Validators))
bzs := make([][]byte, len(vals.Validators))
for i, val := range vals.Validators {
hashers[i] = val
bzs[i] = val.hashBytes()
}
return merkle.SimpleHashFromHashers(hashers)
return merkle.SimpleHashFromByteSlices(bzs)
}
// Add adds val to the validator set and returns true. It returns false if val