lib/protocol: Include WeakHash in BlocksHash (#6799)
This commit is contained in:
@@ -276,9 +276,11 @@ func PermsEqual(a, b uint32) bool {
|
|||||||
|
|
||||||
// BlocksEqual returns true when the two files have identical block lists.
|
// BlocksEqual returns true when the two files have identical block lists.
|
||||||
func (f FileInfo) BlocksEqual(other FileInfo) bool {
|
func (f FileInfo) BlocksEqual(other FileInfo) bool {
|
||||||
// If both sides have blocks hashes then we can just compare those.
|
// If both sides have blocks hashes and they match, we are good. If they
|
||||||
if len(f.BlocksHash) > 0 && len(other.BlocksHash) > 0 {
|
// don't match still check individual block hashes to catch differences
|
||||||
return bytes.Equal(f.BlocksHash, other.BlocksHash)
|
// in weak hashes only (e.g. after switching weak hash algo).
|
||||||
|
if len(f.BlocksHash) > 0 && len(other.BlocksHash) > 0 && bytes.Equal(f.BlocksHash, other.BlocksHash) {
|
||||||
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
// Actually compare the block lists in full.
|
// Actually compare the block lists in full.
|
||||||
@@ -382,6 +384,7 @@ func BlocksHash(bs []BlockInfo) []byte {
|
|||||||
h := sha256.New()
|
h := sha256.New()
|
||||||
for _, b := range bs {
|
for _, b := range bs {
|
||||||
_, _ = h.Write(b.Hash)
|
_, _ = h.Write(b.Hash)
|
||||||
|
_ = binary.Write(h, binary.BigEndian, b.WeakHash)
|
||||||
}
|
}
|
||||||
return h.Sum(nil)
|
return h.Sum(nil)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -887,7 +887,8 @@ func TestBlocksEqual(t *testing.T) {
|
|||||||
{blocksOne, nil, blocksOne, nil, true}, // blocks compared
|
{blocksOne, nil, blocksOne, nil, true}, // blocks compared
|
||||||
{blocksOne, nil, blocksTwo, nil, false}, // blocks compared
|
{blocksOne, nil, blocksTwo, nil, false}, // blocks compared
|
||||||
{blocksOne, hashOne, blocksTwo, hashOne, true}, // hashes equal, blocks not looked at
|
{blocksOne, hashOne, blocksTwo, hashOne, true}, // hashes equal, blocks not looked at
|
||||||
{blocksOne, hashOne, blocksOne, hashTwo, false}, // hashes different, blocks not looked at
|
{blocksOne, hashOne, blocksOne, hashTwo, true}, // hashes different, blocks compared
|
||||||
|
{blocksOne, hashOne, blocksTwo, hashTwo, false}, // hashes different, blocks compared
|
||||||
{blocksOne, hashOne, nil, nil, false}, // blocks is different from no blocks
|
{blocksOne, hashOne, nil, nil, false}, // blocks is different from no blocks
|
||||||
{blocksOne, nil, nil, nil, false}, // blocks is different from no blocks
|
{blocksOne, nil, nil, nil, false}, // blocks is different from no blocks
|
||||||
{nil, hashOne, nil, nil, true}, // nil blocks are equal, even of one side has a hash
|
{nil, hashOne, nil, nil, true}, // nil blocks are equal, even of one side has a hash
|
||||||
|
|||||||
Reference in New Issue
Block a user