From d5e4d7aecfbad958b72564dd7f0429748b570ec4 Mon Sep 17 00:00:00 2001 From: cryptonote-social Date: Mon, 28 Dec 2020 08:30:07 -0800 Subject: [PATCH] ability to batch & send multiple chats per share --- minerlib/chat/chat.go | 29 ++++++++++++++++++++++------- minerlib/minerlib.go | 8 +++++--- stratum/client/client.go | 12 ++++++++---- 3 files changed, 35 insertions(+), 14 deletions(-) diff --git a/minerlib/chat/chat.go b/minerlib/chat/chat.go index f498fa0..4187491 100644 --- a/minerlib/chat/chat.go +++ b/minerlib/chat/chat.go @@ -24,6 +24,11 @@ var ( randID int64 ) +const ( + HASHES_PER_CHAT = 5000 + MAX_CHATS_PER_SHARE = 5 +) + func init() { err := binary.Read(rand.Reader, binary.LittleEndian, &randID) if err != nil { @@ -41,16 +46,26 @@ func SendChat(chat string) int64 { return int64(len(chatQueue)-1) ^ randID } -// GetChatToSend returns the next queued chat message that needs to be delivered. The function -// will return the same result until ChatSent is called. It will return ("", -1) if there are no -// chats to send at this time. -func GetChatToSend() (chat string, id int64) { +// GetChatsToSend returns the next queud chat messages to deliver with a valid mining share. It +// requires at least HASHES_PER_CHAT hashes to be computed per chat returned, and returns up to +// MAX_CHATS_PER_SHARE. Returns nil if there are no chats queued to send. +func GetChatsToSend(diff int64) []client.ChatToSend { mutex.Lock() defer mutex.Unlock() - if chatToSendIndex >= len(chatQueue) { - return "", -1 + if chatToSendIndex == len(chatQueue) { + return nil } - return chatQueue[chatToSendIndex], int64(chatToSendIndex) ^ randID + r := []client.ChatToSend{} + for diff > HASHES_PER_CHAT && chatToSendIndex < len(chatQueue) && len(r) < MAX_CHATS_PER_SHARE { + r = append(r, client.ChatToSend{ + ID: int64(chatToSendIndex) ^ randID, + Message: chatQueue[chatToSendIndex], + }) + chatToSendIndex++ + diff -= HASHES_PER_CHAT + } + // TODO: verify the total bytes we will be sending is within the server's request size limit + return r } func HasChatsToSend() bool { diff --git a/minerlib/minerlib.go b/minerlib/minerlib.go index 16b74f1..a3f6eae 100644 --- a/minerlib/minerlib.go +++ b/minerlib/minerlib.go @@ -666,9 +666,9 @@ func goMine(job client.MultiClientJob, thread int) { } time.Sleep(time.Second) } - chatMsg, chatID := chat.GetChatToSend() + chats := chat.GetChatsToSend(int64(diffTarget)) //crylog.Info("sending chatmsg:", chatMsg) - resp, err := cl.SubmitWork(fnonce, jobid, chatMsg, chatID) + resp, err := cl.SubmitWork(fnonce, jobid, chats) if err != nil { crylog.Warn("Submit work client failure:", jobid, err) cl.Close() @@ -679,7 +679,9 @@ func goMine(job client.MultiClientJob, thread int) { crylog.Warn("Submit work server error:", jobid, resp.Error) return } - chat.ChatSent(chatID) + for i := range chats { + chat.ChatSent(chats[i].ID) + } stats.ShareAccepted(diffTarget) if resp.Result == nil { crylog.Warn("nil result") diff --git a/stratum/client/client.go b/stratum/client/client.go index d3dfe2a..fcfca3c 100644 --- a/stratum/client/client.go +++ b/stratum/client/client.go @@ -308,8 +308,13 @@ func (cl *Client) GetChats(chatToken int64) (*Response, error) { return cl.submitRequest(chatRequest, GET_CHATS_JSON_ID) } +type ChatToSend struct { + ID int64 + Message string +} + // if error is returned then client will be closed and put in not-alive state -func (cl *Client) SubmitWork(nonce string, jobid string, chat string, chatID int64) (*Response, error) { +func (cl *Client) SubmitWork(nonce string, jobid string, chats []ChatToSend) (*Response, error) { submitRequest := &struct { ID uint64 `json:"id"` Method string `json:"method"` @@ -323,9 +328,8 @@ func (cl *Client) SubmitWork(nonce string, jobid string, chat string, chatID int Nonce string `json:"nonce"` Result string `json:"result"` - Chat string `json:"chat"` - ChatID int64 `json:"chat_id"` - }{"696969", jobid, nonce, "", chat, chatID}, + Chats []ChatToSend `json:"chats"` + }{"696969", jobid, nonce, "", chats}, } return cl.submitRequest(submitRequest, SUBMIT_WORK_JSON_ID) }