-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathclient.go
171 lines (140 loc) · 5.21 KB
/
client.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
package gostratum
import (
"net";
"bufio";
"time";
"encoding/json";
"strconv"
)
type Client struct
{
socket net.Conn
seq uint64
dispatcher *Dispatcher
decoder *Decoder
encoder *Encoder
timeout time.Duration
}
func Connect(host string) (*Client, error){
var client Client
var err error
client.socket, err = net.Dial("tcp", host)
if err!=nil {return nil,err}
client.seq = 0
client.dispatcher = MakeDispatcher()
client.decoder = MakeDecoder()
client.encoder = MakeEncoder()
client.SetTimeout(10)
go client.Listen()
return &client, nil
}
func (c *Client) SetTimeout(timeout int64){
c.timeout = time.Duration(timeout) * time.Second
}
func (c *Client) Listen(){
c.socket.SetReadDeadline(time.Time{})
for{
result, err := bufio.NewReader(c.socket).ReadString('\n')
if err!=nil{
c.dispatcher.Error(err)
continue
}
response := &Response{}
err = c.decoder.Decode(result, response)
if err!=nil{
c.dispatcher.Error(err)
continue
}
c.dispatcher.Process(response)
}
}
func (c *Client) Send(request *Request) *Response{
c.seq++
request.ID = c.seq
msg, err:= c.encoder.Encode(request)
if err!=nil {return &Response{Error:err}}
action,err := c.dispatcher.RegisterRequest(request)
if err != nil {return &Response{Error:err}}
_, err = c.socket.Write([]byte(msg));
if err!=nil{
c.dispatcher.Cancel(request.ID)
return &Response{Error:err}
}
action.SetTimeout(c.timeout, func(){
c.dispatcher.Cancel(request.ID)
})
return action.Wait()
}
func (c *Client) Request(method string, params ...string) *Response{
return c.Send(&Request{Method:method, Params:params});
}
func (c *Client) ServerVersion() (string, error){
return c.decoder.DecodeStringResult(c.Request("server.version"))
}
func (c *Client) ServerBanner() (string, error){
return c.decoder.DecodeStringResult(c.Request("server.version"))
}
func (c *Client) ServerDontationAddress() (string, error){
return c.decoder.DecodeStringResult(c.Request("server.version"))
}
func (c *Client) Subscribe(method string, eventHandler func(*json.RawMessage), msgHandler func(*json.RawMessage), params ... string) error{
c.dispatcher.RegisterNotifiactionHandler(method, eventHandler)
response := c.Send(&Request{Method:method, Params:params})
if response.Error !=nil {return response.Error}
if msgHandler != nil{
msgHandler(response.Result)
}else{
eventHandler(response.Result)
}
return nil
}
func (c *Client) PeersSubscribe(callback func([]Peer, error)) error{
return c.Subscribe("server.peers.subscribe", WrapPeersHandler(callback, c.decoder), nil)
}
func (c *Client) BlockHeaderSubscribe(callback func([]BlockHeader, error)) error{
return c.Subscribe("blockchain.headers.subscribe", WrapBlockHeadersHandler(callback, c.decoder), nil)
}
func (c *Client) NumBlocksSubscribe(callback func(int, error))error{
return c.Subscribe("blockchain.numblocks.subscribe", WrapNumBlocksHandler(callback, c.decoder), nil)
}
func (c *Client) AddressSubscribe(address string, callback func(string, string, error)) error {
return c.Subscribe("blockchain.address.subscribe", WrapAddressHandler(callback, c.decoder), func(result *json.RawMessage){
status, err := c.decoder.DecodeString(result)
callback(address, status, err)
}, address)
}
func (c *Client) AddressGetHistory(address string) ([]AddressTransaction, error){
return c.decoder.DecodeAddressTransactionsResult(c.Request("blockchain.address.get_history", address))
}
func (c *Client) AddressGetMemPool(address string) error{
response := c.Request("blockchain.address.get_mempool", address)
return nil
}
func (c *Client) AddressGetBalance(address string) (Balance, error){
return c.decoder.DecodeBalance(c.Request("blockchain.address.get_balance", address))
}
//func (c *Client) AddressGetProof(address string) (AddressProof,error){
// return c.decoder.DecodeAddressProof(c.Request("blockchain.address.get_proof", address))
//}
func (c *Client) AddressListUnspent(address string) ([]UnspentTransaction, error){
return c.decoder.DecodeUnspent(c.Request("blockchain.address.listunspent", address))
}
//func (c *Client) GetAddress(utxo string) error{
// response := c.Request("blockchain.utxo.get_address", utxo)
// fmt.Println("response: ",response)
// return nil
//}
func (c *Client) GetBlockHeader(height uint64) (BlockHeader, error){
return c.decoder.DecodeBlockHeader(c.Request("blockchain.block.get_header", strconv.FormatUint(height, 10)))
}
func (c *Client) GetBlockChunk(chunk uint64) (string, error){
return c.decoder.DecodeStringResult(c.Request("blockchain.block.get_chunk", strconv.FormatUint(chunk, 10)))
}
//func (c *Client) GetTransactionMerkle(txid string, height uint64) error{
// response := c.Request("blockchain.transaction.get_merkle", txid, strconv.FormatUint(height, 10))
// fmt.Println("response: ",response)
// return nil
//}
func (c *Client) BroadcastTransaction(raw string) (string, error){
return c.decoder.DecodeStringResult(c.Request("blockchain.transaction.broadcast", raw))
}