This repository has been archived by the owner on Apr 2, 2024. It is now read-only.
generated from mrz1836/go-template
-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
broadcast_providers.go
113 lines (90 loc) · 3.22 KB
/
broadcast_providers.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
package chainstate
import (
"context"
"errors"
"fmt"
"strings"
"github.com/bitcoin-sv/go-broadcast-client/broadcast"
"github.com/tonicpow/go-minercraft/v2"
)
// generic broadcast provider
type txBroadcastProvider interface {
getName() string
broadcast(ctx context.Context, c *Client) error
}
// mAPI provider
type mapiBroadcastProvider struct {
miner *Miner
txID, txHex string
}
func (provider mapiBroadcastProvider) getName() string {
return provider.miner.Miner.Name
}
func (provider mapiBroadcastProvider) broadcast(ctx context.Context, c *Client) error {
return broadcastMAPI(ctx, c, provider.miner.Miner, provider.txID, provider.txHex)
}
// broadcastMAPI will broadcast a transaction to a miner using mAPI
func broadcastMAPI(ctx context.Context, client ClientInterface, miner *minercraft.Miner, id, hex string) error {
debugLog(client, id, "executing broadcast request in mapi using miner: "+miner.Name)
resp, err := client.Minercraft().SubmitTransaction(ctx, miner, &minercraft.Transaction{
CallBackEncryption: "", // todo: allow customizing the payload
CallBackToken: "",
CallBackURL: "",
DsCheck: false,
MerkleFormat: "",
MerkleProof: false,
RawTx: hex,
})
if err != nil {
debugLog(client, id, "error executing request in mapi using miner: "+miner.Name+" failed: "+err.Error())
return err
}
// Something went wrong - got back an id that does not match
if resp == nil {
return emptyBroadcastResponseErr(id)
}
if !strings.EqualFold(resp.Results.TxID, id) {
return incorrectTxIDReturnedErr(resp.Results.TxID, id)
}
// mAPI success of broadcast
if resp.Results.ReturnResult == mAPISuccess {
return nil
}
// Check error message (for success error message)
if doesErrorContain(resp.Results.ResultDescription, broadcastSuccessErrors) {
return nil
}
// We got a potential real error message?
return errors.New(resp.Results.ResultDescription)
}
func incorrectTxIDReturnedErr(actualTxID, expectedTxID string) error {
return fmt.Errorf("returned tx id [%s] does not match given tx id [%s]", actualTxID, expectedTxID)
}
func emptyBroadcastResponseErr(txID string) error {
return fmt.Errorf("an empty response was returned after broadcasting of tx id [%s]", txID)
}
////
// BroadcastClient provider
type broadcastClientProvider struct {
txID, txHex string
}
func (provider broadcastClientProvider) getName() string {
return ProviderBroadcastClient
}
// Broadcast using BroadcastClient
func (provider broadcastClientProvider) broadcast(ctx context.Context, c *Client) error {
return broadcastWithBroadcastClient(ctx, c, provider.txID, provider.txHex)
}
func broadcastWithBroadcastClient(ctx context.Context, client ClientInterface, txID, hex string) error {
debugLog(client, txID, "executing broadcast request for "+ProviderBroadcastClient)
tx := broadcast.Transaction{
RawTx: hex,
}
result, err := client.BroadcastClient().SubmitTransaction(ctx, &tx)
if err != nil {
debugLog(client, txID, "error broadcast request for "+ProviderBroadcastClient+" failed: "+err.Error())
return err
}
debugLog(client, txID, "result broadcast request for "+ProviderBroadcastClient+" blockhash: "+result.BlockHash+" status: "+result.TxStatus.String())
return nil
}