The core protocol of WoopChain
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
woop/p2p/stream/protocols/sync/message/parse.go

97 lines
2.7 KiB

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
}
// GetEpochStateResponse parse the message to GetEpochStateResponse
func (msg *Message) GetEpochStateResponse() (*GetEpochStateResponse, 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}
}
gesResp := resp.GetGetEpochStateResponse()
if gesResp == nil {
return nil, errors.New("not GetEpochStateResponse")
}
return gesResp, nil
}