pool

package
v0.2.3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 11, 2018 License: MIT Imports: 33 Imported by: 2

README

stratum protocol

miner subscribe

{
  "id": 0,
  "method": "mining.subscribe",
  "params": [
    "cgminer/4.10.0" // client version
  ]
}

server respond subscribe

{
  "error": null,
  "id": 0,
  "method": "mining.subscribe",
  "params": null,
  "result": [
    [
      [
        "mining.set_difficulty",
        "88aea8264161e21b" // difficulty
      ],
      [
        "mining.notify",
        "4d65822107fcfe1e" // session id
      ]
    ],
    "1ffefc07", // extra nonce 1
    4 // extra nonce 2 size
  ]
}

miner request authorize

{
  "id": 1,
  "method": "mining.authorize",
  "params": [
    "a2fd0d1916d23262dcc03529ea8f94b95ef097df25f1dffb25aac81e9eb3d31269321e01c16c.obelisk", // address.workername
    "x" // password
  ]
}

server respond authorize success

{
  "error": null,
  "id": 1,
  "method": "mining.authorize",
  "params": null,
  "result": true // sucess
}

server set difficulty

{
  "id": 0,
  "method": "mining.set_difficulty",
  "params": [
    700 // difficulty
  ]
}

server send notify

{
  "id": 0,
  "method": "mining.notify",
  "params": [
    "4d65822107fcfe20", // jobid
    "0000000000000008c499e08b5253cf743ba60432928c4b2d7df943e0ee2e8135", // block parent id
    "000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000010000000000000062000000000000004e6f6e53696100000000000000000000002020202020536f6674776172653a20736961642d6d696e696e67706f6f6c2d6d6f64756c652076302e30330a506f6f6c206e616d653a2022757365617374506f6f6c22202020202000", // marshaled coinB1Txn
    "0000000000000000", // coinb2
    [
      "fe9851f1f0b9922b80eae7a9fc45dc464556f319b757383b45b25fadd9698eb2"
    ], // MerkleBranches
    "", // version
    "19092e48", // nbits
    "ee51825b00000000", // ntime
    true // cleanjobs
  ]
}

miner submit nonce

{
  "params": [
    "a2fd0d1916d23262dcc03529ea8f94b95ef097df25f1dffb25aac81e9eb3d31269321e01c16c.obelisk", // address.workername
    "4d65822107fcfe20", // jobid
    "0f000000", // extra nonce 2
    "ee51825b00000000", // ntime
    "a3d52ac000000000" // nonce
  ],
  "id": 2,
  "method": "mining.submit"
}

server reject

{
  "id": 2,
  "result": false,
  "error": [
    "22",
    "Submitted nonce did not reach pool diff target"
  ],
  "method": "mining.submit",
  "params": null
}

Documentation

Overview

Package pool is an implementation of the pool module, and is responsible for creating a mining pool, accepting incoming potential block solutions and rewarding the submitters proportionally for their shares.

Index

Constants

View Source
const (

	// MajorVersion is the significant version of the pool module
	MajorVersion = 0
	// MinorVersion is the minor version of the pool module
	MinorVersion = 3
	// CoinID is the coin id used by yiimp to associate various records
	// with Siacoin
	CoinID = 1317
	// CoinSymbol is the coin symbol used by yiimp to associate various records
	// with Siacoin
	CoinSymbol = "SPACE"
	// CoinAlgo is the algo used by yiimp to associate various records
	// with blake2b mining
	CoinAlgo = "blake2b"
)

Variables

