diff --git a/core/services/ocr2/plugins/ccip/execution_reporting_plugin_test.go b/core/services/ocr2/plugins/ccip/execution_reporting_plugin_test.go index 99721075e7..8f70521c9c 100644 --- a/core/services/ocr2/plugins/ccip/execution_reporting_plugin_test.go +++ b/core/services/ocr2/plugins/ccip/execution_reporting_plugin_test.go @@ -1227,7 +1227,7 @@ func Test_calculateObservedMessagesConsensus(t *testing.T) { f: 1, }, want: []ObservedMessage{ - {SeqNr: 1, MsgData: MsgData{TokenData: [][]byte{{0x1}}}}, + {SeqNr: 1, MsgData: MsgData{TokenData: [][]byte{{0x3}}}}, }, }, } diff --git a/core/services/ocr2/plugins/ccip/internal/hashlib/common.go b/core/services/ocr2/plugins/ccip/internal/hashlib/common.go index 28a4405bb4..719247308b 100644 --- a/core/services/ocr2/plugins/ccip/internal/hashlib/common.go +++ b/core/services/ocr2/plugins/ccip/internal/hashlib/common.go @@ -1,6 +1,8 @@ package hashlib import ( + "strconv" + "github.com/smartcontractkit/chainlink/v2/core/utils" ) @@ -10,9 +12,16 @@ func BytesOfBytesKeccak(b [][]byte) ([32]byte, error) { return [32]byte{}, nil } - h := utils.Keccak256Fixed(b[0]) - for _, v := range b[1:] { - h = utils.Keccak256Fixed(append(h[:], v...)) + joinedBytes := make([]byte, 0) + joinedBytes = append(joinedBytes, intToBytes(int64(len(b)))...) + for i := range b { + joinedBytes = append(joinedBytes, intToBytes(int64(len(b[i])))...) + joinedBytes = append(joinedBytes, b[i]...) } - return h, nil + + return utils.Keccak256Fixed(joinedBytes), nil +} + +func intToBytes(v int64) []byte { + return []byte(strconv.FormatInt(v, 10)) } diff --git a/core/services/ocr2/plugins/ccip/internal/hashlib/common_test.go b/core/services/ocr2/plugins/ccip/internal/hashlib/common_test.go index 278944a847..c941fdf336 100644 --- a/core/services/ocr2/plugins/ccip/internal/hashlib/common_test.go +++ b/core/services/ocr2/plugins/ccip/internal/hashlib/common_test.go @@ -4,16 +4,37 @@ import ( "testing" "github.com/stretchr/testify/assert" + + "github.com/smartcontractkit/chainlink/v2/core/utils" ) func TestBytesOfBytesKeccak(t *testing.T) { - h, err := BytesOfBytesKeccak(nil) - assert.NoError(t, err) - assert.Equal(t, [32]byte{}, h) - - h1, err := BytesOfBytesKeccak([][]byte{{0x1}, {0x1}}) - assert.NoError(t, err) - h2, err := BytesOfBytesKeccak([][]byte{{0x1, 0x1}}) - assert.NoError(t, err) - assert.NotEqual(t, h1, h2) + t.Run("simple case", func(t *testing.T) { + h, err := BytesOfBytesKeccak(nil) + assert.NoError(t, err) + assert.Equal(t, [32]byte{}, h) + + h1, err := BytesOfBytesKeccak([][]byte{{0x1}, {0x1}}) + assert.NoError(t, err) + h2, err := BytesOfBytesKeccak([][]byte{{0x1, 0x1}}) + assert.NoError(t, err) + assert.NotEqual(t, h1, h2) + }) + + t.Run("should not have collision", func(t *testing.T) { + s1 := utils.RandomBytes32() + s2 := utils.RandomBytes32() + + hs1, err := BytesOfBytesKeccak([][]byte{s1[:]}) + assert.NoError(t, err) + + h1, err := BytesOfBytesKeccak([][]byte{s1[:], s2[:]}) + assert.NoError(t, err) + + h2, err := BytesOfBytesKeccak([][]byte{append(hs1[:], s2[:]...)}) + assert.NoError(t, err) + + assert.NotEqual(t, h1, h2) + }) + }