Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[stream] added sync stream protobuf message module #3561

Merged
merged 2 commits into from
Mar 3, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
206 changes: 206 additions & 0 deletions p2p/stream/protocols/sync/message/compose.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,206 @@
package message

import (
"github.com/ethereum/go-ethereum/common"
)

// MakeGetBlockNumberRequest makes the GetBlockNumber Request
func MakeGetBlockNumberRequest() *Request {
return &Request{
Request: &Request_GetBlockNumberRequest{
GetBlockNumberRequest: &GetBlockNumberRequest{},
},
}
}

// MakeGetBlockHashesRequest makes GetBlockHashes Request
func MakeGetBlockHashesRequest(bns []uint64) *Request {
return &Request{
Request: &Request_GetBlockHashesRequest{
GetBlockHashesRequest: &GetBlockHashesRequest{
Nums: bns,
},
},
}
}

// MakeGetBlocksByNumRequest makes the GetBlockByNumber request
func MakeGetBlocksByNumRequest(bns []uint64) *Request {
return &Request{
Request: &Request_GetBlocksByNumRequest{
GetBlocksByNumRequest: &GetBlocksByNumRequest{
Nums: bns,
},
},
}
}

//MakeGetBlockByHashesRequest makes the GetBlocksByHashes request
func MakeGetBlocksByHashesRequest(hashes []common.Hash) *Request {
return &Request{
Request: &Request_GetBlocksByHashesRequest{
GetBlocksByHashesRequest: &GetBlocksByHashesRequest{
BlockHashes: hashesToBytes(hashes),
},
},
}
}

// MakeGetEpochStateRequest make GetEpochBlock request
func MakeGetEpochStateRequest(epoch uint64) *Request {
return &Request{
Request: &Request_GetEpochStateRequest{
GetEpochStateRequest: &GetEpochStateRequest{
Epoch: epoch,
},
},
}
}

// MakeErrorResponse makes the error response
func MakeErrorResponseMessage(rid uint64, err error) *Message {
resp := MakeErrorResponse(rid, err)
return makeMessageFromResponse(resp)
}

// MakeErrorResponse makes the error response as a response
func MakeErrorResponse(rid uint64, err error) *Response {
return &Response{
ReqId: rid,
Response: &Response_ErrorResponse{
&ErrorResponse{
Error: err.Error(),
},
},
}
}

// MakeGetBlockNumberResponseMessage makes the GetBlockNumber response message
func MakeGetBlockNumberResponseMessage(rid uint64, bn uint64) *Message {
resp := MakeGetBlockNumberResponse(rid, bn)
return makeMessageFromResponse(resp)
}

// MakeGetBlockNumberResponse makes the GetBlockNumber response
func MakeGetBlockNumberResponse(rid uint64, bn uint64) *Response {
return &Response{
ReqId: rid,
Response: &Response_GetBlockNumberResponse{
GetBlockNumberResponse: &GetBlockNumberResponse{
Number: bn,
},
},
}
}

// MakeGetBlockHashesResponseMessage makes the GetBlockHashes message
func MakeGetBlockHashesResponseMessage(rid uint64, hs []common.Hash) *Message {
resp := MakeGetBlockHashesResponse(rid, hs)
return makeMessageFromResponse(resp)
}

// MakeGetBlockHashesResponse makes the GetBlockHashes response
func MakeGetBlockHashesResponse(rid uint64, hs []common.Hash) *Response {
return &Response{
ReqId: rid,
Response: &Response_GetBlockHashesResponse{
GetBlockHashesResponse: &GetBlockHashesResponse{
Hashes: hashesToBytes(hs),
},
},
}
}

// MakeGetBlocksByNumResponseMessage makes the GetBlocksByNumResponse of Message type
func MakeGetBlocksByNumResponseMessage(rid uint64, blocksBytes [][]byte) *Message {
resp := MakeGetBlocksByNumResponse(rid, blocksBytes)
return makeMessageFromResponse(resp)
}

// MakeGetBlocksByNumResponseMessage make the GetBlocksByNumResponse of Response type
func MakeGetBlocksByNumResponse(rid uint64, blocksBytes [][]byte) *Response {
return &Response{
ReqId: rid,
Response: &Response_GetBlocksByNumResponse{
GetBlocksByNumResponse: &GetBlocksByNumResponse{
BlocksBytes: blocksBytes,
},
},
}
}

// MakeGetBlocksByHashesResponseMessage makes the GetBlocksByHashesResponse of Message type
func MakeGetBlocksByHashesResponseMessage(rid uint64, blocksBytes [][]byte) *Message {
resp := MakeGetBlocksByHashesResponse(rid, blocksBytes)
return makeMessageFromResponse(resp)
}

// MakeGetBlocksByHashesResponse make the GetBlocksByHashesResponse of Response type
func MakeGetBlocksByHashesResponse(rid uint64, blocksBytes [][]byte) *Response {
return &Response{
ReqId: rid,
Response: &Response_GetBlocksByHashesResponse{
GetBlocksByHashesResponse: &GetBlocksByHashesResponse{
BlocksBytes: blocksBytes,
},
},
}
}

// MakeGetEpochStateResponse makes GetEpochStateResponse as message
func MakeGetEpochStateResponseMessage(rid uint64, headerBytes []byte, ssBytes []byte) *Message {
resp := MakeGetEpochStateResponse(rid, headerBytes, ssBytes)
return makeMessageFromResponse(resp)
}

// MakeEpochStateResponse makes GetEpochStateResponse as response
func MakeGetEpochStateResponse(rid uint64, headerBytes []byte, ssBytes []byte) *Response {
return &Response{
ReqId: rid,
Response: &Response_GetEpochStateResponse{
GetEpochStateResponse: &GetEpochStateResponse{
HeaderBytes: headerBytes,
ShardState: ssBytes,
},
},
}
}

// MakeMessageFromRequest makes a message from the request
func MakeMessageFromRequest(req *Request) *Message {
return &Message{
ReqOrResp: &Message_Req{
Req: req,
},
}
}

func makeMessageFromResponse(resp *Response) *Message {
return &Message{
ReqOrResp: &Message_Resp{
Resp: resp,
},
}
}

func hashesToBytes(hashes []common.Hash) [][]byte {
res := make([][]byte, 0, len(hashes))

for _, h := range hashes {
b := make([]byte, common.HashLength)
copy(b, h[:])
res = append(res, b)
}
return res
}

func bytesToHashes(bs [][]byte) []common.Hash {
res := make([]common.Hash, len(bs))

for _, b := range bs {
var h common.Hash
copy(h[:], b)
res = append(res, h)
}
return res
}
3 changes: 3 additions & 0 deletions p2p/stream/protocols/sync/message/generate.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package message

//go:generate protoc msg.proto --go_out=.
Loading