Skip to content

Commit

Permalink
nit
Browse files Browse the repository at this point in the history
Signed-off-by: Joshua Kim <[email protected]>
  • Loading branch information
joshua-kim committed Sep 24, 2024
1 parent 2ad58e3 commit 3af3bc9
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 66 deletions.
8 changes: 4 additions & 4 deletions network/p2p/p2ptest/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,10 +80,10 @@ func NewClient(
return nil
}

require.NoError(t, clientNetwork.Connected(rootCtx, clientNodeID, nil))
require.NoError(t, clientNetwork.Connected(rootCtx, serverNodeID, nil))
require.NoError(t, serverNetwork.Connected(rootCtx, clientNodeID, nil))
require.NoError(t, serverNetwork.Connected(rootCtx, serverNodeID, nil))
require.NoError(t, clientNetwork.Connected(ctx, clientNodeID, nil))
require.NoError(t, clientNetwork.Connected(ctx, serverNodeID, nil))
require.NoError(t, serverNetwork.Connected(ctx, clientNodeID, nil))
require.NoError(t, serverNetwork.Connected(ctx, serverNodeID, nil))

require.NoError(t, serverNetwork.AddHandler(0, handler))
return clientNetwork.NewClient(0)
Expand Down
4 changes: 2 additions & 2 deletions x/sync/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func newModifiedRangeProofHandler(
db merkledb.MerkleDB,
modifyResponse func(response *merkledb.RangeProof),
) p2p.Handler {
handler := NewSyncGetRangeProofHandler(logging.NoLog{}, db)
handler := NewGetRangeProofHandler(logging.NoLog{}, db)

c := counter{m: 2}
return &p2p.TestHandler{
Expand Down Expand Up @@ -79,7 +79,7 @@ func newModifiedChangeProofHandler(
db merkledb.MerkleDB,
modifyResponse func(response *merkledb.ChangeProof),
) p2p.Handler {
handler := NewSyncGetChangeProofHandler(logging.NoLog{}, db)
handler := NewGetChangeProofHandler(logging.NoLog{}, db)

c := counter{m: 2}
return &p2p.TestHandler{
Expand Down
31 changes: 16 additions & 15 deletions x/sync/network_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@ var (
errInvalidBounds = errors.New("start key is greater than end key")
errInvalidRootHash = fmt.Errorf("root hash must have length %d", hashing.HashLen)

_ p2p.Handler = (*SyncGetChangeProofHandler)(nil)
_ p2p.Handler = (*SyncGetRangeProofHandler)(nil)
_ p2p.Handler = (*GetChangeProofHandler)(nil)
_ p2p.Handler = (*GetRangeProofHandler)(nil)
)

func maybeBytesToMaybe(mb *pb.MaybeBytes) maybe.Maybe[[]byte] {
Expand All @@ -60,21 +60,21 @@ func maybeBytesToMaybe(mb *pb.MaybeBytes) maybe.Maybe[[]byte] {
return maybe.Nothing[[]byte]()
}

func NewSyncGetChangeProofHandler(log logging.Logger, db DB) *SyncGetChangeProofHandler {
return &SyncGetChangeProofHandler{
func NewGetChangeProofHandler(log logging.Logger, db DB) *GetChangeProofHandler {
return &GetChangeProofHandler{
log: log,
db: db,
}
}

type SyncGetChangeProofHandler struct {
type GetChangeProofHandler struct {
log logging.Logger
db DB
}

func (*SyncGetChangeProofHandler) AppGossip(context.Context, ids.NodeID, []byte) {}
func (*GetChangeProofHandler) AppGossip(context.Context, ids.NodeID, []byte) {}

func (s *SyncGetChangeProofHandler) AppRequest(ctx context.Context, _ ids.NodeID, _ time.Time, requestBytes []byte) ([]byte, *common.AppError) {
func (g *GetChangeProofHandler) AppRequest(ctx context.Context, _ ids.NodeID, _ time.Time, requestBytes []byte) ([]byte, *common.AppError) {
req := &pb.SyncGetChangeProofRequest{}
if err := proto.Unmarshal(requestBytes, req); err != nil {
return nil, &common.AppError{
Expand Down Expand Up @@ -115,11 +115,12 @@ func (s *SyncGetChangeProofHandler) AppRequest(ctx context.Context, _ ids.NodeID
}

for keyLimit > 0 {
changeProof, err := s.db.GetChangeProof(ctx, startRoot, endRoot, start, end, int(keyLimit))
changeProof, err := g.db.GetChangeProof(ctx, startRoot, endRoot, start, end, int(keyLimit))
if err != nil {
if !errors.Is(err, merkledb.ErrInsufficientHistory) {
// We should only fail to get a change proof if we have insufficient history.
// Other errors are unexpected.
// TODO define custom errors
return nil, &common.AppError{
Code: p2p.ErrUnexpected.Code,
Message: fmt.Sprintf("failed to get change proof: %s", err),
Expand All @@ -138,7 +139,7 @@ func (s *SyncGetChangeProofHandler) AppRequest(ctx context.Context, _ ids.NodeID
// Generate a range proof for the end root ID instead.
proofBytes, err := getRangeProof(
ctx,
s.db,
g.db,
&pb.SyncGetRangeProofRequest{
RootHash: req.EndRootHash,
StartKey: req.StartKey,
Expand Down Expand Up @@ -191,21 +192,21 @@ func (s *SyncGetChangeProofHandler) AppRequest(ctx context.Context, _ ids.NodeID
}
}

func NewSyncGetRangeProofHandler(log logging.Logger, db DB) *SyncGetRangeProofHandler {
return &SyncGetRangeProofHandler{
func NewGetRangeProofHandler(log logging.Logger, db DB) *GetRangeProofHandler {
return &GetRangeProofHandler{
log: log,
db: db,
}
}

type SyncGetRangeProofHandler struct {
type GetRangeProofHandler struct {
log logging.Logger
db DB
}

func (*SyncGetRangeProofHandler) AppGossip(context.Context, ids.NodeID, []byte) {}
func (*GetRangeProofHandler) AppGossip(context.Context, ids.NodeID, []byte) {}

func (s *SyncGetRangeProofHandler) AppRequest(ctx context.Context, _ ids.NodeID, _ time.Time, requestBytes []byte) ([]byte, *common.AppError) {
func (g *GetRangeProofHandler) AppRequest(ctx context.Context, _ ids.NodeID, _ time.Time, requestBytes []byte) ([]byte, *common.AppError) {
req := &pb.SyncGetRangeProofRequest{}
if err := proto.Unmarshal(requestBytes, req); err != nil {
return nil, &common.AppError{
Expand All @@ -227,7 +228,7 @@ func (s *SyncGetRangeProofHandler) AppRequest(ctx context.Context, _ ids.NodeID,

proofBytes, err := getRangeProof(
ctx,
s.db,
g.db,
req,
func(rangeProof *merkledb.RangeProof) ([]byte, error) {
return proto.Marshal(rangeProof.ToProto())
Expand Down
21 changes: 8 additions & 13 deletions x/sync/network_server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ func Test_Server_GetRangeProof(t *testing.T) {
expectedErr: p2p.ErrUnexpected,
},
{
name: "key limit too large",
name: "response bounded by key limit",
request: &pb.SyncGetRangeProofRequest{
RootHash: smallTrieRoot[:],
KeyLimit: 2 * defaultRequestKeyLimit,
Expand All @@ -94,7 +94,7 @@ func Test_Server_GetRangeProof(t *testing.T) {
expectedResponseLen: defaultRequestKeyLimit,
},
{
name: "bytes limit too large",
name: "response bounded by byte limit",
request: &pb.SyncGetRangeProofRequest{
RootHash: smallTrieRoot[:],
KeyLimit: defaultRequestKeyLimit,
Expand All @@ -118,7 +118,7 @@ func Test_Server_GetRangeProof(t *testing.T) {
t.Run(test.name, func(t *testing.T) {
require := require.New(t)

handler := NewSyncGetRangeProofHandler(logging.NoLog{}, smallTrieDB)
handler := NewGetRangeProofHandler(logging.NoLog{}, smallTrieDB)
requestBytes, err := proto.Marshal(test.request)
require.NoError(err)
responseBytes, err := handler.AppRequest(context.Background(), test.nodeID, time.Time{}, requestBytes)
Expand All @@ -130,17 +130,12 @@ func Test_Server_GetRangeProof(t *testing.T) {
require.Nil(responseBytes)
return
}
require.NotNil(responseBytes)

var proof *merkledb.RangeProof
if !test.proofNil {
var proofProto pb.RangeProof
require.NoError(proto.Unmarshal(responseBytes, &proofProto))
var proofProto pb.RangeProof
require.NoError(proto.Unmarshal(responseBytes, &proofProto))

var p merkledb.RangeProof
require.NoError(p.UnmarshalProto(&proofProto))
proof = &p
}
var proof merkledb.RangeProof
require.NoError(proof.UnmarshalProto(&proofProto))

if test.expectedResponseLen > 0 {
require.LessOrEqual(len(proof.KeyValues), test.expectedResponseLen)
Expand Down Expand Up @@ -344,7 +339,7 @@ func Test_Server_GetChangeProof(t *testing.T) {
t.Run(test.name, func(t *testing.T) {
require := require.New(t)

handler := NewSyncGetChangeProofHandler(logging.NoLog{}, serverDB)
handler := NewGetChangeProofHandler(logging.NoLog{}, serverDB)

requestBytes, err := proto.Marshal(test.request)
require.NoError(err)
Expand Down
Loading

0 comments on commit 3af3bc9

Please sign in to comment.