diff --git a/cmd/miner/common/rpc.go b/cmd/miner/common/rpc.go deleted file mode 100644 index 702d7410..00000000 --- a/cmd/miner/common/rpc.go +++ /dev/null @@ -1,134 +0,0 @@ -package common - -import ( - "bytes" - "crypto/tls" - "crypto/x509" - "encoding/json" - "github.com/Qitmeer/qng/cmd/miner/common/socks" - "github.com/Qitmeer/qng/rpc/client/cmds" - "io/ioutil" - "net" - "net/http" - "strings" - "time" -) - -type RpcClient struct { - Cfg *GlobalConfig - GbtID int64 - SubmitID int64 -} - -// newHTTPClient returns a new HTTP client that is configured according to the -// proxy and TLS settings in the associated connection configuration. -func (rpc *RpcClient) newHTTPClient() (*http.Client, error) { - // Configure proxy if needed. - var dial func(network, addr string) (net.Conn, error) - if rpc.Cfg.OptionConfig.Proxy != "" { - proxy := &socks.Proxy{ - Addr: rpc.Cfg.OptionConfig.Proxy, - Username: rpc.Cfg.OptionConfig.ProxyUser, - Password: rpc.Cfg.OptionConfig.ProxyPass, - } - dial = func(network, addr string) (net.Conn, error) { - c, err := proxy.Dial(network, addr) - if err != nil { - return nil, err - } - return c, nil - } - } - - // Configure TLS if needed. - var tlsConfig *tls.Config - if !rpc.Cfg.SoloConfig.NoTLS && rpc.Cfg.SoloConfig.RPCCert != "" { - pem, err := ioutil.ReadFile(rpc.Cfg.SoloConfig.RPCCert) - if err != nil { - return nil, err - } - - pool := x509.NewCertPool() - pool.AppendCertsFromPEM(pem) - tlsConfig = &tls.Config{ - RootCAs: pool, - InsecureSkipVerify: rpc.Cfg.SoloConfig.NoTLS, - } - } else { - tlsConfig = &tls.Config{ - InsecureSkipVerify: rpc.Cfg.SoloConfig.NoTLS, - } - } - - // Create and return the new HTTP client potentially configured with a - // proxy and TLS. - client := http.Client{ - Transport: &http.Transport{ - Dial: dial, - TLSClientConfig: tlsConfig, - DialContext: (&net.Dialer{ - Timeout: time.Duration(rpc.Cfg.OptionConfig.Timeout) * time.Second, - KeepAlive: time.Duration(rpc.Cfg.OptionConfig.Timeout) * time.Second, - DualStack: true, - }).DialContext, - }, - } - return &client, nil -} - -func (rpc *RpcClient) RpcResult(method string, params []interface{}, id string) []byte { - protocol := "http" - if !rpc.Cfg.SoloConfig.NoTLS { - protocol = "https" - } - req, err := cmds.NewRequest(id, method, params) - url := rpc.Cfg.SoloConfig.RPCServer - if !strings.Contains(rpc.Cfg.SoloConfig.RPCServer, "://") { - url = protocol + "://" + url - } - bodyB, err := json.Marshal(req) - if err != nil { - MinerLoger.Error("request failed ", "error", err) - return nil - } - bodyBuff := bytes.NewBuffer(bodyB) - httpRequest, err := http.NewRequest("POST", url, bodyBuff) - if err != nil { - MinerLoger.Error("rpc connect failed ", "error", err) - return nil - } - httpRequest.Close = true - httpRequest.Header.Set("Content-Type", "application/json") - // Configure basic access authorization. - httpRequest.SetBasicAuth(rpc.Cfg.SoloConfig.RPCUser, rpc.Cfg.SoloConfig.RPCPassword) - - // Create the new HTTP client that is configured according to the user- - // specified options and submit the request. - httpClient, err := rpc.newHTTPClient() - if err != nil { - MinerLoger.Error("rpc auth faild ", "error", err) - return nil - } - defer httpClient.CloseIdleConnections() - httpClient.Timeout = time.Duration(rpc.Cfg.OptionConfig.Timeout) * time.Second - httpResponse, err := httpClient.Do(httpRequest) - if err != nil { - MinerLoger.Error("rpc request faild ", "error", err) - return nil - } - defer func() { - _ = httpResponse.Body.Close() - }() - body, err := ioutil.ReadAll(httpResponse.Body) - if err != nil { - MinerLoger.Error("error reading json reply", "error", err) - return nil - } - - if httpResponse.StatusCode != 200 { - time.Sleep(30 * time.Second) - MinerLoger.Error("error http response", "status", httpResponse.Status, "body", string(body), "wait sec", 30) - return nil - } - return body -} diff --git a/cmd/miner/core/robot.go b/cmd/miner/core/robot.go index c295c0a7..97508b70 100644 --- a/cmd/miner/core/robot.go +++ b/cmd/miner/core/robot.go @@ -5,9 +5,10 @@ package core import ( "context" - "github.com/Qitmeer/qng/cmd/miner/common" "strings" "sync" + + "github.com/Qitmeer/qng/cmd/miner/common" ) //var devicesTypesForMining = cl.DeviceTypeAll @@ -30,13 +31,12 @@ type MinerRobot struct { Quit context.Context Work *Work ClDevices []string - Rpc *common.RpcClient Pool bool SubmitStr chan string UseDevices []string } -//init GPU device +// init GPU device func (this *MinerRobot) InitDevice() { this.UseDevices = []string{} if this.Cfg.OptionConfig.UseDevices != "" { diff --git a/cmd/miner/core/work.go b/cmd/miner/core/work.go index d3098841..a1de1e1f 100644 --- a/cmd/miner/core/work.go +++ b/cmd/miner/core/work.go @@ -5,9 +5,10 @@ package core import ( "context" + "sync" + "github.com/Qitmeer/qng/cmd/miner/common" "github.com/Qitmeer/qng/core/types" - "sync" ) const SYMBOL_PMEER = "MEER" @@ -19,10 +20,9 @@ type BaseWork interface { PoolSubmit(subm string) error } -//standard work template +// standard work template type Work struct { Cfg *common.GlobalConfig - Rpc *common.RpcClient Clean bool sync.Mutex Quit context.Context diff --git a/cmd/miner/main.go b/cmd/miner/main.go index 0711d6d6..ad5065d9 100644 --- a/cmd/miner/main.go +++ b/cmd/miner/main.go @@ -5,20 +5,21 @@ package main import ( "context" - "github.com/Qitmeer/qng/cmd/miner/common" - "github.com/Qitmeer/qng/cmd/miner/core" - qitmeer "github.com/Qitmeer/qng/cmd/miner/symbols/lib" "log" "os" "os/signal" "runtime" "strings" "time" + + "github.com/Qitmeer/qng/cmd/miner/common" + "github.com/Qitmeer/qng/cmd/miner/core" + qitmeer "github.com/Qitmeer/qng/cmd/miner/symbols/lib" ) var robotminer core.Robot -//init the config file +// init the config file func init() { cfg, _, err := common.LoadConfig() if err != nil { @@ -50,7 +51,7 @@ func main() { common.MinerLoger.Info("All services exited") } -//get current coin miner +// get current coin miner func GetRobot(cfg *common.GlobalConfig) core.Robot { switch strings.ToUpper(cfg.NecessaryConfig.Symbol) { case core.SYMBOL_PMEER: @@ -58,7 +59,6 @@ func GetRobot(cfg *common.GlobalConfig) core.Robot { r.Cfg = cfg r.NeedGBT = make(chan struct{}, 1) r.Started = uint32(time.Now().Unix()) - r.Rpc = &common.RpcClient{Cfg: cfg} r.SubmitStr = make(chan string) r.PendingBlocks = map[string]qitmeer.PendingBlock{} return r diff --git a/cmd/miner/symbols/lib/robot.go b/cmd/miner/symbols/lib/robot.go index 56c4c0d8..577fe8dc 100644 --- a/cmd/miner/symbols/lib/robot.go +++ b/cmd/miner/symbols/lib/robot.go @@ -103,7 +103,6 @@ func (this *QitmeerRobot) Run(ctx context.Context) { common.MinerLoger.Info(fmt.Sprintf("[%s miner] start", connectName)) this.Work = QitmeerWork{} this.Work.Cfg = this.Cfg - this.Work.Rpc = this.Rpc this.Work.stra = this.Stratu this.Work.Quit = this.Quit this.Work.WorkLock = sync.Mutex{} diff --git a/cmd/miner/symbols/lib/work.go b/cmd/miner/symbols/lib/work.go index 4db8011f..c58e2abd 100644 --- a/cmd/miner/symbols/lib/work.go +++ b/cmd/miner/symbols/lib/work.go @@ -34,6 +34,8 @@ type QitmeerWork struct { WorkLock sync.Mutex WsClient *client.Client LastSubmit time.Time + GbtID int64 + SubmitID int64 } func (this *QitmeerWork) GetPowType() pow.PowType { @@ -83,14 +85,14 @@ func (this *QitmeerWork) Get() bool { // BuildBlock func (this *QitmeerWork) BuildBlock(header *types.BlockHeader) bool { - this.Rpc.GbtID++ + this.GbtID++ this.Block = &BlockHeader{} this.Block.ParentRoot = header.ParentRoot this.Block.WorkData = header.BlockData() this.Block.Target = fmt.Sprintf("%064x", pow.CompactToBig(header.Difficulty)) - this.Block.GBTID = this.Rpc.GbtID - common.LatestGBTID = this.Rpc.GbtID - common.MinerLoger.Debug(fmt.Sprintf("getRemoteBlockTemplate , target :%s , GBTID:%d", this.Block.Target, this.Rpc.GbtID)) + this.Block.GBTID = this.GbtID + common.LatestGBTID = this.GbtID + common.MinerLoger.Debug(fmt.Sprintf("getRemoteBlockTemplate , target :%s , GBTID:%d", this.Block.Target, this.GbtID)) this.GetWorkTime = time.Now().Unix() return true } @@ -100,13 +102,13 @@ func (this *QitmeerWork) Submit(header *types.BlockHeader, gbtID string) (string this.Lock() defer this.Unlock() gbtIDInt64, _ := strconv.ParseInt(gbtID, 10, 64) - if this.Rpc.GbtID != gbtIDInt64 { - common.MinerLoger.Debug(fmt.Sprintf("gbt old , target :%d , current:%d", this.Rpc.GbtID, gbtIDInt64)) + if this.GbtID != gbtIDInt64 { + common.MinerLoger.Debug(fmt.Sprintf("gbt old , target :%d , current:%d", this.GbtID, gbtIDInt64)) return "", 0, ErrSameWork } - this.Rpc.SubmitID++ + this.SubmitID++ - id := fmt.Sprintf("miner_submit_gbtID:%s_id:%d", gbtID, this.Rpc.SubmitID) + id := fmt.Sprintf("miner_submit_gbtID:%s_id:%d", gbtID, this.SubmitID) res, err := this.WsClient.SubmitBlockHeader(header) if err != nil { common.MinerLoger.Error("[submit error] " + id + " " + err.Error())