Skip to content

Commit

Permalink
multi: resolve review issues (1 of x).
Browse files Browse the repository at this point in the history
  • Loading branch information
dnldd committed Dec 17, 2020
1 parent 409410e commit 9a1120d
Show file tree
Hide file tree
Showing 9 changed files with 61 additions and 82 deletions.
5 changes: 2 additions & 3 deletions gui/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -236,13 +236,12 @@ func (c *Cache) updateHashData(hashData map[string][]*pool.HashData) {
poolHashRate := new(big.Rat).SetInt64(0)
for _, data := range hashData {
for _, entry := range data {
hash, _ := new(big.Rat).SetString(entry.HashRate)
poolHashRate = poolHashRate.Add(poolHashRate, hash)
poolHashRate = poolHashRate.Add(poolHashRate, entry.HashRate)
clientInfo[entry.AccountID] = append(clientInfo[entry.AccountID],
&client{
Miner: entry.Miner,
IP: entry.IP,
HashRate: hashString(hash),
HashRate: hashString(entry.HashRate),
})
}
}
Expand Down
51 changes: 5 additions & 46 deletions pool/boltdb.go
Original file line number Diff line number Diff line change
Expand Up @@ -1373,7 +1373,7 @@ func (db *BoltDB) persistHashData(hashData *HashData) error {
})
}

