-
Notifications
You must be signed in to change notification settings - Fork 0
/
interfaces.go
188 lines (161 loc) · 4.97 KB
/
interfaces.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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
package mock
import (
"encoding/hex"
"errors"
"fmt"
"sync"
gosocketio "github.com/OpenBazaar/golang-socketio"
"github.com/OpenBazaar/multiwallet/client"
"github.com/OpenBazaar/multiwallet/model"
"github.com/btcsuite/btcutil"
)
type MockAPIClient struct {
blockChan chan model.Block
txChan chan model.Transaction
listeningAddrs []btcutil.Address
chainTip int
feePerBlock int
info *model.Info
addrToScript func(btcutil.Address) ([]byte, error)
}
func NewMockApiClient(addrToScript func(btcutil.Address) ([]byte, error)) model.APIClient {
return &MockAPIClient{
blockChan: make(chan model.Block),
txChan: make(chan model.Transaction),
chainTip: 0,
addrToScript: addrToScript,
feePerBlock: 1,
info: &MockInfo,
}
}
func (m *MockAPIClient) Start() error {
return nil
}
func (m *MockAPIClient) GetInfo() (*model.Info, error) {
return m.info, nil
}
func (m *MockAPIClient) GetTransaction(txid string) (*model.Transaction, error) {
for _, tx := range MockTransactions {
if tx.Txid == txid {
return &tx, nil
}
}
return nil, errors.New("Not found")
}
func (m *MockAPIClient) GetRawTransaction(txid string) ([]byte, error) {
if raw, ok := MockRawTransactions[txid]; ok {
return raw, nil
}
return nil, errors.New("Not found")
}
func (m *MockAPIClient) GetTransactions(addrs []btcutil.Address) ([]model.Transaction, error) {
txs := make([]model.Transaction, len(MockTransactions))
copy(txs, MockTransactions)
txs[0].Outputs[1].ScriptPubKey.Addresses = []string{addrs[0].String()}
txs[1].Inputs[0].Addr = addrs[0].String()
txs[1].Outputs[1].ScriptPubKey.Addresses = []string{addrs[1].String()}
txs[2].Outputs[1].ScriptPubKey.Addresses = []string{addrs[2].String()}
return txs, nil
}
func (m *MockAPIClient) GetUtxos(addrs []btcutil.Address) ([]model.Utxo, error) {
utxos := make([]model.Utxo, len(MockUtxos))
copy(utxos, MockUtxos)
utxos[0].Address = addrs[1].String()
script, _ := m.addrToScript(addrs[1])
utxos[0].ScriptPubKey = hex.EncodeToString(script)
utxos[1].Address = addrs[2].String()
script, _ = m.addrToScript(addrs[2])
utxos[1].ScriptPubKey = hex.EncodeToString(script)
return utxos, nil
}
func (m *MockAPIClient) BlockNotify() <-chan model.Block {
return m.blockChan
}
func (m *MockAPIClient) TransactionNotify() <-chan model.Transaction {
return m.txChan
}
func (m *MockAPIClient) ListenAddresses(addrs ...btcutil.Address) {
m.listeningAddrs = append(m.listeningAddrs, addrs...)
}
func (m *MockAPIClient) Broadcast(tx []byte) (string, error) {
return "a8c685478265f4c14dada651969c45a65e1aeb8cd6791f2f5bb6a1d9952104d9", nil
}
func (m *MockAPIClient) GetBestBlock() (*model.Block, error) {
return &MockBlocks[m.chainTip], nil
}
func (m *MockAPIClient) EstimateFee(nBlocks int) (int, error) {
return m.feePerBlock * nBlocks, nil
}
func (m *MockAPIClient) Close() {}
func MockWebsocketClientOnClientPool(p *client.ClientPool) *MockSocketClient {
var (
callbacksMap = make(map[string]func(*gosocketio.Channel, interface{}))
mockSocketClient = &MockSocketClient{
callbacks: callbacksMap,
listeningAddresses: []string{},
}
)
for _, c := range p.Clients() {
c.SocketClient = mockSocketClient
}
return mockSocketClient
}
func NewMockWebsocketClient() *MockSocketClient {
var (
callbacksMap = make(map[string]func(*gosocketio.Channel, interface{}))
mockSocketClient = &MockSocketClient{
callbacks: callbacksMap,
listeningAddresses: []string{},
}
)
return mockSocketClient
}
type MockSocketClient struct {
callbackMutex sync.Mutex
callbacks map[string]func(*gosocketio.Channel, interface{})
listeningAddresses []string
}
func (m *MockSocketClient) SendCallback(method string, args ...interface{}) {
if gosocketChan, ok := args[0].(*gosocketio.Channel); ok {
m.callbacks[method](gosocketChan, args[1])
} else {
m.callbacks[method](nil, args[1])
}
}
func (m *MockSocketClient) IsListeningForAddress(addr string) bool {
for _, a := range m.listeningAddresses {
if a == addr {
return true
}
}
return false
}
func (m *MockSocketClient) On(method string, callback interface{}) error {
c, ok := callback.(func(h *gosocketio.Channel, args interface{}))
if !ok {
return fmt.Errorf("failed casting mock callback: %+v", callback)
}
m.callbackMutex.Lock()
defer m.callbackMutex.Unlock()
if method == "bitcoind/addresstxid" {
m.callbacks[method] = c
} else if method == "bitcoind/hashblock" {
m.callbacks[method] = c
}
return nil
}
func (m *MockSocketClient) Emit(method string, args []interface{}) error {
if method == "subscribe" {
subscribeTo, ok := args[0].(string)
if !ok || subscribeTo != "bitcoind/addresstxid" {
return fmt.Errorf("first emit arg is not bitcoind/addresstxid, was: %+v", args[0])
}
addrs, ok := args[1].([]string)
if !ok {
return fmt.Errorf("second emit arg is not address value, was %+v", args[1])
}
m.listeningAddresses = append(m.listeningAddresses, addrs...)
}
return nil
}
func (m *MockSocketClient) Close() {}