View Source
var (
	// ErrDuplicateUserInDifferentCoin is an error when a address used in
	// different coin
	ErrDuplicateUserInDifferentCoin = errors.New("duplicate user in different coin, you need use a different address")
	// ErrNoUsernameInDatabase is an error when can't find a username in db
	ErrNoUsernameInDatabase = errors.New("user is not found in db")
	// ErrCreateClient is an error when can't create a new client
	ErrCreateClient = errors.New("Error when creating a new client")
)
View Source
var (

	// HeaderMemory is the number of previous calls to 'header'
	// that are remembered. Additionally, 'header' will only poll for a
	// new block every 'headerMemory / blockMemory' times it is
	// called. This reduces the amount of memory used, but comes at the cost of
	// not always having the most recent transactions.
	HeaderMemory = build.Select(build.Var{
		Standard: 10000,
		Dev:      500,
		Testing:  50,
	}).(int)

	// BlockMemory is the maximum number of blocks the miner will store
	// Blocks take up to 2 megabytes of memory, which is why this number is
	// limited.
	BlockMemory = build.Select(build.Var{
		Standard: 50,
		Dev:      10,
		Testing:  5,
	}).(int)

	// MaxSourceBlockAge is the maximum amount of time that is allowed to
	// elapse between generating source blocks.
	MaxSourceBlockAge = build.Select(build.Var{
		Standard: 30 * time.Second,
		Dev:      5 * time.Second,
		Testing:  1 * time.Second,
	}).(time.Duration)

	// ShiftDuration is how often we commit mining data to persistent
	// storage when a block hasn't been found.
	ShiftDuration = build.Select(build.Var{
		Standard: 20 * time.Second,
		Dev:      20 * time.Second,
		Testing:  1 * time.Second,
	}).(time.Duration)
)

Functions

func BigToCompact

func BigToCompact(n *big.Int) uint32

BigToCompact converts a whole number N to a compact representation using an unsigned 32-bit number. The compact representation only provides 23 bits of precision, so values larger than (2^23 - 1) only encode the most significant digits of the number. See CompactToBig for details.

Types

type Client

type Client struct {
	// contains filtered or unexported fields
}

A Client represents a user and may have one or more workers associated with it. It is primarily used for accounting and statistics.

func (*Client) Name

func (c *Client) Name() string

Name returns the client's name, which is usually the wallet address

func (*Client) Pool

func (c *Client) Pool() *Pool

Pool returns the client's pool

func (*Client) SetName

func (c *Client) SetName(n string)

SetName sets the client's name

func (*Client) SetWallet

func (c *Client) SetWallet(w types.UnlockHash)

SetWallet sets the unlockhash associated with the client

func (*Client) Wallet

func (c *Client) Wallet() *types.UnlockHash

Wallet returns the unlockhash associated with the client

type ClientRecord

type ClientRecord struct {
	// contains filtered or unexported fields
}

A ClientRecord represents the persistent data portion of the Client record

type Dispatcher

type Dispatcher struct {
	// contains filtered or unexported fields
}

Dispatcher contains a map of ip addresses to handlers

func (*Dispatcher) AddHandler

func (d *Dispatcher) AddHandler(conn net.Conn)

AddHandler connects the incoming connection to the handler which will handle it

func (*Dispatcher) ClearJobAndNotifyClients

func (d *Dispatcher) ClearJobAndNotifyClients()

ClearJobAndNotifyClients clear all stale jobs and tells the dispatcher to notify all clients that the block has changed

func (*Dispatcher) IncrementConnectionsOpened

func (d *Dispatcher) IncrementConnectionsOpened()

IncrementConnectionsOpened increments the number of tcp connections that the pool has ever opened

func (*Dispatcher) ListenHandlers

func (d *Dispatcher) ListenHandlers(port string)

ListenHandlers listens on a passed port and upon accepting the incoming connection, adds the handler to deal with it

func (*Dispatcher) NotifyClients

func (d *Dispatcher) NotifyClients()

NotifyClients tells the dispatcher to notify all clients that the block has changed

func (*Dispatcher) NumConnections

func (d *Dispatcher) NumConnections() int

NumConnections returns the number of open tcp connections

func (*Dispatcher) NumConnectionsOpened

func (d *Dispatcher) NumConnectionsOpened() uint64

NumConnectionsOpened returns the number of tcp connections that the pool has ever opened

type Handler

type Handler struct {
	// contains filtered or unexported fields
}

Handler represents the status (open/closed) of each connection

func (*Handler) Listen

func (h *Handler) Listen()

Listen listens on a connection for incoming data and acts on it

type Job

type Job struct {
	JobID           uint64
	MarshalledBlock []byte
	MerkleRoot      crypto.Hash
	SubmitedNonce   map[string]bool
}

A Job in the stratum server is a unit of work which is passed to the client miner to solve. It is primarily identified by a Job ID and this is used to keep track of what work has been assigned to each client

type Pool

type Pool struct {
	modules.StorageManager
	// contains filtered or unexported fields
}

