github.com/fozzysec/SiaPrime@v0.0.0-20190612043147-66c8e8d11fe3/modules/miningpool/shift.go (about) 1 package pool 2 3 import ( 4 "sync" 5 "sync/atomic" 6 "time" 7 8 ) 9 10 // A Share is how we track each worker's submissions and their difficulty 11 type Share struct { 12 valid bool 13 height int64 14 userid uint64 15 workerid uint64 16 blockDifficulty uint64 17 difficulty float64 18 reward float64 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 sync.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 return atomic.LoadUint64(&s.shiftID) 54 } 55 56 // PoolID returns the pool's unique ID. Multiple stratum servers connecting to 57 // the same database should use unique ids so that workers can be tracked as 58 // belonging to which server. 59 func (s *Shift) PoolID() uint64 { 60 return atomic.LoadUint64(&s.pool) 61 } 62 63 // Shares returns the slice of shares submitted during the shift 64 func (s *Shift) Shares() []Share { 65 s.mu.RLock() 66 defer s.mu.RUnlock() 67 return s.shares 68 } 69 70 // IncrementShares adds a new share to the slice of shares processed during 71 // the shift 72 func (s *Shift) IncrementShares(share *Share) { 73 s.mu.Lock() 74 defer s.mu.Unlock() 75 76 s.shares = append(s.shares, *share) 77 } 78 79 // IncrementInvalid marks a share as having been invalid 80 func (s *Shift) IncrementInvalid() { 81 s.mu.Lock() 82 defer s.mu.Unlock() 83 share := &Share{ 84 userid: s.worker.Parent().cr.clientID, 85 workerid: s.worker.GetID(), 86 valid: false, 87 time: time.Now(), 88 } 89 s.shares = append(s.shares, *share) 90 } 91 92 // LastShareTime returns the most recent time a share was submitted during this 93 // shift 94 func (s *Shift) LastShareTime() time.Time { 95 s.mu.RLock() 96 defer s.mu.RUnlock() 97 return s.lastShareTime 98 } 99 100 // SetLastShareTime specifies the most recent time a share was submitted during 101 // this shift 102 func (s *Shift) SetLastShareTime(t time.Time) { 103 s.mu.Lock() 104 defer s.mu.Unlock() 105 s.lastShareTime = t 106 } 107 108 // StartShiftTime returns the time this shift started 109 func (s *Shift) StartShiftTime() time.Time { 110 s.mu.RLock() 111 defer s.mu.RUnlock() 112 return s.startShiftTime 113 }