Replace encoding/hex and encoding/json dependencies on monero RPC code
This commit is contained in:
parent
5a50924816
commit
b36c9561b9
|
@ -3,8 +3,8 @@ package rpc
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"context"
|
"context"
|
||||||
"encoding/json"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"git.gammaspectra.live/P2Pool/consensus/v3/utils"
|
||||||
"io"
|
"io"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/url"
|
"net/url"
|
||||||
|
@ -137,7 +137,7 @@ func (c *Client) RawRequest(ctx context.Context, endpoint string, params interfa
|
||||||
var body io.Reader
|
var body io.Reader
|
||||||
|
|
||||||
if params != nil {
|
if params != nil {
|
||||||
b, err := json.Marshal(params)
|
b, err := utils.MarshalJSON(params)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("marshal: %w", err)
|
return fmt.Errorf("marshal: %w", err)
|
||||||
}
|
}
|
||||||
|
@ -166,7 +166,7 @@ func (c *Client) JSONRPC(ctx context.Context, method string, params interface{},
|
||||||
address := *c.address
|
address := *c.address
|
||||||
address.Path = endpointJSONRPC
|
address.Path = endpointJSONRPC
|
||||||
|
|
||||||
b, err := json.Marshal(&RequestEnvelope{
|
b, err := utils.MarshalJSON(&RequestEnvelope{
|
||||||
ID: "0",
|
ID: "0",
|
||||||
JSONRPC: versionJSONRPC,
|
JSONRPC: versionJSONRPC,
|
||||||
Method: method,
|
Method: method,
|
||||||
|
@ -215,7 +215,7 @@ func (c *Client) submitRequest(req *http.Request, response interface{}) error {
|
||||||
return fmt.Errorf("non-2xx status code: %d", resp.StatusCode)
|
return fmt.Errorf("non-2xx status code: %d", resp.StatusCode)
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := json.NewDecoder(resp.Body).Decode(response); err != nil {
|
if err := utils.NewJSONDecoder(resp.Body).Decode(response); err != nil {
|
||||||
return fmt.Errorf("decode: %w", err)
|
return fmt.Errorf("decode: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -2,8 +2,8 @@ package rpc_test
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"encoding/json"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"git.gammaspectra.live/P2Pool/consensus/v3/utils"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/http/httptest"
|
"net/http/httptest"
|
||||||
"testing"
|
"testing"
|
||||||
|
@ -100,7 +100,7 @@ func TestClient(t *testing.T) {
|
||||||
)
|
)
|
||||||
|
|
||||||
handler := func(w http.ResponseWriter, r *http.Request) {
|
handler := func(w http.ResponseWriter, r *http.Request) {
|
||||||
err := json.NewDecoder(r.Body).Decode(body)
|
err := utils.NewJSONDecoder(r.Body).Decode(body)
|
||||||
assert.NoError(t, err)
|
assert.NoError(t, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -3,9 +3,9 @@ package daemon
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"context"
|
"context"
|
||||||
"encoding/hex"
|
|
||||||
"errors"
|
"errors"
|
||||||
"git.gammaspectra.live/P2Pool/consensus/v3/monero/client/levin"
|
"git.gammaspectra.live/P2Pool/consensus/v3/monero/client/levin"
|
||||||
|
fasthex "github.com/tmthrgd/go-hex"
|
||||||
"io"
|
"io"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -17,7 +17,7 @@ func (c *Client) GetOIndexes(
|
||||||
ctx context.Context, txid string,
|
ctx context.Context, txid string,
|
||||||
) (indexes []uint64, finalError error) {
|
) (indexes []uint64, finalError error) {
|
||||||
|
|
||||||
binaryTxId, err := hex.DecodeString(txid)
|
binaryTxId, err := fasthex.DecodeString(txid)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,9 +2,9 @@ package daemon
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"encoding/hex"
|
|
||||||
"encoding/json"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"git.gammaspectra.live/P2Pool/consensus/v3/utils"
|
||||||
|
fasthex "github.com/tmthrgd/go-hex"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
|
@ -251,7 +251,7 @@ func (c *Client) SubmitBlock(ctx context.Context, blobs ...[]byte) (*SubmitBlock
|
||||||
|
|
||||||
params := make([]string, 0, len(blobs))
|
params := make([]string, 0, len(blobs))
|
||||||
for _, blob := range blobs {
|
for _, blob := range blobs {
|
||||||
params = append(params, hex.EncodeToString(blob))
|
params = append(params, fasthex.EncodeToString(blob))
|
||||||
}
|
}
|
||||||
|
|
||||||
err := c.JSONRPC(ctx, methodSubmitBlock, params, resp)
|
err := c.JSONRPC(ctx, methodSubmitBlock, params, resp)
|
||||||
|
@ -321,7 +321,7 @@ func (c *Client) GetCoinbaseTxSum(
|
||||||
func (j *GetBlockResult) InnerJSON() (*GetBlockResultJSON, error) {
|
func (j *GetBlockResult) InnerJSON() (*GetBlockResultJSON, error) {
|
||||||
res := &GetBlockResultJSON{}
|
res := &GetBlockResultJSON{}
|
||||||
|
|
||||||
err := json.Unmarshal([]byte(j.JSON), res)
|
err := utils.UnmarshalJSON([]byte(j.JSON), res)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("unmarshal: %w", err)
|
return nil, fmt.Errorf("unmarshal: %w", err)
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,8 +2,8 @@ package daemon
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"encoding/json"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"git.gammaspectra.live/P2Pool/consensus/v3/utils"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
|
@ -229,7 +229,7 @@ func (r *GetTransactionsResult) GetTransactions() ([]*TransactionJSON, error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
t := &TransactionJSON{}
|
t := &TransactionJSON{}
|
||||||
err := json.Unmarshal([]byte(txn.AsJSON), t)
|
err := utils.UnmarshalJSON([]byte(txn.AsJSON), t)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("unmarshal txn '%s': %w",
|
return nil, fmt.Errorf("unmarshal txn '%s': %w",
|
||||||
txn.TxHash, err)
|
txn.TxHash, err)
|
||||||
|
|
Loading…
Reference in a new issue