diff --git a/go.mod b/go.mod index 17128decbd..1b2d09cfbf 100644 --- a/go.mod +++ b/go.mod @@ -289,3 +289,5 @@ replace github.com/ethereum/go-ethereum => github.com/maticnetwork/bor v1.2.7 replace github.com/Masterminds/goutils => github.com/Masterminds/goutils v1.1.1 replace go.mongodb.org/mongo-driver => go.mongodb.org/mongo-driver v1.14.0 + +replace github.com/maticnetwork/polyproto => github.com/anshalshukla/polyproto v0.0.0-20240621120311-ddd84287b14e diff --git a/go.sum b/go.sum index de4c422601..d4dbb4efb2 100644 --- a/go.sum +++ b/go.sum @@ -815,6 +815,8 @@ github.com/alecthomas/units v0.0.0-20190924025748-f65c72e2690d/go.mod h1:rBZYJk5 github.com/allegro/bigcache v1.2.1-0.20190218064605-e24eb225f156 h1:eMwmnE/GDgah4HI848JfFxHt+iPb26b4zyfspmqY0/8= github.com/allegro/bigcache v1.2.1-0.20190218064605-e24eb225f156/go.mod h1:Cb/ax3seSYIx7SuZdm2G2xzfwmv3TPSk2ucNfQESPXM= github.com/andybalholm/brotli v1.0.4/go.mod h1:fO7iG3H7G2nSZ7m0zPUDn85XEX2GTukHGRSepvi9Eig= +github.com/anshalshukla/polyproto v0.0.0-20240621120311-ddd84287b14e h1:mpjnlMMyAXoioTDnltukXW6dnH/aPRmoRkB0bpQS+go= +github.com/anshalshukla/polyproto v0.0.0-20240621120311-ddd84287b14e/go.mod h1:e1mU2EXSwEpn5jM7GfNwu3AupsV6WAGoPFFfswXOF0o= github.com/antihax/optional v1.0.0/go.mod h1:uupD/76wgC+ih3iEmQUL+0Ugr19nfwCT1kdvxnR2qWY= github.com/apache/arrow/go/v10 v10.0.1/go.mod h1:YvhnlEePVnBS4+0z3fhPfUy7W1Ikj0Ih0vcRo/gZ1M0= github.com/apache/arrow/go/v11 v11.0.0/go.mod h1:Eg5OsL5H+e299f7u5ssuXsuHQVEGC4xei5aX110hRiI= @@ -1691,8 +1693,6 @@ github.com/maticnetwork/crand v1.0.2/go.mod h1:/NRNL3bj2eYdqpWmoIP5puxndTpi0XRxp github.com/maticnetwork/heimdall v0.3.1-0.20230227104835-81bd1055b0bc/go.mod h1:P2DoKhovYP9G9Kj2EH/zHaiRJF1jNU7ZJOyelG4UCa8= github.com/maticnetwork/heimdall v1.0.4 h1:xSsLn6JE/KqF3Fs9Wlx1s4dclJf2cduIodx2tL6K/cg= github.com/maticnetwork/heimdall v1.0.4/go.mod h1:Xh7KFvtbs/SVNjOI8IgYmk6JdzYx89eU/XUwH0AgHLs= -github.com/maticnetwork/polyproto v0.0.3-0.20230216113155-340ea926ca53 h1:PjYV+lghs106JKkrYgOnrsfDLoTc11BxZd4rUa4Rus4= -github.com/maticnetwork/polyproto v0.0.3-0.20230216113155-340ea926ca53/go.mod h1:e1mU2EXSwEpn5jM7GfNwu3AupsV6WAGoPFFfswXOF0o= github.com/maticnetwork/tendermint v0.33.0 h1:f+vORM02BoUOlCvnu3Zjw5rv6l6JSNVchWjH03rUuR8= github.com/maticnetwork/tendermint v0.33.0/go.mod h1:D2fcnxGk6bje+LoPwImuKSSYLiK7/G06IynGNDSEcJk= github.com/matryer/moq v0.0.0-20190312154309-6cfb0558e1bd/go.mod h1:9ELz6aaclSIGnZBoaSLZ3NAl1VTufbOrXBPvtcy6WiQ= diff --git a/internal/cli/server/api_service.go b/internal/cli/server/api_service.go new file mode 100644 index 0000000000..fc5f7d6d5a --- /dev/null +++ b/internal/cli/server/api_service.go @@ -0,0 +1,88 @@ +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) GetHeaderByNumber(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) GetBlockByNumber(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) GetTransactionReceipt(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 len(receipts) <= int(txnIndex) { + return nil, errors.New("transaction index out of bounds") + } + + return &protobor.ReceiptResponse{Receipt: ConvertReceiptToProtoReceipt(receipts[txnIndex])}, nil +} + +func (s *Server) GetBorBlockReceipt(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 +} diff --git a/internal/cli/server/server.go b/internal/cli/server/server.go index 34015e23a5..b0268df524 100644 --- a/internal/cli/server/server.go +++ b/internal/cli/server/server.go @@ -21,6 +21,8 @@ import ( semconv "go.opentelemetry.io/otel/semconv/v1.4.0" "google.golang.org/grpc" + protobor "github.com/maticnetwork/polyproto/bor" + "github.com/ethereum/go-ethereum/accounts" "github.com/ethereum/go-ethereum/accounts/keystore" "github.com/ethereum/go-ethereum/cmd/utils" @@ -47,6 +49,8 @@ import ( type Server struct { proto.UnimplementedBorServer + protobor.UnimplementedBorApiServer + node *node.Node backend *eth.Ethereum grpcServer *grpc.Server @@ -438,6 +442,7 @@ func (s *Server) gRPCServerByAddress(addr string) error { func (s *Server) gRPCServerByListener(listener net.Listener) error { s.grpcServer = grpc.NewServer(s.withLoggingUnaryInterceptor()) proto.RegisterBorServer(s.grpcServer, s) + protobor.RegisterBorApiServer(s.grpcServer, s) go func() { if err := s.grpcServer.Serve(listener); err != nil { diff --git a/internal/cli/server/utils.go b/internal/cli/server/utils.go new file mode 100644 index 0000000000..4fa8ba0960 --- /dev/null +++ b/internal/cli/server/utils.go @@ -0,0 +1,79 @@ +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, + BlobGasPrice: receipt.BlobGasPrice.Int64(), + BlockHash: protoutil.ConvertHashToH256(receipt.BlockHash), + BlockNumber: receipt.BlockNumber.Int64(), + TransactionIndex: uint64(receipt.TransactionIndex), + } +}