-
Notifications
You must be signed in to change notification settings - Fork 146
fix(core): fix txn pool for latest runtime #2809
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
Changes from all commits
62748b0
1d651bc
b5e7dee
c2f3017
e66f967
811f964
9a83c71
453c50c
51b331e
621514f
774f976
7028dc2
2cf6d8e
f45c1f9
f87fbdd
092ba0f
7dc03d9
0890725
4c98707
f7645b2
ec61473
3f87fbd
a8a282d
fe26434
0259b40
61246d7
8a9e268
e6c06b1
fd7f33f
017ce25
a1f7d0e
e776bb6
e57e70e
93ca14b
3ac1392
472a21b
52e58f0
9b3fb44
7e69fd6
763941f
79adb4b
57de89d
9b172fe
19bb3c7
0431bfb
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,9 +4,11 @@ | |
| package core | ||
|
|
||
| import ( | ||
| "bytes" | ||
| "path/filepath" | ||
| "testing" | ||
|
|
||
| "github.com/ChainSafe/gossamer/dot/network" | ||
| "github.com/ChainSafe/gossamer/dot/state" | ||
| "github.com/ChainSafe/gossamer/dot/types" | ||
| "github.com/ChainSafe/gossamer/internal/log" | ||
|
|
@@ -19,10 +21,133 @@ import ( | |
| "github.com/ChainSafe/gossamer/lib/runtime/wasmer" | ||
| "github.com/ChainSafe/gossamer/lib/trie" | ||
| "github.com/ChainSafe/gossamer/lib/utils" | ||
| "github.com/ChainSafe/gossamer/pkg/scale" | ||
| "github.com/golang/mock/gomock" | ||
| "github.com/stretchr/testify/require" | ||
| ) | ||
|
|
||
| func balanceKey(t *testing.T, pub []byte) (bKey []byte) { | ||
| t.Helper() | ||
|
|
||
| h0, err := common.Twox128Hash([]byte("System")) | ||
| require.NoError(t, err) | ||
| h1, err := common.Twox128Hash([]byte("Account")) | ||
| require.NoError(t, err) | ||
| h2, err := common.Blake2b128(pub) | ||
| require.NoError(t, err) | ||
| return bytes.Join([][]byte{h0, h1, h2, pub}, nil) | ||
| } | ||
|
|
||
| // Creates test service, used now for testing txnPool but can be used elsewhere when needed | ||
| func createTestService(t *testing.T, genesisFilePath string, | ||
| pubKey []byte, accountInfo types.AccountInfo, ctrl *gomock.Controller) (service *Service, encodedExtrinsic []byte) { | ||
| t.Helper() | ||
|
|
||
| gen, err := genesis.NewGenesisFromJSONRaw(genesisFilePath) | ||
| require.NoError(t, err) | ||
|
|
||
| genesisTrie, err := wasmer.NewTrieFromGenesis(*gen) | ||
| require.NoError(t, err) | ||
|
|
||
| // Extrinsic and context related stuff | ||
| aliceBalanceKey := balanceKey(t, pubKey) | ||
| encodedAccountInfo, err := scale.Marshal(accountInfo) | ||
| require.NoError(t, err) | ||
|
|
||
| genesisHeader := &types.Header{ | ||
| StateRoot: genesisTrie.MustHash(), | ||
| Number: 0, | ||
| } | ||
|
|
||
| cfgKeystore := keystore.NewGlobalKeystore() | ||
| kp, err := sr25519.GenerateKeypair() | ||
| require.NoError(t, err) | ||
| err = cfgKeystore.Acco.Insert(kp) | ||
| require.NoError(t, err) | ||
|
|
||
| // Create state service | ||
| var stateSrvc *state.Service | ||
| testDatadirPath := t.TempDir() | ||
|
|
||
| // Set up block and storage state | ||
| telemetryMock := NewMockClient(ctrl) | ||
| telemetryMock.EXPECT().SendMessage(gomock.Any()).AnyTimes() | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In SendMessage try to make the type
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Having trouble with this. Can I pass in an interface like that? Or do I need to instantiate it and am just being dumb somehow when I am tring
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Try something like
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. hm this is still not working. Tried a couple variants of this and seems to not be a fan still. |
||
|
|
||
| stateConfig := state.Config{ | ||
| Path: testDatadirPath, | ||
| LogLevel: log.Critical, | ||
| Telemetry: telemetryMock, | ||
| } | ||
|
|
||
| stateSrvc = state.NewService(stateConfig) | ||
| stateSrvc.UseMemDB() | ||
|
|
||
| err = stateSrvc.Initialise(gen, genesisHeader, &genesisTrie) | ||
| require.NoError(t, err) | ||
|
|
||
| // Start state service | ||
| err = stateSrvc.Start() | ||
| require.NoError(t, err) | ||
|
|
||
| cfgBlockState := stateSrvc.Block | ||
| cfgStorageState := stateSrvc.Storage | ||
| cfgCodeSubstitutedState := stateSrvc.Base | ||
|
|
||
| var rtCfg wasmer.Config | ||
| rtCfg.Storage = rtstorage.NewTrieState(&genesisTrie) | ||
|
|
||
| rtCfg.CodeHash, err = cfgStorageState.LoadCodeHash(nil) | ||
| require.NoError(t, err) | ||
|
|
||
| nodeStorage := runtime.NodeStorage{} | ||
| nodeStorage.BaseDB = stateSrvc.Base | ||
|
|
||
| rtCfg.NodeStorage = nodeStorage | ||
|
|
||
| cfgRuntime, err := wasmer.NewRuntimeFromGenesis(rtCfg) | ||
| require.NoError(t, err) | ||
|
|
||
| cfgRuntime.(*wasmer.Instance).GetContext().Storage.Set(aliceBalanceKey, encodedAccountInfo) | ||
| // this key is System.UpgradedToDualRefCount -> set to true since all accounts have been upgraded to v0.9 format | ||
| cfgRuntime.(*wasmer.Instance).GetContext().Storage.Set(common.UpgradedToDualRefKey, []byte{1}) | ||
|
|
||
| cfgBlockState.StoreRuntime(cfgBlockState.BestBlockHash(), cfgRuntime) | ||
|
|
||
| // Hash of encrypted centrifuge extrinsic | ||
| testCallArguments := []byte{0xab, 0xcd} | ||
| extHex := runtime.NewTestExtrinsic(t, cfgRuntime, genesisHeader.Hash(), cfgBlockState.BestBlockHash(), | ||
| 0, "System.remark", testCallArguments) | ||
| encodedExtrinsic = common.MustHexToBytes(extHex) | ||
|
|
||
| cfgCodeSubstitutes := make(map[common.Hash]string) | ||
|
|
||
| genesisData, err := cfgCodeSubstitutedState.LoadGenesisData() | ||
| require.NoError(t, err) | ||
|
|
||
| for k, v := range genesisData.CodeSubstitutes { | ||
| cfgCodeSubstitutes[common.MustHexToHash(k)] = v | ||
| } | ||
|
|
||
| cfgCodeSubstitutedState = stateSrvc.Base | ||
|
|
||
| cfg := &Config{ | ||
| Keystore: cfgKeystore, | ||
| LogLvl: log.Critical, | ||
| BlockState: cfgBlockState, | ||
| StorageState: cfgStorageState, | ||
| TransactionState: stateSrvc.Transaction, | ||
| EpochState: stateSrvc.Epoch, | ||
| CodeSubstitutedState: cfgCodeSubstitutedState, | ||
| Runtime: cfgRuntime, | ||
| Network: new(network.Service), | ||
| CodeSubstitutes: cfgCodeSubstitutes, | ||
| } | ||
| service, err = NewService(cfg) | ||
| require.NoError(t, err) | ||
|
|
||
| return service, encodedExtrinsic | ||
| } | ||
|
|
||
| // NewTestService creates a new test core service | ||
| func NewTestService(t *testing.T, cfg *Config) *Service { | ||
| t.Helper() | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,6 +4,7 @@ | |
| package core | ||
|
|
||
| import ( | ||
| "bytes" | ||
| "errors" | ||
| "testing" | ||
|
|
||
|
|
@@ -44,8 +45,9 @@ type mockGetRuntime struct { | |
| } | ||
|
|
||
| type mockBlockState struct { | ||
| bestHeader *mockBestHeader | ||
| getRuntime *mockGetRuntime | ||
| bestHeader *mockBestHeader | ||
| getRuntime *mockGetRuntime | ||
| callsBestBlockHash bool | ||
| } | ||
|
|
||
| type mockStorageState struct { | ||
|
|
@@ -252,6 +254,7 @@ func TestServiceHandleTransactionMessage(t *testing.T) { | |
| getRuntime: &mockGetRuntime{ | ||
| runtime: runtimeMock2, | ||
| }, | ||
| callsBestBlockHash: true, | ||
| }, | ||
| mockStorageState: &mockStorageState{ | ||
| input: &common.Hash{}, | ||
|
|
@@ -261,8 +264,12 @@ func TestServiceHandleTransactionMessage(t *testing.T) { | |
| runtime: runtimeMock2, | ||
| setContextStorage: &mockSetContextStorage{trieState: &storage.TrieState{}}, | ||
| validateTxn: &mockValidateTxn{ | ||
| input: types.Extrinsic(append([]byte{byte(types.TxnExternal)}, testExtrinsic[0]...)), | ||
| err: invalidTransaction, | ||
| input: types.Extrinsic(bytes.Join([][]byte{ | ||
| {byte(types.TxnExternal)}, | ||
| testExtrinsic[0], | ||
| testEmptyHeader.StateRoot.ToBytes(), | ||
| }, nil)), | ||
| err: invalidTransaction, | ||
| }, | ||
| }, | ||
| args: args{ | ||
|
|
@@ -291,6 +298,7 @@ func TestServiceHandleTransactionMessage(t *testing.T) { | |
| getRuntime: &mockGetRuntime{ | ||
| runtime: runtimeMock3, | ||
| }, | ||
| callsBestBlockHash: true, | ||
| }, | ||
| mockStorageState: &mockStorageState{ | ||
| input: &common.Hash{}, | ||
|
|
@@ -308,7 +316,11 @@ func TestServiceHandleTransactionMessage(t *testing.T) { | |
| runtime: runtimeMock3, | ||
| setContextStorage: &mockSetContextStorage{trieState: &storage.TrieState{}}, | ||
| validateTxn: &mockValidateTxn{ | ||
| input: types.Extrinsic(append([]byte{byte(types.TxnExternal)}, testExtrinsic[0]...)), | ||
| input: types.Extrinsic(bytes.Join([][]byte{ | ||
| {byte(types.TxnExternal)}, | ||
| testExtrinsic[0], | ||
| testEmptyHeader.StateRoot.ToBytes(), | ||
| }, nil)), | ||
| validity: &transaction.Validity{Propagate: true}, | ||
| }, | ||
| }, | ||
|
|
@@ -344,6 +356,9 @@ func TestServiceHandleTransactionMessage(t *testing.T) { | |
| tt.mockBlockState.getRuntime.runtime, | ||
| tt.mockBlockState.getRuntime.err) | ||
| } | ||
| if tt.mockBlockState.callsBestBlockHash { | ||
| blockState.EXPECT().BestBlockHash().Return(common.Hash{}) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You could use
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we are tryin to avoid using |
||
| } | ||
| s.blockState = blockState | ||
| } | ||
| if tt.mockStorageState != nil { | ||
|
|
@@ -365,6 +380,19 @@ func TestServiceHandleTransactionMessage(t *testing.T) { | |
| rt.EXPECT().SetContextStorage(tt.mockRuntime.setContextStorage.trieState) | ||
| rt.EXPECT().ValidateTransaction(tt.mockRuntime.validateTxn.input). | ||
| Return(tt.mockRuntime.validateTxn.validity, tt.mockRuntime.validateTxn.err) | ||
| rt.EXPECT().Version().Return(runtime.Version{ | ||
| SpecName: []byte("polkadot"), | ||
| ImplName: []byte("parity-polkadot"), | ||
| AuthoringVersion: authoringVersion, | ||
| SpecVersion: specVersion, | ||
| ImplVersion: implVersion, | ||
| APIItems: []runtime.APIItem{{ | ||
| Name: common.MustBlake2b8([]byte("TaggedTransactionQueue")), | ||
| Ver: 3, | ||
| }}, | ||
| TransactionVersion: transactionVersion, | ||
| StateVersion: stateVersion, | ||
| }) | ||
| } | ||
|
|
||
| res, err := s.HandleTransactionMessage(tt.args.peerID, tt.args.msg) | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.