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

Fix payout #148

Merged
merged 11 commits into from
Dec 10, 2020
Merged
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
- Sign stats request [\#143](https://github.com/NodeFactoryIo/vedran/pull/143) ([MakMuftic](https://github.com/MakMuftic))

### Fix
- Fix payout [\#148](https://github.com/NodeFactoryIo/vedran/pull/148) ([MakMuftic](https://github.com/MakMuftic))

### Changed
- Write byte response directly with io.Write [\#113](https://github.com/NodeFactoryIo/vedran/pull/113) ([mpetrun5](https://github.com/mpetrun5))
Expand Down
17 changes: 15 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ For Westend's WND tokens, see the faucet [instructions on the Wiki](https://wiki

When starting _vedran loadbalancer_ it is possible to configure automatic payout by providing these flags:

`--private-key` - loadbalancers wallet private key, used for sending rewards on payout
`--private-key` - loadbalancers wallet private key (string representation of hex value prefixed with 0x), used for sending rewards on payout

`--payout-interval` - automatic payout interval specified as number of days

Expand All @@ -130,14 +130,27 @@ If all flags have been provided, then each {_payout-interval_} days automatic pa
It is possible to run payout script at any time by invoking `vedran payout` command trough console.
This command has two required flags:

`--private-key` - loadbalancers wallet private key, used for sending founds on payout
`--private-key` - loadbalancers wallet private key (string representation of hex value prefixed with 0x), used for sending founds on payout

`--payout-reward` - defined total reward amount that will be distributed on the payout (amount in Planck)

Additionally, it is possible to change url on which payout script will connect with loadbalancer when executing transactions by setting flag (default value will be _http://localhost:80_)

`--load-balancer-url` - loadbalancer url

### Get private key
You can use [subkey](https://substrate.dev/docs/en/knowledgebase/integrate/subkey) tool to get private key for your wallet.

After installing subkey tool call `subkey inspect "insert your mnemonic here"`.
You can find private key as _Secreet seed_. See example output of subkey command:

```
Secret seed: 0x1a84771145cdcee05e49142aaff2e5d669ce4b29344a09b973b751ae661acabf
Public key (hex): 0xa4548fa9b3b15dc4d1c59789952f0ccf6138dd63faf802637895c941f0522d35
Account ID: 0xa4548fa9b3b15dc4d1c59789952f0ccf6138dd63faf802637895c941f0522d35
SS58 Address: 5FnAq6wrMzri5V6jLfKgBkbR2rSAMkVAHVYWa3eU7TAV5rv9
```

## Vedran loadbalancer API

`POST api/v1/nodes`
Expand Down
2 changes: 1 addition & 1 deletion cmd/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,6 @@ func startCommand(_ *cobra.Command, _ []string) {
WhitelistEnabled: whitelistEnabled,
},
payoutConfiguration,
privateKey,
payoutPrivateKey,
)
}
3 changes: 2 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,13 @@ require (
github.com/cenkalti/backoff v2.2.1+incompatible
github.com/decred/base58 v1.0.2
github.com/dgrijalva/jwt-go v3.2.0+incompatible
github.com/ethereum/go-ethereum v1.9.24 // indirect
github.com/ethereum/go-ethereum v1.9.24
github.com/golang/gddo v0.0.0-20200831202555-721e228c7686
github.com/gorilla/mux v1.8.0
github.com/gorilla/websocket v1.4.1
github.com/gosuri/uitable v0.0.4
github.com/inconshreveable/go-vhost v0.0.0-20160627193104-06d84117953b
github.com/pkg/errors v0.8.1
github.com/sirupsen/logrus v1.6.0
github.com/spf13/cobra v1.0.0
github.com/stretchr/testify v1.6.1
Expand Down
1 change: 1 addition & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,7 @@ github.com/peterh/liner v1.1.1-0.20190123174540-a2c9a5303de7/go.mod h1:CRroGNssy
github.com/pierrec/xxHash v0.1.5 h1:n/jBpwTHiER4xYvK3/CdPVnLDPchj8eTJFFLUb4QHBo=
github.com/pierrec/xxHash v0.1.5/go.mod h1:w2waW5Zoa/Wc4Yqe0wgrIYAGKqRMf7czn2HNKXmuL+I=
github.com/pkg/errors v0.8.0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pkg/errors v0.8.1 h1:iURUrRGxPUNPdy5/HRSm+Yj6okJ6UtLINN0Q9M4+h3I=
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
Expand Down
11 changes: 8 additions & 3 deletions internal/controllers/stats.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package controllers

import (
"encoding/json"
"github.com/ethereum/go-ethereum/common/hexutil"
"net/http"
"time"

Expand Down Expand Up @@ -43,11 +44,15 @@ type LoadbalancerStatsResponse struct {
func (c *ApiController) StatisticsHandlerAllStatsForLoadbalancer(w http.ResponseWriter, r *http.Request) {
sig := r.Header.Get("X-Signature")
if sig == "" {
log.Errorf("Missing signature header")
log.Error("Missing signature header")
http.Error(w, http.StatusText(http.StatusBadRequest), http.StatusBadRequest)
}

verified, err := signature.Verify([]byte(StatsSignedData), []byte(sig), c.privateKey)
sigInBytes, err := hexutil.Decode(sig)
if err != nil {
log.Errorf("Unable to decode signature, because of: %v", err)
http.Error(w, http.StatusText(http.StatusBadRequest), http.StatusBadRequest)
}
verified, err := signature.Verify([]byte(StatsSignedData), sigInBytes, c.privateKey)
if err != nil {
log.Errorf("Failed to verify signature, because %v", err)
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
Expand Down
3 changes: 2 additions & 1 deletion internal/controllers/stats_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/NodeFactoryIo/vedran/internal/models"
"github.com/NodeFactoryIo/vedran/internal/repositories"
mocks "github.com/NodeFactoryIo/vedran/mocks/repositories"
"github.com/ethereum/go-ethereum/common/hexutil"
muxhelpper "github.com/gorilla/mux"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
Expand Down Expand Up @@ -353,7 +354,7 @@ func TestApiController_StatisticsHandlerAllStatsForLoadbalancer(t *testing.T) {

if test.secret != "" {
sig, _ := signature.Sign([]byte(test.signatureData), test.secret)
req.Header.Set("X-Signature", string(sig))
req.Header.Set("X-Signature", hexutil.Encode(sig))
}

rr := httptest.NewRecorder()
Expand Down
1 change: 0 additions & 1 deletion internal/loadbalancer/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@ func StartLoadBalancerServer(
log.Fatalf("Failed creating initial payout because of: %v", err)
} else if len(*p) == 0 {
err := repos.PayoutRepo.Save(&models.Payout{
ID: "1",
Timestamp: time.Now(),
PaymentDetails: nil,
})
Expand Down
2 changes: 1 addition & 1 deletion internal/models/payout.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package models
import "time"

type Payout struct {
ID string `storm:"id"`
ID int `storm:"id,increment"`
Timestamp time.Time `json:"timestamp"`
PaymentDetails map[string]NodeStatsDetails
}
Expand Down
26 changes: 2 additions & 24 deletions internal/payout/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,12 @@ func ExecuteTransaction(
amount big.Int,
keyringPair signature.KeyringPair,
mux *sync.Mutex,
metadataLatest *types.Metadata,
nonce uint32,
) (*TransactionDetails, error) {
// lock segment so goroutines don't access api at the same time
mux.Lock()

metadataLatest, err := api.RPC.State.GetMetadataLatest()
if err != nil {
return nil, err
}

decoded := base58.Decode(to)
// remove the 1st byte (network identifier) & last 2 bytes (blake2b hash)
pubKey := decoded[1 : len(decoded)-2]
Expand Down Expand Up @@ -51,25 +48,6 @@ func ExecuteTransaction(
return nil, err
}

storageKey, err := types.CreateStorageKey(
metadataLatest,
"System",
"Account",
keyringPair.PublicKey,
nil,
)
if err != nil {
return nil, err
}

var accountInfo types.AccountInfo
ok, err := api.RPC.State.GetStorageLatest(storageKey, &accountInfo)
if err != nil || !ok {
return nil, err
}

nonce := uint32(accountInfo.Nonce)

signatureOptions := types.SignatureOptions{
Era: types.ExtrinsicEra{IsMortalEra: false},
Nonce: types.NewUCompactFromUInt(uint64(nonce)),
Expand Down
40 changes: 37 additions & 3 deletions internal/payout/txpayout.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
gsrpc "github.com/NodeFactoryIo/go-substrate-rpc-client"
"github.com/NodeFactoryIo/go-substrate-rpc-client/signature"
"github.com/NodeFactoryIo/go-substrate-rpc-client/types"
"github.com/pkg/errors"
"math/big"
"sync"
)
Expand Down Expand Up @@ -44,17 +45,28 @@ func executeAllTransactions(
// define number of goroutines
wg.Add(len(payoutDistribution))

metadataLatest, err := api.RPC.State.GetMetadataLatest()
if err != nil {
return nil, errors.Wrap(err, "unable to get latest metadat")
}

nonce, err := getNonce(metadataLatest, keyringPair, api)
if err != nil {
return nil, errors.Wrap(err, "unable to get nonce")
}

for nodePayoutAddress, amount := range payoutDistribution {
// execute transaction in separate goroutine and collect results in channels
go func(to string, amount big.Int, wg *sync.WaitGroup, mux *sync.Mutex) {
go func(to string, amount big.Int, wg *sync.WaitGroup, mux *sync.Mutex, nonce uint32) {
defer wg.Done()
transactionDetails, err := ExecuteTransaction(api, to, amount, keyringPair, mux)
transactionDetails, err := ExecuteTransaction(api, to, amount, keyringPair, mux, metadataLatest, nonce)
if err != nil {
fatalErrorsChannel <- err
} else {
resultsChannel <- transactionDetails
}
}(nodePayoutAddress, amount, &wg, &mux)
}(nodePayoutAddress, amount, &wg, &mux, nonce)
nonce += 1
}

go func() {
Expand All @@ -67,6 +79,28 @@ func executeAllTransactions(
return waitForTransactionDetails(waitGroupDoneChannel, fatalErrorsChannel, resultsChannel)
}

func getNonce(metadataLatest *types.Metadata, keyringPair signature.KeyringPair, api *gsrpc.SubstrateAPI) (uint32, error) {
storageKey, err := types.CreateStorageKey(
metadataLatest,
"System",
"Account",
keyringPair.PublicKey,
nil,
)
if err != nil {
return 0, err
}

var accountInfo types.AccountInfo
ok, err := api.RPC.State.GetStorageLatest(storageKey, &accountInfo)
if err != nil || !ok {
return 0, err
}

nonce := uint32(accountInfo.Nonce)
return nonce, err
}

func waitForTransactionDetails(
waitGroupDoneChannel chan bool,
fatalErrorsChannel chan error,
Expand Down
8 changes: 4 additions & 4 deletions internal/repositories/downtime.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,12 @@ func (r *DowntimeRepo) FindDowntimesInsideInterval(nodeID string, from time.Time
q.Eq("NodeId", nodeID),
q.Or(
q.And( // start inside interval
q.Gte("start", from),
q.Lte("start", to),
q.Gte("Start", from),
q.Lte("Start", to),
),
q.And( // end inside interval
q.Gte("end", from),
q.Lte("end", to),
q.Gte("End", from),
q.Lte("End", to),
),
),
)).Find(&downtimes)
Expand Down
2 changes: 1 addition & 1 deletion internal/repositories/payout.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,6 @@ func (p *payoutRepo) GetAll() (*[]models.Payout, error) {

func (p *payoutRepo) FindLatestPayout() (*models.Payout, error) {
var payout models.Payout
err := p.db.Select().OrderBy("Timestamp").First(&payout)
err := p.db.Select().OrderBy("Timestamp").Reverse().First(&payout)
return &payout, err
}
3 changes: 1 addition & 2 deletions internal/repositories/ping.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,5 @@ func (r *pingRepo) CalculateDowntime(nodeId string, pingTime time.Time) (time.Ti

return pingTime, time.Duration(0), err
}

return lastPing.Timestamp, lastPing.Timestamp.Sub(pingTime), nil
return lastPing.Timestamp, pingTime.Sub(lastPing.Timestamp), nil
}
6 changes: 3 additions & 3 deletions internal/schedule/payout/payout_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func Test_numOfDaysSinceLastPayout(t *testing.T) {
{
name: "last payout before 20 days",
latestPayout: &models.Payout{
ID: "",
ID: 1,
Timestamp: timestampSince20days,
PaymentDetails: nil,
},
Expand All @@ -40,7 +40,7 @@ func Test_numOfDaysSinceLastPayout(t *testing.T) {
{
name: "last payout before 2 days",
latestPayout: &models.Payout{
ID: "",
ID: 1,
Timestamp: timestampSince2days,
PaymentDetails: nil,
},
Expand All @@ -52,7 +52,7 @@ func Test_numOfDaysSinceLastPayout(t *testing.T) {
{
name: "last payout before 2 hours",
latestPayout: &models.Payout{
ID: "",
ID: 1,
Timestamp: timestampSince2hours,
PaymentDetails: nil,
},
Expand Down
3 changes: 2 additions & 1 deletion internal/script/payout.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package script
import (
"encoding/json"
"fmt"
"github.com/ethereum/go-ethereum/common/hexutil"
"net/http"
"net/url"
"strconv"
Expand Down Expand Up @@ -41,7 +42,7 @@ func fetchStatsFromEndpoint(endpoint *url.URL, secret string) (*controllers.Load
}

request, _ := http.NewRequest("POST", endpoint.String(), nil)
request.Header.Set("X-Signature", string(sig))
request.Header.Set("X-Signature", hexutil.Encode(sig))

c := &http.Client{}
resp, err := c.Do(request)
Expand Down