-
Notifications
You must be signed in to change notification settings - Fork 923
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(ipld/plugin): don't truncate a type byte when it's not in the data
The code has a special case for the share data with type byte and without. However, it truncates the type byte like it is always there. The #1139 is the first user of share data without a type byte, which uncovered the bug. The type byte causes issues for us again. refactor(ipld/plugin): extract namespaceHasher into a separate file with some additional cleanups In preparation for the upcoming test for the namespaceHasher, we extract it into a separate file. Also, some additional cosmetics were done reducing the API surfuce of the pkg, hiding things that are not supposed to be used outside. test(ipld/plugin): write dummy unit test for the namespaceHasher
- Loading branch information
Showing
4 changed files
with
128 additions
and
61 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
package plugin | ||
|
||
import ( | ||
"fmt" | ||
"hash" | ||
|
||
"github.com/minio/sha256-simd" | ||
mhcore "github.com/multiformats/go-multihash/core" | ||
"github.com/tendermint/tendermint/pkg/consts" | ||
|
||
"github.com/celestiaorg/nmt" | ||
) | ||
|
||
func init() { | ||
mhcore.Register(sha256Namespace8Flagged, func() hash.Hash { | ||
return defaultHasher() | ||
}) | ||
} | ||
|
||
type namespaceHasher struct { | ||
*nmt.Hasher | ||
tp byte | ||
data []byte | ||
} | ||
|
||
func defaultHasher() *namespaceHasher { | ||
return &namespaceHasher{Hasher: nmt.NewNmtHasher(sha256.New(), nmt.DefaultNamespaceIDLen, true)} | ||
} | ||
|
||
func (n *namespaceHasher) Write(data []byte) (int, error) { | ||
if n.data != nil { | ||
return 0, fmt.Errorf("ipld: only one write to hasher is allowed") | ||
} | ||
|
||
ln, nln, hln := len(data), int(n.NamespaceLen), n.Hash.Size() | ||
innerNodeSize, leafNodeSize := (nln*2+hln)*2, nln+consts.ShareSize | ||
switch ln { | ||
default: | ||
return 0, fmt.Errorf("ipld: wrong sized data written to the hasher") | ||
case innerNodeSize: | ||
n.tp = nmt.NodePrefix | ||
case leafNodeSize: | ||
n.tp = nmt.LeafPrefix | ||
case innerNodeSize + typeSize: // w/ additional type byte | ||
n.tp = nmt.NodePrefix | ||
data = data[typeSize:] | ||
case leafNodeSize + typeSize: // w/ additional type byte | ||
n.tp = nmt.LeafPrefix | ||
data = data[typeSize:] | ||
} | ||
|
||
n.data = data | ||
return len(n.data), nil | ||
} | ||
|
||
func (n *namespaceHasher) Sum([]byte) []byte { | ||
isLeafData := n.tp == nmt.LeafPrefix | ||
if isLeafData { | ||
return n.Hasher.HashLeaf(n.data) | ||
} | ||
|
||
flagLen := int(n.NamespaceLen * 2) | ||
sha256Len := n.Hasher.Size() | ||
return n.Hasher.HashNode(n.data[:flagLen+sha256Len], n.data[flagLen+sha256Len:]) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package plugin | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"github.com/tendermint/tendermint/pkg/consts" | ||
) | ||
|
||
func TestNamespaceHasherWrite(t *testing.T) { | ||
leafSize := consts.ShareSize + consts.NamespaceSize | ||
innerSize := nmtHashSize * 2 | ||
tt := []struct { | ||
name string | ||
expectedSize int | ||
writtenSize int | ||
}{ | ||
{ | ||
"Leaf", | ||
leafSize, | ||
leafSize, | ||
}, | ||
{ | ||
"Inner", | ||
innerSize, | ||
innerSize, | ||
}, | ||
{ | ||
"LeafAndType", | ||
leafSize, | ||
leafSize + typeSize, | ||
}, | ||
{ | ||
"InnerAndType", | ||
innerSize, | ||
innerSize + typeSize, | ||
}, | ||
} | ||
|
||
for _, ts := range tt { | ||
t.Run(ts.name, func(t *testing.T) { | ||
h := defaultHasher() | ||
n, err := h.Write(make([]byte, ts.writtenSize)) | ||
assert.NoError(t, err) | ||
assert.Equal(t, ts.expectedSize, n) | ||
assert.Equal(t, ts.expectedSize, len(h.data)) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters