Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

runtime/parachain: add ValidationCodeByHash and copy tests to wazero #3427

Merged
merged 7 commits into from
Aug 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions dot/core/mock_runtime_instance_test.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 15 additions & 0 deletions dot/state/mocks_runtime_test.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 15 additions & 0 deletions dot/sync/mock_runtime_test.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 15 additions & 0 deletions lib/babe/mocks/runtime.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 15 additions & 0 deletions lib/blocktree/mocks_test.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 15 additions & 0 deletions lib/grandpa/mocks_runtime_test.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions lib/runtime/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@ const (
ParachainHostPersistedValidationData = "ParachainHost_persisted_validation_data"
// ParachainHostValidationCode returns parachain host's validation code
ParachainHostValidationCode = "ParachainHost_validation_code"
// ParachainHostValidationCodeByHash returns parachain host's validation code by hash
ParachainHostValidationCodeByHash = "ParachainHost_validation_code_by_hash"
// ParachainHostValidators is the runtime API call ParachainHost_validators
ParachainHostValidators = "ParachainHost_validators"
// ParachainHostValidatorGroups is the runtime API call ParachainHost_validator_groups
Expand Down
1 change: 1 addition & 0 deletions lib/runtime/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ type Instance interface {
) (*parachaintypes.PersistedValidationData, error)
ParachainHostValidationCode(parachaidID uint32, assumption parachaintypes.OccupiedCoreAssumption,
) (*parachaintypes.ValidationCode, error)
ParachainHostValidationCodeByHash(validationCodeHash common.Hash) (*parachaintypes.ValidationCode, error)
ParachainHostValidators() ([]parachaintypes.ValidatorID, error)
ParachainHostValidatorGroups() (*parachaintypes.ValidatorGroups, error)
ParachainHostAvailabilityCores() (*scale.VaryingDataTypeSlice, error)
Expand Down
15 changes: 15 additions & 0 deletions lib/runtime/mocks/mocks.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

26 changes: 26 additions & 0 deletions lib/runtime/wasmer/exports.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import (
"bytes"
"fmt"

"github.com/ChainSafe/gossamer/lib/common"

parachaintypes "github.com/ChainSafe/gossamer/dot/parachain/types"
"github.com/ChainSafe/gossamer/dot/types"
"github.com/ChainSafe/gossamer/lib/crypto/ed25519"
Expand Down Expand Up @@ -405,6 +407,30 @@ func (in *Instance) ParachainHostValidationCode(parachaidID uint32, assumption p
return validationCode, nil
}

// ParachainHostValidationCodeByHash returns validation code for the given hash.
func (in *Instance) ParachainHostValidationCodeByHash(validationCodeHash common.Hash) (
kanishkatn marked this conversation as resolved.
Show resolved Hide resolved
*parachaintypes.ValidationCode, error) {
buffer := bytes.NewBuffer(nil)
encoder := scale.NewEncoder(buffer)
err := encoder.Encode(validationCodeHash)
if err != nil {
return nil, fmt.Errorf("encoding validation code hash: %w", err)
}

encodedValidationCodeHash, err := in.Exec(runtime.ParachainHostValidationCodeByHash, buffer.Bytes())
if err != nil {
return nil, err
}

var validationCode *parachaintypes.ValidationCode
err = scale.Unmarshal(encodedValidationCodeHash, &validationCode)
if err != nil {
return nil, fmt.Errorf("unmarshalling validation code: %w", err)
}

return validationCode, nil
}

// ParachainHostValidators returns the validator set at the current state.
// The specified validators are responsible for backing parachains for the current state.
func (in *Instance) ParachainHostValidators() ([]parachaintypes.ValidatorID, error) {
Expand Down
31 changes: 27 additions & 4 deletions lib/runtime/wasmer/exports_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,16 +42,18 @@ type Storage struct {
type Data struct {
Storage []Storage `yaml:"storage"`
Expected map[string]string `yaml:"expected"`
Lookups map[string]string `yaml:"-"`
}

var parachainTestData Data

func init() {
err := yaml.Unmarshal([]byte(parachainTestDataRaw), &parachainTestData)
if err != nil {
fmt.Println("Error unmarshaling test data:", err)
fmt.Println("Error unmarshalling test data:", err)
return
}
parachainTestData.Lookups = make(map[string]string)
}

// this is generated by printing key ownership proof while running `test_generate_equivocation_report_blob`
Expand Down Expand Up @@ -1321,7 +1323,23 @@ func TestInstance_ParachainHostValidationCode(t *testing.T) {
require.NoError(t, err)
require.NotEmpty(t, validationCode)

expected := []byte(parachainTestData.Expected["validationCode"])
expected := []byte(parachainTestData.Lookups["validationCode"])
require.Equal(t, expected[4:], []byte(*validationCode))
}

func TestInstance_ParachainHostValidationCodeByHash(t *testing.T) {
kanishkatn marked this conversation as resolved.
Show resolved Hide resolved
t.Parallel()
tt := getParachainHostTrie(t)
rt := NewTestInstanceWithTrie(t, runtime.WESTEND_RUNTIME_v0942, tt)

codeHash := parachainTestData.Lookups["currentCodeHash"]
hash := common.MustHexToHash(codeHash)

validationCode, err := rt.ParachainHostValidationCodeByHash(hash)
require.NoError(t, err)
require.NotEmpty(t, validationCode)

expected := []byte(parachainTestData.Lookups["validationCode"])
require.Equal(t, expected[4:], []byte(*validationCode))
}

Expand Down Expand Up @@ -1553,11 +1571,16 @@ func getParachainHostTrie(t *testing.T) *trie.Trie {
err := tt.Put(key, value)
require.NoError(t, err)

// TODO: We currently have to manually set the storage expected values for the exports tests.
// TODO: We currently have to manually set the storage lookup values for the exports tests.
// We could avoid this if we can lookup the value from the `storage` section of the test data easily.
// If we use ma[string]Storage as the type for the storage section, the tests fail for some reason.
// https://github.com/ChainSafe/gossamer/issues/3405
if s.Name == "validationCode" {
parachainTestData.Expected["validationCode"] = string(value)
parachainTestData.Lookups["validationCode"] = string(value)
}

if s.Name == "currentCodeHash" {
parachainTestData.Lookups["currentCodeHash"] = s.Value
kanishkatn marked this conversation as resolved.
Show resolved Hide resolved
}
}

Expand Down
3 changes: 2 additions & 1 deletion lib/runtime/wasmer/testdata/parachain.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,8 @@ storage:
- key: "0x6a0da05ca59913bc38a8630590f2627c1d3719f5b0b12c7105c073c507445948b6ff6f7d467b87a9e8030000"
value: "0x08d007000049080000"
# paras.currentCodeHash[paraId=1000]
- key: 0xcd710b30bd2eab0352ddcc26417aa194e2d1c22ba0a888147714a3487bd51c63b6ff6f7d467b87a9e8030000
- name: currentCodeHash
key: "0xcd710b30bd2eab0352ddcc26417aa194e2d1c22ba0a888147714a3487bd51c63b6ff6f7d467b87a9e8030000"
value: "0xcafdc44448ed62f5f67e5c340b420c51fe3bee7c1577f9fa0e819383b5145337"
# paras.codeByHash
- name: "validationCode"
Expand Down
24 changes: 24 additions & 0 deletions lib/runtime/wazero/instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -1036,6 +1036,30 @@ func (in *Instance) ParachainHostSessionInfo(sessionIndex parachaintypes.Session
return sessionInfo, nil
}

// ParachainHostValidationCodeByHash returns validation code for the given hash.
func (in *Instance) ParachainHostValidationCodeByHash(validationCodeHash common.Hash) (
*parachaintypes.ValidationCode, error) {
buffer := bytes.NewBuffer(nil)
encoder := scale.NewEncoder(buffer)
err := encoder.Encode(validationCodeHash)
if err != nil {
return nil, fmt.Errorf("encoding validation code hash: %w", err)
}

encodedValidationCodeHash, err := in.Exec(runtime.ParachainHostValidationCodeByHash, buffer.Bytes())
if err != nil {
return nil, err
}

var validationCode *parachaintypes.ValidationCode
err = scale.Unmarshal(encodedValidationCodeHash, &validationCode)
if err != nil {
return nil, fmt.Errorf("unmarshalling validation code: %w", err)
}

return validationCode, nil
}

func (*Instance) RandomSeed() {
panic("unimplemented")
}
Expand Down
Loading
Loading