-
Notifications
You must be signed in to change notification settings - Fork 12
/
server_commitments.go
192 lines (151 loc) · 5.79 KB
/
server_commitments.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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
package inx
import (
"context"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"github.com/iotaledger/hive.go/ierrors"
"github.com/iotaledger/hive.go/kvstore"
"github.com/iotaledger/hive.go/runtime/event"
"github.com/iotaledger/hive.go/runtime/workerpool"
inx "github.com/iotaledger/inx/go"
"github.com/iotaledger/iota-core/pkg/model"
iotago "github.com/iotaledger/iota.go/v4"
)
func inxCommitment(commitment *model.Commitment) *inx.Commitment {
if commitment == nil {
return nil
}
return &inx.Commitment{
CommitmentId: inx.NewCommitmentId(commitment.ID()),
Commitment: &inx.RawCommitment{
Data: commitment.Data(),
},
}
}
func (s *Server) ListenToCommitments(req *inx.SlotRangeRequest, srv inx.INX_ListenToCommitmentsServer) error {
createCommitmentPayloadForSlotAndSend := func(slot iotago.SlotIndex) error {
commitment, err := deps.Protocol.Engines.Main.Get().Storage.Commitments().Load(slot)
if err != nil {
if ierrors.Is(err, kvstore.ErrKeyNotFound) {
return status.Errorf(codes.NotFound, "commitment slot %d not found", slot)
}
return status.Errorf(codes.Internal, "failed to load commitment for slot %d: %s", slot, err.Error())
}
if err := srv.Send(inxCommitment(commitment)); err != nil {
return ierrors.Wrap(err, "send error")
}
return nil
}
sendSlotsRange := func(startSlot iotago.SlotIndex, endSlot iotago.SlotIndex) error {
for currentSlot := startSlot; currentSlot <= endSlot; currentSlot++ {
if err := createCommitmentPayloadForSlotAndSend(currentSlot); err != nil {
return err
}
}
return nil
}
// if a startSlot is given, we send all available commitments including the start slot.
// if an endSlot is given, we send all available commitments up to and including min(latestCommitmentSlot, endSlot).
// if no startSlot is given, but an endSlot, we don't send previous commitments.
sendPreviousSlots := func(startSlot iotago.SlotIndex, endSlot iotago.SlotIndex) (iotago.SlotIndex, error) {
if startSlot == 0 {
// no need to send previous commitments
return 0, nil
}
latestCommitment := deps.Protocol.Engines.Main.Get().SyncManager.LatestCommitment()
if startSlot > latestCommitment.Slot() {
// no need to send previous commitments
return 0, nil
}
// Stream all available commitments first
prunedEpoch, hasPruned := deps.Protocol.Engines.Main.Get().SyncManager.LastPrunedEpoch()
if hasPruned && startSlot <= deps.Protocol.CommittedAPI().TimeProvider().EpochEnd(prunedEpoch) {
return 0, status.Errorf(codes.InvalidArgument, "given startSlot %d is older than the current pruningSlot %d", startSlot, deps.Protocol.CommittedAPI().TimeProvider().EpochEnd(prunedEpoch))
}
if endSlot == 0 || endSlot > latestCommitment.Slot() {
endSlot = latestCommitment.Slot()
}
if err := sendSlotsRange(startSlot, endSlot); err != nil {
return 0, err
}
return endSlot, nil
}
stream := &streamRange{
start: iotago.SlotIndex(req.GetStartSlot()),
end: iotago.SlotIndex(req.GetEndSlot()),
}
var err error
stream.lastSent, err = sendPreviousSlots(stream.start, stream.end)
if err != nil {
return err
}
if stream.isBounded() && stream.lastSent >= stream.end {
// We are done sending, so close the stream
return nil
}
catchUpFunc := func(start iotago.SlotIndex, end iotago.SlotIndex) error {
err := sendSlotsRange(start, end)
if err != nil {
err := ierrors.Wrap(err, "sendSlotsRange error")
Component.LogError(err.Error())
return err
}
return nil
}
sendFunc := func(_ iotago.SlotIndex, payload *inx.Commitment) error {
if err := srv.Send(payload); err != nil {
err := ierrors.Wrap(err, "send error")
Component.LogError(err.Error())
return err
}
return nil
}
var innerErr error
ctx, cancel := context.WithCancel(Component.Daemon().ContextStopped())
wp := workerpool.New("ListenToCommitments", workerpool.WithWorkerCount(workerCount)).Start()
unhook := deps.Protocol.Events.Engine.Notarization.LatestCommitmentUpdated.Hook(func(commitment *model.Commitment) {
done, err := handleRangedSend1(commitment.Slot(), inxCommitment(commitment), stream, catchUpFunc, sendFunc)
switch {
case err != nil:
innerErr = err
cancel()
case done:
cancel()
}
}, event.WithWorkerPool(wp)).Unhook
<-ctx.Done()
unhook()
// We need to wait until all tasks are done, otherwise we might call
// "SendMsg" and "CloseSend" in parallel on the grpc stream, which is
// not safe according to the grpc docs.
wp.Shutdown()
wp.ShutdownComplete.Wait()
return innerErr
}
func (s *Server) ForceCommitUntil(_ context.Context, slot *inx.SlotRequest) (*inx.NoParams, error) {
err := deps.Protocol.Engines.Main.Get().Notarization.ForceCommitUntil(slot.Unwrap())
if err != nil {
return nil, ierrors.Wrapf(err, "error while performing force commit until %d", slot.GetSlot())
}
return &inx.NoParams{}, nil
}
func (s *Server) ReadCommitment(_ context.Context, req *inx.CommitmentRequest) (*inx.Commitment, error) {
commitmentSlot := iotago.SlotIndex(req.GetCommitmentSlot())
if req.GetCommitmentId() != nil {
commitmentSlot = req.GetCommitmentId().Unwrap().Slot()
}
commitment, err := deps.Protocol.Engines.Main.Get().Storage.Commitments().Load(commitmentSlot)
if err != nil {
if ierrors.Is(err, kvstore.ErrKeyNotFound) {
return nil, status.Errorf(codes.NotFound, "commitment slot %d not found", req.GetCommitmentSlot())
}
return nil, status.Errorf(codes.Internal, "failed to load commitment for slot %d: %s", commitmentSlot, err.Error())
}
if req.GetCommitmentId() != nil {
// If it was requested by id, make sure the id matches the commitment.
if commitment.ID() != req.GetCommitmentId().Unwrap() {
return nil, status.Errorf(codes.NotFound, "commitment id %s not found, found %s instead", req.GetCommitmentId().Unwrap(), commitment.ID())
}
}
return inxCommitment(commitment), nil
}