Skip to content

Commit

Permalink
Merge pull request #60 from bitcoin-sv/arc-failure
Browse files Browse the repository at this point in the history
Fix ArcBroadcaster to handle script failure and add new headers
  • Loading branch information
rohenaz authored Nov 26, 2024
2 parents 3c5bd7f + 947b39e commit fd67599
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 25 deletions.
2 changes: 0 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ require (
golang.org/x/sync v0.6.0
)

require github.com/stretchr/objx v0.5.2 // indirect

require (
github.com/pkg/errors v0.9.1
github.com/pmezard/go-difflib v1.0.0 // indirect
Expand Down
4 changes: 0 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,6 @@ github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
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.5.2 h1:xuMeJ0Sdp5ZMRXx/aWO6RZxdr3beISkG5/G/aIRr3pY=
github.com/stretchr/objx v0.5.2/go.mod h1:FRsXN1f5AsAjCGJKqEizvkpNtU+EGNCLh3NxZ/8L+MA=
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
golang.org/x/crypto v0.21.0 h1:X31++rzVUdKhX5sWmSOFZxx8UW/ldWx55cbf08iNAMA=
Expand Down
63 changes: 44 additions & 19 deletions transaction/broadcaster/arc.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"errors"
"fmt"
"io"
"log"
"net/http"
"time"

Expand All @@ -17,27 +18,33 @@ import (
type ArcStatus string

const (
RECEIVED ArcStatus = "2"
STORED ArcStatus = "3"
ANNOUNCED_TO_NETWORK ArcStatus = "4"
REQUESTED_BY_NETWORK ArcStatus = "5"
SENT_TO_NETWORK ArcStatus = "6"
ACCEPTED_BY_NETWORK ArcStatus = "7"
SEEN_ON_NETWORK ArcStatus = "8"
REJECTED ArcStatus = "REJECTED"
QUEUED ArcStatus = "QUEUED"
RECEIVED ArcStatus = "RECEIVED"
STORED ArcStatus = "STORED"
ANNOUNCED_TO_NETWORK ArcStatus = "ANNOUNCED_TO_NETWORK"
REQUESTED_BY_NETWORK ArcStatus = "REQUESTED_BY_NETWORK"
SENT_TO_NETWORK ArcStatus = "SENT_TO_NETWORK"
ACCEPTED_BY_NETWORK ArcStatus = "ACCEPTED_BY_NETWORK"
SEEN_ON_NETWORK ArcStatus = "SEEN_ON_NETWORK"
)

type Arc struct {
ApiUrl string
ApiKey string
CallbackUrl *string
CallbackToken *string
FullStatusUpdates bool
MaxTimeout *int
SkipFeeValidation bool
SkipScriptValidation bool
SkipTxValidation bool
WaitForStatus ArcStatus
Client HTTPClient // Added for testing
ApiUrl string
ApiKey string
CallbackUrl *string
CallbackToken *string
CallbackBatch bool
FullStatusUpdates bool
MaxTimeout *int
SkipFeeValidation bool
SkipScriptValidation bool
SkipTxValidation bool
CumulativeFeeValidation bool
WaitForStatus string
WaitFor ArcStatus
Client HTTPClient // Added for testing
Verbose bool
}

type ArcResponse struct {
Expand Down Expand Up @@ -109,6 +116,9 @@ func (a *Arc) Broadcast(t *transaction.Transaction) (*transaction.BroadcastSucce
if a.CallbackToken != nil {
req.Header.Set("X-CallbackToken", *a.CallbackToken)
}
if a.CallbackBatch {
req.Header.Set("X-CallbackBatch", "true")
}
if a.FullStatusUpdates {
req.Header.Set("X-FullStatusUpdates", "true")
}
Expand All @@ -124,8 +134,14 @@ func (a *Arc) Broadcast(t *transaction.Transaction) (*transaction.BroadcastSucce
if a.SkipTxValidation {
req.Header.Set("X-SkipTxValidation", "true")
}
if a.CumulativeFeeValidation {
req.Header.Set("X-CumulativeFeeValidation", "true")
}
if a.WaitForStatus != "" {
req.Header.Set("X-WaitForStatus", string(a.WaitForStatus))
req.Header.Set("X-WaitForStatus", a.WaitForStatus)
}
if a.WaitFor != "" {
req.Header.Set("X-WaitFor", string(a.WaitFor))
}

resp, err := a.Client.Do(req)
Expand All @@ -151,6 +167,9 @@ func (a *Arc) Broadcast(t *transaction.Transaction) (*transaction.BroadcastSucce
}

response := &ArcResponse{}
if a.Verbose {
log.Println("msg", string(msg))
}
err = json.Unmarshal(msg, &response)
if err != nil {
return nil, &transaction.BroadcastFailure{
Expand All @@ -159,6 +178,12 @@ func (a *Arc) Broadcast(t *transaction.Transaction) (*transaction.BroadcastSucce
}
}

if response.TxStatus != nil && *response.TxStatus == REJECTED {
return nil, &transaction.BroadcastFailure{
Code: "400",
Description: response.ExtraInfo,
}
}
if response.Status == 200 {
return &transaction.BroadcastSuccess{
Txid: response.Txid,
Expand Down

0 comments on commit fd67599

Please sign in to comment.