-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbroadcaster.go
161 lines (135 loc) · 3.32 KB
/
broadcaster.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
package tenvis
import (
"context"
"github.com/golang/protobuf/proto"
"github.com/golang/protobuf/ptypes/empty"
"github.com/xdarksome/scp"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
)
type messageChannel struct {
initial chan []*SCPMessage
messages chan *SCPMessage
done chan struct{}
}
func (s *messageChannel) Done() {
s.done <- struct{}{}
}
func newMessageChannel() messageChannel {
return messageChannel{
make(chan []*SCPMessage, 1),
make(chan *SCPMessage, 100),
make(chan struct{}),
}
}
type Broadcaster struct {
key PrivateKey
storage Storage
input chan *scp.Message
output map[chan struct{}]messageChannel
buffer []*SCPMessage
queue chan messageChannel
}
func NewBroadcaster(key PrivateKey, storage Storage) *Broadcaster {
return &Broadcaster{
key: key,
storage: storage,
input: make(chan *scp.Message, 1000000),
output: make(map[chan struct{}]messageChannel),
queue: make(chan messageChannel),
}
}
func (b *Broadcaster) broadcast(m *SCPMessage) {
message := &SCPMessage{
Type: SCPMessage_Type(m.Type),
SlotIndex: m.SlotIndex,
Counter: m.Counter,
Value: m.Value,
}
for done, ch := range b.output {
select {
case <-done:
delete(b.output, done)
default:
ch.messages <- message
}
}
}
func (b *Broadcaster) appendBuffer(m *SCPMessage) {
if b.buffer == nil || b.buffer[len(b.buffer)-1].SlotIndex != m.SlotIndex {
b.buffer = []*SCPMessage{m}
return
}
b.buffer = append(b.buffer, m)
}
func (b *Broadcaster) run() {
for {
select {
case m := <-b.input:
message := &SCPMessage{
Type: SCPMessage_Type(m.Type),
SlotIndex: m.SlotIndex,
Counter: m.Counter,
Value: m.Value,
}
b.appendBuffer(message)
b.broadcast(message)
case ch := <-b.queue:
b.output[ch.done] = ch
ch.initial <- b.buffer
}
}
}
func (b *Broadcaster) newStream() messageChannel {
s := newMessageChannel()
b.queue <- s
return s
}
func (b *Broadcaster) sendMessages(stream Tenvis_StreamSCPMessagesServer, messages ...*SCPMessage) error {
m := &SCPMessages{
List: messages,
}
bytes, err := proto.Marshal(m)
if err != nil {
return status.Error(codes.Internal, "failed to proto marshal output")
}
m.Signature = b.key.Sign(bytes)
if err := stream.Send(m); err != nil {
return status.Error(codes.Internal, "failed to send output")
}
return nil
}
func (b *Broadcaster) StreamSCPMessages(req *empty.Empty, stream Tenvis_StreamSCPMessagesServer) error {
ch := b.newStream()
defer ch.Done()
init := <-ch.initial
if init != nil {
if err := b.sendMessages(stream, init...); err != nil {
return err
}
}
for {
m := <-ch.messages
if err := b.sendMessages(stream, m); err != nil {
return err
}
}
}
func (b *Broadcaster) GetLedger(ctx context.Context, req *GetLedgerRequest) (*LedgerMessage, error) {
ledger, err := b.storage.GetLedger(req.Index)
if err != nil {
return new(LedgerMessage), status.Error(codes.Internal, "failed to get ledger from redis")
}
if ledger == nil {
return new(LedgerMessage), status.Error(codes.NotFound, "ledger not found")
}
message := &LedgerMessage{
Ledger: ledger,
}
bytes, err := proto.Marshal(message)
if err != nil {
return new(LedgerMessage), status.Error(codes.Internal, "failed to proto marshal")
}
message.Signature = b.key.Sign(bytes)
return message, nil
}