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 10, 2024
1 parent 4158e5b commit 2e59a5d
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 63 deletions.
10 changes: 7 additions & 3 deletions network/p2p/p2ptest/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,19 @@ import (

// NewClient generates a client-server pair and returns the client used to
// communicate with a server with the specified handler
func NewClient(t *testing.T, rootCtx context.Context, handler p2p.Handler) *p2p.Client {
func NewClient(
t *testing.T,
rootCtx context.Context,
handler p2p.Handler,
clientNodeID ids.NodeID,
serverNodeID ids.NodeID,
) *p2p.Client {
clientSender := &enginetest.Sender{}
serverSender := &enginetest.Sender{}

clientNodeID := ids.GenerateTestNodeID()
clientNetwork, err := p2p.NewNetwork(logging.NoLog{}, clientSender, prometheus.NewRegistry(), "")
require.NoError(t, err)

serverNodeID := ids.GenerateTestNodeID()
serverNetwork, err := p2p.NewNetwork(logging.NoLog{}, serverSender, prometheus.NewRegistry(), "")
require.NoError(t, err)

Expand Down
4 changes: 2 additions & 2 deletions network/p2p/p2ptest/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func TestNewClient_AppGossip(t *testing.T) {
},
}

client := NewClient(t, ctx, testHandler)
client := NewClient(t, ctx, testHandler, ids.GenerateTestNodeID(), ids.GenerateTestNodeID())
require.NoError(client.AppGossip(ctx, common.SendConfig{}, []byte("foobar")))
<-appGossipChan
}
Expand Down Expand Up @@ -94,7 +94,7 @@ func TestNewClient_AppRequest(t *testing.T) {
},
}

