gitlab.com/SiaPrime/SiaPrime@v1.4.1/modules/miningpool/shift.go (about)

     1  package pool
     2  
     3  import (
     4  	"sync/atomic"
     5  	"time"
     6  
     7  	"github.com/sasha-s/go-deadlock"
     8  )
     9  
    10  // A Share is how we track each worker's submissions and their difficulty
    11  type Share struct {
    12  	userid          int64
    13  	workerid        int64
    14  	height          int64
    15  	valid           bool
    16  	difficulty      float64
    17  	reward          float64
    18  	blockDifficulty uint64
    19  	shareReward     float64
    20  	shareDifficulty float64
    21  	time            time.Time
    22  }
    23  
    24  // A Shift is a period over which a worker submits shares. At the end of the
    25  // period, we record those shares into the database.
    26  type Shift struct {
    27  	mu             deadlock.RWMutex
    28  	shiftID        uint64
    29  	pool           uint64
    30  	worker         *Worker
    31  	blockID        uint64
    32  	shares         []Share
    33  	lastShareTime  time.Time
    34  	startShiftTime time.Time
    35  }
    36  
    37  func (p *Pool) newShift(w *Worker) *Shift {
    38  	currentShiftID := atomic.LoadUint64(&p.shiftID)
    39  
    40  	s := &Shift{
    41  		shiftID:        currentShiftID,
    42  		pool:           p.InternalSettings().PoolID,
    43  		worker:         w,
    44  		startShiftTime: time.Now(),
    45  		shares:         make([]Share, 0),
    46  	}
    47  	// fmt.Printf("New Shift: %s, block %d, shift %d\n", w.Name(), currentBlock, currentShiftID)
    48  	return s
    49  }
    50  
    51  // ShiftID returns the shift's unique ID
    52  func (s *Shift) ShiftID() uint64 {
    53  	s.mu.RLock()
    54  	defer s.mu.RUnlock()
    55  	return s.shiftID
    56  }
    57  
    58  // PoolID returns the pool's unique ID. Multiple stratum servers connecting to
    59  // the same database should use unique ids so that workers can be tracked as
    60  // belonging to which server.
    61  func (s *Shift) PoolID() uint64 {
    62  	s.mu.RLock()
    63  	defer s.mu.RUnlock()
    64  	return s.pool
    65  }
    66  
    67  // Shares returns the slice of shares submitted during the shift
    68  func (s *Shift) Shares() []Share {
    69  	s.mu.RLock()
    70  	defer s.mu.RUnlock()
    71  	return s.shares
    72  }
    73  
    74  // IncrementShares adds a new share to the slice of shares processed during
    75  // the shift
    76  func (s *Shift) IncrementShares(share *Share) {
    77  	s.mu.Lock()
    78  	defer s.mu.Unlock()
    79  
    80  	s.shares = append(s.shares, *share)
    81  }
    82  
    83  // IncrementInvalid marks a share as having been invalid
    84  func (s *Shift) IncrementInvalid() {
    85  	s.mu.Lock()
    86  	defer s.mu.Unlock()
    87  	share := &Share{
    88  		userid:   s.worker.Parent().cr.clientID,
    89  		workerid: s.worker.wr.workerID,
    90  		valid:    false,
    91  		time:     time.Now(),
    92  	}
    93  	s.shares = append(s.shares, *share)
    94  }
    95  
    96  // LastShareTime returns the most recent time a share was submitted during this
    97  // shift
    98  func (s *Shift) LastShareTime() time.Time {
    99  	s.mu.RLock()
   100  	defer s.mu.RUnlock()
   101  	return s.lastShareTime
   102  }
   103  
   104  // SetLastShareTime specifies the most recent time a share was submitted during
   105  // this shift
   106  func (s *Shift) SetLastShareTime(t time.Time) {
   107  	s.mu.Lock()
   108  	defer s.mu.Unlock()
   109  	s.lastShareTime = t
   110  }
   111  
   112  // StartShiftTime returns the time this shift started
   113  func (s *Shift) StartShiftTime() time.Time {
   114  	s.mu.RLock()
   115  	defer s.mu.RUnlock()
   116  	return s.startShiftTime
   117  }