Skip to content

Commit

Permalink
PPLNS / SOLO
Browse files Browse the repository at this point in the history
  • Loading branch information
yuriy0803 committed Apr 7, 2024
1 parent a4563fa commit a9f3430
Show file tree
Hide file tree
Showing 16 changed files with 2,421 additions and 1,217 deletions.
367 changes: 214 additions & 153 deletions api/server.go

Large diffs are not rendered by default.

33 changes: 33 additions & 0 deletions exchange/exchange_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package exchange

import (
"fmt"
"os"
"testing"
)

func TestMain(m *testing.M) {
os.Exit(m.Run())
}

func TestGetData(t *testing.T) {

r := NewRestClient("Test", "https://api.coinmarketcap.com/v1/ticker/?convert=INR", "15s")
Result, err := r.GetData()

if err != nil {
t.Errorf("Error occured : %v", err)
return
}

for k, v := range Result {

fmt.Println("Key: %s , Value, %s", k, v)

}

}

func BytesToString(data []byte) string {
return string(data[:])
}
8 changes: 4 additions & 4 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
//go:build go1.9
// +build go1.9

package main

import (
Expand Down Expand Up @@ -89,10 +86,11 @@ func main() {

startNewrelic()

backend = storage.NewRedisClient(&cfg.Redis, cfg.Coin, cfg.Pplns, cfg.CoinName)
backend = storage.NewRedisClient(&cfg.Redis, cfg.Coin, cfg.Pplns, cfg.CoinName, cfg.CoinSolo)
pong, err := backend.Check()
if err != nil {
log.Printf("Can't establish connection to backend: %v", err)
//os.Exit(0)
} else {
log.Printf("Backend check reply: %v", pong)
}
Expand All @@ -109,9 +107,11 @@ func main() {
if cfg.Payouts.Enabled {
go startPayoutsProcessor()
}

if cfg.Exchange.Enabled {
go startExchangeProcessor()
}

quit := make(chan bool)
<-quit
}
91 changes: 40 additions & 51 deletions payouts/payer.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@ import (
"log"
"math/big"
"os"
"os/exec"
"strconv"
"sync"
"time"

"github.com/yuriy0803/core-geth1/common/hexutil"
Expand All @@ -29,10 +27,12 @@ type PayoutsConfig struct {
Gas string `json:"gas"`
GasPrice string `json:"gasPrice"`
AutoGas bool `json:"autoGas"`
KeepNwFees bool `json:"keepNwFees"`
TxGas string `json:"nwTxGas"`
TxGasPrice string `json:"nwTxGasPrice"`
// In Shannon
Threshold int64 `json:"threshold"`
BgSave bool `json:"bgsave"`
ConcurrentTx int `json:"concurrentTx"`
Threshold int64 `json:"threshold"`
BgSave bool `json:"bgsave"`
}

func (self PayoutsConfig) GasHex() string {
Expand Down Expand Up @@ -108,7 +108,6 @@ func (u *PayoutsProcessor) Start() {
func (u *PayoutsProcessor) process() {
if u.halt {
log.Println("Payments suspended due to last critical error:", u.lastFail)
os.Exit(1)
return
}
mustPay := 0
Expand All @@ -120,12 +119,10 @@ func (u *PayoutsProcessor) process() {
return
}

waitingCount := 0
var wg sync.WaitGroup

for _, login := range payees {
amount, _ := u.backend.GetBalance(login)
amountInShannon := big.NewInt(amount)

ptresh, _ := u.backend.GetThreshold(login)
if ptresh <= 10 {
ptresh = u.config.Threshold
Expand Down Expand Up @@ -182,8 +179,23 @@ func (u *PayoutsProcessor) process() {
break
}

//Calculate the Gas Price in Wei and Computer the Transaction Charges
//Since pool honour only mining to wallet and not to contract, Deduct value equal to gas*21000 - Standard cost price

TxCharges := big.NewInt(0)

if u.config.KeepNwFees {

TxCharges.Mul(util.String2Big(u.config.TxGasPrice), util.String2Big(u.config.TxGas))

//Deduct the Calulated Transaction Charges
amountInWei.Sub(amountInWei, TxCharges)

}

value := hexutil.EncodeBig(amountInWei)
txHash, err := u.rpc.SendTransaction(u.config.Address, login, u.config.GasHex(), u.config.GasPriceHex(), value, u.config.AutoGas)

if err != nil {
log.Printf("Failed to send payment to %s, %v Shannon: %v. Check outgoing tx for %s in block explorer and docs/PAYOUTS.md",
login, amount, err, login)
Expand All @@ -192,18 +204,8 @@ func (u *PayoutsProcessor) process() {
break
}

if postCommand, present := os.LookupEnv("POST_PAYOUT_HOOK"); present {
go func(postCommand string, login string, value string) {
out, err := exec.Command(postCommand, login, value).CombinedOutput()
if err != nil {
log.Printf("WARNING: Error running post payout hook: %s", err.Error())
}
log.Printf("Running post payout hook with result: %s", out)
}(postCommand, login, value)
}

// Log transaction hash
err = u.backend.WritePayment(login, txHash, amount)
err = u.backend.WritePayment(login, txHash, amount, TxCharges.Int64())
if err != nil {
log.Printf("Failed to log payment data for %s, %v Shannon, tx: %s: %v", login, amount, txHash, err)
u.halt = true
Expand All @@ -213,42 +215,29 @@ func (u *PayoutsProcessor) process() {

minersPaid++
totalAmount.Add(totalAmount, big.NewInt(amount))
log.Printf("Paid %v Shannon to %v, TxHash: %v", amount, login, txHash)

wg.Add(1)
waitingCount++
go func(txHash string, login string, wg *sync.WaitGroup) {
// Wait for TX confirmation before further payouts
for {
log.Printf("Waiting for tx confirmation: %v", txHash)
time.Sleep(txCheckInterval)
receipt, err := u.rpc.GetTxReceipt(txHash)
if err != nil {
log.Printf("Failed to get tx receipt for %v: %v", txHash, err)
continue
}
// Tx has been mined
if receipt != nil && receipt.Confirmed() {
if receipt.Successful() {
log.Printf("Payout tx successful for %s: %s", login, txHash)
} else {
log.Printf("Payout tx failed for %s: %s. Address contract throws on incoming tx.", login, txHash)
}
break
log.Printf("Paid %v Shannon to %v, TxHash: %v, Transaction Charges : %v", amountInWei, login, txHash, TxCharges.Int64())

// Wait for TX confirmation before further payouts
for {
log.Printf("Waiting for tx confirmation: %v", txHash)
time.Sleep(txCheckInterval)
receipt, err := u.rpc.GetTxReceipt(txHash)
if err != nil {
log.Printf("Failed to get tx receipt for %v: %v", txHash, err)
continue
}
// Tx has been mined
if receipt != nil && receipt.Confirmed() {
if receipt.Successful() {
log.Printf("Payout tx successful for %s: %s", login, txHash)
} else {
log.Printf("Payout tx failed for %s: %s. Address contract throws on incoming tx.", login, txHash)
}
break
}
wg.Done()
}(txHash, login, &wg)

if waitingCount > u.config.ConcurrentTx {
wg.Wait()
waitingCount = 0
}
}

wg.Wait()
waitingCount = 0

if mustPay > 0 {
log.Printf("Paid total %v Shannon to %v of %v payees", totalAmount, minersPaid, mustPay)
} else {
Expand Down
Loading

0 comments on commit a9f3430

Please sign in to comment.