A Pool contains all the fields necessary for storing status for clients and performing the evaluation and rewarding on submitted shares

func New

func New(cs modules.ConsensusSet, tpool modules.TransactionPool, gw modules.Gateway, wallet modules.Wallet, persistDir string, initConfig config.MiningPoolConfig) (*Pool, error)

New returns an initialized Pool.

func (*Pool) AddClient

func (p *Pool) AddClient(c *Client)

AddClient stores the client with the specified name into memory

func (*Pool) AddClientDB

func (p *Pool) AddClientDB(c *Client) error

AddClientDB add user into accounts

func (*Pool) Client

func (p *Pool) Client(name string) *Client

Client returns the client with the specified name that has been stored in memory

func (*Pool) Close

func (p *Pool) Close() error

Close shuts down the pool.

func (*Pool) DeleteAllWorkerRecords

func (p *Pool) DeleteAllWorkerRecords() error

DeleteAllWorkerRecords deletes all worker records associated with a pool. This should be used on pool startup and shutdown to ensure the database is clean and isn't storing any worker records for non-connected workers.

func (*Pool) FindClientDB

func (p *Pool) FindClientDB(name string) (*Client, error)

FindClientDB find user in accounts

func (*Pool) InternalSettings

func (p *Pool) InternalSettings() modules.PoolInternalSettings

InternalSettings returns the settings of a pool.

func (*Pool) NumConnections

func (p *Pool) NumConnections() int

NumConnections returns the number of tcp connections from clients the pool currently has open

func (*Pool) NumConnectionsOpened

func (p *Pool) NumConnectionsOpened() uint64

NumConnectionsOpened returns the total number of tcp connections from clients the pool has opened since startup

func (*Pool) ProcessConsensusChange

func (p *Pool) ProcessConsensusChange(cc modules.ConsensusChange)

ProcessConsensusChange will update the pool's most recent block.

func (*Pool) ReceiveUpdatedUnconfirmedTransactions

func (p *Pool) ReceiveUpdatedUnconfirmedTransactions(diff *modules.TransactionPoolDiff)

ReceiveUpdatedUnconfirmedTransactions will replace the current unconfirmed set of transactions with the input transactions.

func (*Pool) SetInternalSettings

func (p *Pool) SetInternalSettings(settings modules.PoolInternalSettings) error

SetInternalSettings updates the pool's internal PoolInternalSettings object.

type Session

type Session struct {
	SessionID   uint64
	CurrentJobs []*Job

	Client        *Client
	CurrentWorker *Worker
	CurrentShift  *Shift
	ExtraNonce1   uint32
	// contains filtered or unexported fields
}

A Session captures the interaction with a miner client from when they connect until the connection is closed. A session is tied to a single client and has many jobs associated with it

func (*Session) Authorized

func (s *Session) Authorized() bool

Authorized returns whether or not the session has been authorized

func (*Session) CurrentDifficulty

func (s *Session) CurrentDifficulty() float64

CurrentDifficulty returns the session's current difficulty

func (*Session) DetectDisconnected

func (s *Session) DetectDisconnected() bool

DetectDisconnected checks to see if we haven't heard from a client for too long of a time. It does this via 2 mechanisms:

  1. how long ago was the last share submitted? (the hearbeat)
  2. how low has the difficulty dropped from the highest difficulty the client ever faced?

func (*Session) HighestDifficulty

func (s *Session) HighestDifficulty() float64

HighestDifficulty returns the highest difficulty the session has seen

func (*Session) IsStable

func (s *Session) IsStable() bool

IsStable checks if the session has been running long enough to fill up the vardiff buffer

func (*Session) SetAuthorized

func (s *Session) SetAuthorized(b bool)

SetAuthorized specifies whether or not the session has been authorized

func (*Session) SetClientVersion

func (s *Session) SetClientVersion(v string)

SetClientVersion sets the current client version for the session

func (*Session) SetCurrentDifficulty

func (s *Session) SetCurrentDifficulty(d float64)

SetCurrentDifficulty sets the current difficulty for the session

func (*Session) SetDisableVarDiff

func (s *Session) SetDisableVarDiff(flag bool)

SetDisableVarDiff sets the disable var diff flag for the session

func (*Session) SetHeartbeat

func (s *Session) SetHeartbeat()

