Skip to content

Commit

Permalink
update: enable sudo guards and update tests
Browse files Browse the repository at this point in the history
  • Loading branch information
radkomih committed Jun 7, 2024
1 parent 2f94012 commit 4796de4
Show file tree
Hide file tree
Showing 25 changed files with 265 additions and 179 deletions.
Binary file modified build/runtime-benchmarks.wasm
Binary file not shown.
Binary file modified build/runtime.wasm
Binary file not shown.
8 changes: 8 additions & 0 deletions docs/docs/development/test.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,14 @@ permalink: /development/test
Currently, the project contains unit and integration tests. Integration tests use [Gossamer](https://github.com/LimeChain/gossamer), which
imports all the necessary Host functions and interacts with the Runtime.

Before running the tests, make sure to build the runtime with the benchmarking features enabled, as there are some tests that rely on them.

````bash
make build-benchmarking
````

And then run the tests with:

```bash
make test
```
Expand Down
11 changes: 5 additions & 6 deletions frame/babe/call_plan_config_change.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (

sc "github.com/LimeChain/goscale"
"github.com/LimeChain/gosemble/frame/support"
"github.com/LimeChain/gosemble/frame/system"
babetypes "github.com/LimeChain/gosemble/primitives/babe"
primitives "github.com/LimeChain/gosemble/primitives/types"
)
Expand Down Expand Up @@ -80,12 +81,10 @@ func (_ callPlanConfigChange) PaysFee(baseWeight primitives.Weight) primitives.P
}

func (c callPlanConfigChange) Dispatch(origin primitives.RuntimeOrigin, args sc.VaryingData) (primitives.PostDispatchInfo, error) {
// TODO: enable once 'sudo' module is implemented
//
// err := EnsureRoot(origin)
// if err != nil {
// return primitives.PostDispatchInfo{}, err
// }
err := system.EnsureRoot(origin)
if err != nil {
return primitives.PostDispatchInfo{}, err

Check warning on line 86 in frame/babe/call_plan_config_change.go

View check run for this annotation

Codecov / codecov/patch

frame/babe/call_plan_config_change.go#L86

Added line #L86 was not covered by tests
}

config := args[0].(NextConfigDescriptor)

Expand Down
10 changes: 4 additions & 6 deletions frame/system/call_authorize_upgrade.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,12 +74,10 @@ func (_ callAuthorizeUpgrade) PaysFee(baseWeight primitives.Weight) primitives.P
}

func (c callAuthorizeUpgrade) Dispatch(origin primitives.RuntimeOrigin, args sc.VaryingData) (primitives.PostDispatchInfo, error) {
// TODO: enable once 'sudo' module is implemented
//
// err := EnsureRoot(origin)
// if err != nil {
// return err
// }
err := EnsureRoot(origin)
if err != nil {
return primitives.PostDispatchInfo{}, err

Check warning on line 79 in frame/system/call_authorize_upgrade.go

View check run for this annotation

Codecov / codecov/patch

frame/system/call_authorize_upgrade.go#L79

Added line #L79 was not covered by tests
}

codeHash := args[0].(primitives.H256)

Expand Down
10 changes: 4 additions & 6 deletions frame/system/call_authorize_upgrade_without_checks.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,12 +74,10 @@ func (_ callAuthorizeUpgradeWithoutChecks) PaysFee(baseWeight primitives.Weight)
}

