Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ARCO-153: Parallelise Create UTXO #584

Merged
merged 7 commits into from
Sep 17, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
51 changes: 30 additions & 21 deletions cmd/broadcaster-cli/app/utxos/create/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,14 @@ import (
"fmt"
"log"
"log/slog"

"github.com/bitcoin-sv/arc/pkg/keyset"

"github.com/spf13/cobra"
"github.com/spf13/viper"
"os"
"os/signal"

"github.com/bitcoin-sv/arc/cmd/broadcaster-cli/helper"
"github.com/bitcoin-sv/arc/internal/broadcaster"
"github.com/bitcoin-sv/arc/internal/woc_client"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)

var Cmd = &cobra.Command{
Expand Down Expand Up @@ -78,32 +77,42 @@ var Cmd = &cobra.Command{
return fmt.Errorf("failed to create client: %v", err)
}

names := helper.GetOrderedKeys(keySetsMap)

kameshraj23 marked this conversation as resolved.
Show resolved Hide resolved
wocClient := woc_client.New(!isTestnet, woc_client.WithAuth(wocApiKey), woc_client.WithLogger(logger))
creators := make([]broadcaster.Creator, 0, len(keySetsMap)) // Use the Creator interface for flexibility
for _, keyName := range names {
ks := keySetsMap[keyName]
creator, err := broadcaster.NewUTXOCreator(
logger.With(slog.String("address", ks.Address(!isTestnet)), slog.String("name", keyName)),
client, ks, wocClient, isTestnet, broadcaster.WithFees(miningFeeSat),
)
if err != nil {
return err
}

ks := make([]*keyset.KeySet, len(keySetsMap))
counter := 0
for _, keySet := range keySetsMap {
ks[counter] = keySet
counter++
}
rateBroadcaster, err := broadcaster.NewUTXOCreator(logger, client, ks, wocClient, isTestnet,
broadcaster.WithFees(miningFeeSat),
)
if err != nil {
return fmt.Errorf("failed to create broadcaster: %v", err)
creators = append(creators, creator)
}

err = rateBroadcaster.CreateUtxos(outputs, uint64(satoshisPerOutput))
if err != nil {
return fmt.Errorf("failed to create utxos: %v", err)
}
multiCreator := broadcaster.NewMultiKeyUTXOCreator(logger, creators)
boecklim marked this conversation as resolved.
Show resolved Hide resolved

signalChan := make(chan os.Signal, 1)
signal.Notify(signalChan, os.Interrupt) // Listen for Ctrl+C

// Graceful shutdown on interrupt signal
go func() {
<-signalChan
multiCreator.Shutdown()
}()

logger.Info("Starting UTXO creation")
multiCreator.Start(outputs, uint64(satoshisPerOutput))

return nil
},
}

func init() {

logger := helper.GetLogger()
Cmd.SetHelpFunc(func(command *cobra.Command, strings []string) {
// Hide unused persistent flags
Expand Down
3 changes: 3 additions & 0 deletions internal/broadcaster/broadcaster_mocks.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,6 @@ package broadcaster

// from multi_rate_broadcaster.go
//go:generate moq -pkg mocks -out ./mocks/rate_broadcaster_mock.go . RateBroadcaster

// from multi_utxo_creator.go
//go:generate moq -pkg mocks -out ./mocks/utxo_creator_mock.go . Creator
155 changes: 155 additions & 0 deletions internal/broadcaster/mocks/utxo_creator_mock.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

50 changes: 50 additions & 0 deletions internal/broadcaster/multi_utxo_creator.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package broadcaster

import (
"log/slog"
)

type MultiKeyUTXOCreator struct {
creators []Creator
logger *slog.Logger
}

type Creator interface {
Start(outputs int, satoshisPerOutput uint64) error
Wait()
Shutdown()
}

func NewMultiKeyUTXOCreator(logger *slog.Logger, creators []Creator, opts ...func(p *MultiKeyUTXOCreator)) *MultiKeyUTXOCreator {
mkuc := &MultiKeyUTXOCreator{
creators: creators,
logger: logger,
}

for _, opt := range opts {
opt(mkuc)
}

return mkuc
}

func (mkuc *MultiKeyUTXOCreator) Start(outputs int, satoshisPerOutput uint64) {

for _, creator := range mkuc.creators {
err := creator.Start(outputs, satoshisPerOutput)
if err != nil {
mkuc.logger.Error("failed to start UTXO creator", slog.String("err", err.Error()))
}
}

for _, creator := range mkuc.creators {
creator.Wait()
}
}

func (mkuc *MultiKeyUTXOCreator) Shutdown() {

for _, creator := range mkuc.creators {
creator.Shutdown()
}
}
42 changes: 42 additions & 0 deletions internal/broadcaster/multi_utxo_creator_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package broadcaster_test

import (
"errors"
"log/slog"
"os"
"testing"
"time"

"github.com/bitcoin-sv/arc/internal/broadcaster"
"github.com/bitcoin-sv/arc/internal/broadcaster/mocks"
)

func TestMultiKeyUTXOCreatorStart(t *testing.T) {
t.Run("start and shutdown", func(t *testing.T) {
logger := slog.New(slog.NewTextHandler(os.Stdout, &slog.HandlerOptions{Level: slog.LevelDebug}))

// Create mocks for creators
creators := []broadcaster.Creator{
&mocks.CreatorMock{
StartFunc: func(outputs int, satoshisPerOutput uint64) error { return nil },
WaitFunc: func() {},
ShutdownFunc: func() {},
},
&mocks.CreatorMock{
StartFunc: func(outputs int, satoshisPerOutput uint64) error { return errors.New("failed to start") },
WaitFunc: func() {},
ShutdownFunc: func() {},
},
}

// Initialize the MultiKeyUTXOCreator
mkuc := broadcaster.NewMultiKeyUTXOCreator(logger, creators)

// Start the MultiKeyUTXOCreator
mkuc.Start(100, 1000)
time.Sleep(50 * time.Millisecond)

// Shutdown the MultiKeyUTXOCreator
mkuc.Shutdown()
})
}
Loading
Loading