client := NewClient(t, ctx, testHandler)
client := NewClient(t, ctx, testHandler, ids.GenerateTestNodeID(), ids.GenerateTestNodeID())
require.NoError(tt.appRequestF(
ctx,
client,
Expand Down
48 changes: 20 additions & 28 deletions x/sync/network_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,15 +75,15 @@ type SyncGetChangeProofHandler struct {
func (*SyncGetChangeProofHandler) AppGossip(context.Context, ids.NodeID, []byte) {}

func (s *SyncGetChangeProofHandler) AppRequest(ctx context.Context, _ ids.NodeID, _ time.Time, requestBytes []byte) ([]byte, *common.AppError) {
request := &pb.SyncGetChangeProofRequest{}
if err := proto.Unmarshal(requestBytes, request); err != nil {
req := &pb.SyncGetChangeProofRequest{}
if err := proto.Unmarshal(requestBytes, req); err != nil {
return nil, &common.AppError{
Code: p2p.ErrUnexpected.Code,
Message: fmt.Sprintf("failed to unmarshal request: %s", err),
}
}

if err := validateChangeProofRequest(request); err != nil {
if err := validateChangeProofRequest(req); err != nil {
return nil, &common.AppError{
Code: p2p.ErrUnexpected.Code,
Message: fmt.Sprintf("invalid request: %s", err),
Expand All @@ -92,21 +92,21 @@ func (s *SyncGetChangeProofHandler) AppRequest(ctx context.Context, _ ids.NodeID

// override limits if they exceed caps
var (
keyLimit = min(request.KeyLimit, maxKeyValuesLimit)
bytesLimit = min(int(request.BytesLimit), maxByteSizeLimit)
start = maybeBytesToMaybe(request.StartKey)
end = maybeBytesToMaybe(request.EndKey)
keyLimit = min(req.KeyLimit, maxKeyValuesLimit)
bytesLimit = min(int(req.BytesLimit), maxByteSizeLimit)
start = maybeBytesToMaybe(req.StartKey)
end = maybeBytesToMaybe(req.EndKey)
)

startRoot, err := ids.ToID(request.StartRootHash)
startRoot, err := ids.ToID(req.StartRootHash)
if err != nil {
return nil, &common.AppError{
Code: p2p.ErrUnexpected.Code,
Message: fmt.Sprintf("failed to parse start root hash: %s", err),
}
}

endRoot, err := ids.ToID(request.EndRootHash)
endRoot, err := ids.ToID(req.EndRootHash)
if err != nil {
return nil, &common.AppError{
Code: p2p.ErrUnexpected.Code,
Expand Down Expand Up @@ -140,11 +140,11 @@ func (s *SyncGetChangeProofHandler) AppRequest(ctx context.Context, _ ids.NodeID
ctx,
s.db,
&pb.SyncGetRangeProofRequest{
RootHash: request.EndRootHash,
StartKey: request.StartKey,
EndKey: request.EndKey,
KeyLimit: request.KeyLimit,
BytesLimit: request.BytesLimit,
RootHash: req.EndRootHash,
StartKey: req.StartKey,
EndKey: req.EndKey,
KeyLimit: req.KeyLimit,
BytesLimit: req.BytesLimit,
},
func(rangeProof *merkledb.RangeProof) ([]byte, error) {
return proto.Marshal(&pb.SyncGetChangeProofResponse{
Expand Down Expand Up @@ -191,10 +191,6 @@ func (s *SyncGetChangeProofHandler) AppRequest(ctx context.Context, _ ids.NodeID
}
}

func (*SyncGetChangeProofHandler) CrossChainAppRequest(context.Context, ids.ID, time.Time, []byte) ([]byte, error) {
return nil, nil
}

func NewSyncGetRangeProofHandler(log logging.Logger, db DB) *SyncGetRangeProofHandler {
return &SyncGetRangeProofHandler{
log: log,
Expand All @@ -210,29 +206,29 @@ type SyncGetRangeProofHandler struct {
func (*SyncGetRangeProofHandler) AppGossip(context.Context, ids.NodeID, []byte) {}

func (s *SyncGetRangeProofHandler) AppRequest(ctx context.Context, _ ids.NodeID, _ time.Time, requestBytes []byte) ([]byte, *common.AppError) {
request := &pb.SyncGetRangeProofRequest{}
if err := proto.Unmarshal(requestBytes, request); err != nil {
req := &pb.SyncGetRangeProofRequest{}
if err := proto.Unmarshal(requestBytes, req); err != nil {
return nil, &common.AppError{
Code: p2p.ErrUnexpected.Code,
Message: fmt.Sprintf("failed to unmarshal request: %s", err),
}
}

if err := validateRangeProofRequest(request); err != nil {
if err := validateRangeProofRequest(req); err != nil {
return nil, &common.AppError{
Code: p2p.ErrUnexpected.Code,
Message: fmt.Sprintf("invalid range proof request: %s", err),
}
}

// override limits if they exceed caps
request.KeyLimit = min(request.KeyLimit, maxKeyValuesLimit)
request.BytesLimit = min(request.BytesLimit, maxByteSizeLimit)
req.KeyLimit = min(req.KeyLimit, maxKeyValuesLimit)
req.BytesLimit = min(req.BytesLimit, maxByteSizeLimit)

proofBytes, err := getRangeProof(
ctx,
s.db,
request,
req,
func(rangeProof *merkledb.RangeProof) ([]byte, error) {
return proto.Marshal(rangeProof.ToProto())
},
Expand All @@ -247,10 +243,6 @@ func (s *SyncGetRangeProofHandler) AppRequest(ctx context.Context, _ ids.NodeID,
return proofBytes, nil
}

func (*SyncGetRangeProofHandler) CrossChainAppRequest(context.Context, ids.ID, time.Time, []byte) ([]byte, error) {
return nil, nil
}

// Get the range proof specified by [req].
// If the generated proof is too large, the key limit is reduced
// and the proof is regenerated. This process is repeated until
Expand Down
60 changes: 30 additions & 30 deletions x/sync/sync_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,8 @@ func Test_Completion(t *testing.T) {
ctx := context.Background()
syncer, err := NewManager(ManagerConfig{
DB: db,
RangeProofClient: p2ptest.NewClient(t, ctx, NewSyncGetRangeProofHandler(logging.NoLog{}, emptyDB)),
ChangeProofClient: p2ptest.NewClient(t, ctx, NewSyncGetChangeProofHandler(logging.NoLog{}, emptyDB)),
RangeProofClient: p2ptest.NewClient(t, ctx, NewSyncGetRangeProofHandler(logging.NoLog{}, emptyDB), ids.GenerateTestNodeID(), ids.GenerateTestNodeID()),
ChangeProofClient: p2ptest.NewClient(t, ctx, NewSyncGetChangeProofHandler(logging.NoLog{}, emptyDB), ids.GenerateTestNodeID(), ids.GenerateTestNodeID()),
TargetRoot: emptyRoot,
SimultaneousWorkLimit: 5,
Log: logging.NoLog{},
Expand Down Expand Up @@ -177,8 +177,8 @@ func Test_Sync_FindNextKey_InSync(t *testing.T) {
ctx := context.Background()
syncer, err := NewManager(ManagerConfig{
DB: db,
RangeProofClient: p2ptest.NewClient(t, ctx, NewSyncGetRangeProofHandler(logging.NoLog{}, dbToSync)),
ChangeProofClient: p2ptest.NewClient(t, ctx, NewSyncGetChangeProofHandler(logging.NoLog{}, dbToSync)),
RangeProofClient: p2ptest.NewClient(t, ctx, NewSyncGetRangeProofHandler(logging.NoLog{}, dbToSync), ids.GenerateTestNodeID(), ids.GenerateTestNodeID()),
ChangeProofClient: p2ptest.NewClient(t, ctx, NewSyncGetChangeProofHandler(logging.NoLog{}, dbToSync), ids.GenerateTestNodeID(), ids.GenerateTestNodeID()),
TargetRoot: syncRoot,
SimultaneousWorkLimit: 5,
Log: logging.NoLog{},
Expand Down Expand Up @@ -373,8 +373,8 @@ func Test_Sync_FindNextKey_ExtraValues(t *testing.T) {
ctx := context.Background()
syncer, err := NewManager(ManagerConfig{
DB: db,
RangeProofClient: p2ptest.NewClient(t, ctx, NewSyncGetRangeProofHandler(logging.NoLog{}, dbToSync)),
ChangeProofClient: p2ptest.NewClient(t, ctx, NewSyncGetChangeProofHandler(logging.NoLog{}, dbToSync)),
RangeProofClient: p2ptest.NewClient(t, ctx, NewSyncGetRangeProofHandler(logging.NoLog{}, dbToSync), ids.GenerateTestNodeID(), ids.GenerateTestNodeID()),
ChangeProofClient: p2ptest.NewClient(t, ctx, NewSyncGetChangeProofHandler(logging.NoLog{}, dbToSync), ids.GenerateTestNodeID(), ids.GenerateTestNodeID()),
TargetRoot: syncRoot,
SimultaneousWorkLimit: 5,
Log: logging.NoLog{},
Expand Down Expand Up @@ -436,7 +436,7 @@ func TestFindNextKeyEmptyEndProof(t *testing.T) {
syncer, err := NewManager(ManagerConfig{
DB: db,
RangeProofClient: &p2p.Client{},
ChangeProofClient: p2ptest.NewClient(t, ctx, NewSyncGetChangeProofHandler(logging.NoLog{}, db)),
ChangeProofClient: p2ptest.NewClient(t, ctx, NewSyncGetChangeProofHandler(logging.NoLog{}, db), ids.GenerateTestNodeID(), ids.GenerateTestNodeID()),
TargetRoot: ids.Empty,
SimultaneousWorkLimit: 5,
Log: logging.NoLog{},
Expand Down Expand Up @@ -504,8 +504,8 @@ func Test_Sync_FindNextKey_DifferentChild(t *testing.T) {
ctx := context.Background()
syncer, err := NewManager(ManagerConfig{
DB: db,
RangeProofClient: p2ptest.NewClient(t, ctx, NewSyncGetRangeProofHandler(logging.NoLog{}, dbToSync)),
ChangeProofClient: p2ptest.NewClient(t, ctx, NewSyncGetChangeProofHandler(logging.NoLog{}, dbToSync)),
RangeProofClient: p2ptest.NewClient(t, ctx, NewSyncGetRangeProofHandler(logging.NoLog{}, dbToSync), ids.GenerateTestNodeID(), ids.GenerateTestNodeID()),
ChangeProofClient: p2ptest.NewClient(t, ctx, NewSyncGetChangeProofHandler(logging.NoLog{}, dbToSync), ids.GenerateTestNodeID(), ids.GenerateTestNodeID()),
TargetRoot: syncRoot,
SimultaneousWorkLimit: 5,
Log: logging.NoLog{},
Expand Down Expand Up @@ -774,7 +774,7 @@ func Test_Sync_Result_Correct_Root(t *testing.T) {
response.KeyValues = append(response.KeyValues, merkledb.KeyValue{})
})

return p2ptest.NewClient(t, context.Background(), handler)
return p2ptest.NewClient(t, context.Background(), handler, ids.GenerateTestNodeID(), ids.GenerateTestNodeID())
},
},
{
Expand All @@ -784,7 +784,7 @@ func Test_Sync_Result_Correct_Root(t *testing.T) {
response.KeyValues = response.KeyValues[min(1, len(response.KeyValues)):]
})

return p2ptest.NewClient(t, context.Background(), handler)
return p2ptest.NewClient(t, context.Background(), handler, ids.GenerateTestNodeID(), ids.GenerateTestNodeID())
},
},
{
Expand All @@ -810,7 +810,7 @@ func Test_Sync_Result_Correct_Root(t *testing.T) {
}
})

return p2ptest.NewClient(t, context.Background(), handler)
return p2ptest.NewClient(t, context.Background(), handler, ids.GenerateTestNodeID(), ids.GenerateTestNodeID())
},
},
{
Expand All @@ -821,7 +821,7 @@ func Test_Sync_Result_Correct_Root(t *testing.T) {
_ = slices.Delete(response.KeyValues, i, min(len(response.KeyValues), i+1))
})

return p2ptest.NewClient(t, context.Background(), handler)
return p2ptest.NewClient(t, context.Background(), handler, ids.GenerateTestNodeID(), ids.GenerateTestNodeID())
},
},
{
Expand All @@ -832,7 +832,7 @@ func Test_Sync_Result_Correct_Root(t *testing.T) {
response.EndProof = nil
})

return p2ptest.NewClient(t, context.Background(), handler)
return p2ptest.NewClient(t, context.Background(), handler, ids.GenerateTestNodeID(), ids.GenerateTestNodeID())
},
},
{
Expand All @@ -842,7 +842,7 @@ func Test_Sync_Result_Correct_Root(t *testing.T) {
response.EndProof = nil
})

return p2ptest.NewClient(t, context.Background(), handler)
return p2ptest.NewClient(t, context.Background(), handler, ids.GenerateTestNodeID(), ids.GenerateTestNodeID())
},
},
{
Expand All @@ -854,7 +854,7 @@ func Test_Sync_Result_Correct_Root(t *testing.T) {
response.KeyValues = nil
})

return p2ptest.NewClient(t, context.Background(), handler)
return p2ptest.NewClient(t, context.Background(), handler, ids.GenerateTestNodeID(), ids.GenerateTestNodeID())
},
},
{
Expand All @@ -863,7 +863,7 @@ func Test_Sync_Result_Correct_Root(t *testing.T) {
return p2ptest.NewClient(t, context.Background(), &flakyHandler{
Handler: NewSyncGetRangeProofHandler(logging.NoLog{}, db),
c: &counter{m: 2},
})
}, ids.GenerateTestNodeID(), ids.GenerateTestNodeID())
},
},
{
Expand All @@ -873,7 +873,7 @@ func Test_Sync_Result_Correct_Root(t *testing.T) {
response.KeyChanges = append(response.KeyChanges, make([]merkledb.KeyChange, defaultRequestKeyLimit)...)
})

return p2ptest.NewClient(t, context.Background(), handler)
return p2ptest.NewClient(t, context.Background(), handler, ids.GenerateTestNodeID(), ids.GenerateTestNodeID())
},
},
{
Expand All @@ -883,7 +883,7 @@ func Test_Sync_Result_Correct_Root(t *testing.T) {
response.KeyChanges = response.KeyChanges[min(1, len(response.KeyChanges)):]
})

return p2ptest.NewClient(t, context.Background(), handler)
return p2ptest.NewClient(t, context.Background(), handler, ids.GenerateTestNodeID(), ids.GenerateTestNodeID())
},
},
{
Expand All @@ -894,7 +894,7 @@ func Test_Sync_Result_Correct_Root(t *testing.T) {
_ = slices.Delete(response.KeyChanges, i, min(len(response.KeyChanges), i+1))
})

return p2ptest.NewClient(t, context.Background(), handler)
return p2ptest.NewClient(t, context.Background(), handler, ids.GenerateTestNodeID(), ids.GenerateTestNodeID())
},
},
{
Expand All @@ -905,7 +905,7 @@ func Test_Sync_Result_Correct_Root(t *testing.T) {
response.EndProof = nil
})

return p2ptest.NewClient(t, context.Background(), handler)
return p2ptest.NewClient(t, context.Background(), handler, ids.GenerateTestNodeID(), ids.GenerateTestNodeID())
},
},
{
Expand All @@ -914,7 +914,7 @@ func Test_Sync_Result_Correct_Root(t *testing.T) {
return p2ptest.NewClient(t, context.Background(), &flakyHandler{
Handler: NewSyncGetChangeProofHandler(logging.NoLog{}, db),
c: &counter{m: 2},
})
}, ids.GenerateTestNodeID(), ids.GenerateTestNodeID())
},
},
}
Expand Down Expand Up @@ -943,13 +943,13 @@ func Test_Sync_Result_Correct_Root(t *testing.T) {
)

rangeProofHandler := NewSyncGetRangeProofHandler(logging.NoLog{}, dbToSync)
rangeProofClient = p2ptest.NewClient(t, ctx, rangeProofHandler)
rangeProofClient = p2ptest.NewClient(t, ctx, rangeProofHandler, ids.GenerateTestNodeID(), ids.GenerateTestNodeID())
if tt.rangeProofClient != nil {
rangeProofClient = tt.rangeProofClient(dbToSync)
}

changeProofHandler := NewSyncGetChangeProofHandler(logging.NoLog{}, dbToSync)
changeProofClient = p2ptest.NewClient(t, ctx, changeProofHandler)
changeProofClient = p2ptest.NewClient(t, ctx, changeProofHandler, ids.GenerateTestNodeID(), ids.GenerateTestNodeID())
if tt.changeProofClient != nil {
changeProofClient = tt.changeProofClient(dbToSync)
}
Expand Down Expand Up @@ -1024,8 +1024,8 @@ func Test_Sync_Result_Correct_Root_With_Sync_Restart(t *testing.T) {
ctx := context.Background()
syncer, err := NewManager(ManagerConfig{
DB: db,
RangeProofClient: p2ptest.NewClient(t, ctx, NewSyncGetRangeProofHandler(logging.NoLog{}, dbToSync)),
ChangeProofClient: p2ptest.NewClient(t, ctx, NewSyncGetChangeProofHandler(logging.NoLog{}, dbToSync)),
RangeProofClient: p2ptest.NewClient(t, ctx, NewSyncGetRangeProofHandler(logging.NoLog{}, dbToSync), ids.GenerateTestNodeID(), ids.GenerateTestNodeID()),
ChangeProofClient: p2ptest.NewClient(t, ctx, NewSyncGetChangeProofHandler(logging.NoLog{}, dbToSync), ids.GenerateTestNodeID(), ids.GenerateTestNodeID()),
TargetRoot: syncRoot,
SimultaneousWorkLimit: 5,
Log: logging.NoLog{},
Expand All @@ -1051,8 +1051,8 @@ func Test_Sync_Result_Correct_Root_With_Sync_Restart(t *testing.T) {

newSyncer, err := NewManager(ManagerConfig{
DB: db,
RangeProofClient: p2ptest.NewClient(t, ctx, NewSyncGetRangeProofHandler(logging.NoLog{}, dbToSync)),
ChangeProofClient: p2ptest.NewClient(t, ctx, NewSyncGetChangeProofHandler(logging.NoLog{}, dbToSync)),
RangeProofClient: p2ptest.NewClient(t, ctx, NewSyncGetRangeProofHandler(logging.NoLog{}, dbToSync), ids.GenerateTestNodeID(), ids.GenerateTestNodeID()),
ChangeProofClient: p2ptest.NewClient(t, ctx, NewSyncGetChangeProofHandler(logging.NoLog{}, dbToSync), ids.GenerateTestNodeID(), ids.GenerateTestNodeID()),
TargetRoot: syncRoot,
SimultaneousWorkLimit: 5,
Log: logging.NoLog{},
Expand Down Expand Up @@ -1124,12 +1124,12 @@ func Test_Sync_Result_Correct_Root_Update_Root_During(t *testing.T) {
rangeProofClient := p2ptest.NewClient(t, ctx, &testHandler{
handler: NewSyncGetRangeProofHandler(logging.NoLog{}, dbToSync),
updatedRootChan: updatedRootChan,
})
}, ids.GenerateTestNodeID(), ids.GenerateTestNodeID())

changeProofClient := p2ptest.NewClient(t, ctx, &testHandler{
handler: NewSyncGetChangeProofHandler(logging.NoLog{}, dbToSync),
updatedRootChan: updatedRootChan,
})
}, ids.GenerateTestNodeID(), ids.GenerateTestNodeID())

syncer, err := NewManager(ManagerConfig{
DB: db,
Expand Down

0 comments on commit 2e59a5d

Please sign in to comment.