txs.Hash() to avoid extra allocs

This commit is contained in:
Ethan Buchman
2016-03-12 13:01:08 -05:00
parent a34f9e082e
commit d23f38b4f3
2 changed files with 23 additions and 6 deletions

View File

@ -329,7 +329,7 @@ type Data struct {
// Txs that will be applied by state @ block.Height+1. // Txs that will be applied by state @ block.Height+1.
// NOTE: not all txs here are valid. We're just agreeing on the order first. // NOTE: not all txs here are valid. We're just agreeing on the order first.
// This means that block.AppHash does not include these txs. // This means that block.AppHash does not include these txs.
Txs []Tx `json:"txs"` Txs Txs `json:"txs"`
// Volatile // Volatile
hash []byte hash []byte
@ -342,11 +342,7 @@ func (data *Data) Hash() []byte {
return data.hash return data.hash
} }
if data.hash == nil { if data.hash == nil {
txs := make([]interface{}, len(data.Txs)) data.hash = data.Txs.Hash() // NOTE: leaves of merkle tree are TxIDs
for i, tx := range data.Txs {
txs[i] = tx
}
data.hash = merkle.SimpleHashFromBinaries(txs) // NOTE: leaves are TxIDs.
} }
return data.hash return data.hash
} }

View File

@ -1,3 +1,24 @@
package types package types
import (
"github.com/tendermint/go-merkle"
)
type Tx []byte type Tx []byte
type Txs []Tx
func (txs Txs) Hash() []byte {
// Recursive impl.
// Copied from go-merkle to avoid allocations
switch len(txs) {
case 0:
return nil
case 1:
return txs[0]
default:
left := Txs(txs[:(len(txs)+1)/2]).Hash()
right := Txs(txs[(len(txs)+1)/2:]).Hash()
return merkle.SimpleHashFromTwoHashes(left, right)
}
}