github.com/fozzysec/SiaPrime@v0.0.0-20190612043147-66c8e8d11fe3/modules/miningpool/persist.go (about) 1 package pool 2 3 import ( 4 "os" 5 "unsafe" 6 "sync" 7 "path/filepath" 8 "sync/atomic" 9 10 11 "SiaPrime/config" 12 "SiaPrime/modules" 13 "SiaPrime/persist" 14 "SiaPrime/types" 15 ) 16 17 // persistence is the data that is kept when the pool is restarted. 18 type persistence struct { 19 mu sync.RWMutex 20 21 // Consensus Tracking. 22 BlockHeight types.BlockHeight `json:"blockheight"` 23 RecentChange modules.ConsensusChangeID `json:"recentchange"` 24 25 // Pool Identity. 26 PublicKey types.SiaPublicKey `json:"publickey"` 27 RevisionNumber uint64 `json:"revisionnumber"` 28 Settings modules.PoolInternalSettings `json:"settings"` 29 UnlockHash types.UnlockHash `json:"unlockhash"` 30 31 // Block info 32 Target types.Target `json:"blocktarget"` 33 // Address types.UnlockHash `json:"pooladdress"` 34 BlocksFound []types.BlockID `json:"blocksfound"` 35 } 36 37 func (p *persistence) GetBlockHeight() types.BlockHeight { 38 return (types.BlockHeight)(atomic.LoadUint64((*uint64)(unsafe.Pointer(&p.BlockHeight)))) 39 } 40 41 func (p *persistence) SetBlockHeight(bh types.BlockHeight) { 42 atomic.StoreUint64((*uint64)(unsafe.Pointer(&p.BlockHeight)), (uint64)(bh)) 43 } 44 45 func (p *persistence) GetRecentChange() modules.ConsensusChangeID { 46 p.mu.RLock() 47 defer p.mu.RUnlock() 48 return p.RecentChange 49 } 50 51 func (p *persistence) SetRecentChange(rc modules.ConsensusChangeID) { 52 p.mu.Lock() 53 defer p.mu.Unlock() 54 p.RecentChange = rc 55 } 56 57 func (p *persistence) GetPublicKey() types.SiaPublicKey { 58 p.mu.RLock() 59 defer p.mu.RUnlock() 60 return p.PublicKey 61 } 62 63 func (p *persistence) SetPublicKey(pk types.SiaPublicKey) { 64 p.mu.Lock() 65 defer p.mu.Unlock() 66 p.PublicKey = pk 67 } 68 69 func (p *persistence) GetRevisionNumber() uint64 { 70 return atomic.LoadUint64(&p.RevisionNumber) 71 } 72 73 func (p *persistence) SetRevisionNumber(rn uint64) { 74 atomic.StoreUint64(&p.RevisionNumber, rn) 75 } 76 77 //no on-the-fly pool modification yet, settings won't change after pool startup, no locks needed 78 func (p *persistence) GetSettings() modules.PoolInternalSettings { 79 /*p.mu.RLock() 80 defer p.mu.RUnlock()*/ 81 return p.Settings 82 } 83 84 func (p *persistence) SetSettings(s modules.PoolInternalSettings) { 85 /*p.mu.Lock() 86 defer p.mu.Unlock()*/ 87 p.Settings = s 88 } 89 90 func (p *persistence) GetUnlockHash() types.UnlockHash { 91 p.mu.RLock() 92 defer p.mu.RUnlock() 93 return p.UnlockHash 94 } 95 96 func (p *persistence) SetUnlockHash(h types.UnlockHash) { 97 p.mu.Lock() 98 defer p.mu.Unlock() 99 p.UnlockHash = h 100 } 101 102 func (p *persistence) GetTarget() types.Target { 103 p.mu.RLock() 104 defer p.mu.RUnlock() 105 return p.Target 106 } 107 108 func (p *persistence) SetTarget(t types.Target) { 109 p.mu.Lock() 110 defer p.mu.Unlock() 111 p.Target = t 112 } 113 114 func (p *persistence) GetBlocksFound() []types.BlockID { 115 p.mu.RLock() 116 defer p.mu.RUnlock() 117 return p.BlocksFound 118 } 119 120 func (p *persistence) SetBlocksFound(bf []types.BlockID) { 121 p.mu.Lock() 122 defer p.mu.Unlock() 123 p.BlocksFound = bf 124 } 125 126 // persistData returns a copy of the data in the Pool that will be saved to disk. 127 func (mp *Pool) persistData() persistence { 128 return persistence{ 129 BlockHeight: mp.persist.GetBlockHeight(), 130 RecentChange: mp.persist.GetRecentChange(), 131 PublicKey: mp.persist.GetPublicKey(), 132 RevisionNumber: mp.persist.GetRevisionNumber(), 133 Settings: mp.persist.GetSettings(), 134 UnlockHash: mp.persist.GetUnlockHash(), 135 Target: mp.persist.GetTarget(), 136 BlocksFound: mp.persist.GetBlocksFound(), 137 } 138 } 139 140 // establishDefaults configures the default settings for the pool, overwriting 141 // any existing settings. 142 func (mp *Pool) establishDefaults() error { 143 // Configure the settings object. 144 /* 145 mp.persist.SetSettings(modules.PoolInternalSettings{ 146 PoolName: "", 147 PoolWallet: types.UnlockHash{}, 148 PoolNetworkPort: 3355, 149 PoolDBConnection: "user:pass@127.0.0.1/HDCPool", 150 }) 151 mp.newSourceBlock() 152 */ 153 return nil 154 } 155 156 // loadPersistObject will take a persist object and copy the data into the 157 // host. 158 func (mp *Pool) loadPersistObject(p *persistence) { 159 // Copy over consensus tracking. 160 mp.persist.SetBlockHeight(p.GetBlockHeight()) 161 mp.persist.SetRecentChange(p.GetRecentChange()) 162 163 mp.persist.SetPublicKey(p.GetPublicKey()) 164 mp.persist.SetRevisionNumber(p.GetRevisionNumber()) 165 mp.persist.SetSettings(p.GetSettings()) 166 mp.persist.SetUnlockHash(p.GetUnlockHash()) 167 mp.persist.SetTarget(p.GetTarget()) 168 mp.persist.SetBlocksFound(p.GetBlocksFound()) 169 } 170 171 func (mp *Pool) setPoolSettings(initConfig config.MiningPoolConfig) error { 172 mp.log.Debugf("setPoolSettings called\n") 173 var poolWallet types.UnlockHash 174 175 poolWallet.LoadString(initConfig.PoolWallet) 176 internalSettings := modules.PoolInternalSettings{ 177 PoolNetworkPort: initConfig.PoolNetworkPort, 178 PoolName: initConfig.PoolName, 179 PoolID: initConfig.PoolID, 180 PoolRedisConnection: initConfig.PoolRedisConnection, 181 //PoolDBConnection: initConfig.PoolDBConnection, 182 PoolWallet: poolWallet, 183 } 184 mp.persist.SetSettings(internalSettings) 185 mp.newSourceBlock() 186 return nil 187 } 188 189 func (mp *Pool) hasSettings() bool { 190 _, err := os.Stat(filepath.Join(mp.persistDir, settingsFile)) 191 if err == nil { 192 return true 193 } 194 if os.IsNotExist(err) { 195 return false 196 } 197 return true 198 } 199 200 // load loads the Hosts's persistent data from disk. 201 func (mp *Pool) load() error { 202 203 // Load the old persistence object from disk. Simple task if the version is 204 // the most recent version, but older versions need to be updated to the 205 // more recent structures. 206 p := new(persistence) 207 err := mp.dependencies.loadFile(persistMetadata, p, filepath.Join(mp.persistDir, settingsFile)) 208 mp.log.Printf("Loading persistence metadata") 209 if err == nil { 210 // Copy in the persistence. 211 mp.loadPersistObject(p) 212 } else if os.IsNotExist(err) { 213 mp.log.Printf("Persistence metadata not found.") 214 // There is no pool.json file, set up sane defaults. 215 // return mp.establishDefaults() 216 return nil 217 } else if err != nil { 218 return err 219 } 220 221 return nil 222 } 223 224 // saveSync stores all of the persist data to disk and then syncs to disk. 225 func (mp *Pool) saveSync() error { 226 return persist.SaveJSON(persistMetadata, mp.persistData(), filepath.Join(mp.persistDir, settingsFile)) 227 }