SetHeartbeat indicates that we just received a share submission

func (*Session) SetHighestDifficulty

func (s *Session) SetHighestDifficulty(d float64)

SetHighestDifficulty records the highest difficulty the session has seen

func (*Session) SetLastShareTimestamp

func (s *Session) SetLastShareTimestamp(t time.Time)

SetLastShareTimestamp add a new time stamp

func (*Session) ShareDurationAverage

func (s *Session) ShareDurationAverage() (float64, float64)

ShareDurationAverage caculate the average duration of the

func (*Session) Shift

func (s *Session) Shift() *Shift

Shift returns the current Shift associated with a session

type Share

type Share struct {
	// contains filtered or unexported fields
}

A Share is how we track each worker's submissions and their difficulty

type Shift

type Shift struct {
	// contains filtered or unexported fields
}

A Shift is a period over which a worker submits shares. At the end of the period, we record those shares into the database.

func (*Shift) IncrementInvalid

func (s *Shift) IncrementInvalid()

IncrementInvalid marks a share as having been invalid

func (*Shift) IncrementShares

func (s *Shift) IncrementShares(share *Share)

IncrementShares adds a new share to the slice of shares processed during the shift

func (*Shift) LastShareTime

func (s *Shift) LastShareTime() time.Time

LastShareTime returns the most recent time a share was submitted during this shift

func (*Shift) PoolID

func (s *Shift) PoolID() uint64

PoolID returns the pool's unique ID. Multiple stratum servers connecting to the same database should use unique ids so that workers can be tracked as belonging to which server.

func (*Shift) SaveShift

func (s *Shift) SaveShift() error

SaveShift periodically saves the shares for a given worker to the db

func (*Shift) SetLastShareTime

func (s *Shift) SetLastShareTime(t time.Time)

SetLastShareTime specifies the most recent time a share was submitted during this shift

func (*Shift) Shares

func (s *Shift) Shares() []Share

Shares returns the slice of shares submitted during the shift

func (*Shift) ShiftID

func (s *Shift) ShiftID() uint64

ShiftID returns the shift's unique ID

func (*Shift) StartShiftTime

func (s *Shift) StartShiftTime() time.Time

StartShiftTime returns the time this shift started

type Vardiff

type Vardiff struct {
	// contains filtered or unexported fields
}

Vardiff is a structure representing maximum and minimum share submission times, along with the size of the buffer over which share submission times should be monitored

type Worker

type Worker struct {
	// contains filtered or unexported fields
}

A Worker is an instance of one miner. A Client often represents a user and the worker represents a single miner. There is a one to many client worker relationship

func (*Worker) CurrentDifficulty

func (w *Worker) CurrentDifficulty() float64

CurrentDifficulty returns the average difficulty of all instances of this worker

func (*Worker) IncrementInvalidShares

func (w *Worker) IncrementInvalidShares()

IncrementInvalidShares adds a record of an invalid share submission

func (*Worker) IncrementShares

func (w *Worker) IncrementShares(sessionDifficulty float64, reward float64)

IncrementShares creates a new share according to current session difficulty for the worker to work on

func (*Worker) LastShareTime

func (w *Worker) LastShareTime() time.Time

LastShareTime returns the last time a share was submitted during the current shift

func (*Worker) Name

func (w *Worker) Name() string

Name return the worker's name, typically a wallet address

func (*Worker) Online

func (w *Worker) Online() bool

Online checks if the worker has a tcp session associated with it

func (*Worker) Parent

func (w *Worker) Parent() *Client

Parent returns the worker's client

func (*Worker) Session

func (w *Worker) Session() *Session

Session returns the tcp session associated with the worker

func (*Worker) SetLastShareTime

func (w *Worker) SetLastShareTime(t time.Time)

SetLastShareTime specifies the last time a share was submitted during the current shift

func (*Worker) SetName

func (w *Worker) SetName(n string)

SetName sets the worker's name

func (*Worker) SetParent

func (w *Worker) SetParent(p *Client)

SetParent sets the worker's client

func (*Worker) SetSession

func (w *Worker) SetSession(s *Session)

SetSession sets the tcp session associated with the worker

type WorkerRecord

type WorkerRecord struct {
	// contains filtered or unexported fields
}

A WorkerRecord is used to track worker information in memory

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL