mirror of
https://github.com/kaspanet/kaspad.git
synced 2025-06-05 13:46:42 +00:00

* [NOD-90] Update mining simulator to pull latest block template * [NOD-90] Refactor to reduce global state * [NOD-90] Split onMinerSwitch func * [NOD-90] Replace chooseClient with getRandomClient * [NOD-90] Stop ranging over foundBlock to avoid code repetition
68 lines
1.5 KiB
Go
68 lines
1.5 KiB
Go
package main
|
|
|
|
import (
|
|
"fmt"
|
|
"io/ioutil"
|
|
"log"
|
|
"time"
|
|
|
|
"github.com/daglabs/btcd/dagconfig/daghash"
|
|
"github.com/daglabs/btcd/rpcclient"
|
|
)
|
|
|
|
type simulatorClient struct {
|
|
*rpcclient.Client
|
|
onBlockAdded chan struct{}
|
|
}
|
|
|
|
func connectToServers(cfg *config, addressList []string) ([]*simulatorClient, error) {
|
|
clients := make([]*simulatorClient, len(addressList))
|
|
|
|
var cert []byte
|
|
if !cfg.DisableTLS {
|
|
var err error
|
|
cert, err = ioutil.ReadFile(cfg.CertificatePath)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("Error reading certificates file: %s", err)
|
|
}
|
|
}
|
|
|
|
for i, address := range addressList {
|
|
onBlockAdded := make(chan struct{}, 1)
|
|
ntfnHandlers := &rpcclient.NotificationHandlers{
|
|
OnBlockAdded: func(hash *daghash.Hash, height int32, t time.Time) {
|
|
onBlockAdded <- struct{}{}
|
|
},
|
|
}
|
|
connCfg := &rpcclient.ConnConfig{
|
|
Host: address,
|
|
Endpoint: "ws",
|
|
User: "user",
|
|
Pass: "pass",
|
|
DisableTLS: cfg.DisableTLS,
|
|
}
|
|
|
|
if !cfg.DisableTLS {
|
|
connCfg.Certificates = cert
|
|
}
|
|
|
|
client, err := rpcclient.New(connCfg, ntfnHandlers)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("Error connecting to address %s: %s", address, err)
|
|
}
|
|
|
|
if err := client.NotifyBlocks(); err != nil {
|
|
return nil, fmt.Errorf("Error while registering client %s for block notifications: %s", client.Host(), err)
|
|
}
|
|
|
|
clients[i] = &simulatorClient{
|
|
Client: client,
|
|
onBlockAdded: onBlockAdded,
|
|
}
|
|
|
|
log.Printf("Connected to server %s", address)
|
|
}
|
|
|
|
return clients, nil
|
|
}
|