Skip to content

Commit

Permalink
chore(lib/runtime): export fields of VersionData
Browse files Browse the repository at this point in the history
- Simplify testing in other packages
- Remove version decode method (just use `scale.Unmarshal` directly)
- Remove clone struct with exported fields for scale codec
- Remove version runtime mockery mock
- Refactor multiple `TestInstance_Version*` in one test with test cases
- Add runtime API items as expected fields in tests
- side-effect: add `Get` prefix to getter methods (sus but better than having messy double-structs-which-make-testing-hard)
  • Loading branch information
qdm12 committed Jul 19, 2022
1 parent b3698d7 commit 358f997
Show file tree
Hide file tree
Showing 20 changed files with 487 additions and 741 deletions.
4 changes: 2 additions & 2 deletions dot/core/messages_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,9 @@ func createExtrinsic(t *testing.T, rt runtime.Instance, genHash common.Hash, non
Era: ctypes.ExtrinsicEra{IsImmortalEra: false},
GenesisHash: ctypes.Hash(genHash),
Nonce: ctypes.NewUCompactFromUInt(nonce),
SpecVersion: ctypes.U32(rv.SpecVersion()),
SpecVersion: ctypes.U32(rv.GetSpecVersion()),
Tip: ctypes.NewUCompactFromUInt(0),
TransactionVersion: ctypes.U32(rv.TransactionVersion()),
TransactionVersion: ctypes.U32(rv.GetTransactionVersion()),
}