func (c callAuthorizeUpgradeWithoutChecks) Dispatch(origin primitives.RuntimeOrigin, args sc.VaryingData) (primitives.PostDispatchInfo, error) {
// TODO: enable once 'sudo' module is implemented
//
// err := EnsureRoot(origin)
// if err != nil {
// return err
// }
err := EnsureRoot(origin)
if err != nil {
return primitives.PostDispatchInfo{}, err

Check warning on line 79 in frame/system/call_authorize_upgrade_without_checks.go

View check run for this annotation

Codecov / codecov/patch

frame/system/call_authorize_upgrade_without_checks.go#L79

Added line #L79 was not covered by tests
}

codeHash := args[0].(primitives.H256)

Expand Down
10 changes: 4 additions & 6 deletions frame/system/call_kill_prefix.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,12 +81,10 @@ func (_ callKillPrefix) PaysFee(baseWeight primitives.Weight) primitives.Pays {
}

func (c callKillPrefix) Dispatch(origin primitives.RuntimeOrigin, args sc.VaryingData) (primitives.PostDispatchInfo, error) {
// TODO: enable once 'sudo' module is implemented
//
// err := EnsureRoot(origin)
// if err != nil {
// return primitives.PostDispatchInfo{}, err
// }
err := EnsureRoot(origin)
if err != nil {
return primitives.PostDispatchInfo{}, err

Check warning on line 86 in frame/system/call_kill_prefix.go

View check run for this annotation

Codecov / codecov/patch

frame/system/call_kill_prefix.go#L86

Added line #L86 was not covered by tests
}

prefix := args[0].(sc.Sequence[sc.U8])
subkeys := args[1].(sc.U32)
Expand Down
10 changes: 4 additions & 6 deletions frame/system/call_kill_storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,12 +77,10 @@ func (_ callKillStorage) PaysFee(baseWeight primitives.Weight) primitives.Pays {
}

func (c callKillStorage) Dispatch(origin primitives.RuntimeOrigin, args sc.VaryingData) (primitives.PostDispatchInfo, error) {
// TODO: enable once 'sudo' module is implemented
//
// err := EnsureRoot(origin)
// if err != nil {
// return primitives.PostDispatchInfo{}, err
// }
err := EnsureRoot(origin)
if err != nil {
return primitives.PostDispatchInfo{}, err

Check warning on line 82 in frame/system/call_kill_storage.go

View check run for this annotation

Codecov / codecov/patch

frame/system/call_kill_storage.go#L82

Added line #L82 was not covered by tests
}

keys := args[0].(sc.Sequence[sc.Sequence[sc.U8]])

Expand Down
12 changes: 5 additions & 7 deletions frame/system/call_set_code.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,16 +86,14 @@ func (_ callSetCode) PaysFee(baseWeight primitives.Weight) primitives.Pays {
}

func (c callSetCode) Dispatch(origin primitives.RuntimeOrigin, args sc.VaryingData) (primitives.PostDispatchInfo, error) {
// TODO: enable once 'sudo' module is implemented
//
// err := EnsureRoot(origin)
// if err != nil {
// return primitives.PostDispatchInfo{}, err
// }
err := EnsureRoot(origin)
if err != nil {
return primitives.PostDispatchInfo{}, err

Check warning on line 91 in frame/system/call_set_code.go

View check run for this annotation

Codecov / codecov/patch

frame/system/call_set_code.go#L91

Added line #L91 was not covered by tests
}

codeBlob := args[0].(sc.Sequence[sc.U8])

err := c.codeUpgrader.CanSetCode(codeBlob)
err = c.codeUpgrader.CanSetCode(codeBlob)
if err != nil {
return primitives.PostDispatchInfo{}, err
}
Expand Down
12 changes: 5 additions & 7 deletions frame/system/call_set_code_without_checks.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,16 +83,14 @@ func (_ callSetCodeWithoutChecks) PaysFee(baseWeight primitives.Weight) primitiv
}

func (c callSetCodeWithoutChecks) Dispatch(origin primitives.RuntimeOrigin, args sc.VaryingData) (primitives.PostDispatchInfo, error) {
// TODO: enable once 'sudo' module is implemented
//
// err := EnsureRoot(origin)
// if err != nil {
// return primitives.PostDispatchInfo{}, err
// }
err := EnsureRoot(origin)
if err != nil {
return primitives.PostDispatchInfo{}, err

Check warning on line 88 in frame/system/call_set_code_without_checks.go

View check run for this annotation

Codecov / codecov/patch

frame/system/call_set_code_without_checks.go#L88

Added line #L88 was not covered by tests
}

codeBlob := args[0].(sc.Sequence[sc.U8])

err := c.hookOnSetCode.SetCode(codeBlob)
err = c.hookOnSetCode.SetCode(codeBlob)
if err != nil {
return primitives.PostDispatchInfo{}, err
}
Expand Down
10 changes: 4 additions & 6 deletions frame/system/call_set_heap_pages.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,12 +83,10 @@ func (_ callSetHeapPages) PaysFee(baseWeight primitives.Weight) primitives.Pays
}

func (c callSetHeapPages) Dispatch(origin primitives.RuntimeOrigin, args sc.VaryingData) (primitives.PostDispatchInfo, error) {
// TODO: enable once 'sudo' module is implemented
//
// err := EnsureRoot(origin)
// if err != nil {
// return primitives.PostDispatchInfo{}, err
// }
err := EnsureRoot(origin)
if err != nil {
return primitives.PostDispatchInfo{}, err

Check warning on line 88 in frame/system/call_set_heap_pages.go

View check run for this annotation

Codecov / codecov/patch

frame/system/call_set_heap_pages.go#L88

Added line #L88 was not covered by tests
}

pages := args[0].(sc.U64)

Expand Down
10 changes: 4 additions & 6 deletions frame/system/call_set_storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,12 +77,10 @@ func (_ callSetStorage) PaysFee(baseWeight primitives.Weight) primitives.Pays {
}

func (c callSetStorage) Dispatch(origin primitives.RuntimeOrigin, args sc.VaryingData) (primitives.PostDispatchInfo, error) {
// TODO: enable once 'sudo' module is implemented
//
// err := EnsureRoot(origin)
// if err != nil {
// return primitives.PostDispatchInfo{}, err
// }
err := EnsureRoot(origin)
if err != nil {
return primitives.PostDispatchInfo{}, err

Check warning on line 82 in frame/system/call_set_storage.go

View check run for this annotation

Codecov / codecov/patch

frame/system/call_set_storage.go#L82

Added line #L82 was not covered by tests
}

items := args[0].(sc.Sequence[KeyValue])

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,14 @@ func Test_ApplyAuthorizedUpgrade_DispatchOutcome(t *testing.T) {
assert.NoError(t, err)
codeHash := common.MustBlake2bHash(codeSpecVersion101)

call, err := ctypes.NewCall(metadata, "System.authorize_upgrade", codeHash)
// Set Sudo Key
err = (*storage).Put(append(testhelpers.KeySudoHash, testhelpers.KeyKeyHash...), signature.TestKeyringPairAlice.PublicKey)
assert.NoError(t, err)

callArg, err := ctypes.NewCall(metadata, "System.authorize_upgrade", codeHash)
assert.NoError(t, err)

call, err := ctypes.NewCall(metadata, "Sudo.sudo", callArg)
assert.NoError(t, err)

extrinsic := ctypes.NewExtrinsic(call)
Expand Down
11 changes: 9 additions & 2 deletions runtime/templates/poa/system_authorize_upgrade_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,20 @@ func Test_AuthorizeUpgrade_DispatchOutcome(t *testing.T) {
runtimeVersion, err := rt.Version()
assert.NoError(t, err)

// Set Sudo Key
err = (*storage).Put(append(testhelpers.KeySudoHash, testhelpers.KeyKeyHash...), signature.TestKeyringPairAlice.PublicKey)
assert.NoError(t, err)

testhelpers.InitializeBlock(t, rt, testhelpers.ParentHash, testhelpers.StateRoot, testhelpers.ExtrinsicsRoot, testhelpers.BlockNumber)

codeSpecVersion101, err := os.ReadFile(testhelpers.RuntimeWasmSpecVersion101)
assert.NoError(t, err)
codeHash := common.MustBlake2bHash(codeSpecVersion101)

call, err := ctypes.NewCall(metadata, "System.authorize_upgrade", codeHash)
callArg, err := ctypes.NewCall(metadata, "System.authorize_upgrade", codeHash)
assert.NoError(t, err)

call, err := ctypes.NewCall(metadata, "Sudo.sudo", callArg)
assert.NoError(t, err)

extrinsic := ctypes.NewExtrinsic(call)
Expand Down Expand Up @@ -67,7 +74,7 @@ func Test_AuthorizeUpgrade_DispatchOutcome(t *testing.T) {

decodedCount, err := sc.DecodeCompact[sc.U32](buffer)
assert.NoError(t, err)
assert.Equal(t, sc.U32(3), decodedCount.Number)
assert.Equal(t, sc.U32(4), decodedCount.Number)

testhelpers.AssertEmittedSystemEvent(t, system.EventUpgradeAuthorized, buffer)

Expand Down
9 changes: 8 additions & 1 deletion runtime/templates/poa/system_kill_prefix_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,19 @@ func Test_KillPrefix_DispatchOutcome(t *testing.T) {
runtimeVersion, err := rt.Version()
assert.NoError(t, err)

// Set Sudo Key
err = (*storage).Put(append(testhelpers.KeySudoHash, testhelpers.KeyKeyHash...), signature.TestKeyringPairAlice.PublicKey)
assert.NoError(t, err)

testhelpers.InitializeBlock(t, rt, testhelpers.ParentHash, testhelpers.StateRoot, testhelpers.ExtrinsicsRoot, testhelpers.BlockNumber)

prefix := []byte("test")
limit := uint32(2)

call, err := ctypes.NewCall(metadata, "System.kill_prefix", prefix, limit)
callArg, err := ctypes.NewCall(metadata, "System.kill_prefix", prefix, limit)
assert.NoError(t, err)

call, err := ctypes.NewCall(metadata, "Sudo.sudo", callArg)
assert.NoError(t, err)

extrinsic := ctypes.NewExtrinsic(call)
Expand Down
9 changes: 8 additions & 1 deletion runtime/templates/poa/system_kill_storage_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,21 @@ func Test_KillStorage_DispatchOutcome(t *testing.T) {
runtimeVersion, err := rt.Version()
assert.NoError(t, err)

// Set Sudo Key
err = (*storage).Put(append(testhelpers.KeySudoHash, testhelpers.KeyKeyHash...), signature.TestKeyringPairAlice.PublicKey)
assert.NoError(t, err)

testhelpers.InitializeBlock(t, rt, testhelpers.ParentHash, testhelpers.StateRoot, testhelpers.ExtrinsicsRoot, testhelpers.BlockNumber)

keys := [][]byte{
[]byte("testkey1"),
[]byte("testkey2"),
}

call, err := ctypes.NewCall(metadata, "System.kill_storage", keys)
callArg, err := ctypes.NewCall(metadata, "System.kill_storage", keys)
assert.NoError(t, err)

call, err := ctypes.NewCall(metadata, "Sudo.sudo", callArg)
assert.NoError(t, err)

extrinsic := ctypes.NewExtrinsic(call)
Expand Down
Loading

0 comments on commit 4796de4

Please sign in to comment.