gitlab.com/SiaPrime/SiaPrime@v1.4.1/modules/miningpool/vardiff.go (about) 1 package pool 2 3 import ( 4 "time" 5 6 "gitlab.com/SiaPrime/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("------------------------\n\n") 62 s.log.Printf("Retargeted Duration: %f\n", time.Now().Sub(s.lastVardiffRetarget).Seconds()) 63 } 64 s.lastVardiffRetarget = time.Now() 65 66 unsubmitDuration, historyDuration := s.ShareDurationAverage() 67 if s.disableVarDiff { 68 if s.log != nil { 69 s.log.Printf("HistoryDuration: %f, var diff disabled, current diff: %f\n", historyDuration, s.currentDifficulty) 70 } 71 return false 72 } 73 74 if unsubmitDuration > retargetDuration { 75 if s.IsStable() { 76 s.SetCurrentDifficulty(s.CurrentDifficulty() * 3 / 4) 77 } else { 78 s.SetCurrentDifficulty(s.CurrentDifficulty() * 1 / 2) 79 } 80 if s.log != nil { 81 s.log.Printf("UnsubmitDuration too long: %f, Set new difficulty to: %v\n", unsubmitDuration, s.currentDifficulty) 82 } 83 return true 84 } 85 86 if historyDuration == 0 { 87 if s.log != nil { 88 s.log.Printf("No historyDuration yet\n") 89 } 90 return false 91 } 92 93 if historyDuration < s.vardiff.tmax && historyDuration > s.vardiff.tmin { // close enough 94 if s.log != nil { 95 s.log.Printf("HistoryDuration: %f is inside range\n", historyDuration) 96 } 97 return false 98 } 99 100 var deltaDiff float64 101 deltaDiff = float64(targetDuration) / float64(historyDuration) 102 if s.IsStable() { 103 deltaDiff = 1 - (1-deltaDiff)/8 104 } else { 105 deltaDiff = 1 - (1-deltaDiff)/2 106 } 107 108 if deltaDiff > 2.0 { 109 deltaDiff = 2.0 110 } 111 if deltaDiff < 0.5 { 112 deltaDiff = 0.5 113 } 114 115 if s.log != nil { 116 s.log.Printf("HistoryDuration: %f Delta %f\n", historyDuration, deltaDiff) 117 } 118 119 if s.log != nil { 120 s.log.Printf("Old difficulty was %v\n", s.currentDifficulty) 121 } 122 s.SetCurrentDifficulty(s.CurrentDifficulty() * deltaDiff) 123 if s.log != nil { 124 s.log.Printf("Set new difficulty to %v\n", s.currentDifficulty) 125 } 126 return true 127 }