2020-08-12 13:47:59 +00:00
|
|
|
package remotedbserver
|
|
|
|
|
|
|
|
import (
|
2021-04-22 17:11:37 +00:00
|
|
|
"bytes"
|
2020-08-12 13:47:59 +00:00
|
|
|
"context"
|
|
|
|
|
2021-05-20 18:25:53 +00:00
|
|
|
"github.com/ledgerwatch/erigon/common"
|
|
|
|
"github.com/ledgerwatch/erigon/core/types"
|
|
|
|
"github.com/ledgerwatch/erigon/gointerfaces"
|
|
|
|
"github.com/ledgerwatch/erigon/gointerfaces/remote"
|
|
|
|
types2 "github.com/ledgerwatch/erigon/gointerfaces/types"
|
|
|
|
"github.com/ledgerwatch/erigon/log"
|
|
|
|
"github.com/ledgerwatch/erigon/params"
|
|
|
|
"github.com/ledgerwatch/erigon/rlp"
|
2021-05-17 12:15:19 +00:00
|
|
|
"google.golang.org/protobuf/types/known/emptypb"
|
2020-08-12 13:47:59 +00:00
|
|
|
)
|
|
|
|
|
2021-05-22 10:00:13 +00:00
|
|
|
// EthBackendAPIVersion
|
|
|
|
// 2.0.0 - move all mining-related methods to 'txpool/mining' server
|
2021-06-17 21:55:20 +00:00
|
|
|
// 2.1.0 - add NetPeerCount function
|
|
|
|
var EthBackendAPIVersion = &types2.VersionReply{Major: 2, Minor: 1, Patch: 0}
|
2021-05-22 10:00:13 +00:00
|
|
|
|
2020-08-12 13:47:59 +00:00
|
|
|
type EthBackendServer struct {
|
2020-10-24 06:54:03 +00:00
|
|
|
remote.UnimplementedETHBACKENDServer // must be embedded to have forward compatible implementations.
|
2020-08-12 13:47:59 +00:00
|
|
|
|
2021-06-11 08:34:37 +00:00
|
|
|
eth EthBackend
|
|
|
|
events *Events
|
2020-08-12 13:47:59 +00:00
|
|
|
}
|
|
|
|
|
2021-05-22 10:00:13 +00:00
|
|
|
type EthBackend interface {
|
|
|
|
Etherbase() (common.Address, error)
|
|
|
|
NetVersion() (uint64, error)
|
2021-06-17 21:55:20 +00:00
|
|
|
NetPeerCount() (uint64, error)
|
2021-05-22 10:00:13 +00:00
|
|
|
}
|
|
|
|
|
2021-06-11 08:34:37 +00:00
|
|
|
func NewEthBackendServer(eth EthBackend, events *Events) *EthBackendServer {
|
|
|
|
return &EthBackendServer{eth: eth, events: events}
|
2021-05-17 12:15:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *EthBackendServer) Version(context.Context, *emptypb.Empty) (*types2.VersionReply, error) {
|
2021-05-22 10:00:13 +00:00
|
|
|
return EthBackendAPIVersion, nil
|
2020-08-12 13:47:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *EthBackendServer) Etherbase(_ context.Context, _ *remote.EtherbaseRequest) (*remote.EtherbaseReply, error) {
|
2021-03-19 21:24:49 +00:00
|
|
|
out := &remote.EtherbaseReply{Address: gointerfaces.ConvertAddressToH160(common.Address{})}
|
2020-08-12 13:47:59 +00:00
|
|
|
|
|
|
|
base, err := s.eth.Etherbase()
|
|
|
|
if err != nil {
|
|
|
|
return out, err
|
|
|
|
}
|
|
|
|
|
2021-03-19 21:24:49 +00:00
|
|
|
out.Address = gointerfaces.ConvertAddressToH160(base)
|
2020-08-12 13:47:59 +00:00
|
|
|
return out, nil
|
|
|
|
}
|
2020-08-18 17:22:49 +00:00
|
|
|
|
|
|
|
func (s *EthBackendServer) NetVersion(_ context.Context, _ *remote.NetVersionRequest) (*remote.NetVersionReply, error) {
|
|
|
|
id, err := s.eth.NetVersion()
|
|
|
|
if err != nil {
|
|
|
|
return &remote.NetVersionReply{}, err
|
|
|
|
}
|
|
|
|
return &remote.NetVersionReply{Id: id}, nil
|
|
|
|
}
|
2020-11-17 19:13:41 +00:00
|
|
|
|
2021-06-17 21:55:20 +00:00
|
|
|
func (s *EthBackendServer) NetPeerCount(_ context.Context, _ *remote.NetPeerCountRequest) (*remote.NetPeerCountReply, error) {
|
|
|
|
id, err := s.eth.NetPeerCount()
|
|
|
|
if err != nil {
|
|
|
|
return &remote.NetPeerCountReply{}, err
|
|
|
|
}
|
2021-06-18 13:34:15 +00:00
|
|
|
return &remote.NetPeerCountReply{Count: id}, nil
|
2021-06-17 21:55:20 +00:00
|
|
|
}
|
|
|
|
|
2020-12-04 21:17:13 +00:00
|
|
|
func (s *EthBackendServer) Subscribe(r *remote.SubscribeRequest, subscribeServer remote.ETHBACKEND_SubscribeServer) error {
|
2020-11-17 19:13:41 +00:00
|
|
|
log.Debug("establishing event subscription channel with the RPC daemon")
|
|
|
|
s.events.AddHeaderSubscription(func(h *types.Header) error {
|
2020-12-04 21:17:13 +00:00
|
|
|
select {
|
|
|
|
case <-subscribeServer.Context().Done():
|
|
|
|
return subscribeServer.Context().Err()
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
|
2021-04-27 08:23:04 +00:00
|
|
|
var buf bytes.Buffer
|
|
|
|
if err := rlp.Encode(&buf, h); err != nil {
|
2020-11-17 19:13:41 +00:00
|
|
|
log.Warn("error while marshaling a header", "err", err)
|
|
|
|
return err
|
|
|
|
}
|
2021-04-27 08:23:04 +00:00
|
|
|
payload := buf.Bytes()
|
2020-11-17 19:13:41 +00:00
|
|
|
|
2021-04-27 08:23:04 +00:00
|
|
|
err := subscribeServer.Send(&remote.SubscribeReply{
|
2021-03-25 06:42:45 +00:00
|
|
|
Type: remote.Event_HEADER,
|
2020-11-17 19:13:41 +00:00
|
|
|
Data: payload,
|
|
|
|
})
|
|
|
|
|
|
|
|
// we only close the wg on error because if we successfully sent an event,
|
|
|
|
// that means that the channel wasn't closed and is ready to
|
|
|
|
// receive more events.
|
|
|
|
// if rpcdaemon disconnects, we will receive an error here
|
|
|
|
// next time we try to send an event
|
|
|
|
if err != nil {
|
|
|
|
log.Info("event subscription channel was closed", "reason", err)
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
})
|
2020-12-04 21:17:13 +00:00
|
|
|
|
2020-11-17 19:13:41 +00:00
|
|
|
log.Info("event subscription channel established with the RPC daemon")
|
2021-04-26 16:56:02 +00:00
|
|
|
<-subscribeServer.Context().Done()
|
2020-11-17 19:13:41 +00:00
|
|
|
log.Info("event subscription channel closed with the RPC daemon")
|
|
|
|
return nil
|
|
|
|
}
|
2021-03-23 09:00:07 +00:00
|
|
|
|
2021-04-24 15:46:29 +00:00
|
|
|
func (s *EthBackendServer) ProtocolVersion(_ context.Context, _ *remote.ProtocolVersionRequest) (*remote.ProtocolVersionReply, error) {
|
|
|
|
// Hardcoding to avoid import cycle
|
|
|
|
return &remote.ProtocolVersionReply{Id: 66}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *EthBackendServer) ClientVersion(_ context.Context, _ *remote.ClientVersionRequest) (*remote.ClientVersionReply, error) {
|
2021-06-11 08:34:37 +00:00
|
|
|
return &remote.ClientVersionReply{NodeName: common.MakeName("Erigon", params.VersionWithCommit(params.GitCommit, ""))}, nil
|
2021-04-24 15:46:29 +00:00
|
|
|
}
|