// Sign the transaction using Alice's key
Expand Down
10 changes: 5 additions & 5 deletions dot/core/service_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -613,8 +613,8 @@ func TestService_HandleRuntimeChanges(t *testing.T) {
v, err := rt.Version()
require.NoError(t, err)

currSpecVersion := v.SpecVersion() // genesis runtime version.
hash := s.blockState.BestBlockHash() // genesisHash
currSpecVersion := v.GetSpecVersion() // genesis runtime version.
hash := s.blockState.BestBlockHash() // genesisHash

digest := types.NewDigest()
err = digest.Add(types.PreRuntimeDigest{
Expand Down Expand Up @@ -648,7 +648,7 @@ func TestService_HandleRuntimeChanges(t *testing.T) {

v, err = parentRt.Version()
require.NoError(t, err)
require.Equal(t, v.SpecVersion(), currSpecVersion)
require.Equal(t, v.GetSpecVersion(), currSpecVersion)

bhash1 := newBlock1.Header.Hash()
err = s.blockState.HandleRuntimeChanges(ts, parentRt, bhash1)
Expand All @@ -670,14 +670,14 @@ func TestService_HandleRuntimeChanges(t *testing.T) {

v, err = rt.Version()
require.NoError(t, err)
require.Equal(t, v.SpecVersion(), currSpecVersion)
require.Equal(t, v.GetSpecVersion(), currSpecVersion)

rt, err = s.blockState.GetRuntime(&rtUpdateBhash)
require.NoError(t, err)

v, err = rt.Version()
require.NoError(t, err)
require.Equal(t, v.SpecVersion(), updatedSpecVersion)
require.Equal(t, v.GetSpecVersion(), updatedSpecVersion)
}

func TestService_HandleCodeSubstitutes(t *testing.T) {
Expand Down
50 changes: 24 additions & 26 deletions dot/core/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,19 +61,18 @@ func generateExtrinsic(t *testing.T) (extrinsic, externalExtrinsic types.Extrins
t.Helper()
meta := generateTestCentrifugeMetadata(t)

testAPIItem := runtime.APIItem{
Name: [8]byte{1, 2, 3, 4, 5, 6, 7, 8},
Ver: 99,
rv := runtime.VersionData{
SpecName: []byte("polkadot"),
ImplName: []byte("parity-polkadot"),
AuthoringVersion: authoringVersion,
SpecVersion: specVersion,
ImplVersion: implVersion,
APIItems: []runtime.APIItem{{
Name: [8]byte{1, 2, 3, 4, 5, 6, 7, 8},
Ver: 99,
}},
TransactionVersion: transactionVersion,
}
rv := runtime.NewVersionData(
[]byte("polkadot"),
[]byte("parity-polkadot"),
authoringVersion,
specVersion,
implVersion,
[]runtime.APIItem{testAPIItem},
transactionVersion,
)

keyring, err := keystore.NewSr25519Keyring()
require.NoError(t, err)
Expand All @@ -95,9 +94,9 @@ func generateExtrinsic(t *testing.T) (extrinsic, externalExtrinsic types.Extrins
Era: ctypes.ExtrinsicEra{IsImmortalEra: true},
GenesisHash: testGenHash,
Nonce: ctypes.NewUCompactFromUInt(uint64(0)),
SpecVersion: ctypes.U32(rv.SpecVersion()),
SpecVersion: ctypes.U32(rv.GetSpecVersion()),
Tip: ctypes.NewUCompactFromUInt(0),
TransactionVersion: ctypes.U32(rv.TransactionVersion()),
TransactionVersion: ctypes.U32(rv.GetTransactionVersion()),
}

// Sign the transaction using Alice's default account
Expand Down Expand Up @@ -961,19 +960,18 @@ func TestService_DecodeSessionKeys(t *testing.T) {

func TestServiceGetRuntimeVersion(t *testing.T) {
t.Parallel()
testAPIItem := runtime.APIItem{
Name: [8]byte{1, 2, 3, 4, 5, 6, 7, 8},
Ver: 99,
rv := &runtime.VersionData{
SpecName: []byte("polkadot"),
ImplName: []byte("parity-polkadot"),
AuthoringVersion: authoringVersion,
SpecVersion: specVersion,
ImplVersion: implVersion,
APIItems: []runtime.APIItem{{
Name: [8]byte{1, 2, 3, 4, 5, 6, 7, 8},
Ver: 99,
}},
TransactionVersion: transactionVersion,
}
rv := runtime.NewVersionData(
[]byte("polkadot"),
[]byte("parity-polkadot"),
authoringVersion,
specVersion,
implVersion,
[]runtime.APIItem{testAPIItem},
transactionVersion,
)
emptyTrie := trie.NewEmptyTrie()
ts, err := rtstorage.NewTrieState(emptyTrie)
require.NoError(t, err)
Expand Down
18 changes: 3 additions & 15 deletions dot/rpc/modules/api_mocks.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
modulesmocks "github.com/ChainSafe/gossamer/dot/rpc/modules/mocks"
"github.com/ChainSafe/gossamer/dot/types"
"github.com/ChainSafe/gossamer/lib/common"
runtimemocks "github.com/ChainSafe/gossamer/lib/runtime/mocks"
"github.com/ChainSafe/gossamer/lib/runtime"
"github.com/ChainSafe/gossamer/lib/transaction"
"github.com/stretchr/testify/mock"
)
Expand Down Expand Up @@ -63,22 +63,10 @@ func NewMockCoreAPI() *modulesmocks.CoreAPI {
m := new(modulesmocks.CoreAPI)
m.On("InsertKey", mock.AnythingOfType("crypto.Keypair"), mock.AnythingOfType("string")).Return(nil)
m.On("HasKey", mock.AnythingOfType("string"), mock.AnythingOfType("string")).Return(false, nil)
m.On("GetRuntimeVersion", mock.AnythingOfType("*common.Hash")).Return(NewMockVersion(), nil)
m.On("GetRuntimeVersion", mock.AnythingOfType("*common.Hash")).
Return(&runtime.VersionData{SpecName: []byte(`mock-spec`)}, nil)
m.On("IsBlockProducer").Return(false)
m.On("HandleSubmittedExtrinsic", mock.AnythingOfType("types.Extrinsic")).Return(nil)
m.On("GetMetadata", mock.AnythingOfType("*common.Hash")).Return(nil, nil)
return m
}

// NewMockVersion creates and returns an runtime Version interface mock
func NewMockVersion() *runtimemocks.Version {
m := new(runtimemocks.Version)
m.On("SpecName").Return([]byte(`mock-spec`))
m.On("ImplName").Return(nil)
m.On("AuthoringVersion").Return(uint32(0))
m.On("SpecVersion").Return(uint32(0))
m.On("ImplVersion").Return(uint32(0))
m.On("TransactionVersion").Return(uint32(0))
m.On("APIItems").Return(nil)
return m
}
14 changes: 7 additions & 7 deletions dot/rpc/modules/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -300,13 +300,13 @@ func (sm *StateModule) GetRuntimeVersion(
return err
}

res.SpecName = string(rtVersion.SpecName())
res.ImplName = string(rtVersion.ImplName())
res.AuthoringVersion = rtVersion.AuthoringVersion()
res.SpecVersion = rtVersion.SpecVersion()
res.ImplVersion = rtVersion.ImplVersion()
res.TransactionVersion = rtVersion.TransactionVersion()
res.Apis = ConvertAPIs(rtVersion.APIItems())
res.SpecName = string(rtVersion.GetSpecName())
res.ImplName = string(rtVersion.GetImplName())
res.AuthoringVersion = rtVersion.GetAuthoringVersion()
res.SpecVersion = rtVersion.GetSpecVersion()
res.ImplVersion = rtVersion.GetImplVersion()
res.TransactionVersion = rtVersion.GetTransactionVersion()
res.Apis = ConvertAPIs(rtVersion.GetAPIItems())

return nil
}
Expand Down
25 changes: 12 additions & 13 deletions dot/rpc/modules/state_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -447,19 +447,18 @@ func TestStateModuleGetReadProof(t *testing.T) {

func TestStateModuleGetRuntimeVersion(t *testing.T) {
hash := common.MustHexToHash("0x3aa96b0149b6ca3688878bdbd19464448624136398e3ce45b9e755d3ab61355a")
testAPIItem := runtime.APIItem{
Name: [8]byte{1, 2, 3, 4, 5, 6, 7, 8},
Ver: 99,
}
version := runtime.NewVersionData(
[]byte("polkadot"),
[]byte("parity-polkadot"),
0,
25,
0,
[]runtime.APIItem{testAPIItem},
5,
)
version := &runtime.VersionData{
SpecName: []byte("polkadot"),
ImplName: []byte("parity-polkadot"),
AuthoringVersion: 0,
SpecVersion: 25,
ImplVersion: 0,
APIItems: []runtime.APIItem{{
Name: [8]byte{1, 2, 3, 4, 5, 6, 7, 8},
Ver: 99,
}},
TransactionVersion: 5,
}

mockCoreAPI := new(mocks.CoreAPI)
mockCoreAPI.On("GetRuntimeVersion", &hash).Return(version, nil)
Expand Down
28 changes: 14 additions & 14 deletions dot/rpc/subscription/listeners.go
Original file line number Diff line number Diff line change
Expand Up @@ -410,13 +410,13 @@ func (l *RuntimeVersionListener) Listen() {
return
}
ver := modules.StateRuntimeVersionResponse{}
ver.SpecName = string(rtVersion.SpecName())
ver.ImplName = string(rtVersion.ImplName())
ver.AuthoringVersion = rtVersion.AuthoringVersion()
ver.SpecVersion = rtVersion.SpecVersion()
ver.ImplVersion = rtVersion.ImplVersion()
ver.TransactionVersion = rtVersion.TransactionVersion()
ver.Apis = modules.ConvertAPIs(rtVersion.APIItems())
ver.SpecName = string(rtVersion.GetSpecName())
ver.ImplName = string(rtVersion.GetImplName())
ver.AuthoringVersion = rtVersion.GetAuthoringVersion()
ver.SpecVersion = rtVersion.GetSpecVersion()
ver.ImplVersion = rtVersion.GetImplVersion()
ver.TransactionVersion = rtVersion.GetTransactionVersion()
ver.Apis = modules.ConvertAPIs(rtVersion.GetAPIItems())

go l.wsconn.safeSend(newSubscriptionResponse(stateRuntimeVersionMethod, l.subID, ver))

Expand All @@ -430,13 +430,13 @@ func (l *RuntimeVersionListener) Listen() {

ver := modules.StateRuntimeVersionResponse{}

ver.SpecName = string(info.SpecName())
ver.ImplName = string(info.ImplName())
ver.AuthoringVersion = info.AuthoringVersion()
ver.SpecVersion = info.SpecVersion()
ver.ImplVersion = info.ImplVersion()
ver.TransactionVersion = info.TransactionVersion()
ver.Apis = modules.ConvertAPIs(info.APIItems())
ver.SpecName = string(info.GetSpecName())
ver.ImplName = string(info.GetImplName())
ver.AuthoringVersion = info.GetAuthoringVersion()
ver.SpecVersion = info.GetSpecVersion()
ver.ImplVersion = info.GetImplVersion()
ver.TransactionVersion = info.GetTransactionVersion()
ver.Apis = modules.ConvertAPIs(info.GetAPIItems())

l.wsconn.safeSend(newSubscriptionResponse(stateRuntimeVersionMethod, l.subID, ver))
}
Expand Down
2 changes: 1 addition & 1 deletion dot/rpc/subscription/listeners_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -366,7 +366,7 @@ func TestRuntimeChannelListener_Listen(t *testing.T) {
SpecVersion: 25,
ImplVersion: 0,
TransactionVersion: 5,
Apis: modules.ConvertAPIs(version.APIItems()),
Apis: modules.ConvertAPIs(version.GetAPIItems()),
}

expectedUpdateResponse := newSubcriptionBaseResponseJSON()
Expand Down
4 changes: 2 additions & 2 deletions dot/state/block.go
Original file line number Diff line number Diff line change
Expand Up @@ -598,15 +598,15 @@ func (bs *BlockState) HandleRuntimeChanges(newState *rtstorage.TrieState,

// only update runtime during code substitution if runtime SpecVersion is updated
previousVersion, _ := rt.Version()
if previousVersion.SpecVersion() == newVersion.SpecVersion() {
if previousVersion.GetSpecVersion() == newVersion.GetSpecVersion() {
logger.Info("not upgrading runtime code during code substitution")
bs.StoreRuntime(bHash, rt)
return nil
}

logger.Infof(
"🔄 detected runtime code change, upgrading with block %s from previous code hash %s and spec %d to new code hash %s and spec %d...", //nolint:lll
bHash, codeHash, previousVersion.SpecVersion(), currCodeHash, newVersion.SpecVersion())
bHash, codeHash, previousVersion.GetSpecVersion(), currCodeHash, newVersion.GetSpecVersion())
}

rtCfg := &wasmer.Config{
Expand Down
19 changes: 4 additions & 15 deletions dot/state/block_notify_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (

"github.com/ChainSafe/gossamer/dot/types"
"github.com/ChainSafe/gossamer/lib/runtime"
runtimemocks "github.com/ChainSafe/gossamer/lib/runtime/mocks"
"github.com/stretchr/testify/require"
)

Expand Down Expand Up @@ -149,7 +148,10 @@ func TestService_RegisterUnRegisterConcurrentCalls(t *testing.T) {

go func() {
for i := 0; i < 100; i++ {
testVer := NewMockVersion(uint32(i))
testVer := &runtime.VersionData{
SpecName: []byte("mock-spec"),
SpecVersion: uint32(i),
}
go bs.notifyRuntimeUpdated(testVer)
}
}()
Expand All @@ -165,16 +167,3 @@ func TestService_RegisterUnRegisterConcurrentCalls(t *testing.T) {
}()
}
}

// NewMockVersion creates and returns an runtime Version interface mock
func NewMockVersion(specVer uint32) *runtimemocks.Version {
m := new(runtimemocks.Version)
m.On("SpecName").Return([]byte(`mock-spec`))
m.On("ImplName").Return(nil)
m.On("AuthoringVersion").Return(uint32(0))
m.On("SpecVersion").Return(specVer)
m.On("ImplVersion").Return(uint32(0))
m.On("TransactionVersion").Return(uint32(0))
m.On("APIItems").Return(nil)
return m
}
4 changes: 2 additions & 2 deletions lib/babe/build_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -287,9 +287,9 @@ func TestBuildAndApplyExtrinsic(t *testing.T) {
Era: ctypes.ExtrinsicEra{IsImmortalEra: true},
GenesisHash: genHash,
Nonce: ctypes.NewUCompactFromUInt(uint64(0)),
SpecVersion: ctypes.U32(rv.SpecVersion()),
SpecVersion: ctypes.U32(rv.GetSpecVersion()),
Tip: ctypes.NewUCompactFromUInt(0),
TransactionVersion: ctypes.U32(rv.TransactionVersion()),
TransactionVersion: ctypes.U32(rv.GetTransactionVersion()),
}

// Sign the transaction using Alice's default account
Expand Down
Loading

0 comments on commit 358f997

Please sign in to comment.