Skip to content

Commit

Permalink
abstract broadcaster
Browse files Browse the repository at this point in the history
  • Loading branch information
shruggr committed Nov 5, 2024
1 parent fb652d3 commit 5fb9a0c
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 8 deletions.
5 changes: 2 additions & 3 deletions broadcast/broadcast.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (

"github.com/bitcoin-sv/go-sdk/spv"
"github.com/bitcoin-sv/go-sdk/transaction"
"github.com/shruggr/1sat-indexer/config"
"github.com/shruggr/1sat-indexer/idx"
"github.com/shruggr/1sat-indexer/jb"
"github.com/shruggr/1sat-indexer/lib"
Expand All @@ -22,7 +21,7 @@ type BroadcaseResponse struct {
Error string `json:"error"`
}

func Broadcast(ctx context.Context, tx *transaction.Transaction) (response *BroadcaseResponse) {
func Broadcast(ctx context.Context, tx *transaction.Transaction, broadcaster transaction.Broadcaster) (response *BroadcaseResponse) {
txid := tx.TxID()
response = &BroadcaseResponse{
Txid: txid.String(),
Expand Down Expand Up @@ -100,7 +99,7 @@ func Broadcast(ctx context.Context, tx *transaction.Transaction) (response *Broa
}
}

if _, failure := config.Broadcaster.Broadcast(tx); failure != nil {
if _, failure := broadcaster.Broadcast(tx); failure != nil {
rollbackSpends(ctx, spendOutpoints, response.Txid)
if status, err := strconv.Atoi(failure.Code); err == nil {
response.Status = uint32(status)
Expand Down
2 changes: 1 addition & 1 deletion cmd/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func main() {
Concurrency: CONCURRENCY,
Network: config.Network,
Once: true,
})
}, config.Broadcaster)
log.Println("Listening on", PORT)
app.Listen(fmt.Sprintf(":%d", PORT))
}
6 changes: 4 additions & 2 deletions server/routes/tx/ctrl.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,11 @@ import (
)

var ingest *idx.IngestCtx
var b transaction.Broadcaster

func RegisterRoutes(r fiber.Router, ingestCtx *idx.IngestCtx) {
func RegisterRoutes(r fiber.Router, ingestCtx *idx.IngestCtx, broadcaster transaction.Broadcaster) {
ingest = ingestCtx
b = broadcaster
r.Post("/", BroadcastTx)
r.Get("/:txid", GetTxWithProof)
r.Get("/:txid/raw", GetRawTx)
Expand Down Expand Up @@ -50,7 +52,7 @@ func BroadcastTx(c *fiber.Ctx) (err error) {
return c.SendStatus(400)
}

response := broadcast.Broadcast(c.Context(), tx)
response := broadcast.Broadcast(c.Context(), tx, b)
if response.Success {
ingest.IngestTx(c.Context(), tx, idx.AncestorConfig{Load: true, Parse: true, Save: true})
}
Expand Down
5 changes: 3 additions & 2 deletions server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"strings"
"time"

"github.com/bitcoin-sv/go-sdk/transaction"
"github.com/gofiber/fiber/v2"
"github.com/gofiber/fiber/v2/middleware/compress"
"github.com/gofiber/fiber/v2/middleware/logger"
Expand All @@ -28,7 +29,7 @@ var currentSessions = sse.SessionsLock{
Topics: make(map[string][]*sse.Session),
}

func Initialize(ingestCtx *idx.IngestCtx) *fiber.App {
func Initialize(ingestCtx *idx.IngestCtx, broadcaster transaction.Broadcaster) *fiber.App {
app := fiber.New()
app.Use(recover.New())
app.Use(logger.New())
Expand All @@ -46,7 +47,7 @@ func Initialize(ingestCtx *idx.IngestCtx) *fiber.App {
origins.RegisterRoutes(v5.Group("/origins"), ingestCtx)
own.RegisterRoutes(v5.Group("/own"), ingestCtx)
tag.RegisterRoutes(v5.Group("/tag"), ingestCtx)
tx.RegisterRoutes(v5.Group("/tx"), ingestCtx)
tx.RegisterRoutes(v5.Group("/tx"), ingestCtx, broadcaster)
txos.RegisterRoutes(v5.Group("/txo"), ingestCtx)

app.Get("/v5/sse", func(c *fiber.Ctx) error {
Expand Down

0 comments on commit 5fb9a0c

Please sign in to comment.