github.com/lbryio/lbcd@v0.22.119/rpcclient/examples/lbcdblocknotify/stratumclient.go (about)

     1  package main
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  	"net"
     7  )
     8  
     9  type stratumClient struct {
    10  	server string
    11  	passwd string
    12  	coinid string
    13  	conn   *net.TCPConn
    14  }
    15  
    16  func newStratumClient(server, passwd, coinid string) *stratumClient {
    17  
    18  	return &stratumClient{
    19  		server: server,
    20  	}
    21  }
    22  
    23  func (c *stratumClient) dial() error {
    24  
    25  	addr, err := net.ResolveTCPAddr("tcp", c.server)
    26  	if err != nil {
    27  		return fmt.Errorf("resolve tcp addr: %w", err)
    28  	}
    29  
    30  	conn, err := net.DialTCP("tcp", nil, addr)
    31  	if err != nil {
    32  		return fmt.Errorf("dial tcp: %w", err)
    33  	}
    34  	c.conn = conn
    35  
    36  	return nil
    37  }
    38  
    39  func (c *stratumClient) send(ctx context.Context, msg string) error {
    40  
    41  	select {
    42  	case <-ctx.Done():
    43  		return ctx.Err()
    44  	default:
    45  	}
    46  
    47  	_, err := c.conn.Write([]byte(msg))
    48  
    49  	return err
    50  }
    51  
    52  func stratumUpdateBlockMsg(stratumPass, coinid, blockHash string) string {
    53  
    54  	return fmt.Sprintf(`{"id":1,"method":"mining.update_block","params":[%q,%s,%q]}`,
    55  		stratumPass, coinid, blockHash)
    56  }