Skip to content

Commit 358f997

Browse files
committed
chore(lib/runtime): export fields of VersionData
- 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)
1 parent b3698d7 commit 358f997

20 files changed

+487
-741
lines changed

dot/core/messages_integration_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,9 @@ func createExtrinsic(t *testing.T, rt runtime.Instance, genHash common.Hash, non
5353
Era: ctypes.ExtrinsicEra{IsImmortalEra: false},
5454
GenesisHash: ctypes.Hash(genHash),
5555
Nonce: ctypes.NewUCompactFromUInt(nonce),
56-
SpecVersion: ctypes.U32(rv.SpecVersion()),
56+
SpecVersion: ctypes.U32(rv.GetSpecVersion()),
5757
Tip: ctypes.NewUCompactFromUInt(0),
58-
TransactionVersion: ctypes.U32(rv.TransactionVersion()),
58+
TransactionVersion: ctypes.U32(rv.GetTransactionVersion()),
5959
}
6060

6161
// Sign the transaction using Alice's key

dot/core/service_integration_test.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -613,8 +613,8 @@ func TestService_HandleRuntimeChanges(t *testing.T) {
613613
v, err := rt.Version()
614614
require.NoError(t, err)
615615

616-
currSpecVersion := v.SpecVersion() // genesis runtime version.
617-
hash := s.blockState.BestBlockHash() // genesisHash
616+
currSpecVersion := v.GetSpecVersion() // genesis runtime version.
617+
hash := s.blockState.BestBlockHash() // genesisHash
618618

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

649649
v, err = parentRt.Version()
650650
require.NoError(t, err)
651-
require.Equal(t, v.SpecVersion(), currSpecVersion)
651+
require.Equal(t, v.GetSpecVersion(), currSpecVersion)
652652

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

671671
v, err = rt.Version()
672672
require.NoError(t, err)
673-
require.Equal(t, v.SpecVersion(), currSpecVersion)
673+
require.Equal(t, v.GetSpecVersion(), currSpecVersion)
674674

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

678678
v, err = rt.Version()
679679
require.NoError(t, err)
680-
require.Equal(t, v.SpecVersion(), updatedSpecVersion)
680+
require.Equal(t, v.GetSpecVersion(), updatedSpecVersion)
681681
}
682682

