This repository was archived by the owner on Apr 17, 2025. It is now read-only.
generated from mrz1836/go-template
-
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathnode_test.go
More file actions
92 lines (77 loc) · 2.92 KB
/
node_test.go
File metadata and controls
92 lines (77 loc) · 2.92 KB
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
package nownodes
import (
"bytes"
"encoding/json"
"errors"
"io"
"net/http"
"strings"
)
// validNodeResponse will return a valid response for all supported blockchains
type validNodeResponse struct{}
func (v *validNodeResponse) Do(req *http.Request) (*http.Response, error) {
resp := new(http.Response)
resp.StatusCode = http.StatusBadRequest
// No req found
if req == nil {
return resp, errors.New("missing request")
}
decoder := json.NewDecoder(req.Body)
var data nodePayload
err := decoder.Decode(&data)
if err != nil {
return resp, err
}
// Valid response (get tx)
for _, chain := range sendRawTransactionBlockchains {
if strings.Contains(req.Host, chain.NodeAPIURL()) && data.Method == nodeMethodSendRawTx {
resp.StatusCode = http.StatusOK
resp.Body = io.NopCloser(bytes.NewBuffer([]byte(`{"result": "` + testTxHexID(chain) + `","error": null,"id": "` + data.ID + `"}`)))
return resp, nil
}
}
// Valid response (get mempool entry)
for _, chain := range getMempoolEntryBlockchains {
if strings.Contains(req.Host, chain.NodeAPIURL()) && data.Method == nodeMethodGetMempoolEntry {
resp.StatusCode = http.StatusOK
resp.Body = io.NopCloser(bytes.NewBuffer([]byte(`{"result": {"size": 381,"fee": 9.6e-7,"modifiedfee": 9.6e-7,"time": 1643661192,"height": 724704,"depends": []},"error": null,"id": "` + data.ID + `"}`)))
return resp, nil
}
}
resp.Body = io.NopCloser(bytes.NewBuffer([]byte(`{"error":"no-route-found"}`)))
return resp, errors.New("request not found")
}
// errorNodeErrorResponse will return an error for the "send raw tx" response
type errorNodeErrorResponse struct{}
func (v *errorNodeErrorResponse) Do(req *http.Request) (*http.Response, error) {
resp := new(http.Response)
resp.StatusCode = http.StatusBadRequest
// No req found
if req == nil {
return resp, errors.New("missing request")
}
decoder := json.NewDecoder(req.Body)
var data nodePayload
err := decoder.Decode(&data)
if err != nil {
return resp, err
}
// Error response (send tx)
for _, chain := range sendRawTransactionBlockchains {
if strings.Contains(req.Host, chain.NodeAPIURL()) && data.Method == nodeMethodSendRawTx {
resp.StatusCode = http.StatusInternalServerError
resp.Body = io.NopCloser(bytes.NewBuffer([]byte(`{"result": null,"error": {"code": -27,"message": "Transaction already in the mempool"},"id": "` + data.ID + `"}`)))
return resp, nil
}
}
// Error response (get mempool entry)
for _, chain := range getMempoolEntryBlockchains {
if strings.Contains(req.Host, chain.NodeAPIURL()) && data.Method == nodeMethodGetMempoolEntry {
resp.StatusCode = http.StatusInternalServerError
resp.Body = io.NopCloser(bytes.NewBuffer([]byte(`{"result": null,"error": {"code": -5,"message": "Transaction not in mempool"},"id": "` + data.ID + `"}`)))
return resp, nil
}
}
resp.Body = io.NopCloser(bytes.NewBuffer([]byte(`{"error":"no-route-found"}`)))
return resp, errors.New("request not found")
}