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

feat(BUX-120, BUX-148, BUX-150): broadcast, query subtasks #2

Merged
merged 16 commits into from
Aug 2, 2023
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 0 additions & 22 deletions broadcast/arc/arc_client.go

This file was deleted.

52 changes: 44 additions & 8 deletions broadcast/broadcast.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,21 @@ package broadcast

import (
"context"
"fmt"

"github.com/bitcoin-sv/go-broadcast-client/common"
"github.com/bitcoin-sv/go-broadcast-client/models"
)

type BroadcastFactory func() Broadcaster

type Broadcaster interface {
BestQuoter
// BestQuoter
// FastestQuoter
// FeeQuoter
// PolicyQuoter
// TransactionQuerier
// TransactionSubmitter
TransactionQuerier
TransactionSubmitter
// TransactionsSubmitter
}

Expand All @@ -32,12 +36,44 @@ func NewCompositeBroadcaster(strategy Strategy, factories ...BroadcastFactory) B
}
}

func (c *compositeBroadcaster) BestQuote(ctx context.Context, feeCategory, feeType string) error {
executionFuncs := make([]func(context.Context) error, len(c.broadcasters))
func (c *compositeBroadcaster) QueryTransaction(ctx context.Context, txID string) (*models.QueryTxResponse, error) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why this is here and not in internal?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure why it shouldn't be here

dorzepowski marked this conversation as resolved.
Show resolved Hide resolved
executionFuncs := make([]ExecutionFunc, len(c.broadcasters))
for i, broadcaster := range c.broadcasters {
executionFuncs[i] = func(ctx context.Context) (Result, error) {
return broadcaster.QueryTransaction(ctx, txID)
}
}
result, err := c.strategy.Execute(ctx, executionFuncs)
if err != nil {
return nil, err
}

// Convert result to QueryTxResponse
queryTxResponse, ok := result.(*models.QueryTxResponse)
if !ok {
return nil, fmt.Errorf("unexpected result type: %T", result)
}

return queryTxResponse, nil
}

