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 19, 2023
1 parent 9a8f776 commit cc1420f
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 6 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
14 changes: 10 additions & 4 deletions packages/vm/core/blob/internal.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package blob

import (
"bytes"
"fmt"

"github.com/iotaledger/wasp/packages/hashing"
Expand All @@ -25,11 +26,16 @@ func mustGetBlobHash(fields dict.Dict) (hashing.HashValue, []kv.Key, [][]byte) {
sorted := fields.KeysSorted() // mind determinism
values := make([][]byte, 0, len(sorted))
all := make([][]byte, 0, 2*len(sorted))
for _, k := range sorted {
v := fields.Get(k)
for _, key := range sorted {
v := fields.Get(key)
values = append(values, v)
all = append(all, v)
all = append(all, []byte(k))
// escape all key and value
// TODO may be better solution exist?
dash := []byte("-")
escDash := []byte("/-")
v = bytes.Replace(v, dash, escDash, -1)
k := bytes.Replace([]byte(key), dash, escDash, -1)
all = append(all, k, dash, v)
}
return hashing.HashData(all...), sorted, values
}
Expand Down
44 changes: 44 additions & 0 deletions packages/vm/core/blob/internal_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package blob

import (
"encoding/hex"
"testing"

"github.com/stretchr/testify/require"

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

func TestMustGetBlobHash(t *testing.T) {
t.Run("normal", func(t *testing.T) {
fields := dict.Dict{
"key0": []byte("val0"),
"key1": []byte("val1"),
}

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

resHash, err := hex.DecodeString("2e42c9185213b81ab77236086c13b7e45471c7104d058197a573a4f10b12ab35")
require.NoError(t, err)
require.Equal(t, resHash, h.Bytes())
})
t.Run("with escape separator", func(t *testing.T) {
fields := dict.Dict{
"key0": []byte("-val0"),
"key1": []byte("/-val1"),
"key2": []byte("//-val2"),
}

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

resHash, err := hex.DecodeString("b143143db9451db8e1a3623ae1561d5f1bd18ce4b0aa0c9c651290af54e9ea08")
require.NoError(t, err)
require.Equal(t, resHash, h.Bytes())
})
}

0 comments on commit cc1420f

Please sign in to comment.