-
Notifications
You must be signed in to change notification settings - Fork 110
swarm/hash: Custom multihash implementation #633
Conversation
swarm/hash/multihash.go
Outdated
"fmt" | ||
) | ||
|
||
const ( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
calling this default is misleading. it is for bmt-sha3-128
not sure this should be here even
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it would be helpful if you gave me an overview of which hashing functions, one or more, will be used in the application overall.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍 no to defaults. ideally pull this into a configuration flag
swarm/hash/multihash.go
Outdated
// You should have received a copy of the GNU Lesser General Public License | ||
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>. | ||
|
||
package swarmhash |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
lets not use swarm hash cos it is something else already
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
But the swarmhash will be here in the end, no?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍 please no swarmhash
. use multihash
or hash
. in general i think that in terms of package names - either use the filename or directory name
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure, we can call it something else. But hash
is not good, because that's the name of a package already in use in the stack. And multihash
is misleading because the package will / is indended to cover all hashing functionality in swarm.
Any other good name suggestions? hashing
? hasher
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
multihash is exactly the name since it concerns a self describing hash, hence it is a multihash. or am i seeing things the wrong way?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
And multihash is misleading because the package will / is indended to cover all hashing functionality in swarm.
along the lines of:
hashing.Add("SHA3", SomeNewHashFunc(SHAHash)")
shinynewhash, err := hashing.Hash("foo")
Have a look at: #634
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I vote "swarm/multihash"
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@fjl you would vote swarm/multihash
even if it does more than just treating multihashes?
swarm/storage/resource.go
Outdated
@@ -725,7 +728,14 @@ func (self *ResourceHandler) parseUpdate(chunkdata []byte) (*Signature, uint32, | |||
var intdatalength int | |||
var multihash bool | |||
if datalength == 0 { | |||
intdatalength = isMultihash(chunkdata[cursor:]) | |||
var intheaderlength int |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can you please extract this to a function?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
because it was already?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
sorry I read it wrong
the GetMultihashLength
takes the place of the isMultihash
.. that's a function..? What am I missing?
swarm/hash/multihash.go
Outdated
// You should have received a copy of the GNU Lesser General Public License | ||
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>. | ||
|
||
package swarmhash |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍 please no swarmhash
. use multihash
or hash
. in general i think that in terms of package names - either use the filename or directory name
swarm/hash/multihash.go
Outdated
"fmt" | ||
) | ||
|
||
const ( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍 no to defaults. ideally pull this into a configuration flag
swarm/hash/multihash.go
Outdated
MultihashLength = defaultMultihashLength | ||
) | ||
|
||
func init() { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
remove and replace with a constructor.
swarm/hash/multihash.go
Outdated
} | ||
|
||
// check if valid swarm multihash | ||
func isSwarmMultihashType(code uint8) bool { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
as far as i know typecode 0x1b
infers sha3
but does no infer swarm hash
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
0x1b
is keccak256
.
There is no "swarm hash" in mulithash (yet?)
But a swarm hash uses keccak256
.
This is as close as we get for now, I think.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
then for that matter swarm hash
does not exist. i would get rid of as much of the multihash
references as possible (except for the points where the implementation is indeed generalised) since we are just supporting 1 multihash format thus change the references to describe keccak256
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm, I think this all warrants further discussion. The terms are confusing.
swarm/storage/resource.go
Outdated
if isMultihash(data) == 0 { | ||
return nil, NewResourceError(ErrNothingToReturn, "Invalid multihash") | ||
if _, _, err := swarmhash.GetMultihashLength(data); err != nil { | ||
return nil, NewResourceError(ErrNothingToReturn, fmt.Sprintf("Invalid multihash: %v", err)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this error is not so crystal clear, why can't you just bubble the error from GetMultihashLength
up?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the error texts in swarm/hash
are pretty neutral, e.g "unreadable length field", not so easy to tell further up what that means.
Do you mean we should have typed error maybe?
swarm/storage/resource_test.go
Outdated
if err != nil { | ||
t.Fatal(err) | ||
} | ||
sha1bytes := make([]byte, swarmhash.MultihashLength) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
are we using sha1?
swarm/storage/resource_test.go
Outdated
if !bytes.Equal(swarmhashdecode.Digest, swarmhashbytes.Bytes()) { | ||
t.Fatalf("Decoded SHA1 hash '%x' does not match original hash '%x'", swarmhashdecode.Digest, swarmhashbytes.Bytes()) | ||
if !bytes.Equal(swarmhashdecode, swarmhashbytes.Bytes()) { | ||
t.Fatalf("Decoded SHA1 hash '%x' does not match original hash '%x'", swarmhashdecode, swarmhashbytes.Bytes()) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
sha1?
swarm/storage/resource_test.go
Outdated
if !bytes.Equal(sha1decode.Digest, sha1bytes) { | ||
t.Fatalf("Decoded SHA1 hash '%x' does not match original hash '%x'", sha1decode.Digest, sha1bytes) | ||
if !bytes.Equal(sha1decode, sha1bytes) { | ||
t.Fatalf("Decoded SHA1 hash '%x' does not match original hash '%x'", sha1decode, sha1bytes) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
sha1
swarm/storage/resource_test.go
Outdated
if !bytes.Equal(sha1decode.Digest, sha1bytes) { | ||
t.Fatalf("Decoded SHA1 hash '%x' does not match original hash '%x'", sha1decode.Digest, sha1bytes) | ||
if !bytes.Equal(sha1decode, sha1bytes) { | ||
t.Fatalf("Decoded SHA1 hash '%x' does not match original hash '%x'", sha1decode, sha1bytes) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
more of this
swarm/storage/mru/resource.go
Outdated
intdatalength, intheaderlength, err = swarmhash.GetMultihashLength(chunkdata[cursor:]) | ||
if err != nil { | ||
log.Error("multihash parse error", "err", err) | ||
return nil, 0, 0, "", nil, false, fmt.Errorf("Corrupt multihash data: %v", err) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
no need to wrap err.
swarm/storage/mru/resource.go
Outdated
@@ -286,6 +287,7 @@ func (self *Handler) SetStore(store *storage.NetStore) { | |||
func (self *Handler) Validate(addr storage.Address, data []byte) bool { | |||
signature, period, version, name, parseddata, _, err := self.parseUpdate(data) | |||
if err != nil { | |||
log.Warn("foo", "err", err) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
just log.Warn(err.Error())
swarm/storage/mru/resource.go
Outdated
return nil, NewError(ErrNothingToReturn, "Invalid multihash") | ||
// \TODO perhaps this check should be in newUpdateChunk() | ||
if _, _, err := swarmhash.GetMultihashLength(data); err != nil { | ||
return nil, NewError(ErrNothingToReturn, fmt.Sprintf("Invalid multihash: %v", err)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
no need to wrap err in fmt.Sprintf
- it should be clear where it is coming from and what's wrong with it.
swarm/hash/multihash.go
Outdated
cursor += c | ||
hashlength, c := binary.Uvarint(data[cursor:]) | ||
if c <= 0 { | ||
return 0, 0, fmt.Errorf("unreadable length field") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No format here. Just errors.New...
swarm/hash/multihash.go
Outdated
cursor := 0 | ||
typ, c := binary.Uvarint(data) | ||
if c <= 0 { | ||
return 0, 0, fmt.Errorf("unreadable hashtype field") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No format here.
swarm/hash/multihash.go
Outdated
// we cheekily assume hashlength < maxint | ||
inthashlength := int(hashlength) | ||
if len(data[c:]) < inthashlength { | ||
return 0, 0, fmt.Errorf("length mismatch") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No format.
swarm/hash/multihash_test.go
Outdated
t.Fatalf("expected header length %d, got %d", 2, hl) | ||
} | ||
if _, _, err := GetMultihashLength(expected[1:]); err == nil { | ||
t.Fatalf("expected failure on corrupt header") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No format.
swarm/hash/multihash_test.go
Outdated
t.Fatalf("expected failure on corrupt header") | ||
} | ||
if _, _, err := GetMultihashLength(expected[:len(expected)-2]); err == nil { | ||
t.Fatalf("expected failure on short content") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No format.
In the spirit of the "let's just merge this as is" conclusion of yesterday's meeting I hereby submit this to another review, albeit under a new name. |
swarm/api/api.go
Outdated
apiGetInvalid.Inc(1) | ||
status = http.StatusUnprocessableEntity | ||
log.Warn(fmt.Sprintf("invalid resource multihash code: %x", decodedMultihash.Code)) | ||
log.Warn(fmt.Sprintf("invalid resource multihash: %v", err)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We have structured logs, no need to use fmt.Sprintf
.
Linter is failing and also compilation doesn't seem to work. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if linter and travis green i am fine with this
#595