func (c *compositeBroadcaster) SubmitTransaction(ctx context.Context, tx *common.Transaction) (*models.SubmitTxResponse, error) {
executionFuncs := make([]ExecutionFunc, len(c.broadcasters))
for i, broadcaster := range c.broadcasters {
executionFuncs[i] = func(ctx context.Context) error {
return broadcaster.BestQuote(ctx, feeCategory, feeType)
executionFuncs[i] = func(ctx context.Context) (Result, error) {
return broadcaster.SubmitTransaction(ctx, tx)
}
}
return c.strategy.Execute(ctx, executionFuncs)
result, err := c.strategy.Execute(ctx, executionFuncs)
if err != nil {
return nil, err
}

// Convert result to QueryTxResponse
submitTxResponse, ok := result.(*models.SubmitTxResponse)
if !ok {
return nil, fmt.Errorf("unexpected result type: %T", result)
}

return submitTxResponse, nil
}
54 changes: 23 additions & 31 deletions broadcast/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,39 +2,31 @@ package broadcast

import (
"context"

"github.com/bitcoin-sv/go-broadcast-client/common"
"github.com/bitcoin-sv/go-broadcast-client/models"
)

type BestQuoter interface {
// BestQuote(ctx context.Context, feeCategory, feeType string) (*FeeQuoteResponse, error)
BestQuote(ctx context.Context, feeCategory, feeType string) error
}

// type FastestQuoter interface {
// // FastestQuote(ctx context.Context, timeout time.Duration) (*FeeQuoteResponse, error)
// FastestQuote(ctx context.Context, timeout time.Duration) error
// }

// type FeeQuoter interface {
// // FeeQuote(ctx context.Context, miner *Miner) (*FeeQuoteResponse, error)
// FeeQuote(ctx context.Context) error
// }

// type PolicyQuoter interface {
// // PolicyQuote(ctx context.Context, miner *Miner) (*PolicyQuoteResponse, error)
// PolicyQuote(ctx context.Context) error
// }

// type TransactionQuerier interface {
// // // QueryTransaction(ctx context.Context, miner *Miner, txID string, opts ...QueryTransactionOptFunc) (*QueryTransactionResponse, error)
// QueryTransaction(ctx context.Context, txID string) error
// }

// type TransactionSubmitter interface {
// // SubmitTransaction(ctx context.Context, miner *Miner, tx *Transaction) (*SubmitTransactionResponse, error)
// SubmitTransaction(ctx context.Context) error
// }

// type TransactionsSubmitter interface {
// // SubmitTransactions(ctx context.Context, miner *Miner, txs []Transaction) (*SubmitTransactionsResponse, error)
// SubmitTransactions(ctx context.Context) error
// }
type FastestQuoter interface {
}

type FeeQuoter interface {
}

type PolicyQuoter interface {
}

type TransactionQuerier interface {
// Think about adding TransactionQueryOpts here if clients implement handling it in future
wregulski marked this conversation as resolved.
Show resolved Hide resolved
QueryTransaction(ctx context.Context, txID string) (*models.QueryTxResponse, error)
}

type TransactionSubmitter interface {
SubmitTransaction(ctx context.Context, tx *common.Transaction) (*models.SubmitTxResponse, error)
}

type TransactionsSubmitter interface {
}
23 changes: 13 additions & 10 deletions broadcast/strategy.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ package broadcast

import (
"context"
"fmt"

"github.com/bitcoin-sv/go-broadcast-client/shared"
)

type StrategyName string
Expand All @@ -11,31 +12,33 @@ const (
OneByOneStrategy StrategyName = "OneByOneStrategy"
)

type ExecutionFunc func(context.Context, []func(context.Context) error) error
type Result interface{}

type ExecutionFunc func(context.Context) (Result, error)
type StrategyExecutionFunc func(context.Context, []ExecutionFunc) (Result, error)

type Strategy struct {
name StrategyName
executionFunc ExecutionFunc
executionFunc StrategyExecutionFunc
}

func New(name StrategyName, executionFunc ExecutionFunc) *Strategy {
func New(name StrategyName, executionFunc StrategyExecutionFunc) *Strategy {
return &Strategy{name: name, executionFunc: executionFunc}
}

func (s *Strategy) Execute(ctx context.Context, executionFuncs []func(context.Context) error) error {
func (s *Strategy) Execute(ctx context.Context, executionFuncs []ExecutionFunc) (Result, error) {
return s.executionFunc(ctx, executionFuncs)
}

var (
OneByOne = New(OneByOneStrategy, func(ctx context.Context, executionFuncs []func(context.Context) error) error {
OneByOne = New(OneByOneStrategy, func(ctx context.Context, executionFuncs []ExecutionFunc) (Result, error) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

now, I'm not sure if this is best approach to create this strategy here.
Maybe it would be better to expose only names (as const) and factory function,
and factory function would return appropriate strategy for name

dorzepowski marked this conversation as resolved.
Show resolved Hide resolved
for _, executionFunc := range executionFuncs {
err := executionFunc(ctx)
result, err := executionFunc(ctx)
if err != nil {
continue
}
return nil
return result, nil
}
// return factory.ErrAllBroadcastersFailed
return fmt.Errorf("all broadcasters failed")
return nil, shared.ErrAllBroadcastersFailed
})
)
13 changes: 13 additions & 0 deletions common/transaction.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package common
wregulski marked this conversation as resolved.
Show resolved Hide resolved

// Transaction is the body contents in the "submit transaction" request
type Transaction struct {
CallBackEncryption string `json:"callBackEncryption,omitempty"`
CallBackToken string `json:"callBackToken,omitempty"`
CallBackURL string `json:"callBackUrl,omitempty"`
DsCheck bool `json:"dsCheck,omitempty"`
MerkleFormat string `json:"merkleFormat,omitempty"`
MerkleProof bool `json:"merkleProof,omitempty"`
RawTx string `json:"rawtx"`
WaitForStatus TxStatus `json:"waitForStatus,omitempty"`
}
77 changes: 77 additions & 0 deletions common/tx_status.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package common

// TxStatus is the status of the transaction
type TxStatus string

// List of statuses available here: https://github.com/bitcoin-sv/arc
const (
// Unknown contains value for unknown status
dorzepowski marked this conversation as resolved.
Show resolved Hide resolved
Unknown TxStatus = "UNKNOWN" // 0
// Queued contains value for queued status
Queued TxStatus = "QUEUED" // 1
// Received contains value for received status
Received TxStatus = "RECEIVED" // 2
// Stored contains value for stored status
Stored TxStatus = "STORED" // 3
// AnnouncedToNetwork contains value for announced to network status
AnnouncedToNetwork TxStatus = "ANNOUNCED_TO_NETWORK" // 4
// RequestedByNetwork contains value for requested by network status
RequestedByNetwork TxStatus = "REQUESTED_BY_NETWORK" // 5
// SentToNetwork contains value for sent to network status
SentToNetwork TxStatus = "SENT_TO_NETWORK" // 6
// AcceptedByNetwork contains value for accepted by network status
AcceptedByNetwork TxStatus = "ACCEPTED_BY_NETWORK" // 7
// SeenOnNetwork contains value for seen on network status
SeenOnNetwork TxStatus = "SEEN_ON_NETWORK" // 8
// Mined contains value for mined status
Mined TxStatus = "MINED" // 9
// Confirmed contains value for confirmed status
Confirmed TxStatus = "CONFIRMED" // 108
// Rejected contains value for rejected status
Rejected TxStatus = "REJECTED" // 109
)

// String returns the string representation of the TxStatus
func (s TxStatus) String() string {
statuses := map[TxStatus]string{
Unknown: "UNKNOWN",
Queued: "QUEUED",
Received: "RECEIVED",
Stored: "STORED",
AnnouncedToNetwork: "ANNOUNCED_TO_NETWORK",
RequestedByNetwork: "REQUESTED_BY_NETWORK",
SentToNetwork: "SENT_TO_NETWORK",
AcceptedByNetwork: "ACCEPTED_BY_NETWORK",
SeenOnNetwork: "SEEN_ON_NETWORK",
Mined: "MINED",
Confirmed: "CONFIRMED",
Rejected: "REJECTED",
}

if status, ok := statuses[s]; ok {
return status
}

return "Can't parse status"
}
wregulski marked this conversation as resolved.
Show resolved Hide resolved

// MapTxStatusToInt maps the TxStatus to an int value
func MapTxStatusToInt(status TxStatus) (int, bool) {
waitForStatusMap := map[TxStatus]int{
Unknown: 0,
Queued: 1,
Received: 2,
Stored: 3,
AnnouncedToNetwork: 4,
RequestedByNetwork: 5,
SentToNetwork: 6,
AcceptedByNetwork: 7,
SeenOnNetwork: 8,
Mined: 9,
Confirmed: 108,
Rejected: 109,
}

value, ok := waitForStatusMap[status]
return value, ok
wregulski marked this conversation as resolved.
Show resolved Hide resolved
}
5 changes: 5 additions & 0 deletions config/arc_config.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
package config

var ArcQueryTxRoute = "/v1/tx/"
var ArcPolicyQuoteRoute = "/v1/policy"
var ArcSubmitTxRoute = "/v1/tx"
var ArcSubmitTxsRoute = "/v1/txs"
wregulski marked this conversation as resolved.
Show resolved Hide resolved

type ArcClientConfig struct {
APIUrl string
Token string
Expand Down
4 changes: 2 additions & 2 deletions factory/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ package factory

import (
"github.com/bitcoin-sv/go-broadcast-client/broadcast"
"github.com/bitcoin-sv/go-broadcast-client/broadcast/arc"
"github.com/bitcoin-sv/go-broadcast-client/config"
internal "github.com/bitcoin-sv/go-broadcast-client/internal/arc"
wregulski marked this conversation as resolved.
Show resolved Hide resolved
)

func NewBroadcastClient(factories ...broadcast.BroadcastFactory) broadcast.Broadcaster {
Expand All @@ -16,6 +16,6 @@ func NewBroadcastClient(factories ...broadcast.BroadcastFactory) broadcast.Broad

func WithArc(config config.ArcClientConfig) broadcast.BroadcastFactory {
return func() broadcast.Broadcaster {
return arc.NewArcClient(config)
return internal.NewArcClient(config)
}
}
8 changes: 8 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
module github.com/bitcoin-sv/go-broadcast-client

go 1.20

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/stretchr/objx v0.5.0 // indirect
github.com/stretchr/testify v1.8.4 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
17 changes: 17 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
github.com/stretchr/objx v0.5.0 h1:1zr/of2m5FGMsad5YfcqgdqdWrIhu+EBEJRhR1U7z/c=
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
Loading