gitlab.com/SiaPrime/SiaPrime@v1.4.1/modules/stratumminer/baseclient.go (about) 1 // Package stratumminer provides some utilities and common code for specific client implementations 2 package stratumminer 3 4 import ( 5 "strings" 6 ) 7 8 // NewClient creates a new client given a '[stratum+tcp://]host:port' connectionstring 9 func NewClient(connectionstring, pooluser string) (sc GenericClient) { 10 if strings.HasPrefix(connectionstring, "stratum+tcp://") { 11 sc = &StratumClient{ 12 connectionstring: strings.TrimPrefix(connectionstring, "stratum+tcp://"), 13 User: pooluser, 14 } 15 } else { 16 s := SiadClient{} 17 s.siadurl = "http://" + connectionstring + "/miner/header" 18 sc = &s 19 } 20 return 21 } 22 23 // HeaderReporter defines the required method a SIA client or pool client should implement 24 // for miners to be able to report solved headers 25 type HeaderReporter interface { 26 //SubmitHeader reports a solved header 27 SubmitHeader(header []byte, job interface{}) (err error) 28 } 29 30 // HeaderProvider supplies headers for a miner to mine on 31 type HeaderProvider interface { 32 // GetHeaderForWork providers a header to mine on 33 // the deprecationChannel is closed when the job should be abandoned 34 GetHeaderForWork() (target, header []byte, deprecationChannel chan bool, job interface{}, err error) 35 } 36 37 // DeprecatedJobCall is a function that can be registered on a client to be executed when 38 // the server indicates that all previous jobs should be abandoned 39 type DeprecatedJobCall func() 40 41 // GenericClient defines the interface for a client towards a work provider 42 type GenericClient interface { 43 HeaderProvider 44 HeaderReporter 45 // Start connects to a sia daemon and starts supplying valid headers 46 // It can be empty in case of a "getwork" implementation or maintain a tcp connection 47 // in case of stratum for example 48 Start() 49 // Stop disconnects 50 Stop() 51 // Connected specifies if we're connected or not 52 Connected() bool 53 // SetDeprecatedJobCall sets the function to be called when the previous jobs should be 54 // abandoned 55 SetDeprecatedJobCall(call DeprecatedJobCall) 56 } 57 58 // BaseClient implements some common properties and functionality 59 type BaseClient struct { 60 deprecationChannels map[string]chan bool 61 62 deprecatedJobCall DeprecatedJobCall 63 } 64 65 // DeprecateOutstandingJobs closes all deprecationChannels and removes them from the list 66 // This method is not threadsafe 67 func (sc *BaseClient) DeprecateOutstandingJobs() { 68 if sc.deprecationChannels == nil { 69 sc.deprecationChannels = make(map[string]chan bool) 70 } 71 for jobid, deprecatedJob := range sc.deprecationChannels { 72 close(deprecatedJob) 73 delete(sc.deprecationChannels, jobid) 74 } 75 call := sc.deprecatedJobCall 76 if call != nil { 77 go call() 78 } 79 } 80 81 // AddJobToDeprecate add the jobid to the list of jobs that should be deprecated when the times comes 82 func (sc *BaseClient) AddJobToDeprecate(jobid string) { 83 sc.deprecationChannels[jobid] = make(chan bool) 84 } 85 86 // GetDeprecationChannel return the channel that will be closed when a job gets deprecated 87 func (sc *BaseClient) GetDeprecationChannel(jobid string) chan bool { 88 return sc.deprecationChannels[jobid] 89 } 90 91 // SetDeprecatedJobCall sets the function to be called when the previous jobs should be abandoned 92 func (sc *BaseClient) SetDeprecatedJobCall(call DeprecatedJobCall) { 93 sc.deprecatedJobCall = call 94 }