// updateHashData persists the updated payment to the database.
// updateHashData persists the updated hash data to the database.
func (db *BoltDB) updateHashData(hashData *HashData) error {
const funcName = "updateHashData"
return db.DB.Update(func(tx *bolt.Tx) error {
Expand All @@ -1382,7 +1382,7 @@ func (db *BoltDB) updateHashData(hashData *HashData) error {
return err
}

// Assert the work provided exists before updating.
// Assert the hash data provided exists before updating.
id := []byte(hashData.UUID)
v := bkt.Get(id)
if v == nil {
Expand Down Expand Up @@ -1437,48 +1437,7 @@ func (db *BoltDB) fetchHashData(id string) (*HashData, error) {
return &data, err
}

// fetchAccountHashData fetches all hash data associated with the provided
// account id.
func (db *BoltDB) fetchAccountHashData(id string, minNano int64) ([]*HashData, error) {
const funcName = "fetchAccountHashData"
idB := []byte(id)
data := []*HashData{}

err := db.DB.View(func(tx *bolt.Tx) error {
bkt, err := fetchBucket(tx, hashDataBkt)
if err != nil {
return err
}

cursor := bkt.Cursor()
for k, v := cursor.First(); k != nil; k, v = cursor.Next() {
accID := k[8:]
if !bytes.Equal(idB, accID) {
continue
}

var hashData HashData
err = json.Unmarshal(v, &hashData)
if err != nil {
desc := fmt.Sprintf("%s: unable to unmarshal hash data: %v",
funcName, err)
return errs.DBError(errs.Parse, desc)
}

if hashData.UpdatedOn > minNano {
data = append(data, &hashData)
}
}
return nil
})
if err != nil {
return nil, err
}
return data, err
}

// listHashData fetches all hash data updated before the provided minimum time
// provided.
// listHashData fetches all hash data updated after the provided minimum time.
func (db *BoltDB) listHashData(minNano int64) (map[string][]*HashData, error) {
const funcName = "listHashData"
data := make(map[string][]*HashData)
Expand Down Expand Up @@ -1513,8 +1472,8 @@ func (db *BoltDB) listHashData(minNano int64) (map[string][]*HashData, error) {
return data, err
}

// pruneHashData prunes all hash data that have not been since the provided
// minimum time.
// pruneHashData prunes all hash data that have not been updated since
// the provided minimum time.
func (db *BoltDB) pruneHashData(minNano int64) error {
funcName := "pruneHashData"

Expand Down
8 changes: 4 additions & 4 deletions pool/boltupgrades.go
Original file line number Diff line number Diff line change
Expand Up @@ -404,7 +404,7 @@ func removeTxFeeReserveUpgrade(tx *bolt.Tx) error {
}

if dbVersion != oldVersion {
desc := fmt.Sprintf("%s: inappropriately called", err)
desc := fmt.Sprintf("%s: inappropriately called", funcName)
return errs.DBError(errs.DBUpgrade, desc)
}

Expand Down Expand Up @@ -437,7 +437,7 @@ func shareCreatedOnUpgrade(tx *bolt.Tx) error {
}

if dbVersion != oldVersion {
desc := fmt.Sprintf("%s: inappropriately called", err)
desc := fmt.Sprintf("%s: inappropriately called", funcName)
return errs.DBError(errs.DBUpgrade, desc)
}

Expand Down Expand Up @@ -506,7 +506,7 @@ func paymentUUIDUpgrade(tx *bolt.Tx) error {
}

if dbVersion != oldVersion {
desc := fmt.Sprintf("%s: inappropriately called", err)
desc := fmt.Sprintf("%s: inappropriately called", funcName)
return errs.DBError(errs.DBUpgrade, desc)
}

Expand Down Expand Up @@ -601,7 +601,7 @@ func hashDataUpgrade(tx *bolt.Tx) error {
}

if dbVersion != oldVersion {
desc := fmt.Sprintf("%s: inappropriately called", err)
desc := fmt.Sprintf("%s: inappropriately called", funcName)
return errs.DBError(errs.DBUpgrade, desc)
}

Expand Down
3 changes: 3 additions & 0 deletions pool/chainstate.go
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,9 @@ func (cs *ChainState) handleChainUpdates(ctx context.Context) {
}

// Prune all hash data not updated in the past minute.
// A connected client should have updated multiple times
// in a minute. Only disconnected miners would not have
// updated within the timeframe.
nowNano := time.Now().Add(-time.Minute).UnixNano()
err = cs.cfg.db.pruneHashData(nowNano)
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion pool/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -1137,7 +1137,7 @@ func (c *Client) hashMonitor() {
continue
}

hashData.HashRate = hash.RatString()
hashData.HashRate = hash
hashData.UpdatedOn = time.Now().UnixNano()

err = c.cfg.db.updateHashData(hashData)
Expand Down
14 changes: 7 additions & 7 deletions pool/hashdata.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@ import (
// HashData represents client identification and hashrate information
// for a mining client.
type HashData struct {
UUID string `json:"uuid"`
AccountID string `json:"accountid"`
Miner string `json:"miner"`
IP string `json:"ip"`
HashRate string `json:"hashrate"`
UpdatedOn int64 `json:"updatedon"`
UUID string `json:"uuid"`
AccountID string `json:"accountid"`
Miner string `json:"miner"`
IP string `json:"ip"`
HashRate *big.Rat `json:"hashrate"`
UpdatedOn int64 `json:"updatedon"`
}

// hashDataID generates a unique hash data id.
Expand All @@ -35,7 +35,7 @@ func newHashData(miner string, accountID string, ip string, extraNonce1 string,
return &HashData{
UUID: hashDataID(accountID, extraNonce1),
AccountID: accountID,
HashRate: hashRate.RatString(),
HashRate: hashRate,
IP: ip,
Miner: miner,
UpdatedOn: nowNano,
Expand Down
10 changes: 8 additions & 2 deletions pool/hashdata_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func testHashData(t *testing.T) {
}

// Ensure hash data can be fetched.
hashID := hashDataID(xID, extraNonce1)
hashID := hashData.UUID
fetchedHashData, err := db.fetchHashData(hashID)
if err != nil {
t.Fatal(err)
Expand All @@ -37,7 +37,7 @@ func testHashData(t *testing.T) {
hashData.UpdatedOn, fetchedHashData.UpdatedOn)
}

if fetchedHashData.HashRate != hashData.HashRate {
if fetchedHashData.HashRate.RatString() != hashData.HashRate.RatString() {
t.Fatalf("expected hash rate value of %v, got %v",
hashData.HashRate, fetchedHashData.HashRate)
}
Expand Down Expand Up @@ -88,6 +88,12 @@ func testHashData(t *testing.T) {
t.Fatalf("expected one hash data, got %d", len(dataset))
}

// Ensure the account id is a key of the hash data set returned.
_, ok := dataset[xID]
if !ok {
t.Fatalf("expected dataset to have %s as an account id key", xID)
}

newUpdatedOn := hashData.UpdatedOn + 100
hashData.UpdatedOn = newUpdatedOn

Expand Down
3 changes: 2 additions & 1 deletion pool/hub.go
Original file line number Diff line number Diff line change
Expand Up @@ -571,7 +571,8 @@ func (h *Hub) Run(ctx context.Context) {
h.shutdown()
}

// FetchClients returns all hash data from connected pool clients.
// FetchHashData returns all hash data from connected pool clients
// which have been updated in the last minute.
func (h *Hub) FetchHashData() (map[string][]*HashData, error) {
aMinuteAgo := time.Now().Add(-time.Minute).UnixNano()
hashData, err := h.cfg.DB.listHashData(aMinuteAgo)
Expand Down
47 changes: 29 additions & 18 deletions pool/postgres.go
Original file line number Diff line number Diff line change
Expand Up @@ -229,9 +229,10 @@ func decodeShareRows(rows *sql.Rows) ([]*Share, error) {

// decodeHashDataRows deserializes the provided SQL rows into a slice of
// HashData structs.
func decodeHashDataRows(rows *sql.Rows) ([]*HashData, error) {
func decodeHashDataRows(rows *sql.Rows) (map[string][]*HashData, error) {
const funcName = "decodeHashDataRows"
var toReturn []*HashData

toReturn := make(map[string][]*HashData)
for rows.Next() {
var uuid, accountID, miner, ip, hashRate string
var updatedOn int64
Expand All @@ -243,8 +244,15 @@ func decodeHashDataRows(rows *sql.Rows) ([]*HashData, error) {
return nil, errs.DBError(errs.Decode, desc)
}

hashData := &HashData{uuid, accountID, miner, ip, hashRate, updatedOn}
toReturn = append(toReturn, hashData)
hashRat, ok := new(big.Rat).SetString(hashRate)
if !ok {
desc := fmt.Sprintf("%s: unable to decode big.Rat string: %v",
funcName, err)
return nil, errs.DBError(errs.Parse, desc)
}

hashData := &HashData{uuid, accountID, miner, ip, hashRat, updatedOn}
toReturn[accountID] = append(toReturn[accountID], hashData)
}

err := rows.Err()
Expand Down Expand Up @@ -992,7 +1000,8 @@ func (db *PostgresDB) persistHashData(hashData *HashData) error {
const funcName = "persistHashData"

_, err := db.DB.Exec(insertHashData, hashData.UUID, hashData.AccountID,
hashData.Miner, hashData.IP, hashData.HashRate, hashData.UpdatedOn)
hashData.Miner, hashData.IP, hashData.HashRate.RatString(),
hashData.UpdatedOn)
if err != nil {

var pqError *pq.Error
Expand All @@ -1010,13 +1019,13 @@ func (db *PostgresDB) persistHashData(hashData *HashData) error {
return nil
}

// updateHashData persists the updated payment to the database.
// updateHashData persists the updated hash data to the database.
func (db *PostgresDB) updateHashData(hashData *HashData) error {
const funcName = "updateHashData"

result, err := db.DB.Exec(updateHashData,
hashData.UUID, hashData.AccountID, hashData.Miner,
hashData.IP, hashData.HashRate, hashData.UpdatedOn)
hashData.IP, hashData.HashRate.RatString(), hashData.UpdatedOn)
if err != nil {
return err
}
Expand Down Expand Up @@ -1053,11 +1062,18 @@ func (db *PostgresDB) fetchHashData(id string) (*HashData, error) {
funcName, id, err)
return nil, errs.DBError(errs.FetchEntry, desc)
}
return &HashData{uuid, accountID, miner, ip, hashRate, updatedOn}, nil

hashRat, ok := new(big.Rat).SetString(hashRate)
if !ok {
desc := fmt.Sprintf("%s: unable to decode big.Rat string: %v",
funcName, err)
return nil, errs.DBError(errs.Parse, desc)
}

return &HashData{uuid, accountID, miner, ip, hashRat, updatedOn}, nil
}

// listHashData fetches all hash data updated before the provided minimum time
// provided.
// listHashData fetches all hash data updated after the provided minimum time.
func (db *PostgresDB) listHashData(minNano int64) (map[string][]*HashData, error) {
const funcName = "listHashData"
rows, err := db.DB.Query(listHashData, minNano)
Expand All @@ -1067,21 +1083,16 @@ func (db *PostgresDB) listHashData(minNano int64) (map[string][]*HashData, error
return nil, errs.DBError(errs.FetchEntry, desc)
}

data, err := decodeHashDataRows(rows)
hashData, err := decodeHashDataRows(rows)
if err != nil {
return nil, err
}

hashData := make(map[string][]*HashData)
for _, entry := range data {
hashData[entry.AccountID] = append(hashData[entry.AccountID], entry)
}

return hashData, nil
}

// pruneHashData prunes all hash data that have not been since the provided
// minimum time.
// pruneHashData prunes all hash data that have not been updated since
// the provided minimum time.
func (db *PostgresDB) pruneHashData(minNano int64) error {
const funcName = "pruneHashData"

Expand Down

0 comments on commit 9a1120d

Please sign in to comment.