@@ -28,10 +28,9 @@ import (
28
28
"github.com/ChainSafe/gossamer/lib/crypto/sr25519"
29
29
"github.com/ChainSafe/gossamer/lib/keystore"
30
30
"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"
35
34
"github.com/stretchr/testify/require"
36
35
)
37
36
@@ -75,40 +74,84 @@ func TestService_ProcessBlockAnnounceMessage(t *testing.T) {
75
74
require .Equal (t , expected , net .Message )
76
75
}
77
76
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 )
80
81
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 )
84
84
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 )
87
103
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 )
89
128
90
129
ks := keystore .NewGlobalKeystore ()
91
130
ks .Acco .Insert (kp )
92
131
93
132
cfg := & Config {
94
- Runtime : rt ,
95
133
Keystore : ks ,
96
134
TransactionState : state .NewTransactionState (),
97
135
IsBlockProducer : true ,
136
+ BlockProducer : & mockBlockProducer {},
98
137
}
99
138
100
139
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 )
101
143
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 )
104
147
105
- msg := & network. TransactionMessage { Extrinsics : []types. Extrinsic { ext }}
148
+ extBytes := createExtrinsics ( t , s . rt , genHash , 0 )
106
149
150
+ msg := & network.TransactionMessage {Extrinsics : []types.Extrinsic {extBytes }}
107
151
err = s .HandleTransactionMessage (msg )
108
- require .Nil (t , err )
152
+ require .NoError (t , err )
109
153
110
154
pending := s .transactionState .(* state.TransactionState ).Pending ()
111
155
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 )
114
157
}
0 commit comments