Skip to content
This repository has been archived by the owner on Oct 21, 2024. It is now read-only.

Commit

Permalink
feat: removed bestQuote, fastestQuote, implemented slice return from …
Browse files Browse the repository at this point in the history
…policyQuote and feeQuote (#11)
  • Loading branch information
kuba-4chain authored Aug 15, 2023
1 parent 7596179 commit 3d7f23c
Show file tree
Hide file tree
Showing 17 changed files with 385 additions and 460 deletions.
7 changes: 0 additions & 7 deletions broadcast/config.go

This file was deleted.

15 changes: 2 additions & 13 deletions broadcast/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,14 @@ package broadcast

import (
"context"
"time"
)

type BestQuoter interface {
GetBestQuote(ctx context.Context) (*FeeQuote, error)
}

type FastestQuoter interface {
GetFastestQuote(ctx context.Context, timeout time.Duration) (*FeeQuote, error)
}

type FeeQuoter interface {
GetFeeQuote(ctx context.Context) (*FeeQuote, error)
GetFeeQuote(ctx context.Context) ([]*FeeQuote, error)
}

type PolicyQuoter interface {
GetPolicyQuote(ctx context.Context) (*PolicyQuoteResponse, error)
GetPolicyQuote(ctx context.Context) ([]*PolicyQuoteResponse, error)
}

// TransactionQuerier is the interface that wraps the QueryTransaction method.
Expand All @@ -45,8 +36,6 @@ type TransactionsSubmitter interface {

// Client is a grouping interface that represents the entire exposed functionality of the broadcast client.
type Client interface {
BestQuoter
FastestQuoter
FeeQuoter
PolicyQuoter
TransactionQuerier
Expand Down
149 changes: 149 additions & 0 deletions broadcast/internal/acceptance_tests/arc_fee_quote_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
package acceptancetests

import (
"context"
"errors"
"io"
"net/http"
"strings"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"

"github.com/bitcoin-sv/go-broadcast-client/broadcast"
broadcast_client "github.com/bitcoin-sv/go-broadcast-client/broadcast/broadcast-client"
"github.com/bitcoin-sv/go-broadcast-client/broadcast/internal/arc"
)

var firstSuccessfulFeeQuoteResponse = `
{
"policy": {
"maxscriptsizepolicy": 100000000,
"maxtxsigopscountspolicy": 4294967295,
"maxtxsizepolicy": 100000000,
"miningFee": {
"bytes": 1000,
"satoshis": 1
}
},
"timestamp": "2023-08-10T13:49:07.308687569Z"
}
`

var secondSuccessfulFeeQuoteResponse = `
{
"policy": {
"maxscriptsizepolicy": 100000000,
"maxtxsigopscountspolicy": 4294967295,
"maxtxsizepolicy": 100000000,
"miningFee": {
"bytes": 1000,
"satoshis": 2
}
},
"timestamp": "2023-08-10T13:49:07.308687569Z"
}
`

func TestFeeQuote(t *testing.T) {
t.Run("Should successfully query from multiple ArcClients", func(t *testing.T) {
// given
httpClientMock := &arc.MockHttpClient{}
broadcaster := broadcast_client.Builder().
WithHttpClient(httpClientMock).
WithArc(broadcast_client.ArcClientConfig{APIUrl: "http://arc1-api-url", Token: "arc1-token"}).
WithArc(broadcast_client.ArcClientConfig{APIUrl: "http://arc2-api-url", Token: "arc2-token"}).
Build()

httpResponse1 := &http.Response{
StatusCode: http.StatusOK,
Body: io.NopCloser(strings.NewReader(firstSuccessfulFeeQuoteResponse)),
}
httpResponse2 := &http.Response{
StatusCode: http.StatusOK,
Body: io.NopCloser(strings.NewReader(secondSuccessfulFeeQuoteResponse)),
}
httpClientMock.On("DoRequest", mock.Anything, mock.Anything).
Return(httpResponse1, nil).
Once()
httpClientMock.On("DoRequest", mock.Anything, mock.Anything).
Return(httpResponse2, nil).
Once()

// when
result, err := broadcaster.GetPolicyQuote(context.Background())

// then
assert.NoError(t, err)
assert.NotNil(t, result)
})

t.Run("Should return error if all ArcClients return errors", func(t *testing.T) {
// given
httpClientMock := &arc.MockHttpClient{}
broadcaster := broadcast_client.Builder().
WithHttpClient(httpClientMock).
WithArc(broadcast_client.ArcClientConfig{APIUrl: "http://arc1-api-url", Token: "arc1-token"}).
WithArc(broadcast_client.ArcClientConfig{APIUrl: "http://arc2-api-url", Token: "arc2-token"}).
Build()

httpResponse := &http.Response{}
httpClientMock.On("DoRequest", mock.Anything, mock.Anything).
Return(httpResponse, errors.New("http error")).
Twice()

// when
result, err := broadcaster.GetPolicyQuote(context.Background())

// then
assert.Error(t, err)
assert.Nil(t, result)
assert.EqualError(t, err, broadcast.ErrNoMinerResponse.Error())
})

t.Run("Should successfully query from single ArcClient", func(t *testing.T) {
// given
httpClientMock := &arc.MockHttpClient{}
broadcaster := broadcast_client.Builder().
WithHttpClient(httpClientMock).
WithArc(broadcast_client.ArcClientConfig{APIUrl: "http://arc1-api-url", Token: "arc1-token"}).
Build()

httpResponse1 := &http.Response{
StatusCode: http.StatusOK,
Body: io.NopCloser(strings.NewReader(firstSuccessfulFeeQuoteResponse)),
}
httpClientMock.On("DoRequest", mock.Anything, mock.Anything).
Return(httpResponse1, nil).
Once()

// when
result, err := broadcaster.GetPolicyQuote(context.Background())

// then
assert.NoError(t, err)
assert.NotNil(t, result)
})

t.Run("Should return error if single ArcClient returns error", func(t *testing.T) {
// given
httpClientMock := &arc.MockHttpClient{}
httpResponse := &http.Response{}
broadcaster := broadcast_client.Builder().
WithHttpClient(httpClientMock).
WithArc(broadcast_client.ArcClientConfig{APIUrl: "http://arc1-api-url", Token: "arc1-token"}).
Build()

httpClientMock.On("DoRequest", mock.Anything, mock.Anything).
Return(httpResponse, errors.New("http error")).
Once()

// when
result, err := broadcaster.GetPolicyQuote(context.Background())

// then
assert.Error(t, err)
assert.Nil(t, result)
})
}
149 changes: 149 additions & 0 deletions broadcast/internal/acceptance_tests/arc_policy_quote_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
package acceptancetests

import (
"context"
"errors"
"io"
"net/http"
"strings"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"

"github.com/bitcoin-sv/go-broadcast-client/broadcast"
broadcast_client "github.com/bitcoin-sv/go-broadcast-client/broadcast/broadcast-client"
"github.com/bitcoin-sv/go-broadcast-client/broadcast/internal/arc"
)

var firstSuccessfulPolicyResponse = `
{
"policy": {
"maxscriptsizepolicy": 100000000,
"maxtxsigopscountspolicy": 4294967295,
"maxtxsizepolicy": 100000000,
"miningFee": {
"bytes": 1000,
"satoshis": 1
}
},
"timestamp": "2023-08-10T13:49:07.308687569Z"
}
`

var secondSuccessfulPolicyResponse = `
{
"policy": {
"maxscriptsizepolicy": 100000000,
"maxtxsigopscountspolicy": 4294967295,
"maxtxsizepolicy": 100000000,
"miningFee": {
"bytes": 1000,
"satoshis": 2
}
},
"timestamp": "2023-08-10T13:49:07.308687569Z"
}
`

func TestPolicyQuote(t *testing.T) {
t.Run("Should successfully query from multiple ArcClients", func(t *testing.T) {
// given
httpClientMock := &arc.MockHttpClient{}
broadcaster := broadcast_client.Builder().
WithHttpClient(httpClientMock).
WithArc(broadcast_client.ArcClientConfig{APIUrl: "http://arc1-api-url", Token: "arc1-token"}).
WithArc(broadcast_client.ArcClientConfig{APIUrl: "http://arc2-api-url", Token: "arc2-token"}).
Build()

httpResponse1 := &http.Response{
StatusCode: http.StatusOK,
Body: io.NopCloser(strings.NewReader(firstSuccessfulPolicyResponse)),
}
httpResponse2 := &http.Response{
StatusCode: http.StatusOK,
Body: io.NopCloser(strings.NewReader(secondSuccessfulPolicyResponse)),
}
httpClientMock.On("DoRequest", mock.Anything, mock.Anything).
Return(httpResponse1, nil).
Once()
httpClientMock.On("DoRequest", mock.Anything, mock.Anything).
Return(httpResponse2, nil).
Once()

// when
result, err := broadcaster.GetPolicyQuote(context.Background())

// then
assert.NoError(t, err)
assert.NotNil(t, result)
})

t.Run("Should return error if all ArcClients return errors", func(t *testing.T) {
// given
httpClientMock := &arc.MockHttpClient{}
broadcaster := broadcast_client.Builder().
WithHttpClient(httpClientMock).
WithArc(broadcast_client.ArcClientConfig{APIUrl: "http://arc1-api-url", Token: "arc1-token"}).
WithArc(broadcast_client.ArcClientConfig{APIUrl: "http://arc2-api-url", Token: "arc2-token"}).
Build()

httpResponse := &http.Response{}
httpClientMock.On("DoRequest", mock.Anything, mock.Anything).
Return(httpResponse, errors.New("http error")).
Twice()

// when
result, err := broadcaster.GetPolicyQuote(context.Background())

// then
assert.Error(t, err)
assert.Nil(t, result)
assert.EqualError(t, err, broadcast.ErrNoMinerResponse.Error())
})

t.Run("Should successfully query from single ArcClient", func(t *testing.T) {
// given
httpClientMock := &arc.MockHttpClient{}
broadcaster := broadcast_client.Builder().
WithHttpClient(httpClientMock).
WithArc(broadcast_client.ArcClientConfig{APIUrl: "http://arc1-api-url", Token: "arc1-token"}).
Build()

httpResponse1 := &http.Response{
StatusCode: http.StatusOK,
Body: io.NopCloser(strings.NewReader(firstSuccessfulPolicyResponse)),
}
httpClientMock.On("DoRequest", mock.Anything, mock.Anything).
Return(httpResponse1, nil).
Once()

// when
result, err := broadcaster.GetPolicyQuote(context.Background())

// then
assert.NoError(t, err)
assert.NotNil(t, result)
})

t.Run("Should return error if single ArcClient returns error", func(t *testing.T) {
// given
httpClientMock := &arc.MockHttpClient{}
httpResponse := &http.Response{}
broadcaster := broadcast_client.Builder().
WithHttpClient(httpClientMock).
WithArc(broadcast_client.ArcClientConfig{APIUrl: "http://arc1-api-url", Token: "arc1-token"}).
Build()

httpClientMock.On("DoRequest", mock.Anything, mock.Anything).
Return(httpResponse, errors.New("http error")).
Once()

// when
result, err := broadcaster.GetPolicyQuote(context.Background())

// then
assert.Error(t, err)
assert.Nil(t, result)
})
}
21 changes: 0 additions & 21 deletions broadcast/internal/arc/arc_best_quote.go

This file was deleted.

Loading

0 comments on commit 3d7f23c

Please sign in to comment.