-
Notifications
You must be signed in to change notification settings - Fork 648
/
request.go
100 lines (86 loc) · 2.61 KB
/
request.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
// Copyright (C) 2019-2023, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package sync
import (
"context"
"encoding/hex"
"fmt"
"github.com/ava-labs/avalanchego/ids"
)
var (
_ Request = (*RangeProofRequest)(nil)
_ Request = (*ChangeProofRequest)(nil)
)
// A request to this node for a proof.
type Request interface {
fmt.Stringer
Handle(ctx context.Context, nodeID ids.NodeID, requestID uint32, h Handler) error
}
type rangeProofHandler interface {
// Generates a range proof and sends it to [nodeID].
// TODO danlaine how should we handle context cancellation?
HandleRangeProofRequest(
ctx context.Context,
nodeID ids.NodeID,
requestID uint32,
request *RangeProofRequest,
) error
}
type changeProofHandler interface {
// Generates a change proof and sends it to [nodeID].
// TODO danlaine how should we handle context cancellation?
HandleChangeProofRequest(
ctx context.Context,
nodeID ids.NodeID,
requestID uint32,
request *ChangeProofRequest,
) error
}
type Handler interface {
rangeProofHandler
changeProofHandler
}
type RangeProofRequest struct {
Root ids.ID `serialize:"true"`
Start []byte `serialize:"true"`
End []byte `serialize:"true"`
KeyLimit uint16 `serialize:"true"`
BytesLimit uint32 `serialize:"true"`
}
func (r *RangeProofRequest) Handle(ctx context.Context, nodeID ids.NodeID, requestID uint32, h Handler) error {
return h.HandleRangeProofRequest(ctx, nodeID, requestID, r)
}
func (r RangeProofRequest) String() string {
return fmt.Sprintf(
"RangeProofRequest(Root=%s, Start=%s, End=%s, KeyLimit=%d, BytesLimit=%d)",
r.Root,
hex.EncodeToString(r.Start),
hex.EncodeToString(r.End),
r.KeyLimit,
r.BytesLimit,
)
}
// ChangeProofRequest is a request to receive trie leaves at specified Root within Start and End byte range
// Limit outlines maximum number of leaves to returns starting at Start
type ChangeProofRequest struct {
StartingRoot ids.ID `serialize:"true"`
EndingRoot ids.ID `serialize:"true"`
Start []byte `serialize:"true"`
End []byte `serialize:"true"`
KeyLimit uint16 `serialize:"true"`
BytesLimit uint32 `serialize:"true"`
}
func (r *ChangeProofRequest) Handle(ctx context.Context, nodeID ids.NodeID, requestID uint32, h Handler) error {
return h.HandleChangeProofRequest(ctx, nodeID, requestID, r)
}
func (r ChangeProofRequest) String() string {
return fmt.Sprintf(
"ChangeProofRequest(StartRoot=%s, EndRoot=%s, Start=%s, End=%s, KeyLimit=%d, BytesLimit=%d)",
r.StartingRoot,
r.EndingRoot,
hex.EncodeToString(r.Start),
hex.EncodeToString(r.End),
r.KeyLimit,
r.BytesLimit,
)
}