Merge pull request #1301 from tendermint/types-data+header+non-nil-panics
types: Hash invoked for nil Data and Header should not panic
This commit is contained in:
commit
8c0c8e8e01
|
@ -179,7 +179,7 @@ type Header struct {
|
||||||
// Hash returns the hash of the header.
|
// Hash returns the hash of the header.
|
||||||
// Returns nil if ValidatorHash is missing.
|
// Returns nil if ValidatorHash is missing.
|
||||||
func (h *Header) Hash() cmn.HexBytes {
|
func (h *Header) Hash() cmn.HexBytes {
|
||||||
if len(h.ValidatorsHash) == 0 {
|
if h == nil || len(h.ValidatorsHash) == 0 {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
return merkle.SimpleHashFromMap(map[string]merkle.Hasher{
|
return merkle.SimpleHashFromMap(map[string]merkle.Hasher{
|
||||||
|
@ -413,6 +413,9 @@ type Data struct {
|
||||||
|
|
||||||
// Hash returns the hash of the data
|
// Hash returns the hash of the data
|
||||||
func (data *Data) Hash() cmn.HexBytes {
|
func (data *Data) Hash() cmn.HexBytes {
|
||||||
|
if data == nil {
|
||||||
|
return (Txs{}).Hash()
|
||||||
|
}
|
||||||
if data.hash == nil {
|
if data.hash == nil {
|
||||||
data.hash = data.Txs.Hash() // NOTE: leaves of merkle tree are TxIDs
|
data.hash = data.Txs.Hash() // NOTE: leaves of merkle tree are TxIDs
|
||||||
}
|
}
|
||||||
|
|
|
@ -3,7 +3,9 @@ package types
|
||||||
import (
|
import (
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
"github.com/stretchr/testify/require"
|
"github.com/stretchr/testify/require"
|
||||||
|
|
||||||
crypto "github.com/tendermint/go-crypto"
|
crypto "github.com/tendermint/go-crypto"
|
||||||
cmn "github.com/tendermint/tmlibs/common"
|
cmn "github.com/tendermint/tmlibs/common"
|
||||||
)
|
)
|
||||||
|
@ -73,3 +75,15 @@ func makeBlockID(hash string, partSetSize int, partSetHash string) BlockID {
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var nilBytes []byte
|
||||||
|
|
||||||
|
func TestNilHeaderHashDoesntCrash(t *testing.T) {
|
||||||
|
assert.Equal(t, []byte((*Header)(nil).Hash()), nilBytes)
|
||||||
|
assert.Equal(t, []byte((new(Header)).Hash()), nilBytes)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestNilDataHashDoesntCrash(t *testing.T) {
|
||||||
|
assert.Equal(t, []byte((*Data)(nil).Hash()), nilBytes)
|
||||||
|
assert.Equal(t, []byte(new(Data).Hash()), nilBytes)
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue