Skip to content

Commit

Permalink
pool: Gen extra nonce before initialize.
Browse files Browse the repository at this point in the history
This modifies the new client logic to create the extra nonce before to
creating the client and using it during initialization.  Constructors
should generally handle everything that can error prior to creating the
object since it is wasteful to create an object only to immediately
throw it away when there is an error.
  • Loading branch information
davecgh authored and jholdstock committed Sep 14, 2023
1 parent 7b1fff0 commit b96abb7
Showing 1 changed file with 18 additions and 27 deletions.
45 changes: 18 additions & 27 deletions pool/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,39 +150,30 @@ type Client struct {
wg sync.WaitGroup
}

// generateExtraNonce1 generates a random 4-byte extraNonce1
// for the client.
func (c *Client) generateExtraNonce1() error {
id := make([]byte, 4)
_, err := rand.Read(id)
if err != nil {
return err
}
c.extraNonce1 = hex.EncodeToString(id)
return nil
}

// NewClient creates client connection instance.
func NewClient(ctx context.Context, conn net.Conn, addr *net.TCPAddr, cCfg *ClientConfig) (*Client, error) {
ctx, cancel := context.WithCancel(ctx)
c := &Client{
addr: addr,
cfg: cCfg,
conn: conn,
ctx: ctx,
cancel: cancel,
ch: make(chan Message),
readCh: make(chan readPayload),
encoder: json.NewEncoder(conn),
reader: bufio.NewReaderSize(conn, maxMessageSize),
hashRate: ZeroRat,
}
err := c.generateExtraNonce1()
// Generate a random 4-byte value to use as extraNonce1 for the client.
var extraNonce1 [4]byte
_, err := rand.Read(extraNonce1[:])
if err != nil {
return nil, err
}

return c, nil
ctx, cancel := context.WithCancel(ctx)
c := Client{
addr: addr,
cfg: cCfg,
conn: conn,
ctx: ctx,
cancel: cancel,
ch: make(chan Message),
extraNonce1: hex.EncodeToString(extraNonce1[:]),
readCh: make(chan readPayload),
encoder: json.NewEncoder(conn),
reader: bufio.NewReaderSize(conn, maxMessageSize),
hashRate: ZeroRat,
}
return &c, nil
}

// shutdown terminates all client processes and established connections.
Expand Down

0 comments on commit b96abb7

Please sign in to comment.