Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Payment feature #4

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion config/daemon.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ type DaemonOptions struct {
}

func (d *DaemonOptions) String() string {
return d.User + ":" + d.Password + "@" + d.Host + strconv.FormatInt(int64(d.Port), 10)
return d.User + "@" + d.Host + ":" + strconv.FormatInt(int64(d.Port), 10)
}

func (d *DaemonOptions) URL() string {
Expand Down
15 changes: 8 additions & 7 deletions config/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,14 @@ type Options struct {
EmitInvalidBlockHashes bool `json:"emitInvalidBlockHashes"`
TCPProxyProtocol bool `json:"tcpProxyProtocol"` // http://www.haproxy.org/download/1.8/doc/proxy-protocol.txt

API *APIOptions `json:"api"`
Banning *BanningOptions `json:"banning"`
Ports map[int]*PortOptions `json:"ports"`
Daemons []*DaemonOptions `json:"daemons"`
P2P *P2POptions `json:"p2p"`
Storage *RedisOptions `json:"storage"`
Algorithm *AlgorithmOptions `json:"algorithm"`
API *APIOptions `json:"api"`
Banning *BanningOptions `json:"banning"`
Ports map[int]*PortOptions `json:"ports"`
Daemons []*DaemonOptions `json:"daemons"`
P2P *P2POptions `json:"p2p"`
Storage *RedisOptions `json:"storage"`
Algorithm *AlgorithmOptions `json:"algorithm"`
PaymentOptions *PaymentOptions `json:"payment"`
}

func (o *Options) TotalFeePercent() float64 {
Expand Down
7 changes: 7 additions & 0 deletions config/payment.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,10 @@ func (r *Recipient) GetScript() []byte {

return r.script
}

type PaymentOptions struct {
Interval int64 `json:"interval"`
MinPayment float64 `json:"minPayment"`

Daemon int `json:"daemon"`
}
15 changes: 15 additions & 0 deletions daemons/getbalance.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,18 @@
package daemons

import (
"encoding/json"
"fmt"
)

type GetBalance float64

func BytesToGetBalance(b []byte) (GetBalance, error) {
var getBalance GetBalance
err := json.Unmarshal(b, &getBalance)
if err != nil {
return 0.0, fmt.Errorf("unmashal getBalance response %s failed with error %s", b, err)
}

return getBalance, nil
}
5 changes: 4 additions & 1 deletion daemons/getblocktemplate.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,9 +93,12 @@ type GetBlockTemplate struct {

// then JobManager.ProcessTemplate(rpcData)
func (dm *DaemonManager) GetBlockTemplate() (getBlockTemplate *GetBlockTemplate, err error) {
instance, result, _ := dm.Cmd("getblocktemplate",
instance, result, _, err := dm.Cmd("getblocktemplate",
[]interface{}{map[string]interface{}{"capabilities": []string{"coinbasetxn", "workid", "coinbase/append"}, "rules": []string{"segwit"}}},
)
if err != nil {
return nil, err
}

if result.Error != nil {
return nil, errors.New(fmt.Sprint("getblocktemplate call failed for daemon instance ", instance, " with error ", result.Error))
Expand Down
41 changes: 41 additions & 0 deletions daemons/gettransaction.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package daemons

import (
"encoding/json"
"fmt"
)

type GetTransaction struct {
Amount float64 `json:"amount"`
Confirmations int `json:"confirmations"`
Generated bool `json:"generated"`
Blockhash string `json:"blockhash"`
Blockheight int `json:"blockheight"`
Blockindex int `json:"blockindex"`
Blocktime int `json:"blocktime"`
Txid string `json:"txid"`
Walletconflicts []interface{} `json:"walletconflicts"`
Time int `json:"time"`
Timereceived int `json:"timereceived"`
Bip125Replaceable string `json:"bip125-replaceable"`
Details []GetTransactionDetail `json:"details"`
Hex string `json:"hex"`
}

type GetTransactionDetail struct {
Address string `json:"address"`
Category string `json:"category"`
Amount float64 `json:"amount"`
Label string `json:"label"`
Vout int `json:"vout"`
}

func BytesToGetTransaction(b []byte) (*GetTransaction, error) {
var getTransaction GetTransaction
err := json.Unmarshal(b, &getTransaction)
if err != nil {
return nil, fmt.Errorf("getTransaction call failed with error %s", err)
}

return &getTransaction, nil
}
28 changes: 16 additions & 12 deletions daemons/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bytes"
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"net/http"
"strconv"
Expand Down Expand Up @@ -52,7 +53,7 @@ func (dm *DaemonManager) Check() {
}

func (dm *DaemonManager) IsAllOnline() bool {
responses, _ := dm.CmdAll("getpeerinfo", []interface{}{})
_, responses := dm.CmdAll("getpeerinfo", []interface{}{})
for _, res := range responses {
if res.StatusCode/100 != 2 {
return false
Expand Down Expand Up @@ -89,7 +90,11 @@ func (dm *DaemonManager) DoHttpRequest(daemon *config.DaemonOptions, reqRawData
return client.Do(req)
}

func (dm *DaemonManager) BatchCmd(commands []interface{}) (*config.DaemonOptions, []*JsonRpcResponse, error) {
// BatchCmd will run a batch cmd on the wallet's rpc server
// the batch cmd is called on daemons one by one and return the first successful response
// batch cmd is list/set of normal rpc cmd, but batched into one rpc request
// the response is also a list
func (dm *DaemonManager) BatchCmd(commands []interface{}) (int, []*JsonRpcResponse, error) {
requestJson := make([]map[string]interface{}, len(commands))
for i := range commands {
requestJson[i] = map[string]interface{}{
Expand All @@ -103,23 +108,23 @@ func (dm *DaemonManager) BatchCmd(commands []interface{}) (*config.DaemonOptions
raw, _ := json.Marshal(requestJson)
res, err := dm.DoHttpRequest(dm.Daemons[i], raw)
if err != nil {
return dm.Daemons[i], nil, err
return i, nil, err
}
var rpcResponses []*JsonRpcResponse
err = json.NewDecoder(res.Body).Decode(&rpcResponses)
if err != nil {
return dm.Daemons[i], nil, err
return i, nil, err
}

return dm.Daemons[i], rpcResponses, err
return i, rpcResponses, err
}

return nil, nil, nil
return -1, nil, nil
}

// CmdAll sends the rpc call to all daemon, and never break because of any error.
// So the elem in responses may be nil
func (dm *DaemonManager) CmdAll(method string, params []interface{}) (responses []*http.Response, results []*JsonRpcResponse) {
func (dm *DaemonManager) CmdAll(method string, params []interface{}) (results []*JsonRpcResponse, responses []*http.Response) {
responses = make([]*http.Response, len(dm.Daemons))
results = make([]*JsonRpcResponse, len(dm.Daemons))

Expand Down Expand Up @@ -173,7 +178,7 @@ func (dm *DaemonManager) CmdAll(method string, params []interface{}) (responses

wg.Wait()

return responses, results
return results, responses
}

func (dm *DaemonManager) CheckStatusCode(statusCode int) error {
Expand Down Expand Up @@ -205,7 +210,7 @@ func (dm *DaemonManager) CheckStatusCode(statusCode int) error {

// Cmd will call daemons one by one and return the first answer
// one by one not all is to try fetching from the same one not random one
func (dm *DaemonManager) Cmd(method string, params []interface{}) (*config.DaemonOptions, *JsonRpcResponse, *http.Response) {
func (dm *DaemonManager) Cmd(method string, params []interface{}) (int, *JsonRpcResponse, *http.Response, error) {
reqRawData, err := json.Marshal(map[string]interface{}{
"id": utils.RandPositiveInt64(),
"method": method,
Expand Down Expand Up @@ -239,9 +244,8 @@ func (dm *DaemonManager) Cmd(method string, params []interface{}) (*config.Daemo
log.Error(err)
}

return dm.Daemons[i], &result, res
return i, &result, res, nil
}

log.Error("failed getting GBT from all daemons!")
return nil, nil, nil
return -1, nil, nil, fmt.Errorf("all daemons are down") // avoid nil panic
}
18 changes: 18 additions & 0 deletions daemons/sendmany.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package daemons

import (
"encoding/json"
"fmt"
)

type SendMany string // receive the raw tx hex string

func BytesToSendMany(b []byte) (*SendMany, error) {
var sendMany SendMany
err := json.Unmarshal(b, &sendMany)
if err != nil {
return nil, fmt.Errorf("getTransaction call failed with error %s", err)
}

return &sendMany, nil
}
8 changes: 4 additions & 4 deletions daemons/submitblock.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ import (
func (dm *DaemonManager) SubmitBlock(blockHex string) {
var results []*JsonRpcResponse
if dm.Coin.NoSubmitBlock {
_, results = dm.CmdAll("getblocktemplate", []interface{}{map[string]interface{}{"mode": "submit", "data": blockHex}})
results, _ = dm.CmdAll("getblocktemplate", []interface{}{map[string]interface{}{"mode": "submit", "data": blockHex}})
} else {
_, results = dm.CmdAll("submitblock", []interface{}{blockHex})
results, _ = dm.CmdAll("submitblock", []interface{}{blockHex})
}

for i := range results {
Expand All @@ -27,11 +27,11 @@ func (dm *DaemonManager) SubmitBlock(blockHex string) {
var result string
err := json.Unmarshal(results[i].Result, &result)
if err == nil && result == "rejected" {
log.Error("Daemon instance rejected a supposedly valid block")
log.Warn("Daemon instance rejected a supposedly valid block")
}

if err == nil && result == "invalid" {
log.Error("Daemon instance rejected an invalid block")
log.Warn("Daemon instance rejected an invalid block")
}

if err == nil && result == "inconclusive" {
Expand Down
9 changes: 5 additions & 4 deletions daemons/validateaddress.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@ import (
"fmt"
)

// DEPRECATION WARNING: Parts of this command have been deprecated and moved to getaddressinfo.
type ValidateAddress struct {
Isvalid bool `json:"isvalid"`
Address string `json:"address"`
ScriptPubKey string `json:"scriptPubKey"`
Ismine bool `json:"ismine"`
IsMine bool `json:"ismine"`
Iswatchonly bool `json:"iswatchonly"`
Isscript bool `json:"isscript"`
Iswitness bool `json:"iswitness"`
Expand Down Expand Up @@ -37,12 +38,12 @@ type ValidateAddress struct {
} `json:"labels"`
}

func BytesToValidateAddress(b []byte) *ValidateAddress {
func BytesToValidateAddress(b []byte) (*ValidateAddress, error) {
var validateAddress ValidateAddress
err := json.Unmarshal(b, &validateAddress)
if err != nil {
log.Fatal(fmt.Sprint("validateAddress call failed with error ", err))
return nil, fmt.Errorf("unmashal validateAddress response %s failed with error %s", b, err)
}

return &validateAddress
return &validateAddress, nil
}
13 changes: 6 additions & 7 deletions jobs/jobManager.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,18 @@ package jobs

import (
"encoding/hex"
"math/big"
"net"
"strconv"
"strings"
"time"

logging "github.com/ipfs/go-log/v2"
"github.com/mining-pool/not-only-mining-pool/algorithm"
"github.com/mining-pool/not-only-mining-pool/config"
"github.com/mining-pool/not-only-mining-pool/daemons"
"github.com/mining-pool/not-only-mining-pool/storage"
"github.com/mining-pool/not-only-mining-pool/types"
"github.com/mining-pool/not-only-mining-pool/utils"
"math/big"
"net"
"strconv"
"strings"
"time"
)

var log = logging.Logger("jobMgr")
Expand Down Expand Up @@ -93,7 +92,7 @@ func (jm *JobManager) ProcessShare(share *types.Share) {
}

func (jm *JobManager) CheckBlockAccepted(blockHash string) (isAccepted bool, tx string) {
_, results := jm.DaemonManager.CmdAll("getblock", []interface{}{blockHash})
results, _ := jm.DaemonManager.CmdAll("getblock", []interface{}{blockHash})

isAccepted = true
for i := range results {
Expand Down
Loading