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

fix(rpc/modules): use westend-local in TestAuthorModule_SubmitExtrinsic_invalid test #3051

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
3 changes: 2 additions & 1 deletion dot/core/helpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"github.com/ChainSafe/gossamer/lib/trie"
"github.com/ChainSafe/gossamer/lib/utils"
"github.com/ChainSafe/gossamer/pkg/scale"
"github.com/centrifuge/go-substrate-rpc-client/v4/signature"
"github.com/golang/mock/gomock"
"github.com/stretchr/testify/require"
)
Expand Down Expand Up @@ -116,7 +117,7 @@ func createTestService(t *testing.T, genesisFilePath string,
// Hash of encrypted centrifuge extrinsic
testCallArguments := []byte{0xab, 0xcd}
extHex := runtime.NewTestExtrinsic(t, cfgRuntime, genesisHeader.Hash(), cfgBlockState.BestBlockHash(),
0, "System.remark", testCallArguments)
0, signature.TestKeyringPairAlice, "System.remark", testCallArguments)
encodedExtrinsic = common.MustHexToBytes(extHex)

cfgCodeSubstitutes := make(map[common.Hash]string)
Expand Down
4 changes: 2 additions & 2 deletions dot/core/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -472,14 +472,14 @@ func (s *Service) HasKey(pubKeyStr, keystoreType string) (bool, error) {
}

