Skip to content

Commit

Permalink
Fix sha sum on wasm
Browse files Browse the repository at this point in the history
  • Loading branch information
nolag committed Sep 23, 2024
1 parent e0ce4ff commit a6fd7a0
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 7 deletions.
7 changes: 4 additions & 3 deletions core/services/job/wasm_file_spec_factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,10 @@ func (w WasmFileSpecFactory) rawSpecAndShaFromWasm(wasm, config []byte) ([]byte,
}

func (w WasmFileSpecFactory) sha(wasm, config []byte) string {
sha := sha256.New()
sha.Write(wasm)
return fmt.Sprintf("%x", sha.Sum(config))
sum := sha256.New()
sum.Write(wasm)
sum.Write(config)
return fmt.Sprintf("%x", sum.Sum(nil))
}

var _ WorkflowSpecFactory = (*WasmFileSpecFactory)(nil)
8 changes: 5 additions & 3 deletions core/services/job/wasm_file_spec_factory_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package job_test
import (
"crypto/sha256"
"encoding/json"
"fmt"
"os"
"os/exec"
"testing"
Expand Down Expand Up @@ -31,20 +32,21 @@ func TestWasmFileSpecFactory(t *testing.T) {

rawBinary, err := os.ReadFile(binaryLocation)
require.NoError(t, err)
expected, err := host.GetWorkflowSpec(&host.ModuleConfig{Logger: logger.NullLogger}, rawBinary, config)
expected, err := host.GetWorkflowSpec(&host.ModuleConfig{Logger: logger.NullLogger, IsUncompressed: true}, rawBinary, config)
require.NoError(t, err)

expectedSha := sha256.New()
expectedSha.Write(rawBinary)
require.Equal(t, expectedSha.Sum(config), []byte(actualSha))
expectedSha.Write(config)
require.Equal(t, fmt.Sprintf("%x", expectedSha.Sum(nil)), actualSha)

require.Equal(t, *expected, actual)
}

func createTestBinary(t *testing.T) string {
const testBinaryLocation = "testdata/wasm/testmodule.wasm"

cmd := exec.Command("go", "build", "-o", testBinaryLocation, "github.com/smartcontractkit/chainlink/v2/core/services/job/testdata/wasm")
cmd := exec.Command("go1.22.7", "build", "-o", testBinaryLocation, "github.com/smartcontractkit/chainlink/v2/core/services/job/testdata/wasm")
cmd.Env = append(os.Environ(), "GOOS=wasip1", "GOARCH=wasm")

output, err := cmd.CombinedOutput()
Expand Down
3 changes: 2 additions & 1 deletion core/services/job/yaml_spec_factory_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package job_test

import (
"crypto/sha256"
"fmt"
"testing"

"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -74,5 +75,5 @@ func TestYamlSpecFactory_GetSpec(t *testing.T) {
require.NoError(t, err)

require.Equal(t, expected, actual)
assert.Equal(t, sha256.Sum256([]byte(anyYamlSpec)), actualSha)
assert.Equal(t, fmt.Sprintf("%x", sha256.Sum256([]byte(anyYamlSpec))), actualSha)
}

0 comments on commit a6fd7a0

Please sign in to comment.