Skip to content

Commit

Permalink
improved test coverage (#475)
Browse files Browse the repository at this point in the history
* feat(ARCO-73, ARCO-78): improve test coverage in broadcaster and peer_handler

* feat(ARCO-83): improve test coverage in pkg/api/handler/helpers

* feat: improve test coverage of broadcaster
  • Loading branch information
kuba-4chain authored Jun 25, 2024
1 parent 62a8ed1 commit 62792df
Show file tree
Hide file tree
Showing 7 changed files with 1,040 additions and 48 deletions.
8 changes: 1 addition & 7 deletions cmd/broadcaster-cli/helper/functions.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
package helper

import (
"errors"
"fmt"
"github.com/lmittmann/tint"
"log/slog"
"net/url"
"os"
"strconv"
"strings"
Expand All @@ -16,11 +14,7 @@ import (
)

func CreateClient(auth *broadcaster.Auth, arcServer string) (broadcaster.ArcClient, error) {
arcServerUrl, err := url.Parse(arcServer)
if err != nil {
return nil, errors.New("arcUrl is not a valid url")
}
return broadcaster.NewHTTPBroadcaster(arcServerUrl.String(), auth), nil
return broadcaster.NewHTTPBroadcaster(arcServer, auth)
}

func GetKeySetsKeyFile(keyFile string) (fundingKeySet *keyset.KeySet, receivingKeySet *keyset.KeySet, err error) {
Expand Down
79 changes: 39 additions & 40 deletions internal/broadcaster/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,15 @@ import (
"fmt"
"io"
"net/http"
"net/url"

"github.com/bitcoin-sv/arc/pkg/api"
"github.com/bitcoin-sv/arc/pkg/metamorph/metamorph_api"
"github.com/libsv/go-bt/v2"
)

type APIBroadcaster struct {
arcServer string
auth *Auth
arcClient api.ClientInterface
}

type Auth struct {
Expand All @@ -32,19 +32,16 @@ type Response struct {
BlockHash string `json:"blockHash"`
}

func NewHTTPBroadcaster(arcServer string, auth *Auth) *APIBroadcaster {
return &APIBroadcaster{
arcServer: arcServer,
auth: auth,
}
}

func (a *APIBroadcaster) BroadcastTransactions(ctx context.Context, txs []*bt.Tx, waitForStatus metamorph_api.Status, callbackURL string, callbackToken string, fullStatusUpdates bool, skipFeeValidation bool) ([]*metamorph_api.TransactionStatus, error) {
arcClient, err := a.getArcClient()
func NewHTTPBroadcaster(arcServer string, auth *Auth) (*APIBroadcaster, error) {
arcClient, err := getArcClient(arcServer, auth)
if err != nil {
return nil, err
}

return &APIBroadcaster{arcClient: arcClient}, nil
}

func (a *APIBroadcaster) BroadcastTransactions(ctx context.Context, txs []*bt.Tx, waitForStatus metamorph_api.Status, callbackURL string, callbackToken string, fullStatusUpdates bool, skipFeeValidation bool) ([]*metamorph_api.TransactionStatus, error) {
waitFor := api.WaitForStatus(waitForStatus)
params := &api.POSTTransactionsParams{
XWaitForStatus: &waitFor,
Expand All @@ -70,7 +67,7 @@ func (a *APIBroadcaster) BroadcastTransactions(ctx context.Context, txs []*bt.Tx
}

var response *http.Response
response, err = arcClient.POSTTransactions(ctx, params, body)
response, err := a.arcClient.POSTTransactions(ctx, params, body)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -118,11 +115,6 @@ func (a *APIBroadcaster) BroadcastTransactions(ctx context.Context, txs []*bt.Tx
}

func (a *APIBroadcaster) BroadcastTransaction(ctx context.Context, tx *bt.Tx, waitFor metamorph_api.Status, callbackURL string) (*metamorph_api.TransactionStatus, error) {
arcClient, err := a.getArcClient()
if err != nil {
return nil, err
}

waitForStatus := api.WaitForStatus(waitFor)
params := &api.POSTTransactionParams{
XWaitForStatus: &waitForStatus,
Expand All @@ -136,60 +128,67 @@ func (a *APIBroadcaster) BroadcastTransaction(ctx context.Context, tx *bt.Tx, wa
RawTx: hex.EncodeToString(tx.ExtendedBytes()),
}

var response *api.POSTTransactionResponse
response, err = arcClient.POSTTransactionWithResponse(ctx, params, arcBody)
response, err := a.arcClient.POSTTransaction(ctx, params, arcBody)
if err != nil {
return nil, err
}

var bodyBytes []byte
bodyBytes, err = io.ReadAll(response.Body)
if err != nil {
return nil, err
}

if response.StatusCode() != http.StatusOK && response.StatusCode() != http.StatusCreated {
if response != nil && response.HTTPResponse != nil {
if response.StatusCode != http.StatusOK && response.StatusCode != http.StatusCreated {
if response.Body != nil {
// read body into json map
var body map[string]interface{}
err = json.Unmarshal(response.Body, &body)
err = json.Unmarshal(bodyBytes, &body)
if err == nil {
responseBody, ok := body["detail"].(string)
if ok {
return nil, errors.New(responseBody)
}
return nil, fmt.Errorf("error: %s", string(response.Body))
return nil, fmt.Errorf("error: %s", string(bodyBytes))
}
}
return nil, errors.New("error: " + response.Status())
return nil, errors.New("error: " + response.Status)
}

bodyResponse := response.JSON200

blockHeight := *bodyResponse.BlockHeight
txStatus := bodyResponse.TxStatus

res := &metamorph_api.TransactionStatus{
Txid: bodyResponse.Txid,
Status: metamorph_api.Status(metamorph_api.Status_value[txStatus]),
BlockHeight: blockHeight,
BlockHash: *bodyResponse.BlockHash,
var bodyResponse Response
err = json.Unmarshal(bodyBytes, &bodyResponse)
if err != nil {
return nil, err
}

if bodyResponse.ExtraInfo != nil {
res.RejectReason = *bodyResponse.ExtraInfo
res := &metamorph_api.TransactionStatus{
Txid: bodyResponse.Txid,
Status: metamorph_api.Status(metamorph_api.Status_value[bodyResponse.TxStatus]),
BlockHeight: bodyResponse.BlockHeight,
BlockHash: bodyResponse.BlockHash,
RejectReason: bodyResponse.ExtraInfo,
}

return res, nil
}

func (a *APIBroadcaster) getArcClient() (*api.ClientWithResponses, error) {
func getArcClient(arcServer string, auth *Auth) (*api.Client, error) {
_, err := url.Parse(arcServer)
if arcServer == "" || err != nil {
return nil, errors.New("arcUrl is not a valid url")
}

opts := make([]api.ClientOption, 0)

if a.auth.Authorization != "" {
if auth != nil && auth.Authorization != "" {
// custom provider
opts = append(opts, api.WithRequestEditorFn(func(ctx context.Context, req *http.Request) error {
req.Header.Add("Authorization", a.auth.Authorization)
req.Header.Add("Authorization", auth.Authorization)
return nil
}))
}

arcClient, err := api.NewClientWithResponses(a.arcServer, opts...)
arcClient, err := api.NewClient(arcServer, opts...)
if err != nil {
return nil, err
}
Expand Down
Loading

0 comments on commit 62792df

Please sign in to comment.