// DecodeSessionKeys executes the runtime DecodeSessionKeys and return the scale encoded keys
func (s *Service) DecodeSessionKeys(enc []byte) ([]byte, error) {
func (s *Service) DecodeSessionKeys(encodedSessionKeys []byte) ([]byte, error) {
bestBlockHash := s.blockState.BestBlockHash()
rt, err := s.blockState.GetRuntime(bestBlockHash)
if err != nil {
return nil, err
}

return rt.DecodeSessionKeys(enc)
return rt.DecodeSessionKeys(encodedSessionKeys)
}

// GetRuntimeVersion gets the current RuntimeVersion
Expand Down
5 changes: 2 additions & 3 deletions dot/rpc/modules/author.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,12 +108,12 @@ func (am *AuthorModule) HasSessionKeys(r *http.Request, req *HasSessionKeyReques
return err
}

pkeys, err := scale.Marshal(pubKeysBytes)
encodedKeys, err := scale.Marshal(pubKeysBytes)
if err != nil {
return err
}

data, err := am.coreAPI.DecodeSessionKeys(pkeys)
data, err := am.coreAPI.DecodeSessionKeys(encodedKeys)
if err != nil {
*res = false
return err
Expand All @@ -133,7 +133,6 @@ func (am *AuthorModule) HasSessionKeys(r *http.Request, req *HasSessionKeyReques
for _, key := range *decodedKeys {
encType := keystore.Name(key.Type[:])
ok, err := am.coreAPI.HasKey(common.BytesToHex(key.Data), string(encType))

if err != nil || !ok {
*res = false
return err
Expand Down
50 changes: 34 additions & 16 deletions dot/rpc/modules/author_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,8 @@ func TestAuthorModule_SubmitExtrinsic_Integration(t *testing.T) {

// creating an extrisinc to the System.remark call using a sample argument
extHex := runtime.NewTestExtrinsic(t,
integrationTestController.runtime, genesisHash, genesisHash, 0, "System.remark", []byte{0xab, 0xcd})
integrationTestController.runtime, genesisHash, genesisHash, 0,
signature.TestKeyringPairAlice, "System.remark", []byte{0xab, 0xcd})

extBytes := common.MustHexToBytes(extHex)

Expand All @@ -193,12 +194,12 @@ func TestAuthorModule_SubmitExtrinsic_Integration(t *testing.T) {
expected := &transaction.ValidTransaction{
Extrinsic: expectedExtrinsic,
Validity: &transaction.Validity{
Priority: 39325240425794630,
Priority: 36074,
Requires: nil,
Provides: [][]byte{
common.MustHexToBytes("0xd43593c715fdd31c61141abd04a99fd6822c8558854ccde39a5684e7a56da27d00000000"),
},
Longevity: 18446744073709551614,
Longevity: 18446744073709551613,
Propagate: true,
},
}
Expand All @@ -212,15 +213,25 @@ func TestAuthorModule_SubmitExtrinsic_Integration(t *testing.T) {
require.Equal(t, expectedHash, *res)
}

func TestAuthorModule_SubmitExtrinsic_invalid(t *testing.T) {
func TestAuthorModule_SubmitExtrinsic_bad_proof(t *testing.T) {
t.Parallel()
testInvalidKeyringPairAlice := signature.KeyringPair{
URI: "//Alice",
PublicKey: []byte{0xd5, 0x36, 0x13, 0xc7, 0x15, 0xfd, 0xd3,
0x1c, 0x61, 0x14, 0x1a, 0xb4, 0x4, 0xa9, 0x9f, 0xd6, 0x82,
0x2c, 0x85, 0x58, 0x85, 0x2c, 0xcd, 0xe3, 0x9a, 0x56, 0x84,
0xe7, 0xa5, 0x6d, 0x12, 0x7d},
Address: "5GrwvaEF5zXb26Fz9rcQpDWS57CtERHpNehXCPcNoHGKutQY",
}

integrationTestController := setupStateAndRuntime(t, t.TempDir(), useInstanceFromGenesis)

genesisHash := integrationTestController.genesisHeader.Hash()

// creating an extrisinc to the System.remark call using a sample argument
extHex := runtime.NewTestExtrinsic(t,
integrationTestController.runtime, genesisHash, genesisHash, 0, "System.remark", []byte{})
integrationTestController.runtime, genesisHash, genesisHash, 0,
testInvalidKeyringPairAlice, "System.remark", []byte{0xab, 0xcd})

ctrl := gomock.NewController(t)
net2test := NewMockNetwork(ctrl)
Expand All @@ -233,7 +244,7 @@ func TestAuthorModule_SubmitExtrinsic_invalid(t *testing.T) {

res := new(ExtrinsicHashResponse)
err := auth.SubmitExtrinsic(nil, &Extrinsic{extHex}, res)
require.EqualError(t, err, "ancient birth block")
require.EqualError(t, err, "bad proof")

txOnPool := integrationTestController.stateSrv.Transaction.PendingInPool()
require.Len(t, txOnPool, 0)
Expand Down Expand Up @@ -269,7 +280,8 @@ func TestAuthorModule_SubmitExtrinsic_AlreadyInPool(t *testing.T) {

// creating an extrisinc to the System.remark call using a sample argument
extHex := runtime.NewTestExtrinsic(t,
integrationTestController.runtime, genesisHash, genesisHash, 0, "System.remark", []byte{})
integrationTestController.runtime, genesisHash, genesisHash, 0,
signature.TestKeyringPairAlice, "System.remark", []byte{})
extBytes := common.MustHexToBytes(extHex)

integrationTestController.network = NewMockNetwork(nil)
Expand Down Expand Up @@ -456,8 +468,8 @@ func TestAuthorModule_HasKey_Integration(t *testing.T) {
func TestAuthorModule_HasSessionKeys_Integration(t *testing.T) {
t.Parallel()

const granSeed = "0xf25586ceb64a043d887631fa08c2ed790ef7ae3c7f28de5172005f8b9469e529"
const granPubK = "0x6b802349d948444d41397da09ec597fbd8ae8fdd3dfa153b2bb2bddcf020457c"
const granSeed = "0xabf8e5bdbe30c65656c0a3cbd181ff8a56294a69dfedd27982aace4a76909115"
const granPubK = "0x88dc3417d5058ec4b4503e0c12ea1a0a89be200fe98922423d4334014fa6b0ee"

const sr25519Seed = "0xe5be9a5092b81bca64be81d212e7f2f9eba183bb7a90954f7b76361f6edb5c0a"
const sr25519Pubk = "0xd43593c715fdd31c61141abd04a99fd6822c8558854ccde39a5684e7a56da27d"
Expand All @@ -472,7 +484,7 @@ func TestAuthorModule_HasSessionKeys_Integration(t *testing.T) {
pubk: granPubK,
},
{
ktype: []string{"babe", "imon", "audi"},
ktype: []string{"babe", "imon", "para", "asgn", "audi"},
seed: sr25519Seed,
pubk: sr25519Pubk,
},
Expand All @@ -484,24 +496,30 @@ func TestAuthorModule_HasSessionKeys_Integration(t *testing.T) {
waitErr error
}{
"public keys are in the right order, should return true": {
pubSessionKeys: "0x6b802349d948444d41397da09ec597fbd8ae8fdd3dfa153b2bb2bddcf020457c" + // gran
pubSessionKeys: "0x88dc3417d5058ec4b4503e0c12ea1a0a89be200fe98922423d4334014fa6b0ee" + // gran
"d43593c715fdd31c61141abd04a99fd6822c8558854ccde39a5684e7a56da27d" + // babe
"d43593c715fdd31c61141abd04a99fd6822c8558854ccde39a5684e7a56da27d" + // imon
"d43593c715fdd31c61141abd04a99fd6822c8558854ccde39a5684e7a56da27d" + // para
"d43593c715fdd31c61141abd04a99fd6822c8558854ccde39a5684e7a56da27d" + // asgn
"d43593c715fdd31c61141abd04a99fd6822c8558854ccde39a5684e7a56da27d", // audi
expect: true,
},
"unknown public keys in the right order, should return false": {
pubSessionKeys: "0x740550da19ef14023ea3e903545a6700160a55be2e4b733b577c91b053e38b8d" + // gran
"de6fa0da51c52cc117d77aeb329595b15070db444e7ed4c4adec714b291c1845" + // babe
"de6fa0da51c52cc117d77aeb329595b15070db444e7ed4c4adec714b291c1845" + // imon
"de6fa0da51c52cc117d77aeb329595b15070db444e7ed4c4adec714b291c1845" + // para
"de6fa0da51c52cc117d77aeb329595b15070db444e7ed4c4adec714b291c1845" + // asgn
"de6fa0da51c52cc117d77aeb329595b15070db444e7ed4c4adec714b291c1845", // audi
expect: false,
},
"public keys are not in the right order, should return false": {
pubSessionKeys: "0xd43593c715fdd31c61141abd04a99fd6822c8558854ccde39a5684e7a56da27d" + // gran
"6b802349d948444d41397da09ec597fbd8ae8fdd3dfa153b2bb2bddcf020457c" + // babe
pubSessionKeys: "0x6b802349d948444d41397da09ec597fbd8ae8fdd3dfa153b2bb2bddcf020457c" + // babe
"d43593c715fdd31c61141abd04a99fd6822c8558854ccde39a5684e7a56da27d" + // gran
"d43593c715fdd31c61141abd04a99fd6822c8558854ccde39a5684e7a56da27d" + // imon
"d43593c715fdd31c61141abd04a99fd6822c8558854ccde39a5684e7a56da27d", // audi
"d43593c715fdd31c61141abd04a99fd6822c8558854ccde39a5684e7a56da27d" + // audi
"d43593c715fdd31c61141abd04a99fd6822c8558854ccde39a5684e7a56da27d" + // para
"d43593c715fdd31c61141abd04a99fd6822c8558854ccde39a5684e7a56da27d", // asgn
expect: false,
},
"incomplete keys": {
Expand Down Expand Up @@ -630,7 +648,7 @@ type integrationTestController struct {
func setupStateAndRuntime(t *testing.T, basepath string, useInstance useRuntimeInstance) *integrationTestController {
t.Helper()

gen, genesisTrie, genesisHeader := newTestGenesisWithTrieAndHeader(t)
gen, genesisTrie, genesisHeader := newWestendLocalGenesisWithTrieAndHeader(t)

ctrl := gomock.NewController(t)
telemetryMock := NewMockTelemetry(ctrl)
Expand Down Expand Up @@ -690,7 +708,7 @@ func setupStateAndPopulateTrieState(t *testing.T, basepath string,
useInstance useRuntimeInstance) *integrationTestController {
t.Helper()

gen, genesisTrie, genesisHeader := newTestGenesisWithTrieAndHeader(t)
gen, genesisTrie, genesisHeader := newWestendLocalGenesisWithTrieAndHeader(t)

ctrl := gomock.NewController(t)
telemetryMock := NewMockTelemetry(ctrl)
Expand Down
6 changes: 3 additions & 3 deletions dot/rpc/modules/chain_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,7 @@ func TestChainGetBlockHash_Array(t *testing.T) {
func TestChainGetFinalizedHead(t *testing.T) {
state := newTestStateService(t)
svc := NewChainModule(state.Block)
_, _, genesisHeader := newTestGenesisWithTrieAndHeader(t)
_, _, genesisHeader := newWestendLocalGenesisWithTrieAndHeader(t)
var res ChainHashResponse
err := svc.GetFinalizedHead(nil, &EmptyRequest{}, &res)
require.NoError(t, err)
Expand All @@ -306,7 +306,7 @@ func TestChainGetFinalizedHeadByRound(t *testing.T) {
err := svc.GetFinalizedHeadByRound(nil, &req, &res)
require.NoError(t, err)

_, _, genesisHeader := newTestGenesisWithTrieAndHeader(t)
_, _, genesisHeader := newWestendLocalGenesisWithTrieAndHeader(t)
expected := genesisHeader.Hash()
require.Equal(t, common.BytesToHex(expected[:]), res)

Expand Down Expand Up @@ -351,7 +351,7 @@ func newTestStateService(t *testing.T) *state.Service {
stateSrvc := state.NewService(config)
stateSrvc.UseMemDB()

gen, genesisTrie, genesisHeader := newTestGenesisWithTrieAndHeader(t)
gen, genesisTrie, genesisHeader := newWestendLocalGenesisWithTrieAndHeader(t)

err := stateSrvc.Initialise(&gen, &genesisHeader, &genesisTrie)
require.NoError(t, err)
Expand Down
2 changes: 1 addition & 1 deletion dot/rpc/modules/childstate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import (
func createTestTrieState(t *testing.T) (*trie.Trie, common.Hash) {
t.Helper()

_, genesisTrie, _ := newTestGenesisWithTrieAndHeader(t)
_, genesisTrie, _ := newWestendLocalGenesisWithTrieAndHeader(t)
tr := rtstorage.NewTrieState(&genesisTrie)

tr.Put([]byte(":first_key"), []byte(":value1"))
Expand Down
2 changes: 1 addition & 1 deletion dot/rpc/modules/dev_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func newState(t *testing.T) (*state.BlockState, *state.EpochState) {

db := state.NewInMemoryDB(t)

_, genesisTrie, genesisHeader := newTestGenesisWithTrieAndHeader(t)
_, genesisTrie, genesisHeader := newWestendLocalGenesisWithTrieAndHeader(t)
tries := state.NewTries()
tries.SetTrie(&genesisTrie)
bs, err := state.NewBlockStateFromGenesis(db, tries, &genesisHeader, telemetryMock)
Expand Down
4 changes: 2 additions & 2 deletions dot/rpc/modules/helpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,11 @@ func makeChange(keyHex, valueHex string) [2]*string {
return [2]*string{&keyHex, &valueHex}
}

func newTestGenesisWithTrieAndHeader(t *testing.T) (
func newWestendLocalGenesisWithTrieAndHeader(t *testing.T) (
gen genesis.Genesis, genesisTrie trie.Trie, genesisHeader types.Header) {
t.Helper()

genesisPath := utils.GetGssmrV3SubstrateGenesisRawPathTest(t)
genesisPath := utils.GetWestendLocalRawGenesisPath(t)
genesisPtr, err := genesis.NewGenesisFromJSONRaw(genesisPath)
require.NoError(t, err)
gen = *genesisPtr
Expand Down
4 changes: 3 additions & 1 deletion lib/babe/babe_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"github.com/ChainSafe/gossamer/lib/runtime/wasmer"
"github.com/ChainSafe/gossamer/lib/trie"
"github.com/ChainSafe/gossamer/pkg/scale"
"github.com/centrifuge/go-substrate-rpc-client/v4/signature"
"github.com/golang/mock/gomock"

"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -313,7 +314,8 @@ func TestService_HandleSlotWithLaggingSlot(t *testing.T) {
epochData, err := babeService.initiateEpoch(testEpochIndex)
require.NoError(t, err)

ext := runtime.NewTestExtrinsic(t, rt, parentHash, parentHash, 0, "System.remark", []byte{0xab, 0xcd})
ext := runtime.NewTestExtrinsic(t, rt, parentHash, parentHash, 0,
signature.TestKeyringPairAlice, "System.remark", []byte{0xab, 0xcd})
block := createTestBlock(t, babeService, emptyHeader, [][]byte{common.MustHexToBytes(ext)},
1, testEpochIndex, epochData)

Expand Down
10 changes: 6 additions & 4 deletions lib/babe/build_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import (
"github.com/ChainSafe/gossamer/pkg/scale"

cscale "github.com/centrifuge/go-substrate-rpc-client/v4/scale"
signaturev4 "github.com/centrifuge/go-substrate-rpc-client/v4/signature"
"github.com/centrifuge/go-substrate-rpc-client/v4/signature"
ctypes "github.com/centrifuge/go-substrate-rpc-client/v4/types"
"github.com/centrifuge/go-substrate-rpc-client/v4/types/codec"
"github.com/ethereum/go-ethereum/metrics"
Expand Down Expand Up @@ -98,7 +98,8 @@ func TestBuildBlock_ok(t *testing.T) {
epochData, err := babeService.initiateEpoch(testEpochIndex)
require.NoError(t, err)

ext := runtime.NewTestExtrinsic(t, rt, parentHash, parentHash, 0, "System.remark", []byte{0xab, 0xcd})
ext := runtime.NewTestExtrinsic(t, rt, parentHash, parentHash, 0,
signature.TestKeyringPairAlice, "System.remark", []byte{0xab, 0xcd})
block := createTestBlock(t, babeService, emptyHeader, [][]byte{common.MustHexToBytes(ext)},
1, testEpochIndex, epochData)

Expand Down Expand Up @@ -183,7 +184,8 @@ func TestApplyExtrinsic(t *testing.T) {
header1, err := rt.FinalizeBlock()
require.NoError(t, err)

extHex := runtime.NewTestExtrinsic(t, rt, parentHash, parentHash, 0, "System.remark", []byte{0xab, 0xcd})
extHex := runtime.NewTestExtrinsic(t, rt, parentHash, parentHash, 0,
signature.TestKeyringPairAlice, "System.remark", []byte{0xab, 0xcd})
extBytes := common.MustHexToBytes(extHex)
_, err = rt.ValidateTransaction(append([]byte{byte(types.TxnExternal)}, extBytes...))
require.NoError(t, err)
Expand Down Expand Up @@ -258,7 +260,7 @@ func TestBuildAndApplyExtrinsic(t *testing.T) {
}

// Sign the transaction using Alice's default account
err = ext.Sign(signaturev4.TestKeyringPairAlice, o)
err = ext.Sign(signature.TestKeyringPairAlice, o)
require.NoError(t, err)

extEnc := bytes.Buffer{}
Expand Down
13 changes: 2 additions & 11 deletions lib/keystore/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -299,19 +299,10 @@ func UnlockKeys(ks Inserter, dir, unlock, password string) error {
// and returns the crypto.KeyType
func DetermineKeyType(t string) crypto.KeyType {
switch t {
case "babe":
return crypto.Sr25519Type
case "gran":
return crypto.Ed25519Type
case "acco":
return crypto.Sr25519Type
case "aura":
return crypto.Sr25519Type
case "imon":
return crypto.Sr25519Type
case "audi":
return crypto.Sr25519Type
case "dumy":
case "acco", "babe", "para", "asgn",
"aura", "imon", "audi", "dumy":
return crypto.Sr25519Type
}
return crypto.UnknownType
Expand Down
10 changes: 10 additions & 0 deletions lib/keystore/keystore.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ var (
AccoName Name = "acco"
AuraName Name = "aura"
ImonName Name = "imon"
ParaName Name = "para"
AsgnName Name = "asgn"
AudiName Name = "audi"
DumyName Name = "dumy"
)
Expand Down Expand Up @@ -62,6 +64,8 @@ type GlobalKeystore struct {
Gran Keystore
Acco Keystore
Aura Keystore
Para Keystore
Asgn Keystore
Imon Keystore
Audi Keystore
Dumy Keystore
Expand All @@ -74,6 +78,8 @@ func NewGlobalKeystore() *GlobalKeystore {
Gran: NewBasicKeystore(GranName, crypto.Ed25519Type),
Acco: NewGenericKeystore(AccoName), // TODO: which type is used? can an account be either type? (#1872)
Aura: NewBasicKeystore(AuraName, crypto.Sr25519Type),
Para: NewBasicKeystore(ParaName, crypto.Sr25519Type),
Asgn: NewBasicKeystore(AsgnName, crypto.Sr25519Type),
Imon: NewBasicKeystore(ImonName, crypto.Sr25519Type),
Audi: NewBasicKeystore(AudiName, crypto.Sr25519Type),
Dumy: NewGenericKeystore(DumyName),
Expand All @@ -94,6 +100,10 @@ func (k *GlobalKeystore) GetKeystore(name []byte) (Keystore, error) {
return k.Aura, nil
case ImonName:
return k.Imon, nil
case ParaName:
return k.Para, nil
case AsgnName:
return k.Asgn, nil
case AudiName:
return k.Audi, nil
case DumyName:
Expand Down
4 changes: 2 additions & 2 deletions lib/runtime/test_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ type MetadataVersioner interface {

// NewTestExtrinsic builds a new extrinsic using centrifuge pkg
func NewTestExtrinsic(t *testing.T, rt MetadataVersioner, genHash, blockHash common.Hash,
nonce uint64, call string, args ...interface{}) string {
nonce uint64, keyRingPair signature.KeyringPair, call string, args ...interface{}) string {
t.Helper()

rawMeta, err := rt.Metadata()
Expand Down Expand Up @@ -254,7 +254,7 @@ func NewTestExtrinsic(t *testing.T, rt MetadataVersioner, genHash, blockHash com
}

// Sign the transaction using Alice's key
err = ext.Sign(signature.TestKeyringPairAlice, o)
err = ext.Sign(keyRingPair, o)
require.NoError(t, err)

extEnc, err := codec.EncodeToHex(ext)
Expand Down
Loading