forked from gtfierro/wave
-
Notifications
You must be signed in to change notification settings - Fork 2
/
server.go
127 lines (118 loc) · 2.77 KB
/
server.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
package main
import (
"context"
"sync"
"github.com/gogo/protobuf/proto"
"github.com/google/trillian"
"github.com/immesys/wave/storage/vldmstorage3/vldmpb"
)
type api struct {
submu sync.Mutex
subs []*mrsubscription
}
type mrsubscription struct {
Ch chan *trillian.SignedMapRoot
Ctx context.Context
}
var API *api
func init() {
API = &api{}
}
func (a *api) SubmitMapRoot(mr *trillian.SignedMapRoot) {
newsubs := []*mrsubscription{}
a.submu.Lock()
for _, sub := range a.subs {
if sub.Ctx.Err() != nil {
continue
}
sub.Ch <- mr
newsubs = append(newsubs, sub)
}
a.subs = newsubs
a.submu.Unlock()
}
func (a *api) SubscribeMapHeads(p *vldmpb.SubscribeParams, r vldmpb.VLDM_SubscribeMapHeadsServer) error {
ch := make(chan *trillian.SignedMapRoot, 100)
a.submu.Lock()
a.subs = append(a.subs, &mrsubscription{
Ch: ch,
Ctx: r.Context(),
})
a.submu.Unlock()
for {
select {
case mh := <-ch:
ba, err := proto.Marshal(mh)
if err != nil {
panic(err)
}
err = r.Send(&vldmpb.MapHead{
TrillianSignedMapRoot: ba,
})
if err != nil {
return err
}
case <-r.Context().Done():
return r.Context().Err()
}
}
}
func (a *api) GetLogHead(ctx context.Context, p *vldmpb.GetLogHeadParams) (*vldmpb.GetLogHeadResponse, error) {
logId = TreeID_Root
if p.IsOperation {
logId = TreeID_Op
}
resp, err := logclient.GetLatestSignedLogRoot(ctx, &trillian.GetLatestSignedLogRootRequest{
LogId: logId,
})
if err != nil {
return nil, err
}
ba, err := proto.Marshal(resp.SignedLogRoot)
if err != nil {
panic(err)
}
return &vldmpb.GetLogHeadResponse{TrillianSignedLogRoot: ba}, nil
}
func (a *api) GetLogItem(ctx context.Context, p *vldmpb.GetLogItemParams) (*vldmpb.GetLogItemResponse, error) {
logId = TreeID_Root
if p.IsOperation {
logId = TreeID_Op
}
resp, err := logclient.GetEntryAndProof(ctx, &trillian.GetEntryAndProofRequest{
LogId: logId,
LeafIndex: p.Index,
TreeSize: p.Size,
})
if err != nil {
return nil, err
}
ba, err := proto.Marshal(resp)
if err != nil {
panic(err)
}
return &vldmpb.GetLogItemResponse{TrillianGetEntryAndProofResponse: ba}, nil
}
func (a *api) GetLogConsistency(ctx context.Context, p *vldmpb.GetConsistencyParams) (*vldmpb.GetConsistencyResponse, error) {
logId = TreeID_Root
if p.IsOperation {
logId = TreeID_Op
}
resp, err := logclient.GetConsistencyProof(ctx, &trillian.GetConsistencyProofRequest{
LogId: logId,
FirstTreeSize: p.From,
SecondTreeSize: p.To,
})
if err != nil {
return nil, err
}
ba, err := proto.Marshal(resp.Proof)
if err != nil {
panic(err)
}
return &vldmpb.GetConsistencyResponse{TrillianProof: ba}, nil
}
func (a *api) SubmitSignedMapHead(ctx context.Context, p *vldmpb.SubmitParams) (*vldmpb.SubmitResponse, error) {
//TODO
return nil, nil
}