github.com/fozzysec/SiaPrime@v0.0.0-20190612043147-66c8e8d11fe3/modules/miningpool/vardiff.go (about) 1 package pool 2 3 import ( 4 "time" 5 6 "SiaPrime/build" 7 ) 8 9 var ( 10 targetDuration = build.Select(build.Var{ 11 Standard: 5.0, 12 Dev: 5.0, 13 Testing: 3.0, 14 }).(float64) // targeted seconds between shares 15 retargetDuration = build.Select(build.Var{ 16 Standard: 15.0, 17 Dev: 15.0, 18 Testing: 9.0, 19 }).(float64) // how often do we consider changing difficulty 20 variancePercent = build.Select(build.Var{ 21 Standard: 15, 22 Dev: 15, 23 Testing: 15, 24 }).(int) // how much we let the share duration vary between retargetting 25 ) 26 27 // Vardiff is a structure representing maximum and minimum share submission 28 // times, along with the size of the buffer over which share submission times 29 // should be monitored 30 type Vardiff struct { 31 tmax float64 32 tmin float64 33 bufSize uint64 34 } 35 36 func (s *Session) newVardiff() *Vardiff { 37 variance := float64(targetDuration) * (float64(variancePercent) / 100.0) 38 size := uint64(retargetDuration / targetDuration * 4) 39 if size > numSharesToAverage { 40 size = numSharesToAverage 41 } 42 v := &Vardiff{ 43 tmax: targetDuration + variance, 44 tmin: targetDuration - variance, 45 bufSize: numSharesToAverage, 46 } 47 48 s.lastVardiffRetarget = time.Now().Add(time.Duration(-retargetDuration / 2.0)) 49 s.lastVardiffTimestamp = time.Now() 50 51 return v 52 } 53 54 func (s *Session) checkDiffOnNewShare() bool { 55 56 s.lastVardiffTimestamp = time.Now() 57 if time.Now().Sub(s.lastVardiffRetarget).Seconds() < retargetDuration { 58 return false 59 } 60 if s.log != nil { 61 //s.log.Printf("Retargeted Duration: %f\n", time.Now().Sub(s.lastVardiffRetarget).Seconds()) 62 } 63 s.lastVardiffRetarget = time.Now() 64 65 unsubmitDuration, historyDuration := s.ShareDurationAverage() 66 if s.GetDisableVarDiff() { 67 return false 68 } 69 70 if unsubmitDuration > retargetDuration { 71 if s.IsStable() { 72 s.SetCurrentDifficulty(s.CurrentDifficulty() * 3 / 4) 73 } else { 74 s.SetCurrentDifficulty(s.CurrentDifficulty() * 1 / 2) 75 } 76 return true 77 } 78 79 if historyDuration == 0 { 80 if s.log != nil { 81 //s.log.Printf("No historyDuration yet\n") 82 } 83 return false 84 } 85 86 if historyDuration < s.vardiff.tmax && historyDuration > s.vardiff.tmin { // close enough 87 if s.log != nil { 88 //s.log.Printf("HistoryDuration: %f is inside range\n", historyDuration) 89 } 90 return false 91 } 92 93 var deltaDiff float64 94 deltaDiff = float64(targetDuration) / float64(historyDuration) 95 if s.IsStable() { 96 deltaDiff = 1 - (1-deltaDiff)/8 97 } else { 98 deltaDiff = 1 - (1-deltaDiff)/2 99 } 100 101 if deltaDiff > 2.0 { 102 deltaDiff = 2.0 103 } 104 if deltaDiff < 0.5 { 105 deltaDiff = 0.5 106 } 107 108 if s.log != nil { 109 //s.log.Printf("HistoryDuration: %f Delta %f\n", historyDuration, deltaDiff) 110 } 111 112 s.SetCurrentDifficulty(s.CurrentDifficulty() * deltaDiff) 113 return true 114 }