Skip to content

Commit

Permalink
rpcdaemon: Split BorEvent gRPC into BorEvents and BorTxnLookup (#11877)
Browse files Browse the repository at this point in the history
- `BorEventResponse` was pulling double duty by being used for block
number lookups as well as event lookups.
- As a result, we were using `EventsByBlock` instead of `EventLookup`
and interpreting the presence of the bor TX hash in `kv.BorTxLookup` by
the absence of returned events.
- This PR refactors the code so that remote calls to `EventLookup` use
`BlockReader.EventLookup`, preventing any discrepancies between the two.
- Naming has also been updated to be clearer.

Depends on: erigontech/interfaces#231
  • Loading branch information
shohamc1 authored Sep 5, 2024
1 parent 92d8c17 commit 313c720
Show file tree
Hide file tree
Showing 9 changed files with 524 additions and 273 deletions.
13 changes: 9 additions & 4 deletions erigon-lib/direct/eth_backend_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,11 @@ import (
"context"
"io"

remote "github.com/erigontech/erigon-lib/gointerfaces/remoteproto"
types "github.com/erigontech/erigon-lib/gointerfaces/typesproto"
"google.golang.org/grpc"
"google.golang.org/protobuf/types/known/emptypb"

remote "github.com/erigontech/erigon-lib/gointerfaces/remoteproto"
types "github.com/erigontech/erigon-lib/gointerfaces/typesproto"
)

type EthBackendClientDirect struct {
Expand Down Expand Up @@ -227,6 +228,10 @@ func (s *EthBackendClientDirect) PendingBlock(ctx context.Context, in *emptypb.E
return s.server.PendingBlock(ctx, in)
}

func (s *EthBackendClientDirect) BorEvent(ctx context.Context, in *remote.BorEventRequest, opts ...grpc.CallOption) (*remote.BorEventReply, error) {
return s.server.BorEvent(ctx, in)
func (s *EthBackendClientDirect) BorTxnLookup(ctx context.Context, in *remote.BorTxnLookupRequest, opts ...grpc.CallOption) (*remote.BorTxnLookupReply, error) {
return s.server.BorTxnLookup(ctx, in)
}

func (s *EthBackendClientDirect) BorEvents(ctx context.Context, in *remote.BorEventsRequest, opts ...grpc.CallOption) (*remote.BorEventsReply, error) {
return s.server.BorEvents(ctx, in)
}
2 changes: 1 addition & 1 deletion erigon-lib/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ go 1.21.5

require (
github.com/erigontech/erigon-snapshot v1.3.1-0.20240814160410-2ce37904b978
github.com/erigontech/interfaces v0.0.0-20240819221809-43fdab2057da
github.com/erigontech/interfaces v0.0.0-20240905113027-2c1accea17aa
github.com/erigontech/mdbx-go v0.38.4
github.com/erigontech/secp256k1 v1.1.0
github.com/rs/dnscache v0.0.0-20211102005908-e0241e321417
Expand Down
4 changes: 2 additions & 2 deletions erigon-lib/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -148,8 +148,8 @@ github.com/envoyproxy/go-control-plane v0.9.4/go.mod h1:6rpuAdCZL397s3pYoYcLgu1m
github.com/envoyproxy/protoc-gen-validate v0.1.0/go.mod h1:iSmxcyjqTsJpI2R4NaDN7+kN2VEUnK/pcBlmesArF7c=
github.com/erigontech/erigon-snapshot v1.3.1-0.20240814160410-2ce37904b978 h1:7ECOf7Us3+/706WGZXIX84qQc6zmxQby8fGbFLiqFlU=
github.com/erigontech/erigon-snapshot v1.3.1-0.20240814160410-2ce37904b978/go.mod h1:ooHlCl+eEYzebiPu+FP6Q6SpPUeMADn8Jxabv3IKb9M=
github.com/erigontech/interfaces v0.0.0-20240819221809-43fdab2057da h1:ZayX+yo/IRJ1CGVsC8R32tqU2uUEvJr/L6L4RUAa/+s=
github.com/erigontech/interfaces v0.0.0-20240819221809-43fdab2057da/go.mod h1:N7OUkhkcagp9+7yb4ycHsG2VWCOmuJ1ONBecJshxtLE=
github.com/erigontech/interfaces v0.0.0-20240905113027-2c1accea17aa h1:iVT7tKZN3L6Jug928bzyAu7peeslHD1QDbdbmyMyUgo=
github.com/erigontech/interfaces v0.0.0-20240905113027-2c1accea17aa/go.mod h1:N7OUkhkcagp9+7yb4ycHsG2VWCOmuJ1ONBecJshxtLE=
github.com/erigontech/mdbx-go v0.38.4 h1:S9T7mTe9KPcFe4dOoOtVdI6gPzht9y7wMnYfUBgrQLo=
github.com/erigontech/mdbx-go v0.38.4/go.mod h1:IcOLQDPw3VM/asP6T5JVPPN4FHHgJtY16XfYjzWKVNI=
github.com/erigontech/secp256k1 v1.1.0 h1:mO3YJMUSoASE15Ya//SoHiisptUhdXExuMUN1M0X9qY=
Expand Down
503 changes: 319 additions & 184 deletions erigon-lib/gointerfaces/remoteproto/ethbackend.pb.go

Large diffs are not rendered by default.

195 changes: 142 additions & 53 deletions erigon-lib/gointerfaces/remoteproto/ethbackend_grpc.pb.go

Large diffs are not rendered by default.

26 changes: 13 additions & 13 deletions erigon-lib/gointerfaces/remoteproto/kv.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions erigon-lib/kv/remotedbserver/remotedbserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -661,7 +661,7 @@ func (s *KvServer) DomainRange(_ context.Context, req *remote.DomainRangeReq) (*
reply := &remote.Pairs{}
fromKey, toKey, limit := req.FromKey, req.ToKey, int(req.Limit)
if req.PageToken != "" {
var pagination remote.ParisPagination
var pagination remote.PairsPagination
if err := unmarshalPagination(req.PageToken, &pagination); err != nil {
return nil, err
}
Expand Down Expand Up @@ -697,7 +697,7 @@ func (s *KvServer) DomainRange(_ context.Context, req *remote.DomainRangeReq) (*
if err != nil {
return err
}
reply.NextPageToken, err = marshalPagination(&remote.ParisPagination{NextKey: nextK, Limit: int64(limit)})
reply.NextPageToken, err = marshalPagination(&remote.PairsPagination{NextKey: nextK, Limit: int64(limit)})
if err != nil {
return err
}
Expand All @@ -712,7 +712,7 @@ func (s *KvServer) DomainRange(_ context.Context, req *remote.DomainRangeReq) (*
func (s *KvServer) Range(_ context.Context, req *remote.RangeReq) (*remote.Pairs, error) {
from, limit := req.FromPrefix, int(req.Limit)
if req.PageToken != "" {
var pagination remote.ParisPagination
var pagination remote.PairsPagination
if err := unmarshalPagination(req.PageToken, &pagination); err != nil {
return nil, err
}
Expand Down Expand Up @@ -751,7 +751,7 @@ func (s *KvServer) Range(_ context.Context, req *remote.RangeReq) (*remote.Pairs
if err != nil {
return err
}
reply.NextPageToken, err = marshalPagination(&remote.ParisPagination{NextKey: nextK, Limit: int64(limit)})
reply.NextPageToken, err = marshalPagination(&remote.PairsPagination{NextKey: nextK, Limit: int64(limit)})
if err != nil {
return err
}
Expand Down
33 changes: 28 additions & 5 deletions ethdb/privateapi/ethbackend.go
Original file line number Diff line number Diff line change
Expand Up @@ -328,18 +328,41 @@ func (s *EthBackendServer) SubscribeLogs(server remote.ETHBACKEND_SubscribeLogsS
return errors.New("no logs filter available")
}

func (s *EthBackendServer) BorEvent(ctx context.Context, req *remote.BorEventRequest) (*remote.BorEventReply, error) {
func (s *EthBackendServer) BorTxnLookup(ctx context.Context, req *remote.BorTxnLookupRequest) (*remote.BorTxnLookupReply, error) {
tx, err := s.db.BeginRo(ctx)
if err != nil {
return nil, err
}
defer tx.Rollback()
_, ok, err := s.blockReader.EventLookup(ctx, tx, gointerfaces.ConvertH256ToHash(req.BorTxHash))

blockNum, ok, err := s.blockReader.EventLookup(ctx, tx, gointerfaces.ConvertH256ToHash(req.BorTxHash))
if err != nil {
return nil, err
}
if !ok {
return &remote.BorEventReply{}, nil
return &remote.BorTxnLookupReply{
BlockNumber: blockNum,
Present: ok,
}, nil
}

func (s *EthBackendServer) BorEvents(ctx context.Context, req *remote.BorEventsRequest) (*remote.BorEventsReply, error) {
tx, err := s.db.BeginRo(ctx)
if err != nil {
return nil, err
}
return &remote.BorEventReply{}, nil
defer tx.Rollback()

events, err := s.blockReader.EventsByBlock(ctx, tx, gointerfaces.ConvertH256ToHash(req.BlockHash), req.BlockNum)
if err != nil {
return nil, err
}

eventsRaw := make([][]byte, len(events))
for i, e := range events {
eventsRaw[i] = e
}

return &remote.BorEventsReply{
EventRlps: eventsRaw,
}, nil
}
13 changes: 6 additions & 7 deletions turbo/snapshotsync/freezeblocks/block_reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -288,26 +288,25 @@ func (r *RemoteBlockReader) LastEventId(ctx context.Context, tx kv.Tx) (uint64,
return 0, false, errors.New("not implemented")
}

func (r *RemoteBlockReader) EventLookup(ctx context.Context, tx kv.Getter, txnHash common.Hash) (uint64, bool, error) {
reply, err := r.client.BorEvent(ctx, &remote.BorEventRequest{BorTxHash: gointerfaces.ConvertHashToH256(txnHash)})
func (r *RemoteBlockReader) EventLookup(ctx context.Context, tx kv.Getter, borTxnHash common.Hash) (uint64, bool, error) {
reply, err := r.client.BorTxnLookup(ctx, &remote.BorTxnLookupRequest{BorTxHash: gointerfaces.ConvertHashToH256(borTxnHash)})
if err != nil {
return 0, false, err
}
if reply == nil || len(reply.EventRlps) == 0 {
if reply == nil {
return 0, false, nil
}
return reply.BlockNumber, true, nil
return reply.BlockNumber, reply.Present, nil
}

func (r *RemoteBlockReader) EventsByBlock(ctx context.Context, tx kv.Tx, hash common.Hash, blockHeight uint64) ([]rlp.RawValue, error) {
borTxnHash := bortypes.ComputeBorTxHash(blockHeight, hash)
reply, err := r.client.BorEvent(ctx, &remote.BorEventRequest{BorTxHash: gointerfaces.ConvertHashToH256(borTxnHash)})
reply, err := r.client.BorEvents(ctx, &remote.BorEventsRequest{BlockHash: gointerfaces.ConvertHashToH256(hash), BlockNum: blockHeight})
if err != nil {
return nil, err
}
result := make([]rlp.RawValue, len(reply.EventRlps))
for i, r := range reply.EventRlps {
result[i] = rlp.RawValue(r)
result[i] = r
}
return result, nil
}
Expand Down

0 comments on commit 313c720

Please sign in to comment.