-
Notifications
You must be signed in to change notification settings - Fork 1
/
abec-rpc.go
261 lines (221 loc) · 7.26 KB
/
abec-rpc.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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
package core
import (
"bytes"
"encoding/json"
"fmt"
"io"
"net/http"
"time"
)
// Define data types.
type AbecRPCClient struct {
httpClient *http.Client
endpoint string
username string
password string
}
type AbecJSONRPCRequest struct {
JSONRPC string `json:"jsonrpc"`
Method string `json:"method"`
Params []interface{} `json:"params"`
ID string `json:"id"`
}
type AbecJSONRPCResponse struct {
Result json.RawMessage `json:"result"`
Error json.RawMessage `json:"error"`
ID string `json:"id"`
}
type AbecChainInfo struct {
NumBlocks int64 `json:"blocks"`
IsTestnet bool `json:"testnet"`
Version int64 `json:"version"`
ProtocolVersion int64 `json:"protocolversion"`
RelayFee float64 `json:"relayfee"`
}
type AbecMempool map[string]struct {
Size int64 `json:"size"`
FullSize int64 `json:"fullsize"`
Fee float64 `json:"fee"`
Time int64 `json:"time"`
Height int64 `json:"height"`
StartingPriority float64 `json:"startingpriority"`
CurrentPriority float64 `json:"currentpriority"`
}
type AbecBlock struct {
Height int64 `json:"height"`
Confirmations int64 `json:"confirmations"`
Version int64 `json:"version"`
VersionHex string `json:"versionHex"`
Time int64 `json:"time"`
Nonce uint64 `json:"nonce"`
Size int64 `json:"size"`
FullSize int64 `json:"fullsize"`
Difficulty float64 `json:"difficulty"`
BlockHash string `json:"hash"`
PrevBlockHash string `json:"previousblockhash"`
NextBlockHash string `json:"nextblockhash"`
ContentHash string `json:"contenthash"`
MerkleRoot string `json:"merkleroot"`
Bits string `json:"bits"`
SealHash string `json:"sealhash"`
Mixdigest string `json:"mixdigest"`
TxHashes []string `json:"tx"`
RawTxs []*AbecTx `json:"rawTx"`
}
type AbecTx struct {
Hex string `json:"hex"`
TxID string `json:"txid"`
TxHash string `json:"hash"`
Time int64 `json:"time"`
BlockHash string `json:"blockhash"`
BlockTime int64 `json:"blocktime"`
Confirmations int64 `bson:"confirmations"`
Version int64 `json:"version"`
Size int64 `json:"size"`
FullSize int64 `json:"fullsize"`
Fee float64 `json:"fee"`
Witness string `json:"witness"`
Vin []*AbecTxVin `json:"vin"`
Vout []*AbecTxVout `json:"vout"`
}
type AbecTxVin struct {
UTXORing AbecUTXORing `json:"prevutxoring"`
SerialNumber string `json:"serialnumber"`
}
type AbecUTXORing struct {
Version int64 `json:"version"`
BlockHashes []string `json:"blockhashs"`
OutPoints []struct {
TxHash string `json:"txid"`
Index int64 `json:"index"`
} `json:"outpoints"`
}
type AbecTxVout struct {
N int64 `json:"n"`
Script string `json:"script"`
}
// Define methods for AbecRPCClient.
func NewAbecRPCClient(endpoint string, username string, password string) *AbecRPCClient {
return &AbecRPCClient{
httpClient: &http.Client{},
endpoint: endpoint,
username: username,
password: password,
}
}
func (client *AbecRPCClient) newRequest(id string, method string, params []interface{}) (*http.Request, error) {
jsonReq := &AbecJSONRPCRequest{
JSONRPC: "1.0",
Method: method,
Params: params,
ID: id,
}
jsonBody, err := json.Marshal(jsonReq)
if err != nil {
return nil, err
}
httpReq, err := http.NewRequest(http.MethodPost, client.endpoint, bytes.NewBuffer(jsonBody))
if err != nil {
return nil, err
}
httpReq.Header.Set("Content-Type", "application/json")
httpReq.SetBasicAuth(client.username, client.password)
return httpReq, nil
}
func (client *AbecRPCClient) callForBytes(method string, params []interface{}) (Bytes, error) {
id := fmt.Sprintf("%d", time.Now().UnixMilli())
req, err := client.newRequest(id, method, params)
if err != nil {
return nil, err
}
LOG.debug("Request(%s): %s(%+v)\n", id, method, params)
resp, err := client.httpClient.Do(req)
if err != nil {
LOG.debug("Response(%s): ERROR(%s)\n", id, err)
return nil, err
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
if err != nil {
LOG.debug("Response(%s): ERROR(%s)\n", id, err)
return nil, err
}
LOG.debug("Response(%s): %s\n", id, body)
respObj := &AbecJSONRPCResponse{}
err = json.Unmarshal(body, respObj)
if err != nil {
return nil, err
}
errorStr := string(respObj.Error)
if len(errorStr) > 0 && errorStr != "null" {
return nil, fmt.Errorf("abec.%s: %s", method, respObj.Error)
}
return AsBytes(respObj.Result), nil
}
func AbecRPCClientCallForResult[ResultType any](client *AbecRPCClient, result *ResultType, method string, params []interface{}) (Bytes, *ResultType, error) {
resultBytes, err := client.callForBytes(method, params)
if err != nil {
return nil, nil, err
}
if result == nil {
return resultBytes, nil, nil
}
err = resultBytes.JSONUnmarshal(result)
if err != nil {
return resultBytes, nil, err
}
return resultBytes, result, nil
}
func (client *AbecRPCClient) GetChainInfo() (Bytes, *AbecChainInfo, error) {
return AbecRPCClientCallForResult(client, &AbecChainInfo{}, "getinfo", nil)
}
func (client *AbecRPCClient) GetMempool() (Bytes, *AbecMempool, error) {
return AbecRPCClientCallForResult(client, &AbecMempool{}, "getrawmempool", []interface{}{true})
}
func (client *AbecRPCClient) GetBlockHash(height int64) (Bytes, *string, error) {
return AbecRPCClientCallForResult(client, new(string), "getblockhash", []interface{}{height})
}
func (client *AbecRPCClient) GetBlock(hash string) (Bytes, *AbecBlock, error) {
return AbecRPCClientCallForResult(client, &AbecBlock{}, "getblockabe", []interface{}{hash, 1})
}
func (client *AbecRPCClient) GetBlockBytes(hash string) (Bytes, error) {
var data string
_, result, err := AbecRPCClientCallForResult(client, &data, "getblockabe", []interface{}{hash, 0})
if err != nil {
return nil, err
}
blockBytes := MakeBytesFromHexString(*result)
return blockBytes, nil
}
func (client *AbecRPCClient) GetTxBytes(hash string) (Bytes, error) {
var data string
_, result, err := AbecRPCClientCallForResult(client, &data, "getrawtransaction", []interface{}{hash, false})
if err != nil {
return nil, err
}
blockBytes := MakeBytesFromHexString(*result)
return blockBytes, nil
}
func (client *AbecRPCClient) GetRawTx(hash string) (Bytes, *AbecTx, error) {
return AbecRPCClientCallForResult(client, &AbecTx{}, "getrawtransaction", []interface{}{hash, true})
}
func (client *AbecRPCClient) GetBlockByHeight(height int64) (Bytes, *AbecBlock, error) {
_, hash, err := client.GetBlockHash(height)
if err != nil {
return nil, nil, err
}
return client.GetBlock(*hash)
}
func (client *AbecRPCClient) GetBlockBytesByHeight(height int64) (Bytes, error) {
_, hash, err := client.GetBlockHash(height)
if err != nil {
return nil, err
}
return client.GetBlockBytes(*hash)
}
func (client *AbecRPCClient) GetEstimatedTxFee() int64 {
return AbelToNeutrino(0.1)
}
func (client *AbecRPCClient) SendRawTx(txStr string) (Bytes, *string, error) {
return AbecRPCClientCallForResult(client, new(string), "sendrawtransactionabe", []interface{}{txStr})
}