-
Notifications
You must be signed in to change notification settings - Fork 490
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1373 from maticnetwork/bor-grpc
Implement gRPC bidirectional communication between bor and heimdall
- Loading branch information
Showing
7 changed files
with
193 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
package server | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
|
||
"github.com/ethereum/go-ethereum/core/types" | ||
"github.com/ethereum/go-ethereum/rpc" | ||
|
||
protobor "github.com/maticnetwork/polyproto/bor" | ||
protoutil "github.com/maticnetwork/polyproto/utils" | ||
) | ||
|
||
func (s *Server) GetRootHash(ctx context.Context, req *protobor.GetRootHashRequest) (*protobor.GetRootHashResponse, error) { | ||
rootHash, err := s.backend.APIBackend.GetRootHash(ctx, req.StartBlockNumber, req.EndBlockNumber) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return &protobor.GetRootHashResponse{RootHash: rootHash}, nil | ||
} | ||
|
||
func (s *Server) GetVoteOnHash(ctx context.Context, req *protobor.GetVoteOnHashRequest) (*protobor.GetVoteOnHashResponse, error) { | ||
vote, err := s.backend.APIBackend.GetVoteOnHash(ctx, req.StartBlockNumber, req.EndBlockNumber, req.Hash, req.MilestoneId) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return &protobor.GetVoteOnHashResponse{Response: vote}, nil | ||
} | ||
|
||
func headerToProtoborHeader(h *types.Header) *protobor.Header { | ||
return &protobor.Header{ | ||
Number: h.Number.Uint64(), | ||
ParentHash: protoutil.ConvertHashToH256(h.ParentHash), | ||
Time: h.Time, | ||
} | ||
} | ||
|
||
func (s *Server) HeaderByNumber(ctx context.Context, req *protobor.GetHeaderByNumberRequest) (*protobor.GetHeaderByNumberResponse, error) { | ||
header, err := s.backend.APIBackend.HeaderByNumber(ctx, rpc.BlockNumber(req.Number)) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return &protobor.GetHeaderByNumberResponse{Header: headerToProtoborHeader(header)}, nil | ||
} | ||
|
||
func (s *Server) BlockByNumber(ctx context.Context, req *protobor.GetBlockByNumberRequest) (*protobor.GetBlockByNumberResponse, error) { | ||
block, err := s.backend.APIBackend.BlockByNumber(ctx, rpc.BlockNumber(req.Number)) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return &protobor.GetBlockByNumberResponse{Block: blockToProtoBlock(block)}, nil | ||
} | ||
|
||
func blockToProtoBlock(h *types.Block) *protobor.Block { | ||
return &protobor.Block{ | ||
Header: headerToProtoborHeader(h.Header()), | ||
} | ||
} | ||
|
||
func (s *Server) TransactionReceipt(ctx context.Context, req *protobor.ReceiptRequest) (*protobor.ReceiptResponse, error) { | ||
_, _, blockHash, _, txnIndex, err := s.backend.APIBackend.GetTransaction(ctx, protoutil.ConvertH256ToHash(req.Hash)) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
receipts, err := s.backend.APIBackend.GetReceipts(ctx, blockHash) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
if receipts == nil { | ||
return nil, errors.New("no receipts found") | ||
} | ||
|
||
if len(receipts) <= int(txnIndex) { | ||
return nil, errors.New("transaction index out of bounds") | ||
} | ||
|
||
return &protobor.ReceiptResponse{Receipt: ConvertReceiptToProtoReceipt(receipts[txnIndex])}, nil | ||
} | ||
|
||
func (s *Server) BorBlockReceipt(ctx context.Context, req *protobor.ReceiptRequest) (*protobor.ReceiptResponse, error) { | ||
receipt, err := s.backend.APIBackend.GetBorBlockReceipt(ctx, protoutil.ConvertH256ToHash(req.Hash)) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return &protobor.ReceiptResponse{Receipt: ConvertReceiptToProtoReceipt(receipt)}, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
package server | ||
|
||
import ( | ||
"github.com/ethereum/go-ethereum/common" | ||
"github.com/ethereum/go-ethereum/core/types" | ||
"github.com/ethereum/go-ethereum/internal/cli/server/proto" | ||
"github.com/ethereum/go-ethereum/p2p" | ||
|
||
protobor "github.com/maticnetwork/polyproto/bor" | ||
protocommon "github.com/maticnetwork/polyproto/common" | ||
protoutil "github.com/maticnetwork/polyproto/utils" | ||
) | ||
|
||
func PeerInfoToPeer(info *p2p.PeerInfo) *proto.Peer { | ||
return &proto.Peer{ | ||
Id: info.ID, | ||
Enode: info.Enode, | ||
Enr: info.ENR, | ||
Caps: info.Caps, | ||
Name: info.Name, | ||
Trusted: info.Network.Trusted, | ||
Static: info.Network.Static, | ||
} | ||
} | ||
|
||
func ConvertBloomToProtoBloom(bloom types.Bloom) *protobor.Bloom { | ||
return &protobor.Bloom{ | ||
Bloom: bloom.Bytes(), | ||
} | ||
} | ||
|
||
func ConvertLogsToProtoLogs(logs []*types.Log) []*protobor.Log { | ||
var protoLogs []*protobor.Log | ||
for _, log := range logs { | ||
protoLog := &protobor.Log{ | ||
Address: protoutil.ConvertAddressToH160(log.Address), | ||
Topics: ConvertTopicsToProtoTopics(log.Topics), | ||
Data: log.Data, | ||
BlockNumber: log.BlockNumber, | ||
TxHash: protoutil.ConvertHashToH256(log.TxHash), | ||
TxIndex: uint64(log.TxIndex), | ||
BlockHash: protoutil.ConvertHashToH256(log.BlockHash), | ||
Index: uint64(log.Index), | ||
Removed: log.Removed, | ||
} | ||
protoLogs = append(protoLogs, protoLog) | ||
} | ||
|
||
return protoLogs | ||
} | ||
|
||
func ConvertTopicsToProtoTopics(topics []common.Hash) []*protocommon.H256 { | ||
var protoTopics []*protocommon.H256 | ||
for _, topic := range topics { | ||
protoTopics = append(protoTopics, protoutil.ConvertHashToH256(topic)) | ||
} | ||
|
||
return protoTopics | ||
} | ||
|
||
func ConvertReceiptToProtoReceipt(receipt *types.Receipt) *protobor.Receipt { | ||
return &protobor.Receipt{ | ||
Type: uint64(receipt.Type), | ||
PostState: receipt.PostState, | ||
Status: receipt.Status, | ||
CumulativeGasUsed: receipt.CumulativeGasUsed, | ||
Bloom: ConvertBloomToProtoBloom(receipt.Bloom), | ||
Logs: ConvertLogsToProtoLogs(receipt.Logs), | ||
TxHash: protoutil.ConvertHashToH256(receipt.TxHash), | ||
ContractAddress: protoutil.ConvertAddressToH160(receipt.ContractAddress), | ||
GasUsed: receipt.GasUsed, | ||
EffectiveGasPrice: receipt.EffectiveGasPrice.Int64(), | ||
BlobGasUsed: receipt.BlobGasUsed, | ||
BlockHash: protoutil.ConvertHashToH256(receipt.BlockHash), | ||
BlockNumber: receipt.BlockNumber.Int64(), | ||
TransactionIndex: uint64(receipt.TransactionIndex), | ||
} | ||
} |