From 6f693412276230c0f4331a8cffacdf5529bd7599 Mon Sep 17 00:00:00 2001 From: 8lecramm <89603217+8lecramm@users.noreply.github.com> Date: Sun, 19 Jun 2022 21:28:53 +0200 Subject: [PATCH] Preparation for testing branch --- derohe-proxy.go | 12 +++++++----- go.mod | 2 +- go.sum | 10 ++++------ proxy/client.go | 3 --- proxy/feature.go | 29 ++++++++++++++++++++++++----- proxy/server.go | 16 ++++++++++++++++ 6 files changed, 52 insertions(+), 20 deletions(-) diff --git a/derohe-proxy.go b/derohe-proxy.go index 504e9b8..ddd9f93 100644 --- a/derohe-proxy.go +++ b/derohe-proxy.go @@ -51,14 +51,16 @@ func main() { } } - if Arguments["--minimal"].(bool) { - minimal = true - fmt.Printf("%v Forward only 2 jobs per block\n", time.Now().Format(time.Stamp)) - } - if Arguments["--nonce"].(bool) { nonce = true + minimal = true fmt.Printf("%v Nonce editing is enabled\n", time.Now().Format(time.Stamp)) + fmt.Printf("%v Switch to >minimal< mode\n", time.Now().Format(time.Stamp)) + } + + if Arguments["--minimal"].(bool) && !Arguments["--nonce"].(bool) { + minimal = true + fmt.Printf("%v Forward only 2 jobs per block\n", time.Now().Format(time.Stamp)) } fmt.Printf("%v Logging every %d seconds\n", time.Now().Format(time.Stamp), log_intervall) diff --git a/go.mod b/go.mod index e2f008f..075eb73 100644 --- a/go.mod +++ b/go.mod @@ -3,7 +3,7 @@ module derohe-proxy go 1.18 require ( - github.com/chzyer/readline v1.5.0 + github.com/bitfield/qrand v0.2.1 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 diff --git a/go.sum b/go.sum index d06de40..3c20bf5 100644 --- a/go.sum +++ b/go.sum @@ -1,17 +1,13 @@ github.com/OneOfOne/xxhash v1.2.2/go.mod h1:HSdplMjZKSmBqAxg5vPj2TmRDmfkzw+cTzAElWljhcU= github.com/benbjohnson/clock v1.1.0 h1:Q92kusRqC1XV2MjkWETPvjJVqKetz1OzxZB7mHJLju8= github.com/benbjohnson/clock v1.1.0/go.mod h1:J11/hYXuz8f4ySSvYwY0FKfm+ezbsZBKZxNJlLklBHA= +github.com/bitfield/qrand v0.2.1 h1:Fwp3GkWlr8Y/tfjB2GrXqYeR3nl/SEjFMnCDxVdVUGo= +github.com/bitfield/qrand v0.2.1/go.mod h1:D3AK1+Wqay/g3Z0tBM5OiO2YjR5MK2XYgDkmx8eec2w= github.com/blang/semver/v4 v4.0.0 h1:1PFHFE6yCCTv8C1TeyNNarDzntLi7wMI5i/pzqYIsAM= github.com/blang/semver/v4 v4.0.0/go.mod h1:IbckMUScFkM3pff0VJDNKRiT6TG/YpiHIM2yvyW5YoQ= 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= @@ -28,6 +24,8 @@ github.com/go-logr/logr v1.2.3 h1:2DntVwHkVopvECVRSlL5PSo9eG+cAkDCuckLubN+rq0= github.com/go-logr/logr v1.2.3/go.mod h1:jdQByPbusPIv2/zmleS9BjJVeZ6kBagPoEUsqbVz/1A= github.com/go-logr/zapr v1.2.3 h1:a9vnzlIBPQBBkeaR9IuMUfmVOrQlkoC4YfPoFkX3T7A= github.com/go-logr/zapr v1.2.3/go.mod h1:eIauM6P8qSvTw5o2ez6UEAfGjQKrxQTl5EoK+Qa2oG4= +github.com/google/go-cmp v0.3.1 h1:Xye71clBPdm5HgqGwUkwhbynsUJZhDbS20FvLhQ2izg= +github.com/google/go-cmp v0.3.1/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU= github.com/gorilla/websocket v1.5.0 h1:PPwGk2jz7EePpoHN/+ClbZu8SPxiqlu12wZP/3sWmnc= github.com/gorilla/websocket v1.5.0/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE= github.com/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI= diff --git a/proxy/client.go b/proxy/client.go index 7b76550..d4d7444 100644 --- a/proxy/client.go +++ b/proxy/client.go @@ -4,7 +4,6 @@ import ( "crypto/tls" "encoding/json" "fmt" - "math/rand" "net/url" "time" @@ -23,8 +22,6 @@ func Start_client(v string, w string, min_jobs bool, nonce bool) { var last_diff uint64 var last_height uint64 - rand.Seed(time.Now().UnixMilli()) - for { u := url.URL{Scheme: "wss", Host: v, Path: "/ws/" + w} diff --git a/proxy/feature.go b/proxy/feature.go index fd0c2de..697d508 100644 --- a/proxy/feature.go +++ b/proxy/feature.go @@ -2,15 +2,24 @@ package proxy import ( "bytes" + "encoding/binary" "encoding/hex" "encoding/json" "fmt" - "math/rand" + "sync" + "time" + "github.com/bitfield/qrand" "github.com/deroproject/derohe/block" "github.com/deroproject/derohe/rpc" ) +type nonce_store struct { + nonce [8]byte + timestamp time.Time + sync.RWMutex +} + func edit_blob(input []byte, miner [32]byte, nonce bool) (output []byte) { var err error var params rpc.GetBlockTemplate_Result @@ -37,13 +46,23 @@ func edit_blob(input []byte, miner [32]byte, nonce bool) (output []byte) { // Insert random nonce if nonce { - for i := range mbl.Nonce { - mbl.Nonce[i] = rand.Uint32() + var qnonce [8]byte + Found.Lock() + // send nonce pattern to all nodes, lasts for 2 hours or until another nonce has been found + // TODO: add command argument + if binary.BigEndian.Uint64(Found.nonce[:]) > 0 && time.Now().Sub(Found.timestamp) < time.Hour*2 { + copy(qnonce[:], Found.nonce[:]) + qrand.Read(qnonce[0:2]) + mbl.Nonce[1] = binary.BigEndian.Uint32(qnonce[0:4]) + mbl.Nonce[2] = binary.BigEndian.Uint32(qnonce[4:8]) + } else { + qrand.Read(qnonce[:]) + mbl.Nonce[1] = binary.BigEndian.Uint32(qnonce[0:4]) + mbl.Nonce[2] = binary.BigEndian.Uint32(qnonce[4:8]) } + Found.Unlock() } - mbl.Flags = 3735928559 // ;) - params.Blockhashing_blob = fmt.Sprintf("%x", mbl.Serialize()) encoder := json.NewEncoder(&out) diff --git a/proxy/server.go b/proxy/server.go index ab5e494..7a59441 100644 --- a/proxy/server.go +++ b/proxy/server.go @@ -5,6 +5,8 @@ import ( "crypto/elliptic" "crypto/rand" "crypto/x509" + "encoding/hex" + "encoding/json" "encoding/pem" "fmt" "math/big" @@ -14,6 +16,7 @@ import ( "sync" "time" + "github.com/deroproject/derohe/block" "github.com/deroproject/derohe/globals" "github.com/deroproject/derohe/rpc" "github.com/deroproject/graviton" @@ -46,6 +49,7 @@ var client_list = map[*websocket.Conn]*user_session{} var miners_count int var Wallet_count map[string]uint var Address string +var Found nonce_store func Start_server(listen string) { var err error @@ -168,6 +172,8 @@ func newUpgrader() *websocket.Upgrader { u.OnMessage(func(c *websocket.Conn, messageType websocket.MessageType, data []byte) { + var params rpc.SubmitBlock_Params + if messageType != websocket.TextMessage { return } @@ -177,6 +183,16 @@ func newUpgrader() *websocket.Upgrader { SendToDaemon(data) fmt.Printf("%v Submitting result from miner: %v, Wallet: %v\n", time.Now().Format(time.Stamp), c.RemoteAddr().String(), client_list[c].address.String()) + // Store nonce + if err := json.Unmarshal(data, ¶ms); err == nil { + Found.RLock() + var mbl []byte + mbl, _ = hex.DecodeString(params.MiniBlockhashing_blob) + Found.timestamp = time.Now() + copy(Found.nonce[:], mbl[block.MINIBLOCK_SIZE-8:block.MINIBLOCK_SIZE]) + fmt.Printf("%v Nonce %x, timestamp %v\n", time.Now().Format(time.Stamp), Found.nonce, Found.timestamp.Format(time.Stamp)) + Found.RUnlock() + } }) u.OnClose(func(c *websocket.Conn, err error) {