This repository has been archived by the owner on Feb 20, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
blockstream.go
175 lines (141 loc) · 3.29 KB
/
blockstream.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
package blockstream
import (
"context"
"errors"
"sync"
access "github.com/Wondertan/go-libp2p-access"
blockstore "github.com/ipfs/go-ipfs-blockstore"
logging "github.com/ipfs/go-log/v2"
"github.com/libp2p/go-libp2p-core/host"
"github.com/libp2p/go-libp2p-core/network"
"github.com/libp2p/go-libp2p-core/peer"
"github.com/libp2p/go-libp2p-core/protocol"
"github.com/Wondertan/go-blockstream/block"
)
var ErrNoProviders = errors.New("blockstream: no providers")
var log = logging.Logger("blockstream")
const Protocol protocol.ID = "/blockstream/1.0.0"
const collectorsDefault = 32
type BlockStream struct {
ctx context.Context
Host host.Host
Granter access.Granter
Blocks blockstore.Blockstore
reqs chan *block.Request
collectors int
wg sync.WaitGroup
}
type Option func(plain *BlockStream)
func Collectors(c int) Option {
return func(bs *BlockStream) {
bs.collectors = c
}
}
func NewBlockStream(ctx context.Context, host host.Host, bstore blockstore.Blockstore, granter access.Granter, opts ...Option) *BlockStream {
bs := &BlockStream{
ctx: ctx,
Host: host,
Granter: granter,
Blocks: bstore,
reqs: make(chan *block.Request, 16),
collectors: collectorsDefault,
}
for _, opt := range opts {
opt(bs)
}
for range make([]bool, collectorsDefault) {
block.NewCollector(ctx, bs.reqs, bstore, maxMsgSize)
}
host.SetStreamHandler(Protocol, func(s network.Stream) {
err := bs.handler(s)
if err != nil {
log.Error(err)
s.Reset()
}
})
return bs
}
func (bs *BlockStream) Close() error {
bs.wg.Wait()
return nil
}
// Session starts new BlockStream session between current node and providing 'peers'.
func (bs *BlockStream) Session(ctx context.Context, peers []peer.ID, opts ...SessionOption) (*Session, error) {
ses := newSession(ctx, opts...)
if ses.offline {
return ses, nil
}
tkn, err := access.GetToken(ctx)
if err != nil {
return nil, err
}
for _, p := range peers {
s, err := bs.Host.NewStream(ctx, p, Protocol)
if err != nil {
return nil, err
}
err = giveHand(s, tkn)
if err != nil {
s.Reset()
return nil, err
}
var once sync.Once
ses.addProvider(s, func(f func() error) {
bs.wg.Add(1)
defer bs.wg.Done()
if err := f(); err != nil {
once.Do(func() {
s.Reset()
log.Errorf("Failed provider %s for session %s: %s", p.Pretty(), tkn, err)
if ses.removeProvider() == 0 {
log.Errorf("Terminating session %s: %s", tkn, err)
ses.err = ErrNoProviders
ses.cancel()
}
})
}
})
}
return ses, nil
}
func (bs *BlockStream) handler(s network.Stream) error {
p := s.Conn().RemotePeer()
var done chan<- error
_, err := takeHand(s, func(t access.Token) (err error) {
done, err = bs.Granter.Granted(t, p)
return
})
if err != nil {
return err
}
var once sync.Once
newResponder(bs.ctx, s, bs.reqs,
func(f func() error) {
bs.wg.Add(1)
defer bs.wg.Done()
err := f()
if err != nil {
log.Errorf("Failed to respond to peer %s: %s", p.Pretty(), err)
}
once.Do(func() {
if err != nil {
s.Reset()
done <- err
return
}
close(done)
})
},
)
return nil
}
type (
onToken func(access.Token) error
сlose func(func() error)
)
var logClose = func(f func() error) {
err := f()
if err != nil {
log.Error(err)
}
}