-
Notifications
You must be signed in to change notification settings - Fork 2
/
parse.go
81 lines (73 loc) · 2.24 KB
/
parse.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
package message
import (
"fmt"
"github.com/pkg/errors"
)
// ResponseError is the error from an error response
type ResponseError struct {
msg string
}
// Error is the error string of ResponseError
func (err *ResponseError) Error() string {
return fmt.Sprintf("[RESPONSE] %v", err.msg)
}
// GetBlockNumberResponse parse the message to GetBlockNumberResponse
func (msg *Message) GetBlockNumberResponse() (*GetBlockNumberResponse, error) {
resp := msg.GetResp()
if resp == nil {
return nil, errors.New("not response message")
}
if errResp := resp.GetErrorResponse(); errResp != nil {
return nil, &ResponseError{errResp.Error}
}
bnResp := resp.GetGetBlockNumberResponse()
if bnResp == nil {
return nil, errors.New("not GetBlockNumber response")
}
return bnResp, nil
}
// GetBlockHashesResponse parse the message to GetBlockHashesResponse
func (msg *Message) GetBlockHashesResponse() (*GetBlockHashesResponse, error) {
resp := msg.GetResp()
if resp == nil {
return nil, errors.New("not response message")
}
if errResp := resp.GetErrorResponse(); errResp != nil {
return nil, &ResponseError{errResp.Error}
}
ghResp := resp.GetGetBlockHashesResponse()
if ghResp == nil {
return nil, errors.New("not GetBlockHashesResponse")
}
return ghResp, nil
}
// GetBlocksByNumberResponse parse the message to GetBlocksByNumberResponse
func (msg *Message) GetBlocksByNumberResponse() (*GetBlocksByNumResponse, error) {
resp := msg.GetResp()
if resp == nil {
return nil, errors.New("not response message")
}
if errResp := resp.GetErrorResponse(); errResp != nil {
return nil, &ResponseError{errResp.Error}
}
gbResp := resp.GetGetBlocksByNumResponse()
if gbResp == nil {
return nil, errors.New("not GetBlocksByNumResponse")
}
return gbResp, nil
}
// GetBlocksByHashesResponse parse the message to GetBlocksByHashesResponse
func (msg *Message) GetBlocksByHashesResponse() (*GetBlocksByHashesResponse, error) {
resp := msg.GetResp()
if resp == nil {
return nil, errors.New("not response message")
}
if errResp := resp.GetErrorResponse(); errResp != nil {
return nil, &ResponseError{errResp.Error}
}
gbResp := resp.GetGetBlocksByHashesResponse()
if gbResp == nil {
return nil, errors.New("not GetBlocksByHashesResponse")
}
return gbResp, nil
}