Skip to content

Commit 58b8725

Browse files
authored
fix(dot/core): Fix handle transaction message test. (#1607)
* fix(dot/core): Fix handle transaction message test. * Remove hardcoded extrinsic value. * Add subkey for Macos. * Add subkey in the coverage report.
1 parent fc8472f commit 58b8725

File tree

6 files changed

+90
-27
lines changed

6 files changed

+90
-27
lines changed

.github/workflows/build.yml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,13 @@ jobs:
6868
key: ${{ runner.os }}-go-mod-${{ hashFiles('**/go.sum') }}
6969
restore-keys: ${{ runner.os }}-go-mod
7070

71+
- name: Install Subkey
72+
run: |
73+
wget -P $HOME/.local/bin/ https://chainbridge.ams3.digitaloceanspaces.com/subkey-v2.0.0
74+
mv $HOME/.local/bin/subkey-v2.0.0 $HOME/.local/bin/subkey
75+
chmod +x $HOME/.local/bin/subkey
76+
echo "$HOME/.local/bin" >> $GITHUB_PATH
77+
7178
- name: Generate coverage report
7279
run: |
7380
go test ./... -short -coverprofile=coverage.out -covermode=atomic -timeout=20m

.github/workflows/tests.yml

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,17 @@ jobs:
3939

4040
- name: Install Subkey
4141
run: |
42-
wget -P $HOME/.local/bin/ https://chainbridge.ams3.digitaloceanspaces.com/subkey-v2.0.0
43-
mv $HOME/.local/bin/subkey-v2.0.0 $HOME/.local/bin/subkey
42+
if [ "$RUNNER_OS" == "Linux" ]; then
43+
wget -P $HOME/.local/bin/ https://chainbridge.ams3.digitaloceanspaces.com/subkey-v2.0.0
44+
mv $HOME/.local/bin/subkey-v2.0.0 $HOME/.local/bin/subkey
45+
elif [ "$RUNNER_OS" == "macOS" ]; then
46+
wget -P $HOME/.local/bin/ https://chainbridge.ams3.digitaloceanspaces.com/subkey-v2.0.0-macos
47+
mv $HOME/.local/bin/subkey-v2.0.0-macos $HOME/.local/bin/subkey
48+
else
49+
echo "Subkey for $RUNNER_OS is not supported"
50+
exit 1
51+
fi
52+
4453
chmod +x $HOME/.local/bin/subkey
4554
echo "$HOME/.local/bin" >> $GITHUB_PATH
4655
@@ -81,6 +90,13 @@ jobs:
8190
key: ${{ runner.os }}-go-mod-${{ hashFiles('**/go.sum') }}
8291
restore-keys: ${{ runner.os }}-go-mod
8392

93+
- name: Install Subkey
94+
run: |
95+
wget -P $HOME/.local/bin/ https://chainbridge.ams3.digitaloceanspaces.com/subkey-v2.0.0
96+
mv $HOME/.local/bin/subkey-v2.0.0 $HOME/.local/bin/subkey
97+
chmod +x $HOME/.local/bin/subkey
98+
echo "$HOME/.local/bin" >> $GITHUB_PATH
99+
84100
- name: Generate coverage report
85101
run: |
86102
go test ./... -short -coverprofile=coverage.out -covermode=atomic -timeout=20m

dot/core/messages.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ package core
1818

1919
import (
2020
"github.com/ChainSafe/gossamer/dot/network"
21+
"github.com/ChainSafe/gossamer/dot/types"
2122
"github.com/ChainSafe/gossamer/lib/transaction"
2223
)
2324

@@ -33,10 +34,8 @@ func (s *Service) HandleTransactionMessage(msg *network.TransactionMessage) erro
3334
txs := msg.Extrinsics
3435

3536
for _, tx := range txs {
36-
tx := tx // pin
37-
3837
// validate each transaction
39-
val, err := s.rt.ValidateTransaction(tx)
38+
val, err := s.rt.ValidateTransaction(append([]byte{byte(types.TxnExternal)}, tx...))
4039
if err != nil {
4140
logger.Error("failed to validate transaction", "err", err)
4241
return err

dot/core/messages_test.go

Lines changed: 62 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,9 @@ import (
2828
"github.com/ChainSafe/gossamer/lib/crypto/sr25519"
2929
"github.com/ChainSafe/gossamer/lib/keystore"
3030
"github.com/ChainSafe/gossamer/lib/runtime"
31-
"github.com/ChainSafe/gossamer/lib/runtime/wasmer"
32-
"github.com/ChainSafe/gossamer/lib/trie"
33-
34-
log "github.com/ChainSafe/log15"
31+
"github.com/ChainSafe/gossamer/lib/scale"
32+
"github.com/centrifuge/go-substrate-rpc-client/v2/signature"
33+
ctypes "github.com/centrifuge/go-substrate-rpc-client/v2/types"
3534
"github.com/stretchr/testify/require"
3635
)
3736

@@ -75,40 +74,84 @@ func TestService_ProcessBlockAnnounceMessage(t *testing.T) {
7574
require.Equal(t, expected, net.Message)
7675
}
7776

78-
func TestService_HandleTransactionMessage(t *testing.T) {
79-
// this currently fails due to not being able to call validate_transaction
77+
func createExtrinsics(t *testing.T, rt runtime.Instance, genHash common.Hash, nonce uint64) types.Extrinsic {
78+
t.Helper()
79+
rawMeta, err := rt.Metadata()
80+
require.NoError(t, err)
8081

81-
t.Skip()
82-
tt := trie.NewEmptyTrie()
83-
rt := wasmer.NewTestInstanceWithTrie(t, runtime.NODE_RUNTIME, tt, log.LvlTrace)
82+
decoded, err := scale.Decode(rawMeta, []byte{})
83+
require.NoError(t, err)
8484

85-
kp, err := sr25519.GenerateKeypair()
86-
require.Nil(t, err)
85+
meta := &ctypes.Metadata{}
86+
err = ctypes.DecodeFromBytes(decoded.([]byte), meta)
87+
require.NoError(t, err)
88+
89+
rv, err := rt.Version()
90+
require.NoError(t, err)
91+
92+
keyring, err := keystore.NewSr25519Keyring()
93+
require.NoError(t, err)
94+
95+
bob, err := ctypes.NewAddressFromHexAccountID(keyring.Bob().Public().Hex())
96+
require.NoError(t, err)
97+
98+
c, err := ctypes.NewCall(meta, "Balances.transfer", bob, ctypes.NewUCompactFromUInt(12345))
99+
require.NoError(t, err)
100+
101+
// Create the extrinsic
102+
ext := ctypes.NewExtrinsic(c)
87103

88-
// TODO: load BABE authority key
104+
o := ctypes.SignatureOptions{
105+
BlockHash: ctypes.Hash(genHash),
106+
Era: ctypes.ExtrinsicEra{IsImmortalEra: true},
107+
GenesisHash: ctypes.Hash(genHash),
108+
Nonce: ctypes.NewUCompactFromUInt(nonce),
109+
SpecVersion: ctypes.U32(rv.SpecVersion()),
110+
Tip: ctypes.NewUCompactFromUInt(0),
111+
TransactionVersion: ctypes.U32(rv.TransactionVersion()),
112+
}
113+
114+
// Sign the transaction using Alice's default account
115+
err = ext.Sign(signature.TestKeyringPairAlice, o)
116+
require.NoError(t, err)
117+
118+
extEnc, err := ctypes.EncodeToHexString(ext)
119+
require.NoError(t, err)
120+
121+
extBytes := types.Extrinsic(common.MustHexToBytes(extEnc))
122+
return extBytes
123+
}
124+
125+
func TestService_HandleTransactionMessage(t *testing.T) {
126+
kp, err := sr25519.GenerateKeypair()
127+
require.NoError(t, err)
89128

90129
ks := keystore.NewGlobalKeystore()
91130
ks.Acco.Insert(kp)
92131

93132
cfg := &Config{
94-
Runtime: rt,
95133
Keystore: ks,
96134
TransactionState: state.NewTransactionState(),
97135
IsBlockProducer: true,
136+
BlockProducer: &mockBlockProducer{},
98137
}
99138

100139
s := NewTestService(t, cfg)
140+
genHash := s.blockState.GenesisHash()
141+
header, err := types.NewHeader(genHash, common.Hash{}, common.Hash{}, big.NewInt(1), types.NewEmptyDigest())
142+
require.NoError(t, err)
101143

102-
// https://github.com/paritytech/substrate/blob/5420de3face1349a97eb954ae71c5b0b940c31de/core/transaction-pool/src/tests.rs#L95
103-
ext := []byte{1, 212, 53, 147, 199, 21, 253, 211, 28, 97, 20, 26, 189, 4, 169, 159, 214, 130, 44, 133, 88, 133, 76, 205, 227, 154, 86, 132, 231, 165, 109, 162, 125, 142, 175, 4, 21, 22, 135, 115, 99, 38, 201, 254, 161, 126, 37, 252, 82, 135, 97, 54, 147, 201, 18, 144, 156, 178, 38, 170, 71, 148, 242, 106, 72, 69, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 216, 5, 113, 87, 87, 40, 221, 120, 247, 252, 137, 201, 74, 231, 222, 101, 85, 108, 102, 39, 31, 190, 210, 14, 215, 124, 19, 160, 180, 203, 54, 110, 167, 163, 149, 45, 12, 108, 80, 221, 65, 238, 57, 237, 199, 16, 10, 33, 185, 8, 244, 184, 243, 139, 5, 87, 252, 245, 24, 225, 37, 154, 163, 142}
144+
// initialise block header
145+
err = s.rt.InitializeBlock(header)
146+
require.NoError(t, err)
104147

105-
msg := &network.TransactionMessage{Extrinsics: []types.Extrinsic{ext}}
148+
extBytes := createExtrinsics(t, s.rt, genHash, 0)
106149

150+
msg := &network.TransactionMessage{Extrinsics: []types.Extrinsic{extBytes}}
107151
err = s.HandleTransactionMessage(msg)
108-
require.Nil(t, err)
152+
require.NoError(t, err)
109153

110154
pending := s.transactionState.(*state.TransactionState).Pending()
111155
require.NotEqual(t, 0, len(pending))
112-
tx := []byte(pending[0].Extrinsic)
113-
require.Equal(t, ext, tx)
156+
require.Equal(t, extBytes, pending[0].Extrinsic)
114157
}

go.sum

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -269,7 +269,6 @@ github.com/kisielk/errcheck v1.2.0/go.mod h1:/BMXB+zMLi60iA8Vv6Ksmxu/1UDYcXs4uQL
269269
github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck=
270270
github.com/kkdai/bstream v0.0.0-20161212061736-f391b8402d23/go.mod h1:J+Gs4SYgM6CZQHDETBtE9HaSEkGmuNXF86RwHhHUvq4=
271271
github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ=
272-
github.com/konsorten/go-windows-terminal-sequences v1.0.3 h1:CE8S1cTafDpPvMhIxNJKvHsGVBgn1xWYf1NbHQhywc8=
273272
github.com/konsorten/go-windows-terminal-sequences v1.0.3/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ=
274273
github.com/koron/go-ssdp v0.0.0-20191105050749-2e1c40ed0b5d h1:68u9r4wEvL3gYg2jvAOgROwZ3H+Y3hIDk4tbbmIjcYQ=
275274
github.com/koron/go-ssdp v0.0.0-20191105050749-2e1c40ed0b5d/go.mod h1:5Ky9EC2xfoUKUor0Hjgi2BJhCSXJfMOFlmyYrVKGQMk=
@@ -608,7 +607,6 @@ github.com/russross/blackfriday v1.5.2/go.mod h1:JO/DiYxRf+HjHt06OyowR9PTA263kcR
608607
github.com/russross/blackfriday/v2 v2.0.1/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
609608
github.com/shurcooL/sanitized_anchor_name v1.0.0/go.mod h1:1NzhyTcUVG4SuEtjjoZeVRXNmyL/1OwPU0+IJeTBvfc=
610609
github.com/sirupsen/logrus v1.2.0/go.mod h1:LxeOpSwHxABJmUn/MG1IvRgCAasNZTLOkJPxbbu5VWo=
611-
github.com/sirupsen/logrus v1.6.0 h1:UBcNElsrwanuuMsnGSlYmtmgbb23qDR5dG+6X6Oo89I=
612610
github.com/sirupsen/logrus v1.6.0/go.mod h1:7uNnSEd1DgxDLC74fIahvMZmmYsHGZGEOFrfsX/uA88=
613611
github.com/smola/gocompat v0.2.0/go.mod h1:1B0MlxbmoZNo3h8guHp8HztB3BSYR5itql9qtVc0ypY=
614612
github.com/spacemonkeygo/openssl v0.0.0-20181017203307-c2dcc5cca94a/go.mod h1:7AyxJNCJ7SBZ1MfVQCWD6Uqo2oubI2Eq2y2eqf+A5r0=

lib/grandpa/round_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ func setupGrandpa(t *testing.T, kp *ed25519.Keypair) (*Service, chan GrandpaMess
105105
DigestHandler: &mockDigestHandler{},
106106
Voters: voters,
107107
Keypair: kp,
108-
LogLvl: log.LvlTrace,
108+
LogLvl: log.LvlInfo,
109109
Authority: true,
110110
Network: net,
111111
}

0 commit comments

Comments
 (0)