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

Commit

Permalink
Merge branch 'master' into feat-beef-prepare
Browse files Browse the repository at this point in the history
  • Loading branch information
arkadiuszos4chain authored Sep 18, 2023
2 parents ec60db0 + 987f698 commit 978ef79
Show file tree
Hide file tree
Showing 18 changed files with 15 additions and 653 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ jobs:
with:
redis-version: 6
- name: Run GoReleaser
uses: goreleaser/goreleaser-action@v4.6.0
uses: goreleaser/goreleaser-action@v5.0.0
with:
distribution: goreleaser
version: latest
Expand Down
21 changes: 1 addition & 20 deletions chainstate/broadcast.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,16 +101,6 @@ func createActiveProviders(c *Client, txID, txHex string) []txBroadcastProvider
}
}

if shouldBroadcastToWhatsOnChain(c) {
pvdr := whatsOnChainBroadcastProvider{txID: txID, txHex: txHex}
providers = append(providers, &pvdr)
}

if shouldBroadcastToNowNodes(c) {
pvdr := nowNodesBroadcastProvider{uniqueID: txID, txID: txID, txHex: txHex}
providers = append(providers, &pvdr)
}

if shouldBroadcastWithBroadcastClient(c) {
pvdr := broadcastClientProvider{txID: txID, txHex: txHex}
providers = append(providers, &pvdr)
Expand All @@ -124,18 +114,9 @@ func shouldBroadcastWithMAPI(c *Client) bool {
(c.Network() == MainNet || c.Network() == TestNet) // Only supported on main and test right now
}

func shouldBroadcastToWhatsOnChain(c *Client) bool {
return !utils.StringInSlice(ProviderWhatsOnChain, c.options.config.excludedProviders)
}

func shouldBroadcastToNowNodes(c *Client) bool {
return !utils.StringInSlice(ProviderNowNodes, c.options.config.excludedProviders) &&
c.NowNodes() != nil // Only if NowNodes is loaded (requires API key)
}

func shouldBroadcastWithBroadcastClient(c *Client) bool {
return !utils.StringInSlice(ProviderBroadcastClient, c.options.config.excludedProviders) &&
c.BroadcastClient() != nil // Only if NowNodes is loaded (requires API key)
c.BroadcastClient() != nil
}

func broadcastToProvider(ctx, fallbackCtx context.Context, provider txBroadcastProvider, txID string,
Expand Down
78 changes: 0 additions & 78 deletions chainstate/broadcast_providers.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"strings"

"github.com/bitcoin-sv/go-broadcast-client/broadcast"
"github.com/mrz1836/go-nownodes"
"github.com/tonicpow/go-minercraft/v2"
)

Expand Down Expand Up @@ -70,83 +69,6 @@ func broadcastMAPI(ctx context.Context, client ClientInterface, miner *minercraf

////

// WhatsOnChain provider
type whatsOnChainBroadcastProvider struct {
txID, txHex string
}

func (provider whatsOnChainBroadcastProvider) getName() string {
return ProviderWhatsOnChain
}

func (provider whatsOnChainBroadcastProvider) broadcast(ctx context.Context, c *Client) error {
return broadcastWhatsOnChain(ctx, c, provider.txID, provider.txHex)
}

// broadcastWhatsOnChain will broadcast a transaction to WhatsOnChain
func broadcastWhatsOnChain(ctx context.Context, client ClientInterface, id, hex string) error {
debugLog(client, id, "executing broadcast request for "+ProviderWhatsOnChain)

txID, err := client.WhatsOnChain().BroadcastTx(ctx, hex)
if err != nil {

// Check error message (for success error message)
if doesErrorContain(err.Error(), broadcastSuccessErrors) {
return nil
}
return err
}

// Something went wrong - got back an id that does not match
if !strings.EqualFold(txID, id) {
return incorrectTxIDReturnedErr(txID, id)
}

// Success
return nil
}

////

// NowNodes provider
type nowNodesBroadcastProvider struct {
uniqueID, txID, txHex string
}

func (provider nowNodesBroadcastProvider) getName() string {
return ProviderNowNodes
}

// Broadcast using NowNodes
func (provider nowNodesBroadcastProvider) broadcast(ctx context.Context, c *Client) error {
return broadcastNowNodes(ctx, c, provider.uniqueID, provider.txID, provider.txHex)
}

// broadcastNowNodes will broadcast a transaction to NowNodes
func broadcastNowNodes(ctx context.Context, client ClientInterface, uniqueID, txID, hex string) error {
debugLog(client, txID, "executing broadcast request for "+ProviderNowNodes)

result, err := client.NowNodes().SendRawTransaction(ctx, nownodes.BSV, hex, uniqueID)
if err != nil {

// Check error message (for success error message)
if doesErrorContain(err.Error(), broadcastSuccessErrors) {
return nil
}
return err
}

// Something went wrong - got back an id that does not match
if !strings.EqualFold(result.Result, txID) {
return incorrectTxIDReturnedErr(result.Result, txID)
}

// Success
return nil
}

////

func incorrectTxIDReturnedErr(actualTxID, expectedTxID string) error {
return fmt.Errorf("returned tx id [%s] does not match given tx id [%s]", actualTxID, expectedTxID)
}
Expand Down
158 changes: 1 addition & 157 deletions chainstate/broadcast_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,6 @@ func TestClient_Broadcast_Success(t *testing.T) {
c := NewTestClient(
context.Background(), t,
WithMinercraft(&minerCraftBroadcastSuccess{}),
WithNowNodes(&nowNodesTxNotFound{}), // Not found
WithWhatsOnChain(&whatsOnChainTxNotFound{}), // Not Found
)
providers, err := c.Broadcast(
context.Background(), broadcastExample1TxID, broadcastExample1TxHex, defaultBroadcastTimeOut,
Expand All @@ -49,98 +47,6 @@ func TestClient_Broadcast_Success(t *testing.T) {
assert.True(t, containsAtLeastOneElement(miners, minercraft.MinerTaal, minercraft.MinerMempool, minercraft.MinerGorillaPool, minercraft.MinerMatterpool))

assert.NotContains(t, miners, ProviderWhatsOnChain)
assert.NotContains(t, miners, ProviderNowNodes)
})

t.Run("broadcast - success (WhatsOnChain)", func(t *testing.T) {
c := NewTestClient(
context.Background(), t,
WithWhatsOnChain(&whatsOnChainBroadcastSuccess{}),
WithNowNodes(&nowNodesTxNotFound{}), // Not Found
WithMinercraft(&minerCraftTxNotFound{}), // Not found
)
provider, err := c.Broadcast(
context.Background(), broadcastExample1TxID, broadcastExample1TxHex, defaultBroadcastTimeOut,
)
require.NoError(t, err)
assert.Equal(t, ProviderWhatsOnChain, provider)
})

t.Run("broadcast - success (NowNodes)", func(t *testing.T) {
c := NewTestClient(
context.Background(), t,
WithNowNodes(&nowNodesBroadcastSuccess{}),
WithWhatsOnChain(&whatsOnChainTxNotFound{}), // Not Found
WithMinercraft(&minerCraftTxNotFound{}), // Not found
)
provider, err := c.Broadcast(
context.Background(), broadcastExample1TxID, broadcastExample1TxHex, defaultBroadcastTimeOut,
)
require.NoError(t, err)
assert.Equal(t, ProviderNowNodes, provider)
})

t.Run("broadcast - success (NowNodes timeouts)", func(t *testing.T) {
c := NewTestClient(
context.Background(), t,
WithNowNodes(&nowNodesBroadcastTimeout{}), // Timeout
WithWhatsOnChain(&whatsOnChainBroadcastSuccess{}), // Success
WithMinercraft(&minerCraftBroadcastSuccess{}), // Success
)
providers, err := c.Broadcast(
context.Background(), broadcastExample1TxID, broadcastExample1TxHex, defaultBroadcastTimeOut,
)
require.NoError(t, err)
miners := strings.Split(providers, ",")

assert.GreaterOrEqual(t, len(miners), 1)

assert.True(t, containsAtLeastOneElement(miners, minercraft.MinerTaal, minercraft.MinerMempool, minercraft.MinerGorillaPool, minercraft.MinerMatterpool, ProviderWhatsOnChain))

assert.NotContains(t, miners, ProviderNowNodes)
})

t.Run("broadcast - success (WhatsOnChain timeouts)", func(t *testing.T) {
c := NewTestClient(
context.Background(), t,
WithWhatsOnChain(&whatsOnChainBroadcastTimeout{}), // Timeout
WithNowNodes(&nowNodesBroadcastSuccess{}), // Success
WithMinercraft(&minerCraftBroadcastSuccess{}), // Success
)
providers, err := c.Broadcast(
context.Background(), broadcastExample1TxID, broadcastExample1TxHex, defaultBroadcastTimeOut,
)
require.NoError(t, err)
miners := strings.Split(providers, ",")

assert.GreaterOrEqual(t, len(miners), 1)

assert.True(t, containsAtLeastOneElement(miners, minercraft.MinerTaal, minercraft.MinerMempool, minercraft.MinerGorillaPool, minercraft.MinerMatterpool, ProviderNowNodes))

assert.NotContains(t, miners, ProviderWhatsOnChain)
})

t.Run("broadcast - success (mAPI timeouts)", func(t *testing.T) {
c := NewTestClient(
context.Background(), t,
WithMinercraft(&minerCraftBroadcastTimeout{}), // Timeout
WithWhatsOnChain(&whatsOnChainBroadcastSuccess{}), // Success
WithNowNodes(&nowNodesBroadcastSuccess{}), // Success
)
providers, err := c.Broadcast(
context.Background(), broadcastExample1TxID, broadcastExample1TxHex, defaultBroadcastTimeOut,
)
require.NoError(t, err)
miners := strings.Split(providers, ",")

assert.GreaterOrEqual(t, len(miners), 1)

assert.True(t, containsAtLeastOneElement(miners, ProviderWhatsOnChain, ProviderNowNodes))

assert.NotContains(t, miners, minercraft.MinerTaal)
assert.NotContains(t, miners, minercraft.MinerMempool)
assert.NotContains(t, miners, minercraft.MinerGorillaPool)
assert.NotContains(t, miners, minercraft.MinerMatterpool)
})
}

Expand All @@ -152,36 +58,6 @@ func TestClient_Broadcast_OnChain(t *testing.T) {
c := NewTestClient(
context.Background(), t,
WithMinercraft(&minerCraftTxOnChain{}),
WithWhatsOnChain(&whatsOnChainTxNotFound{}), // Not Found
WithNowNodes(&nowNodesTxNotFound{}), // Not Found
)
provider, err := c.Broadcast(
context.Background(), onChainExample1TxID, onChainExample1TxHex, defaultBroadcastTimeOut,
)
require.NoError(t, err)
assert.NotEmpty(t, provider)
})

t.Run("broadcast - tx already on-chain (WhatsOnChain)", func(t *testing.T) {
c := NewTestClient(
context.Background(), t,
WithWhatsOnChain(&whatsOnChainTxOnChain{}),
WithMinercraft(&minerCraftTxNotFound{}), // Not found
WithNowNodes(&nowNodesTxNotFound{}), // Not Found
)
provider, err := c.Broadcast(
context.Background(), onChainExample1TxID, onChainExample1TxHex, defaultBroadcastTimeOut,
)
require.NoError(t, err)
assert.NotEmpty(t, provider)
})

t.Run("broadcast - tx already on-chain (NowNodes)", func(t *testing.T) {
c := NewTestClient(
context.Background(), t,
WithNowNodes(&nowNodesTxOnChain{}),
WithWhatsOnChain(&whatsOnChainTxNotFound{}), // Not Found
WithMinercraft(&minerCraftTxNotFound{}), // Not found
)
provider, err := c.Broadcast(
context.Background(), onChainExample1TxID, onChainExample1TxHex, defaultBroadcastTimeOut,
Expand All @@ -199,43 +75,13 @@ func TestClient_Broadcast_InMempool(t *testing.T) {
c := NewTestClient(
context.Background(), t,
WithMinercraft(&minerCraftInMempool{}),
WithNowNodes(&nowNodesTxNotFound{}), // Not Found
WithWhatsOnChain(&whatsOnChainTxNotFound{}), // Not Found
)
provider, err := c.Broadcast(
context.Background(), onChainExample1TxID, onChainExample1TxHex, defaultBroadcastTimeOut,
)
require.NoError(t, err)
assert.NotEmpty(t, provider)
})

t.Run("broadcast - in mempool (WhatsOnChain)", func(t *testing.T) {
c := NewTestClient(
context.Background(), t,
WithWhatsOnChain(&whatsOnChainInMempool{}),
WithNowNodes(&nowNodesTxNotFound{}), // Not Found
WithMinercraft(&minerCraftTxNotFound{}), // Not found
)
provider, err := c.Broadcast(
context.Background(), broadcastExample1TxID, broadcastExample1TxHex, defaultBroadcastTimeOut,
)
require.NoError(t, err)
assert.NotEmpty(t, provider)
})

t.Run("broadcast - in mempool (NowNodes)", func(t *testing.T) {
c := NewTestClient(
context.Background(), t,
WithNowNodes(&nowNodeInMempool{}),
WithWhatsOnChain(&whatsOnChainTxNotFound{}), // Not found
WithMinercraft(&minerCraftTxNotFound{}), // Not Found
)
provider, err := c.Broadcast(
context.Background(), broadcastExample1TxID, broadcastExample1TxHex, defaultBroadcastTimeOut,
)
require.NoError(t, err)
assert.NotEmpty(t, provider)
})
}

// TestClient_Broadcast will test the method Broadcast()
Expand Down Expand Up @@ -265,9 +111,7 @@ func TestClient_Broadcast(t *testing.T) {
t.Run("broadcast - all providers fail", func(t *testing.T) {
c := NewTestClient(
context.Background(), t,
WithNowNodes(&nowNodesTxNotFound{}), // Not found
WithWhatsOnChain(&whatsOnChainTxNotFound{}), // Not found
WithMinercraft(&minerCraftTxNotFound{}), // Not Found
WithMinercraft(&minerCraftTxNotFound{}), // Not Found
)
provider, err := c.Broadcast(
context.Background(), broadcastExample1TxID, broadcastExample1TxHex, defaultBroadcastTimeOut,
Expand Down
2 changes: 1 addition & 1 deletion chainstate/chainstate.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ func (c *Client) Broadcast(ctx context.Context, id, txHex string, timeout time.D

// QueryTransaction will get the transaction info from all providers returning the "first" valid result
//
// Note: this is slow, but follows a specific order: mAPI -> WhatsOnChain -> NowNodes
// Note: this is slow, but follows a specific order: mAPI -> WhatsOnChain
func (c *Client) QueryTransaction(
ctx context.Context, id string, requiredIn RequiredIn, timeout time.Duration,
) (*TransactionInfo, error) {
Expand Down
16 changes: 0 additions & 16 deletions chainstate/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (
broadcastClient "github.com/bitcoin-sv/go-broadcast-client/broadcast/broadcast-client"
"github.com/libsv/go-bt/v2"
zLogger "github.com/mrz1836/go-logger"
"github.com/mrz1836/go-nownodes"
"github.com/mrz1836/go-whatsonchain"
"github.com/newrelic/go-agent/v3/newrelic"
"github.com/tonicpow/go-minercraft/v2"
Expand Down Expand Up @@ -42,8 +41,6 @@ type (
minercraftConfig *minercraftConfig // minercraftConfig configuration
minercraft minercraft.ClientInterface // Minercraft client
network Network // Current network (mainnet, testnet, stn)
nowNodes nownodes.ClientInterface // NOWNodes client
nowNodesAPIKey string // If set, use this key
queryTimeout time.Duration // Timeout for transaction query
whatsOnChain whatsonchain.ClientInterface // WhatsOnChain client
whatsOnChainAPIKey string // If set, use this key
Expand Down Expand Up @@ -104,9 +101,6 @@ func NewClient(ctx context.Context, opts ...ClientOps) (ClientInterface, error)
// Start WhatsOnChain
client.startWhatsOnChain(ctx)

// Start NowNodes
client.startNowNodes(ctx)

// Return the client
return client, nil
}
Expand All @@ -128,11 +122,6 @@ func (c *Client) Close(ctx context.Context) {
c.options.config.whatsOnChain = nil
}

// Close NowNodes
if c.options.config.nowNodes != nil {
c.options.config.nowNodes = nil
}

// Stop the active Monitor (if not already stopped)
if c.options.monitor != nil {
_ = c.options.monitor.Stop(ctx)
Expand Down Expand Up @@ -188,11 +177,6 @@ func (c *Client) WhatsOnChain() whatsonchain.ClientInterface {
return c.options.config.whatsOnChain
}

// NowNodes will return the NowNodes client
func (c *Client) NowNodes() nownodes.ClientInterface {
return c.options.config.nowNodes
}

// BroadcastClient will return the BroadcastClient client
func (c *Client) BroadcastClient() broadcast.Client {
return c.options.config.broadcastClient
Expand Down
Loading

0 comments on commit 978ef79

Please sign in to comment.