@@ -55,6 +55,7 @@ type LocalNetwork struct {
55
55
// validators lists the validator nodes in the network. Validators
56
56
// are created during network startup and run for the full duration
57
57
// of the network.
58
+ // TODO dynamically created validators are not added to this list. Can't be added without old epoch is sealed, because method randomValidatorRPC could return a non validator node.
58
59
validators []* node.OperaNode
59
60
60
61
// nodes provide a register for all nodes in the network, including
@@ -180,58 +181,65 @@ func (n *LocalNetwork) createNode(nodeConfig *node.OperaNodeConfig) (*node.Opera
180
181
181
182
// CreateNode creates nodes in the network during run.
182
183
func (n * LocalNetwork ) CreateNode (config * driver.NodeConfig ) (driver.Node , error ) {
184
+ return n .createNode (& node.OperaNodeConfig {
185
+ Label : config .Name ,
186
+ NetworkConfig : & n .config ,
187
+ VmImplementation : n .config .VmImplementation ,
188
+ })
189
+ }
190
+
191
+ // CreateValidatorNode creates validator non genesis validator node in the network during run.
192
+ func (n * LocalNetwork ) CreateValidatorNode (config * driver.NodeConfig ) (driver.Node , error ) {
183
193
newValId := 0
184
- if config .Validator {
185
- log .Printf ("Creating validator node %s" , config .Name )
186
- rpcClient , err := n .DialRandomRpc ()
187
- if err != nil {
188
- return nil , err
189
- }
190
- defer rpcClient .Close ()
194
+ //
195
+ rpcClient , err := n .dialRandomGenesisValidatorRpc ()
196
+ if err != nil {
197
+ return nil , err
198
+ }
199
+ defer rpcClient .Close ()
191
200
192
- // get a representation of the deployed contract
193
- SFCContract , err := contract .NewSFC (common .HexToAddress ("0xFC00FACE00000000000000000000000000000000" ), rpcClient )
194
- if err != nil {
195
- return nil , fmt .Errorf ("failed to get SFC contract representation; %v" , err )
196
- }
201
+ // get a representation of the deployed contract
202
+ SFCContract , err := contract .NewSFC (common .HexToAddress ("0xFC00FACE00000000000000000000000000000000" ), rpcClient )
203
+ if err != nil {
204
+ return nil , fmt .Errorf ("failed to get SFC contract representation; %v" , err )
205
+ }
197
206
198
- var lastValId * big.Int
199
- lastValId , err = SFCContract .LastValidatorID (nil )
200
- if err != nil {
201
- return nil , fmt .Errorf ("failed to get validator count; %v" , err )
202
- }
207
+ var lastValId * big.Int
208
+ lastValId , err = SFCContract .LastValidatorID (nil )
209
+ if err != nil {
210
+ return nil , fmt .Errorf ("failed to get validator count; %v" , err )
211
+ }
203
212
204
- newValId = int (lastValId .Int64 ()) + 1
205
- fmt . Println ( "newValId" , newValId )
213
+ newValId = int (lastValId .Int64 ()) + 1
214
+ log . Printf ( "Creating validator node %s; %d" , config . Name , newValId )
206
215
207
- privateKeyECDSA := evmcore .FakeKey (uint32 (newValId ))
208
- validatorPubKey := validatorpk.PubKey {
209
- Raw : crypto .FromECDSAPub (& privateKeyECDSA .PublicKey ),
210
- Type : validatorpk .Types .Secp256k1 ,
211
- }
216
+ privateKeyECDSA := evmcore .FakeKey (uint32 (newValId ))
217
+ validatorPubKey := validatorpk.PubKey {
218
+ Raw : crypto .FromECDSAPub (& privateKeyECDSA .PublicKey ),
219
+ Type : validatorpk .Types .Secp256k1 ,
220
+ }
212
221
213
- validatorPrivateKeykHex := strings .TrimPrefix (hexutil .Encode (crypto .FromECDSA (privateKeyECDSA )), "0x" )
214
- validatorAccount , err := app .NewAccount (newValId , validatorPrivateKeykHex , validatorPubKey .Raw , 0xfa3 )
215
- if err != nil {
216
- return nil , fmt .Errorf ("failed to create validator account: %v" , err )
217
- }
222
+ validatorPrivateKeykHex := strings .TrimPrefix (hexutil .Encode (crypto .FromECDSA (privateKeyECDSA )), "0x" )
223
+ validatorAccount , err := app .NewAccount (newValId , validatorPrivateKeykHex , validatorPubKey .Bytes () , 0xfa3 )
224
+ if err != nil {
225
+ return nil , fmt .Errorf ("failed to create validator account: %v" , err )
226
+ }
218
227
219
- _ , err = validatorAccount .CreateValidator (SFCContract , rpcClient )
220
- if err != nil {
221
- return nil , err
222
- }
223
- err = app .WaitUntilAccountNonceIs (crypto .PubkeyToAddress (privateKeyECDSA .PublicKey ), 1 , rpcClient )
224
- if err != nil {
225
- return nil , fmt .Errorf ("createValidator; failed to wait for nonce; %v" , err )
226
- }
228
+ _ , err = validatorAccount .CreateValidator (SFCContract , rpcClient )
229
+ if err != nil {
230
+ return nil , err
231
+ }
232
+ err = app .WaitUntilAccountNonceIs (crypto .PubkeyToAddress (privateKeyECDSA .PublicKey ), 1 , rpcClient )
233
+ if err != nil {
234
+ return nil , fmt .Errorf ("createValidator; failed to wait for nonce; %v" , err )
235
+ }
227
236
228
- lastValId , err = SFCContract .LastValidatorID (nil )
229
- if err != nil {
230
- return nil , fmt .Errorf ("failed to get validator count; %v" , err )
231
- }
232
- if newValId != int (lastValId .Int64 ()) {
233
- return nil , fmt .Errorf ("failed to create validator %d" , newValId )
234
- }
237
+ lastValId , err = SFCContract .LastValidatorID (nil )
238
+ if err != nil {
239
+ return nil , fmt .Errorf ("failed to get validator count; %v" , err )
240
+ }
241
+ if newValId != int (lastValId .Int64 ()) {
242
+ return nil , fmt .Errorf ("failed to create validator %d" , newValId )
235
243
}
236
244
237
245
return n .createNode (& node.OperaNodeConfig {
@@ -276,7 +284,11 @@ func (n *LocalNetwork) DialRandomRpc() (rpc2.RpcClient, error) {
276
284
return nodes [rand .Intn (len (nodes ))].DialRpc ()
277
285
}
278
286
279
- func (n * LocalNetwork ) dialRandomValidatorRpc () (rpc2.RpcClient , error ) {
287
+ // dialRandomGenesisValidatorRpc dials a random genesis validator node.
288
+ // When network starts and still doesn't have any traffic, then first transaction must come from validator node.
289
+ // Caused by: the regular nodes even when connected won't send transactions from their txpool,
290
+ // because they don't know whether they are on head or not if blockchain is empty.
291
+ func (n * LocalNetwork ) dialRandomGenesisValidatorRpc () (rpc2.RpcClient , error ) {
280
292
return n .validators [rand .Intn (len (n .validators ))].DialRpc ()
281
293
}
282
294
@@ -336,16 +348,8 @@ func (a *localApplication) GetReceivedTransactions() (uint64, error) {
336
348
return a .controller .GetReceivedTransactions ()
337
349
}
338
350
339
- //func (n *LocalNetwork) CreateValidator(config *driver.NodeConfig) (driver.Node, error) {
340
- // return n.createNode(&node.OperaNodeConfig{
341
- // Label: config.Name,
342
- // NetworkConfig: &n.config,
343
- // VmImplementation: n.config.VmImplementation,
344
- // })
345
- //}
346
-
347
351
func (n * LocalNetwork ) CreateApplication (config * driver.ApplicationConfig ) (driver.Application , error ) {
348
- rpcClient , err := n .dialRandomValidatorRpc ()
352
+ rpcClient , err := n .dialRandomGenesisValidatorRpc ()
349
353
if err != nil {
350
354
return nil , fmt .Errorf ("failed to connect to RPC to initialize the application; %v" , err )
351
355
}
0 commit comments