code reorganization

This commit is contained in:
8lecramm 2022-05-15 02:15:30 +02:00
parent 2078467db4
commit 47fda6a514
6 changed files with 49 additions and 17 deletions

View File

@ -44,10 +44,20 @@ func main() {
go proxy.Start_server(listen_addr)
// Wait for first miner connection to grad wallet address
// Wait for first miner connection to grab wallet address
for proxy.CountMiners() < 1 {
time.Sleep(time.Second * 1)
}
proxy.Start_client(daemon_address, proxy.Address)
go proxy.Start_client(daemon_address, proxy.Address)
var current_time time.Time
last_time := time.Now()
//fmt.Printf("%4d miners connected\t\tBlocks:%4d\tMiniblocks:%4d\tRejected:%4d\n", proxy.CountMiners(), proxy.Blocks, proxy.Minis, proxy.Rejected)
for {
current_time = time.Now()
if current_time.Sub(last_time) >= time.Minute {
fmt.Printf("%4d miners connected\t\tBlocks:%4d\tMiniblocks:%4d\tRejected:%4d\n", proxy.CountMiners(), proxy.Blocks, proxy.Minis, proxy.Rejected)
last_time = time.Now()
}
}
}

3
go.mod
View File

@ -3,6 +3,7 @@ module derohe-proxy
go 1.18
require (
github.com/chzyer/readline v1.5.0
github.com/deroproject/derohe v0.0.0-20220502125456-607af6dfdc9a
github.com/deroproject/graviton v0.0.0-20220130070622-2c248a53b2e1
github.com/docopt/docopt-go v0.0.0-20180111231733-ee0de3bc6815
@ -28,7 +29,7 @@ require (
go.uber.org/zap v1.21.0 // indirect
golang.org/x/crypto v0.0.0-20210513122933-cd7d49e622d5 // indirect
golang.org/x/net v0.0.0-20210510120150-4163338589ed // indirect
golang.org/x/sys v0.0.0-20210510120138-977fb7262007 // indirect
golang.org/x/sys v0.0.0-20220310020820-b874c991c1a5 // indirect
golang.org/x/xerrors v0.0.0-20220411194840-2f41105eb62f // indirect
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b // indirect
)

9
go.sum
View File

@ -6,6 +6,12 @@ github.com/blang/semver/v4 v4.0.0/go.mod h1:IbckMUScFkM3pff0VJDNKRiT6TG/YpiHIM2y
github.com/caarlos0/env/v6 v6.9.1 h1:zOkkjM0F6ltnQ5eBX6IPI41UP/KDGEK7rRPwGCNos8k=
github.com/caarlos0/env/v6 v6.9.1/go.mod h1:hvp/ryKXKipEkcuYjs9mI4bBCg+UI0Yhgm5Zu0ddvwc=
github.com/cespare/xxhash v1.1.0/go.mod h1:XrSqR1VqqWfGrhpAt58auRo0WTKS1nRRg3ghfAqPWnc=
github.com/chzyer/logex v1.2.0 h1:+eqR0HfOetur4tgnC8ftU5imRnhi4te+BadWS95c5AM=
github.com/chzyer/logex v1.2.0/go.mod h1:9+9sk7u7pGNWYMkh0hdiL++6OeibzJccyQU4p4MedaY=
github.com/chzyer/readline v1.5.0 h1:lSwwFrbNviGePhkewF1az4oLmcwqCZijQ2/Wi3BGHAI=
github.com/chzyer/readline v1.5.0/go.mod h1:x22KAscuvRqlLoK9CsoYsmxoXZMMFVyOl86cAH8qUic=
github.com/chzyer/test v0.0.0-20210722231415-061457976a23 h1:dZ0/VyGgQdVGAss6Ju0dt5P0QltE0SFY5Woh6hbIfiQ=
github.com/chzyer/test v0.0.0-20210722231415-061457976a23/go.mod h1:Q3SI9o4m/ZMnBNeIyt5eFwwo7qiLfzFZmjNmxjkiQlU=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
@ -82,8 +88,9 @@ golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7w
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210330210617-4fbd30eecc44/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210510120138-977fb7262007 h1:gG67DSER+11cZvqIMb8S8bt0vZtiN6xWYARwirrOSfE=
golang.org/x/sys v0.0.0-20210510120138-977fb7262007/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220310020820-b874c991c1a5 h1:y/woIyUBFbpQGKS0u1aHF/40WUDnek3fPOyD08H5Vng=
golang.org/x/sys v0.0.0-20220310020820-b874c991c1a5/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=

View File

@ -2,15 +2,20 @@ package proxy
import (
"crypto/tls"
"encoding/json"
"fmt"
"math/rand"
"net/url"
"time"
"github.com/deroproject/derohe/rpc"
"github.com/gorilla/websocket"
)
var connection *websocket.Conn
var Blocks uint64
var Minis uint64
var Rejected uint64
// proxy-client
func Start_client(v string, w string) {
@ -35,6 +40,8 @@ func Start_client(v string, w string) {
continue
}
var params rpc.GetBlockTemplate_Result
for {
msg_type, recv_data, err := connection.ReadMessage()
if err != nil {
@ -45,12 +52,19 @@ func Start_client(v string, w string) {
continue
}
go SendTemplatesToNode(recv_data)
if err = json.Unmarshal(recv_data, &params); err != nil {
continue
}
Blocks = params.Blocks
Minis = params.MiniBlocks
Rejected = params.Rejected
go SendTemplateToNodes(recv_data)
}
}
}
func SendToDaemon(buffer []byte) {
connection.SetWriteDeadline(time.Now().Add(100 * time.Millisecond))
connection.WriteMessage(websocket.TextMessage, buffer)
}

View File

@ -35,8 +35,8 @@ func edit_blob(input []byte) (output []byte) {
}
params.Blockhashing_blob = fmt.Sprintf("%x", mbl.Serialize())
encoder := json.NewEncoder(&out)
if err = encoder.Encode(params); err != nil {
return
}

View File

@ -24,7 +24,6 @@ import (
)
var server *nbhttp.Server
var results uint
var memPool = sync.Pool{
New: func() interface{} {
@ -95,8 +94,8 @@ func CountMiners() int {
}
// forward all incoming templates from daemon to all miners
func SendTemplatesToNode(data []byte) {
var rand_nonce []byte
func SendTemplateToNodes(input []byte) {
var data []byte
for rk, rv := range client_list {
@ -104,13 +103,15 @@ func SendTemplatesToNode(data []byte) {
break
}
if rand_nonce = edit_blob(data); rand_nonce != nil {
data = rand_nonce
if nonce := edit_blob(input); nonce != nil {
data = nonce
} else {
fmt.Println(time.Now().Format(time.Stamp), "Failed to change nonce")
data = input
}
go func(k *websocket.Conn, v *user_session) {
defer globals.Recover(2)
fmt.Printf("%4d miners connected\r", CountMiners())
k.SetWriteDeadline(time.Now().Add(100 * time.Millisecond))
k.WriteMessage(websocket.TextMessage, data)
@ -151,6 +152,7 @@ func onWebsocket(w http.ResponseWriter, r *http.Request) {
defer client_list_mutex.Unlock()
client_list[wsConn] = &session
Address = address
fmt.Println(time.Now().Format(time.Stamp), "Incoming connection from IP:", wsConn.RemoteAddr().String())
}
// forward results to daemon
@ -167,16 +169,14 @@ func newUpgrader() *websocket.Upgrader {
defer client_list_mutex.Unlock()
SendToDaemon(data)
fmt.Println(time.Now().Format(time.Stamp), "Submitting result")
results++
fmt.Println("Submitted results:", results)
fmt.Println(time.Now().Format(time.Stamp), "Submitting result from miner IP:", c.RemoteAddr().String())
})
u.OnClose(func(c *websocket.Conn, err error) {
client_list_mutex.Lock()
defer client_list_mutex.Unlock()
delete(client_list, c)
fmt.Println(time.Now().Format(time.Stamp), "Interrupted or lost connection:", c.RemoteAddr().String())
})
return u