gitlab.com/SiaPrime/SiaPrime@v1.4.1/modules/miningpool/client.go (about)

     1  package pool
     2  
     3  import (
     4  	"path/filepath"
     5  
     6  	"github.com/sasha-s/go-deadlock"
     7  
     8  	"gitlab.com/SiaPrime/SiaPrime/persist"
     9  	"gitlab.com/SiaPrime/SiaPrime/types"
    10  )
    11  
    12  //
    13  // A ClientRecord represents the persistent data portion of the Client record
    14  //
    15  type ClientRecord struct {
    16  	clientID int64
    17  	name     string
    18  	wallet   types.UnlockHash
    19  }
    20  
    21  //
    22  // A Client represents a user and may have one or more workers associated with it.  It is primarily used for
    23  // accounting and statistics.
    24  //
    25  type Client struct {
    26  	cr   ClientRecord
    27  	mu   deadlock.RWMutex
    28  	pool *Pool
    29  	log  *persist.Logger
    30  }
    31  
    32  // newClient creates a new Client record
    33  func newClient(p *Pool, name string) (*Client, error) {
    34  	var err error
    35  	// id := p.newStratumID()
    36  	c := &Client{
    37  		cr: ClientRecord{
    38  			name: name,
    39  		},
    40  		pool: p,
    41  	}
    42  	c.cr.wallet.LoadString(name)
    43  	// check if this worker instance is an original or copy
    44  	// TODO why do we need to make a copy instead of the original?
    45  	if p.Client(name) != nil {
    46  		//return c, nil
    47  		return p.Client(name), nil
    48  	}
    49  
    50  	// Create the perist directory if it does not yet exist.
    51  	dirname := filepath.Join(p.persistDir, "clients", name)
    52  	err = p.dependencies.mkdirAll(dirname, 0700)
    53  	if err != nil {
    54  		return nil, err
    55  	}
    56  
    57  	// Initialize the logger, and set up the stop call that will close the
    58  	// logger.
    59  	c.log, err = p.dependencies.newLogger(filepath.Join(dirname, "client.log"))
    60  	if err != nil {
    61  		return nil, err
    62  	}
    63  
    64  	return c, err
    65  }
    66  
    67  // Name returns the client's name, which is usually the wallet address
    68  func (c *Client) Name() string {
    69  	c.mu.RLock()
    70  	defer c.mu.RUnlock()
    71  
    72  	return c.cr.name
    73  }
    74  
    75  // SetName sets the client's name
    76  func (c *Client) SetName(n string) {
    77  	c.mu.Lock()
    78  	defer c.mu.Unlock()
    79  	c.cr.name = n
    80  
    81  }
    82  
    83  // Wallet returns the unlockhash associated with the client
    84  func (c *Client) Wallet() *types.UnlockHash {
    85  	c.mu.RLock()
    86  	defer c.mu.RUnlock()
    87  
    88  	return &c.cr.wallet
    89  }
    90  
    91  // SetWallet sets the unlockhash associated with the client
    92  func (c *Client) SetWallet(w types.UnlockHash) {
    93  	c.mu.Lock()
    94  	defer c.mu.Unlock()
    95  	c.cr.wallet = w
    96  }
    97  
    98  // Pool returns the client's pool
    99  func (c *Client) Pool() *Pool {
   100  	c.mu.RLock()
   101  	defer c.mu.RUnlock()
   102  	return c.pool
   103  }