Skip to content

Commit 47550e9

Browse files
authored
Add generics to x/sync (#4275)
1 parent 8b8ac94 commit 47550e9

File tree

13 files changed

+1415
-1323
lines changed

13 files changed

+1415
-1323
lines changed

x/sync/client_test.go renamed to x/merkledb/client_test.go

Lines changed: 35 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// Copyright (C) 2019-2025, Ava Labs, Inc. All rights reserved.
22
// See the file LICENSE for licensing terms.
33

4-
package sync
4+
package merkledb
55

66
import (
77
"context"
@@ -17,50 +17,57 @@ import (
1717
"github.com/ava-labs/avalanchego/network/p2p"
1818
"github.com/ava-labs/avalanchego/snow/engine/common"
1919
"github.com/ava-labs/avalanchego/trace"
20-
"github.com/ava-labs/avalanchego/x/merkledb"
2120

2221
pb "github.com/ava-labs/avalanchego/proto/pb/sync"
22+
xsync "github.com/ava-labs/avalanchego/x/sync"
2323
)
2424

25-
var _ p2p.Handler = (*flakyHandler)(nil)
25+
var (
26+
_ p2p.Handler = (*xsync.GetChangeProofHandler[*RangeProof, *ChangeProof])(nil)
27+
_ p2p.Handler = (*xsync.GetRangeProofHandler[*RangeProof, *ChangeProof])(nil)
28+
_ p2p.Handler = (*flakyHandler)(nil)
29+
)
2630

27-
func newDefaultDBConfig() merkledb.Config {
28-
return merkledb.Config{
31+
func newDefaultDBConfig() Config {
32+
return Config{
2933
IntermediateWriteBatchSize: 100,
30-
HistoryLength: defaultRequestKeyLimit,
31-
ValueNodeCacheSize: defaultRequestKeyLimit,
32-
IntermediateWriteBufferSize: defaultRequestKeyLimit,
33-
IntermediateNodeCacheSize: defaultRequestKeyLimit,
34+
HistoryLength: xsync.DefaultRequestKeyLimit,
35+
ValueNodeCacheSize: xsync.DefaultRequestKeyLimit,
36+
IntermediateWriteBufferSize: xsync.DefaultRequestKeyLimit,
37+
IntermediateNodeCacheSize: xsync.DefaultRequestKeyLimit,
3438
Reg: prometheus.NewRegistry(),
3539
Tracer: trace.Noop,
36-
BranchFactor: merkledb.BranchFactor16,
40+
BranchFactor: BranchFactor16,
3741
}
3842
}
3943

4044
func newFlakyRangeProofHandler(
4145
t *testing.T,
42-
db merkledb.MerkleDB,
43-
modifyResponse func(response *merkledb.RangeProof),
46+
db MerkleDB,
47+
modifyResponse func(response *RangeProof),
4448
) p2p.Handler {
45-
handler := NewGetRangeProofHandler(db)
49+
var (
50+
c = counter{m: 2}
51+
rangeProofMarshaler = rangeProofMarshaler
52+
handler = xsync.NewGetRangeProofHandler(db, rangeProofMarshaler)
53+
)
4654

47-
c := counter{m: 2}
4855
return &p2p.TestHandler{
4956
AppRequestF: func(ctx context.Context, nodeID ids.NodeID, deadline time.Time, requestBytes []byte) ([]byte, *common.AppError) {
5057
responseBytes, appErr := handler.AppRequest(ctx, nodeID, deadline, requestBytes)
5158
if appErr != nil {
5259
return nil, appErr
5360
}
5461

55-
proof := &merkledb.RangeProof{}
56-
require.NoError(t, proof.UnmarshalBinary(responseBytes))
62+
proof, err := rangeProofMarshaler.Unmarshal(responseBytes)
63+
require.NoError(t, err)
5764

5865
// Half of requests are modified
5966
if c.Inc() == 0 {
6067
modifyResponse(proof)
6168
}
6269

63-
responseBytes, err := proof.MarshalBinary()
70+
responseBytes, err = rangeProofMarshaler.Marshal(proof)
6471
if err != nil {
6572
return nil, &common.AppError{Code: 123, Message: err.Error()}
6673
}
@@ -72,15 +79,18 @@ func newFlakyRangeProofHandler(
7279

7380
func newFlakyChangeProofHandler(
7481
t *testing.T,
75-
db merkledb.MerkleDB,
76-
modifyResponse func(response *merkledb.ChangeProof),
82+
db MerkleDB,
83+
modifyResponse func(response *ChangeProof),
7784
) p2p.Handler {
78-
handler := NewGetChangeProofHandler(db)
85+
var (
86+
c = counter{m: 2}
87+
rangeProofMarshaler = rangeProofMarshaler
88+
changeProofMarshaler = changeProofMarshaler
89+
handler = xsync.NewGetChangeProofHandler(db, rangeProofMarshaler, changeProofMarshaler)
90+
)
7991

80-
c := counter{m: 2}
8192
return &p2p.TestHandler{
8293
AppRequestF: func(ctx context.Context, nodeID ids.NodeID, deadline time.Time, requestBytes []byte) ([]byte, *common.AppError) {
83-
var err error
8494
responseBytes, appErr := handler.AppRequest(ctx, nodeID, deadline, requestBytes)
8595
if appErr != nil {
8696
return nil, appErr
@@ -89,15 +99,15 @@ func newFlakyChangeProofHandler(
8999
response := &pb.GetChangeProofResponse{}
90100
require.NoError(t, proto.Unmarshal(responseBytes, response))
91101

92-
proof := &merkledb.ChangeProof{}
93-
require.NoError(t, proof.UnmarshalBinary(response.GetChangeProof()))
102+
proof, err := changeProofMarshaler.Unmarshal(response.GetChangeProof())
103+
require.NoError(t, err)
94104

95105
// Half of requests are modified
96106
if c.Inc() == 0 {
97107
modifyResponse(proof)
98108
}
99109

100-
proofBytes, err := proof.MarshalBinary()
110+
proofBytes, err := changeProofMarshaler.Marshal(proof)
101111
require.NoError(t, err)
102112
responseBytes, err = proto.Marshal(&pb.GetChangeProofResponse{
103113
Response: &pb.GetChangeProofResponse_ChangeProof{
@@ -142,14 +152,3 @@ func (c *counter) Inc() int {
142152
c.i++
143153
return result
144154
}
145-
146-
type waitingHandler struct {
147-
p2p.NoOpHandler
148-
handler p2p.Handler
149-
updatedRootChan chan struct{}
150-
}
151-
152-
func (w *waitingHandler) AppRequest(ctx context.Context, nodeID ids.NodeID, deadline time.Time, requestBytes []byte) ([]byte, *common.AppError) {
153-
<-w.updatedRootChan
154-
return w.handler.AppRequest(ctx, nodeID, deadline, requestBytes)
155-
}

x/merkledb/db.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import (
2424
"github.com/ava-labs/avalanchego/utils/set"
2525
"github.com/ava-labs/avalanchego/utils/units"
2626

27+
xsync "github.com/ava-labs/avalanchego/x/sync"
2728
oteltrace "go.opentelemetry.io/otel/trace"
2829
)
2930

@@ -37,7 +38,8 @@ const (
3738
)
3839

3940
var (
40-
_ MerkleDB = (*merkleDB)(nil)
41+
_ MerkleDB = (*merkleDB)(nil)
42+
_ xsync.DB[*RangeProof, *ChangeProof] = (MerkleDB)(nil)
4143

4244
metadataPrefix = []byte{0}
4345
valueNodePrefix = []byte{1}
@@ -69,13 +71,12 @@ type ChangeProofer interface {
6971
// GetChangeProof returns a proof for a subset of the key/value changes in key range
7072
// [start, end] that occurred between [startRootID] and [endRootID].
7173
// Returns at most [maxLength] key/value pairs.
72-
// Returns [ErrInsufficientHistory] if this node has insufficient history
74+
// Returns [xsync.ErrInsufficientHistory] if this node has insufficient history
7375
// to generate the proof.
7476
// Returns ErrEmptyProof if [endRootID] is ids.Empty.
7577
// Note that [endRootID] == ids.Empty means the trie is empty
7678
// (i.e. we don't need a change proof.)
77-
// Returns [ErrNoEndRoot], which wraps [ErrInsufficientHistory], if the
78-
// history doesn't contain the [endRootID].
79+
// Returns [xsync.ErrNoEndRoot], if the history doesn't contain the [endRootID].
7980
GetChangeProof(
8081
ctx context.Context,
8182
startRootID ids.ID,
@@ -123,6 +124,7 @@ type RangeProofer interface {
123124
// Returns ErrEmptyProof if [rootID] is ids.Empty.
124125
// Note that [rootID] == ids.Empty means the trie is empty
125126
// (i.e. we don't need a range proof.)
127+
// Returns [xsync.ErrNoEndRoot], if the history doesn't contain the [rootID].
126128
GetRangeProofAtRoot(
127129
ctx context.Context,
128130
rootID ids.ID,

0 commit comments

Comments
 (0)