From a4705b73ffcd8beb0662c291217302facc235773 Mon Sep 17 00:00:00 2001 From: vitalibalashka Date: Thu, 14 Sep 2023 08:16:15 +0200 Subject: [PATCH] feat: remove option to use whatsonchain and nownode for broadcasting --- chainstate/broadcast.go | 19 ---- chainstate/broadcast_providers.go | 78 --------------- chainstate/broadcast_test.go | 157 +----------------------------- 3 files changed, 1 insertion(+), 253 deletions(-) diff --git a/chainstate/broadcast.go b/chainstate/broadcast.go index 1005cacf..a9177186 100644 --- a/chainstate/broadcast.go +++ b/chainstate/broadcast.go @@ -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) @@ -124,15 +114,6 @@ 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) diff --git a/chainstate/broadcast_providers.go b/chainstate/broadcast_providers.go index 3cfd677f..56511b24 100644 --- a/chainstate/broadcast_providers.go +++ b/chainstate/broadcast_providers.go @@ -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" ) @@ -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) } diff --git a/chainstate/broadcast_test.go b/chainstate/broadcast_test.go index 0fee581c..96f45b0b 100644 --- a/chainstate/broadcast_test.go +++ b/chainstate/broadcast_test.go @@ -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, @@ -51,97 +49,6 @@ func TestClient_Broadcast_Success(t *testing.T) { 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) - }) } // TestClient_Broadcast_OnChain will test the method Broadcast() @@ -152,36 +59,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, @@ -199,8 +76,6 @@ 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, @@ -208,34 +83,6 @@ func TestClient_Broadcast_InMempool(t *testing.T) { 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() @@ -265,9 +112,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,