github.com/fozzysec/SiaPrime@v0.0.0-20190612043147-66c8e8d11fe3/modules/miningpool/client.go (about) 1 package pool 2 3 import ( 4 //"github.com/sasha-s/go-deadlock" 5 "sync" 6 "unsafe" 7 "sync/atomic" 8 9 "SiaPrime/persist" 10 "SiaPrime/types" 11 ) 12 13 // 14 // A ClientRecord represents the persistent data portion of the Client record 15 // 16 type ClientRecord struct { 17 //clientID int64 18 clientID uint64 19 name string 20 wallet types.UnlockHash 21 } 22 23 // 24 // A Client represents a user and may have one or more workers associated with it. It is primarily used for 25 // accounting and statistics. 26 // 27 type Client struct { 28 cr ClientRecord 29 mu sync.RWMutex 30 pool *Pool 31 log *persist.Logger 32 } 33 34 // newClient creates a new Client record 35 func newClient(p *Pool, name string) (*Client, error) { 36 // id := p.newStratumID() 37 c := &Client{ 38 cr: ClientRecord{ 39 name: name, 40 }, 41 pool: p, 42 log: p.log, 43 } 44 c.cr.wallet.LoadString(name) 45 // check if this worker instance is an original or copy 46 // TODO why do we need to make a copy instead of the original? 47 if p.Client(name) != nil { 48 //return c, nil 49 return p.Client(name), nil 50 } 51 52 return c, nil 53 } 54 55 // Name returns the client's name, which is usually the wallet address 56 func (c *Client) Name() string { 57 c.mu.RLock() 58 defer c.mu.RUnlock() 59 60 return c.cr.name 61 } 62 63 // SetName sets the client's name 64 func (c *Client) SetName(n string) { 65 c.mu.Lock() 66 defer c.mu.Unlock() 67 c.cr.name = n 68 69 } 70 71 func (c *Client) SetID(id uint64) { 72 atomic.StoreUint64(&c.cr.clientID, id) 73 } 74 75 func (c *Client) GetID() uint64 { 76 return atomic.LoadUint64(&c.cr.clientID) 77 } 78 79 // Wallet returns the unlockhash associated with the client 80 func (c *Client) Wallet() *types.UnlockHash { 81 return (*types.UnlockHash)(atomic.LoadPointer((*unsafe.Pointer)(unsafe.Pointer(&c.cr.wallet)))) 82 } 83 84 // SetWallet sets the unlockhash associated with the client 85 func (c *Client) SetWallet(w types.UnlockHash) { 86 c.mu.Lock() 87 defer c.mu.Unlock() 88 c.cr.wallet = w 89 } 90 91 // Pool returns the client's pool 92 //no lock needed, a client's pool won't change 93 func (c *Client) Pool() *Pool { 94 return (*Pool)(atomic.LoadPointer((*unsafe.Pointer)(unsafe.Pointer(&c.pool)))) 95 }