Skip to content

Commit

Permalink
fix: Fix blobHash collision
Browse files Browse the repository at this point in the history
  • Loading branch information
howjmay committed Sep 18, 2023
1 parent 9a8f776 commit 373c479
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 3 deletions.
4 changes: 2 additions & 2 deletions contracts/wasm/corecontracts/test/core_blob_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (
)

// this is the expected blob hash for key0/val0 key1/val1
const expectedBlobHash = "0x5fec3bfc701d80bdf75e337cb3dcb401c2423d15fc17a74d5b644dae143118b1"
const expectedBlobHash = "0xc3a428b98c8a0385da56aceaa36d82032fb22fe372bb78b9ec9993a24acb1d35"

func setupBlob(t *testing.T) *wasmsolo.SoloContext {
ctx := setup(t)
Expand Down Expand Up @@ -81,7 +81,7 @@ func TestListBlobs(t *testing.T) {
fStore.Params.Blobs().GetBytes("key1").SetValue([]byte("_val1"))
fStore.Func.Post()
require.NoError(t, ctx.Err)
expectedHash := "0x462af4abe5977f4dd985a0a097705925b9fa6c033c9d931c1e2171f710693462"
expectedHash := "0xc5b04323dd23505d1c6af7dfdf3ef3ea102c516ad787fcc1ec4ccee39168af54"
require.Equal(t, expectedHash, fStore.Results.Hash().Value().String())

fList := coreblob.ScFuncs.ListBlobs(ctx)
Expand Down
2 changes: 1 addition & 1 deletion packages/vm/core/blob/internal.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ func mustGetBlobHash(fields dict.Dict) (hashing.HashValue, []kv.Key, [][]byte) {
for _, k := range sorted {
v := fields.Get(k)
values = append(values, v)
all = append(all, v)
all = append(all, []byte(k))
all = append(all, v)
}
return hashing.HashData(all...), sorted, values
}
Expand Down
26 changes: 26 additions & 0 deletions packages/vm/core/blob/internal_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package blob

import (
"encoding/hex"
"testing"

"github.com/iotaledger/wasp/packages/kv/dict"

Check failure on line 7 in packages/vm/core/blob/internal_test.go

View workflow job for this annotation

GitHub Actions / Lint

File is not `goimports`-ed with -local github.com/iotaledger (goimports)
"github.com/stretchr/testify/require"
)

func TestMustGetBlobHash(t *testing.T) {
fields := dict.Dict{
"field1": []byte("value1"),
"field2": []byte("value2"),
}

h, keys, values := mustGetBlobHash(fields)
for i, k := range keys {
require.Equal(t, fields[k], values[i])
}

// blobHash = hash(fieldName1 || binaryChunk1 || fieldName2 || binaryChunk2 || ... || fieldNameN || binaryChunkN)
resHash, err := hex.DecodeString("d45cd5dee59f83b3334c1acce7a38f12280d264aada764ed0d65e8a88c4693b0")
require.NoError(t, err)
require.Equal(t, resHash, h.Bytes())
}

0 comments on commit 373c479

Please sign in to comment.