683683
func TestService_HandleCodeSubstitutes(t *testing.T) {

dot/core/service_test.go

Lines changed: 24 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -61,19 +61,18 @@ func generateExtrinsic(t *testing.T) (extrinsic, externalExtrinsic types.Extrins
6161
t.Helper()
6262
meta := generateTestCentrifugeMetadata(t)
6363

64-
testAPIItem := runtime.APIItem{
65-
Name: [8]byte{1, 2, 3, 4, 5, 6, 7, 8},
66-
Ver: 99,
64+
rv := runtime.VersionData{
65+
SpecName: []byte("polkadot"),
66+
ImplName: []byte("parity-polkadot"),
67+
AuthoringVersion: authoringVersion,
68+
SpecVersion: specVersion,
69+
ImplVersion: implVersion,
70+
APIItems: []runtime.APIItem{{
71+
Name: [8]byte{1, 2, 3, 4, 5, 6, 7, 8},
72+
Ver: 99,
73+
}},
74+
TransactionVersion: transactionVersion,
6775
}
68-
rv := runtime.NewVersionData(
69-
[]byte("polkadot"),
70-
[]byte("parity-polkadot"),
71-
authoringVersion,
72-
specVersion,
73-
implVersion,
74-
[]runtime.APIItem{testAPIItem},
75-
transactionVersion,
76-
)
7776

7877
keyring, err := keystore.NewSr25519Keyring()
7978
require.NoError(t, err)
@@ -95,9 +94,9 @@ func generateExtrinsic(t *testing.T) (extrinsic, externalExtrinsic types.Extrins
9594
Era: ctypes.ExtrinsicEra{IsImmortalEra: true},
9695
GenesisHash: testGenHash,
9796
Nonce: ctypes.NewUCompactFromUInt(uint64(0)),
98-
SpecVersion: ctypes.U32(rv.SpecVersion()),
97+
SpecVersion: ctypes.U32(rv.GetSpecVersion()),
9998
Tip: ctypes.NewUCompactFromUInt(0),
100-
TransactionVersion: ctypes.U32(rv.TransactionVersion()),
99+
TransactionVersion: ctypes.U32(rv.GetTransactionVersion()),
101100
}
102101

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

962961
func TestServiceGetRuntimeVersion(t *testing.T) {
963962
t.Parallel()
964-
testAPIItem := runtime.APIItem{
965-
Name: [8]byte{1, 2, 3, 4, 5, 6, 7, 8},
966-
Ver: 99,
963+
rv := &runtime.VersionData{
964+
SpecName: []byte("polkadot"),
965+
ImplName: []byte("parity-polkadot"),
966+
AuthoringVersion: authoringVersion,
967+
SpecVersion: specVersion,
968+
ImplVersion: implVersion,
969+
APIItems: []runtime.APIItem{{
970+
Name: [8]byte{1, 2, 3, 4, 5, 6, 7, 8},
971+
Ver: 99,
972+
}},
973+
TransactionVersion: transactionVersion,
967974
}
968-
rv := runtime.NewVersionData(
969-
[]byte("polkadot"),
970-
[]byte("parity-polkadot"),
971-
authoringVersion,
972-
specVersion,
973-
implVersion,
974-
[]runtime.APIItem{testAPIItem},
975-
transactionVersion,
976-
)
977975
emptyTrie := trie.NewEmptyTrie()
978976
ts, err := rtstorage.NewTrieState(emptyTrie)
979977
require.NoError(t, err)

dot/rpc/modules/api_mocks.go

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import (
77
modulesmocks "github.com/ChainSafe/gossamer/dot/rpc/modules/mocks"
88
"github.com/ChainSafe/gossamer/dot/types"
99
"github.com/ChainSafe/gossamer/lib/common"
10-
runtimemocks "github.com/ChainSafe/gossamer/lib/runtime/mocks"
10+
"github.com/ChainSafe/gossamer/lib/runtime"
1111
"github.com/ChainSafe/gossamer/lib/transaction"
1212
"github.com/stretchr/testify/mock"
1313
)
@@ -63,22 +63,10 @@ func NewMockCoreAPI() *modulesmocks.CoreAPI {
6363
m := new(modulesmocks.CoreAPI)
6464
m.On("InsertKey", mock.AnythingOfType("crypto.Keypair"), mock.AnythingOfType("string")).Return(nil)
6565
m.On("HasKey", mock.AnythingOfType("string"), mock.AnythingOfType("string")).Return(false, nil)
66-
m.On("GetRuntimeVersion", mock.AnythingOfType("*common.Hash")).Return(NewMockVersion(), nil)
66+
m.On("GetRuntimeVersion", mock.AnythingOfType("*common.Hash")).
67+
Return(&runtime.VersionData{SpecName: []byte(`mock-spec`)}, nil)
6768
m.On("IsBlockProducer").Return(false)
6869
m.On("HandleSubmittedExtrinsic", mock.AnythingOfType("types.Extrinsic")).Return(nil)
6970
m.On("GetMetadata", mock.AnythingOfType("*common.Hash")).Return(nil, nil)
7071
return m
7172
}
72-
73-
// NewMockVersion creates and returns an runtime Version interface mock
74-
func NewMockVersion() *runtimemocks.Version {
75-
m := new(runtimemocks.Version)
76-
m.On("SpecName").Return([]byte(`mock-spec`))
77-
m.On("ImplName").Return(nil)
78-
m.On("AuthoringVersion").Return(uint32(0))
79-
m.On("SpecVersion").Return(uint32(0))
80-
m.On("ImplVersion").Return(uint32(0))
81-
m.On("TransactionVersion").Return(uint32(0))
82-
m.On("APIItems").Return(nil)
83-
return m
84-
}

dot/rpc/modules/state.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -300,13 +300,13 @@ func (sm *StateModule) GetRuntimeVersion(
300300
return err
301301
}
302302

303-
res.SpecName = string(rtVersion.SpecName())
304-
res.ImplName = string(rtVersion.ImplName())
305-
res.AuthoringVersion = rtVersion.AuthoringVersion()
306-
res.SpecVersion = rtVersion.SpecVersion()
307-
res.ImplVersion = rtVersion.ImplVersion()
308-
res.TransactionVersion = rtVersion.TransactionVersion()
309-
res.Apis = ConvertAPIs(rtVersion.APIItems())
303+
res.SpecName = string(rtVersion.GetSpecName())
304+
res.ImplName = string(rtVersion.GetImplName())
305+
res.AuthoringVersion = rtVersion.GetAuthoringVersion()
306+
res.SpecVersion = rtVersion.GetSpecVersion()
307+
res.ImplVersion = rtVersion.GetImplVersion()
308+
res.TransactionVersion = rtVersion.GetTransactionVersion()
309+
res.Apis = ConvertAPIs(rtVersion.GetAPIItems())
310310

311311
return nil
312312
}

dot/rpc/modules/state_test.go

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -447,19 +447,18 @@ func TestStateModuleGetReadProof(t *testing.T) {
447447

448448
func TestStateModuleGetRuntimeVersion(t *testing.T) {
449449
hash := common.MustHexToHash("0x3aa96b0149b6ca3688878bdbd19464448624136398e3ce45b9e755d3ab61355a")
450-
testAPIItem := runtime.APIItem{
451-
Name: [8]byte{1, 2, 3, 4, 5, 6, 7, 8},
452-
Ver: 99,
453-
}
454-
version := runtime.NewVersionData(
455-
[]byte("polkadot"),
456-
[]byte("parity-polkadot"),
457-
0,
458-
25,
459-
0,
460-
[]runtime.APIItem{testAPIItem},
461-
5,
462-
)
450+
version := &runtime.VersionData{
451+
SpecName: []byte("polkadot"),
452+
ImplName: []byte("parity-polkadot"),
453+
AuthoringVersion: 0,
454+
SpecVersion: 25,
455+
ImplVersion: 0,
456+
APIItems: []runtime.APIItem{{
457+
Name: [8]byte{1, 2, 3, 4, 5, 6, 7, 8},
458+
Ver: 99,
459+
}},
460+
TransactionVersion: 5,
461+
}
463462

464463
mockCoreAPI := new(mocks.CoreAPI)
465464
mockCoreAPI.On("GetRuntimeVersion", &hash).Return(version, nil)

dot/rpc/subscription/listeners.go

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -410,13 +410,13 @@ func (l *RuntimeVersionListener) Listen() {
410410
return
411411
}
412412
ver := modules.StateRuntimeVersionResponse{}
413-
ver.SpecName = string(rtVersion.SpecName())
414-
ver.ImplName = string(rtVersion.ImplName())
415-
ver.AuthoringVersion = rtVersion.AuthoringVersion()
416-
ver.SpecVersion = rtVersion.SpecVersion()
417-
ver.ImplVersion = rtVersion.ImplVersion()
418-
ver.TransactionVersion = rtVersion.TransactionVersion()
419-
ver.Apis = modules.ConvertAPIs(rtVersion.APIItems())
413+
ver.SpecName = string(rtVersion.GetSpecName())
414+
ver.ImplName = string(rtVersion.GetImplName())
415+
ver.AuthoringVersion = rtVersion.GetAuthoringVersion()
416+
ver.SpecVersion = rtVersion.GetSpecVersion()
417+
ver.ImplVersion = rtVersion.GetImplVersion()
418+
ver.TransactionVersion = rtVersion.GetTransactionVersion()
419+
ver.Apis = modules.ConvertAPIs(rtVersion.GetAPIItems())
420420

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

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

431431
ver := modules.StateRuntimeVersionResponse{}
432432

433-
ver.SpecName = string(info.SpecName())
434-
ver.ImplName = string(info.ImplName())
435-
ver.AuthoringVersion = info.AuthoringVersion()
436-
ver.SpecVersion = info.SpecVersion()
437-
ver.ImplVersion = info.ImplVersion()
438-
ver.TransactionVersion = info.TransactionVersion()
439-
ver.Apis = modules.ConvertAPIs(info.APIItems())
433+
ver.SpecName = string(info.GetSpecName())
434+
ver.ImplName = string(info.GetImplName())
435+
ver.AuthoringVersion = info.GetAuthoringVersion()
436+
ver.SpecVersion = info.GetSpecVersion()
437+
ver.ImplVersion = info.GetImplVersion()
438+
ver.TransactionVersion = info.GetTransactionVersion()
439+
ver.Apis = modules.ConvertAPIs(info.GetAPIItems())
440440

441441
l.wsconn.safeSend(newSubscriptionResponse(stateRuntimeVersionMethod, l.subID, ver))
442442
}

dot/rpc/subscription/listeners_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -366,7 +366,7 @@ func TestRuntimeChannelListener_Listen(t *testing.T) {
366366
SpecVersion: 25,
367367
ImplVersion: 0,
368368
TransactionVersion: 5,
369-
Apis: modules.ConvertAPIs(version.APIItems()),
369+
Apis: modules.ConvertAPIs(version.GetAPIItems()),
370370
}
371371

372372
expectedUpdateResponse := newSubcriptionBaseResponseJSON()

dot/state/block.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -598,15 +598,15 @@ func (bs *BlockState) HandleRuntimeChanges(newState *rtstorage.TrieState,
598598

599599
// only update runtime during code substitution if runtime SpecVersion is updated
600600
previousVersion, _ := rt.Version()
601-
if previousVersion.SpecVersion() == newVersion.SpecVersion() {
601+
if previousVersion.GetSpecVersion() == newVersion.GetSpecVersion() {
602602
logger.Info("not upgrading runtime code during code substitution")
603603
bs.StoreRuntime(bHash, rt)
604604
return nil
605605
}
606606

607607
logger.Infof(
608608
"🔄 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
609-
bHash, codeHash, previousVersion.SpecVersion(), currCodeHash, newVersion.SpecVersion())
609+
bHash, codeHash, previousVersion.GetSpecVersion(), currCodeHash, newVersion.GetSpecVersion())
610610
}
611611

612612
rtCfg := &wasmer.Config{

dot/state/block_notify_test.go

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import (
1010

1111
"github.com/ChainSafe/gossamer/dot/types"
1212
"github.com/ChainSafe/gossamer/lib/runtime"
13-
runtimemocks "github.com/ChainSafe/gossamer/lib/runtime/mocks"
1413
"github.com/stretchr/testify/require"
1514
)
1615

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

150149
go func() {
151150
for i := 0; i < 100; i++ {
152-
testVer := NewMockVersion(uint32(i))
151+
testVer := &runtime.VersionData{
152+
SpecName: []byte("mock-spec"),
153+
SpecVersion: uint32(i),
154+
}
153155
go bs.notifyRuntimeUpdated(testVer)
154156
}
155157
}()
@@ -165,16 +167,3 @@ func TestService_RegisterUnRegisterConcurrentCalls(t *testing.T) {
165167
}()
166168
}
167169
}
168-
169-
// NewMockVersion creates and returns an runtime Version interface mock
170-
func NewMockVersion(specVer uint32) *runtimemocks.Version {
171-
m := new(runtimemocks.Version)
172-
m.On("SpecName").Return([]byte(`mock-spec`))
173-
m.On("ImplName").Return(nil)
174-
m.On("AuthoringVersion").Return(uint32(0))
175-
m.On("SpecVersion").Return(specVer)
176-
m.On("ImplVersion").Return(uint32(0))
177-
m.On("TransactionVersion").Return(uint32(0))
178-
m.On("APIItems").Return(nil)
179-
return m
180-
}

lib/babe/build_integration_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -287,9 +287,9 @@ func TestBuildAndApplyExtrinsic(t *testing.T) {
287287
Era: ctypes.ExtrinsicEra{IsImmortalEra: true},
288288
GenesisHash: genHash,
289289
Nonce: ctypes.NewUCompactFromUInt(uint64(0)),
290-
SpecVersion: ctypes.U32(rv.SpecVersion()),
290+
SpecVersion: ctypes.U32(rv.GetSpecVersion()),
291291
Tip: ctypes.NewUCompactFromUInt(0),
292-
TransactionVersion: ctypes.U32(rv.TransactionVersion()),
292+
TransactionVersion: ctypes.U32(rv.GetTransactionVersion()),
293293
}
294294

295295
// Sign the transaction using Alice's default account

0 commit comments

Comments
 (0)