Skip to content

Commit

Permalink
Adjust to only send work cancel to non-bpow requests
Browse files Browse the repository at this point in the history
  • Loading branch information
bbedward committed Mar 9, 2024
1 parent c503ce3 commit e65760d
Showing 1 changed file with 18 additions and 8 deletions.
26 changes: 18 additions & 8 deletions net/rpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"encoding/json"
"fmt"
"io"
"io/ioutil"
"net/http"
"time"

Expand Down Expand Up @@ -38,7 +37,7 @@ func (client *RPCClient) MakeRequest(request interface{}) ([]byte, error) {
}
defer resp.Body.Close()
// Try to decode+deserialize
body, err := ioutil.ReadAll(resp.Body)
body, err := io.ReadAll(resp.Body)
if err != nil {
klog.Errorf("Error decoding response body %s", err)
return nil, err
Expand Down Expand Up @@ -127,6 +126,11 @@ func (client *RPCClient) MakeBlockRequest(hash string) (models.BlockResponse, er
return blockResponse, nil
}

type workResult struct {
result string
source string // "bpowClient" or "httpRequest"
}

func (client *RPCClient) WorkGenerate(hash string, difficultyMultiplier int) (string, error) {
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()
Expand All @@ -143,7 +147,7 @@ func (client *RPCClient) WorkGenerate(hash string, difficultyMultiplier int) (st
return "", fmt.Errorf("No work providers available")
}

results := make(chan string, chanSize)
results := make(chan workResult, chanSize)
errors := make(chan error, chanSize)

if client.BpowClient != nil {
Expand All @@ -153,7 +157,7 @@ func (client *RPCClient) WorkGenerate(hash string, difficultyMultiplier int) (st
errors <- err
return
}
results <- res
results <- workResult{result: res, source: "bpowClient"}
}()
}

Expand All @@ -164,16 +168,18 @@ func (client *RPCClient) WorkGenerate(hash string, difficultyMultiplier int) (st

select {
case res := <-results:
client.sendWorkCancel(hash)
return res, nil
if res.source != "httpRequest" {
client.sendWorkCancel(hash) // Only send work cancel if the result did not come from HTTP request
}
return res.result, nil
case err := <-errors:
return "", err
case <-ctx.Done():
return "", ctx.Err()
}
}

func (client *RPCClient) httpWorkGenerate(ctx context.Context, hash string, difficultyMultiplier int, results chan<- string, errors chan<- error) {
func (client *RPCClient) httpWorkGenerate(ctx context.Context, hash string, difficultyMultiplier int, results chan<- workResult, errors chan<- error) {
difficulty := "fffffff800000000"
if difficultyMultiplier < 64 {
difficulty = "fffffe0000000000"
Expand Down Expand Up @@ -212,7 +218,11 @@ func (client *RPCClient) httpWorkGenerate(ctx context.Context, hash string, diff
return
}

results <- workResp.Work
// Send the result along with the source identifier
results <- workResult{
result: workResp.Work,
source: "httpRequest",
}
}

func (client *RPCClient) sendWorkCancel(hash string) {
Expand Down

0 comments on commit e65760d

Please sign in to comment.