Skip to content

Commit 59b085a

Browse files
authored
Merge pull request ethereum#1373 from maticnetwork/bor-grpc
Implement gRPC bidirectional communication between bor and heimdall
2 parents 70f3205 + 7a85412 commit 59b085a

File tree

7 files changed

+193
-15
lines changed

7 files changed

+193
-15
lines changed

eth/bor_api_backend.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ func (b *EthAPIBackend) GetRootHash(ctx context.Context, starBlockNr uint64, end
3939
return root, nil
4040
}
4141

42-
// GetRootHash returns root hash for given start and end block
42+
// GetVoteOnHash returns the vote on hash
4343
func (b *EthAPIBackend) GetVoteOnHash(ctx context.Context, starBlockNr uint64, endBlockNr uint64, hash string, milestoneId string) (bool, error) {
4444
var api *bor.API
4545

eth/filters/IBackend.go

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ require (
6565
github.com/kylelemons/godebug v1.1.0
6666
github.com/maticnetwork/crand v1.0.2
6767
github.com/maticnetwork/heimdall v1.0.7
68-
github.com/maticnetwork/polyproto v0.0.3-0.20230216113155-340ea926ca53
68+
github.com/maticnetwork/polyproto v0.0.3
6969
github.com/mattn/go-colorable v0.1.13
7070
github.com/mattn/go-isatty v0.0.20
7171
github.com/mitchellh/cli v1.1.5

go.sum

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1706,8 +1706,9 @@ github.com/maticnetwork/crand v1.0.2/go.mod h1:/NRNL3bj2eYdqpWmoIP5puxndTpi0XRxp
17061706
github.com/maticnetwork/heimdall v1.0.4/go.mod h1:Xh7KFvtbs/SVNjOI8IgYmk6JdzYx89eU/XUwH0AgHLs=
17071707
github.com/maticnetwork/heimdall v1.0.7 h1:QStn+hbZKxfE+PqecaorA/uATDPuQoi+U9Z7IIonb60=
17081708
github.com/maticnetwork/heimdall v1.0.7/go.mod h1:+ANI5+VV28ahwfdl7oMzrcNwaTEs1Fn6z39BqBGcvaA=
1709-
github.com/maticnetwork/polyproto v0.0.3-0.20230216113155-340ea926ca53 h1:PjYV+lghs106JKkrYgOnrsfDLoTc11BxZd4rUa4Rus4=
17101709
github.com/maticnetwork/polyproto v0.0.3-0.20230216113155-340ea926ca53/go.mod h1:e1mU2EXSwEpn5jM7GfNwu3AupsV6WAGoPFFfswXOF0o=
1710+
github.com/maticnetwork/polyproto v0.0.3 h1:a69rIp97fcl3ABY4LlVX9B2t1qhLa0Jhny3HNOzReBU=
1711+
github.com/maticnetwork/polyproto v0.0.3/go.mod h1:e1mU2EXSwEpn5jM7GfNwu3AupsV6WAGoPFFfswXOF0o=
17111712
github.com/maticnetwork/tendermint v0.33.0 h1:f+vORM02BoUOlCvnu3Zjw5rv6l6JSNVchWjH03rUuR8=
17121713
github.com/maticnetwork/tendermint v0.33.0/go.mod h1:D2fcnxGk6bje+LoPwImuKSSYLiK7/G06IynGNDSEcJk=
17131714
github.com/matryer/try v0.0.0-20161228173917-9ac251b645a2/go.mod h1:0KeJpeMD6o+O4hW7qJOT7vyQPKrWmj26uf5wMc/IiIs=

internal/cli/server/api_service.go

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
package server
2+
3+
import (
4+
"context"
5+
"errors"
6+
7+
"github.com/ethereum/go-ethereum/core/types"
8+
"github.com/ethereum/go-ethereum/rpc"
9+
10+
protobor "github.com/maticnetwork/polyproto/bor"
11+
protoutil "github.com/maticnetwork/polyproto/utils"
12+
)
13+
14+
func (s *Server) GetRootHash(ctx context.Context, req *protobor.GetRootHashRequest) (*protobor.GetRootHashResponse, error) {
15+
rootHash, err := s.backend.APIBackend.GetRootHash(ctx, req.StartBlockNumber, req.EndBlockNumber)
16+
if err != nil {
17+
return nil, err
18+
}
19+
20+
return &protobor.GetRootHashResponse{RootHash: rootHash}, nil
21+
}
22+
23+
func (s *Server) GetVoteOnHash(ctx context.Context, req *protobor.GetVoteOnHashRequest) (*protobor.GetVoteOnHashResponse, error) {
24+
vote, err := s.backend.APIBackend.GetVoteOnHash(ctx, req.StartBlockNumber, req.EndBlockNumber, req.Hash, req.MilestoneId)
25+
if err != nil {
26+
return nil, err
27+
}
28+
29+
return &protobor.GetVoteOnHashResponse{Response: vote}, nil
30+
}
31+
32+
func headerToProtoborHeader(h *types.Header) *protobor.Header {
33+
return &protobor.Header{
34+
Number: h.Number.Uint64(),
35+
ParentHash: protoutil.ConvertHashToH256(h.ParentHash),
36+
Time: h.Time,
37+
}
38+
}
39+
40+
func (s *Server) HeaderByNumber(ctx context.Context, req *protobor.GetHeaderByNumberRequest) (*protobor.GetHeaderByNumberResponse, error) {
41+
header, err := s.backend.APIBackend.HeaderByNumber(ctx, rpc.BlockNumber(req.Number))
42+
if err != nil {
43+
return nil, err
44+
}
45+
46+
return &protobor.GetHeaderByNumberResponse{Header: headerToProtoborHeader(header)}, nil
47+
}
48+
49+
func (s *Server) BlockByNumber(ctx context.Context, req *protobor.GetBlockByNumberRequest) (*protobor.GetBlockByNumberResponse, error) {
50+
block, err := s.backend.APIBackend.BlockByNumber(ctx, rpc.BlockNumber(req.Number))
51+
if err != nil {
52+
return nil, err
53+
}
54+
55+
return &protobor.GetBlockByNumberResponse{Block: blockToProtoBlock(block)}, nil
56+
}
57+
58+
func blockToProtoBlock(h *types.Block) *protobor.Block {
59+
return &protobor.Block{
60+
Header: headerToProtoborHeader(h.Header()),
61+
}
62+
}
63+
64+
func (s *Server) TransactionReceipt(ctx context.Context, req *protobor.ReceiptRequest) (*protobor.ReceiptResponse, error) {
65+
_, _, blockHash, _, txnIndex, err := s.backend.APIBackend.GetTransaction(ctx, protoutil.ConvertH256ToHash(req.Hash))
66+
if err != nil {
67+
return nil, err
68+
}
69+
70+
receipts, err := s.backend.APIBackend.GetReceipts(ctx, blockHash)
71+
if err != nil {
72+
return nil, err
73+
}
74+
75+
if receipts == nil {
76+
return nil, errors.New("no receipts found")
77+
}
78+
79+
if len(receipts) <= int(txnIndex) {
80+
return nil, errors.New("transaction index out of bounds")
81+
}
82+
83+
return &protobor.ReceiptResponse{Receipt: ConvertReceiptToProtoReceipt(receipts[txnIndex])}, nil
84+
}
85+
86+
func (s *Server) BorBlockReceipt(ctx context.Context, req *protobor.ReceiptRequest) (*protobor.ReceiptResponse, error) {
87+
receipt, err := s.backend.APIBackend.GetBorBlockReceipt(ctx, protoutil.ConvertH256ToHash(req.Hash))
88+
if err != nil {
89+
return nil, err
90+
}
91+
92+
return &protobor.ReceiptResponse{Receipt: ConvertReceiptToProtoReceipt(receipt)}, nil
93+
}

internal/cli/server/server.go

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,6 @@ import (
1111
"runtime"
1212
"time"
1313

14-
"github.com/mattn/go-colorable"
15-
"github.com/mattn/go-isatty"
16-
"go.opentelemetry.io/otel"
17-
"go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc"
18-
"go.opentelemetry.io/otel/propagation"
19-
"go.opentelemetry.io/otel/sdk/resource"
20-
sdktrace "go.opentelemetry.io/otel/sdk/trace"
21-
semconv "go.opentelemetry.io/otel/semconv/v1.4.0"
22-
"google.golang.org/grpc"
23-
2414
"github.com/ethereum/go-ethereum/accounts"
2515
"github.com/ethereum/go-ethereum/accounts/keystore"
2616
"github.com/ethereum/go-ethereum/cmd/utils"
@@ -40,14 +30,28 @@ import (
4030
"github.com/ethereum/go-ethereum/metrics/prometheus"
4131
"github.com/ethereum/go-ethereum/node"
4232
"github.com/ethereum/go-ethereum/rpc"
33+
"github.com/mattn/go-colorable"
34+
"github.com/mattn/go-isatty"
35+
"go.opentelemetry.io/otel"
36+
"go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc"
37+
"go.opentelemetry.io/otel/propagation"
38+
"go.opentelemetry.io/otel/sdk/resource"
39+
sdktrace "go.opentelemetry.io/otel/sdk/trace"
40+
semconv "go.opentelemetry.io/otel/semconv/v1.4.0"
41+
"google.golang.org/grpc"
42+
"google.golang.org/grpc/reflection"
4343

4444
// Force-load the tracer engines to trigger registration
4545
_ "github.com/ethereum/go-ethereum/eth/tracers/js"
4646
_ "github.com/ethereum/go-ethereum/eth/tracers/native"
47+
48+
protobor "github.com/maticnetwork/polyproto/bor"
4749
)
4850

4951
type Server struct {
5052
proto.UnimplementedBorServer
53+
protobor.UnimplementedBorApiServer
54+
5155
node *node.Node
5256
backend *eth.Ethereum
5357
grpcServer *grpc.Server
@@ -439,6 +443,8 @@ func (s *Server) gRPCServerByAddress(addr string) error {
439443
func (s *Server) gRPCServerByListener(listener net.Listener) error {
440444
s.grpcServer = grpc.NewServer(s.withLoggingUnaryInterceptor())
441445
proto.RegisterBorServer(s.grpcServer, s)
446+
protobor.RegisterBorApiServer(s.grpcServer, s)
447+
reflection.Register(s.grpcServer)
442448

443449
go func() {
444450
if err := s.grpcServer.Serve(listener); err != nil {

internal/cli/server/utils.go

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
package server
2+
3+
import (
4+
"github.com/ethereum/go-ethereum/common"
5+
"github.com/ethereum/go-ethereum/core/types"
6+
"github.com/ethereum/go-ethereum/internal/cli/server/proto"
7+
"github.com/ethereum/go-ethereum/p2p"
8+
9+
protobor "github.com/maticnetwork/polyproto/bor"
10+
protocommon "github.com/maticnetwork/polyproto/common"
11+
protoutil "github.com/maticnetwork/polyproto/utils"
12+
)
13+
14+
func PeerInfoToPeer(info *p2p.PeerInfo) *proto.Peer {
15+
return &proto.Peer{
16+
Id: info.ID,
17+
Enode: info.Enode,
18+
Enr: info.ENR,
19+
Caps: info.Caps,
20+
Name: info.Name,
21+
Trusted: info.Network.Trusted,
22+
Static: info.Network.Static,
23+
}
24+
}
25+
26+
func ConvertBloomToProtoBloom(bloom types.Bloom) *protobor.Bloom {
27+
return &protobor.Bloom{
28+
Bloom: bloom.Bytes(),
29+
}
30+
}
31+
32+
func ConvertLogsToProtoLogs(logs []*types.Log) []*protobor.Log {
33+
var protoLogs []*protobor.Log
34+
for _, log := range logs {
35+
protoLog := &protobor.Log{
36+
Address: protoutil.ConvertAddressToH160(log.Address),
37+
Topics: ConvertTopicsToProtoTopics(log.Topics),
38+
Data: log.Data,
39+
BlockNumber: log.BlockNumber,
40+
TxHash: protoutil.ConvertHashToH256(log.TxHash),
41+
TxIndex: uint64(log.TxIndex),
42+
BlockHash: protoutil.ConvertHashToH256(log.BlockHash),
43+
Index: uint64(log.Index),
44+
Removed: log.Removed,
45+
}
46+
protoLogs = append(protoLogs, protoLog)
47+
}
48+
49+
return protoLogs
50+
}
51+
52+
func ConvertTopicsToProtoTopics(topics []common.Hash) []*protocommon.H256 {
53+
var protoTopics []*protocommon.H256
54+
for _, topic := range topics {
55+
protoTopics = append(protoTopics, protoutil.ConvertHashToH256(topic))
56+
}
57+
58+
return protoTopics
59+
}
60+
61+
func ConvertReceiptToProtoReceipt(receipt *types.Receipt) *protobor.Receipt {
62+
return &protobor.Receipt{
63+
Type: uint64(receipt.Type),
64+
PostState: receipt.PostState,
65+
Status: receipt.Status,
66+
CumulativeGasUsed: receipt.CumulativeGasUsed,
67+
Bloom: ConvertBloomToProtoBloom(receipt.Bloom),
68+
Logs: ConvertLogsToProtoLogs(receipt.Logs),
69+
TxHash: protoutil.ConvertHashToH256(receipt.TxHash),
70+
ContractAddress: protoutil.ConvertAddressToH160(receipt.ContractAddress),
71+
GasUsed: receipt.GasUsed,
72+
EffectiveGasPrice: receipt.EffectiveGasPrice.Int64(),
73+
BlobGasUsed: receipt.BlobGasUsed,
74+
BlockHash: protoutil.ConvertHashToH256(receipt.BlockHash),
75+
BlockNumber: receipt.BlockNumber.Int64(),
76+
TransactionIndex: uint64(receipt.TransactionIndex),
77+
}
78+
}

0 commit comments

Comments
 (0)