This repository has been archived by the owner on Sep 3, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 17
/
transport.go
97 lines (83 loc) · 3.01 KB
/
transport.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
package main
import (
"fmt"
"github.com/dghubble/sling"
"github.com/fatih/color"
"github.com/kristjank/ark-go/cmd/model"
"github.com/kristjank/ark-go/core"
log "github.com/sirupsen/logrus"
"github.com/spf13/viper"
)
type postStatsResponse struct {
Success bool `json:"success,omitempty"`
LogID int `json:"logID,omitempty"`
Error string `json:"error,omitempty"`
}
func sendStatisticsData(payRec *model.PaymentRecord) {
response := new(postStatsResponse)
error := new(postStatsResponse)
statURL := fmt.Sprintf("http://%s:%d", viper.GetString("client.statPeer"), viper.GetInt("client.statPort"))
statsBase := sling.New().Base(statURL).Client(nil).Add("Content-Type", "application/json")
resp, err := statsBase.New().Post("log/payment").BodyJSON(payRec).Receive(response, error)
if err != nil {
log.Error("Error sending statistics data", resp, err)
}
}
func splitAndDeliverPayload(payload core.TransactionPayload) {
//calculating number of chunks (based on 20tx in one chunk to send to one peer)
payoutsFolderName := createLogFolder()
var divided [][]*core.Transaction
numPeers := len(payload.Transactions) / 20
if numPeers == 0 {
numPeers = 1
}
chunkSize := (len(payload.Transactions) + numPeers - 1) / numPeers
if chunkSize == 0 {
chunkSize = 1
}
//sliptting the payload to number of needed peers
for i := 0; i < len(payload.Transactions); i += chunkSize {
end := i + chunkSize
if end > len(payload.Transactions) {
end = len(payload.Transactions)
}
divided = append(divided, payload.Transactions[i:end])
}
//end of spliting transactions
var tmpPayload core.TransactionPayload
splitcout := 0
for chunkIx, h := range divided {
tmpPayload.Transactions = h
splitcout += len(h)
deliverPayloadThreaded(tmpPayload, chunkIx, payoutsFolderName)
}
if splitcout != len(payload.Transactions) {
log.Info("TX spliting not OK")
}
}
func deliverPayloadThreaded(tmpPayload core.TransactionPayload, chunkIx int, logFolder string) {
numberOfPeers2MultiBroadCastTo := viper.GetInt("client.multibroadcast")
if numberOfPeers2MultiBroadCastTo > 15 {
numberOfPeers2MultiBroadCastTo = 15
log.Warn("Max broadcast number too high - set by user, reseting to value 15")
}
log.Info("Starting multibroadcast/multithreaded parallel payout to ", numberOfPeers2MultiBroadCastTo, " number of peers")
peers := arkclient.GetRandomXPeers(numberOfPeers2MultiBroadCastTo)
for i := 0; i < numberOfPeers2MultiBroadCastTo; i++ {
wg.Add(1)
//treaded function
go func(tmpPayload core.TransactionPayload, peer core.Peer, chunkIx int, logFolder string) {
defer wg.Done()
filename := fmt.Sprintf("log/%s/Batch_%02d_Peer%s.csv", logFolder, chunkIx, peer.IP)
arkTmpClient := core.NewArkClientFromPeer(peer)
res, _, _ := arkTmpClient.PostTransaction(tmpPayload)
if res.Success {
color.Set(color.FgHiGreen)
log2csv(tmpPayload, res.TransactionIDs, filename, "OK")
} else {
color.Set(color.FgHiRed)
log2csv(tmpPayload, nil, filename, res.Error)
}
}(tmpPayload, peers[i], chunkIx, logFolder)
}
}