reconnect.go raw
1 //go:build js && wasm
2
3 package main
4
5 import (
6 "context"
7
8 "git.smesh.lol/nostrino/pkg/chaindata"
9 "git.smesh.lol/nostrino/pkg/chainsource"
10 )
11
12 // resilient is a chain source that survives losing the tunnel.
13 //
14 // A header sync is a long conversation over a relay, and the transport underneath it is a
15 // browser WebSocket to a public relay: it can go away between two requests for reasons that have
16 // nothing to do with the chain. The browser store keeps what was verified, so the honest response
17 // to a dead connection is to open a new one and ask again — not to fail the wallet's whole sync
18 // because a frame arrived late.
19 //
20 // One retry per request, on a fresh client: a second failure is reported to the wallet, which
21 // shows it and tries again from its own side.
22 // The wrapper must expose every optional capability the adapter underneath it has. A wrapper that
23 // only satisfies the interface it was written for silently takes capabilities away from the wallet.
24 var _ interface {
25 Prepare(chaindata.Context) (chaindata.Position, error)
26 } = (*resilient)(nil)
27
28 type resilient struct {
29 connect func(context.Context) (*chainsource.Nostrino, error)
30 close func()
31 source *chainsource.Nostrino
32 }
33
34 func newResilient(connect func(context.Context) (*chainsource.Nostrino, error), close func()) (*resilient, error) {
35 r := &resilient{connect: connect, close: close}
36
37 if _, err := r.client(context.Background()); err != nil {
38 return nil, err
39 }
40
41 return r, nil
42 }
43
44 func (r *resilient) client(ctx context.Context) (*chainsource.Nostrino, error) {
45 if r.source != nil {
46 return r.source, nil
47 }
48
49 source, err := r.connect(ctx)
50
51 if err != nil {
52 return nil, err
53 }
54
55 r.source = source
56
57 return source, nil
58 }
59
60 // reset drops the dead client so the next request builds a new one.
61 func (r *resilient) reset() {
62 if r.close != nil {
63 r.close()
64 r.close = nil
65 }
66
67 r.source = nil
68 }
69
70 func (r *resilient) run(ctx chaindata.Context, call func(*chainsource.Nostrino) error) error {
71 source, err := r.client(context.Background())
72
73 if err == nil {
74 err = call(source)
75
76 if err == nil {
77 return nil
78 }
79 }
80
81 // One reconnect, then the wallet hears the truth.
82 r.reset()
83
84 source, again := r.client(context.Background())
85
86 if again != nil {
87 return again
88 }
89
90 return call(source)
91 }
92
93 func (r *resilient) Position(ctx chaindata.Context) (chaindata.Position, error) {
94 var out chaindata.Position
95
96 err := r.run(ctx, func(s *chainsource.Nostrino) error {
97 position, err := s.Position(ctx)
98 out = position
99
100 return err
101 })
102
103 return out, err
104 }
105
106 func (r *resilient) Sync(ctx chaindata.Context, from uint32) (chaindata.Position, error) {
107 var out chaindata.Position
108
109 err := r.run(ctx, func(s *chainsource.Nostrino) error {
110 position, err := s.Sync(ctx, from)
111 out = position
112
113 return err
114 })
115
116 return out, err
117 }
118
119 func (r *resilient) Match(ctx chaindata.Context, scripts [][]byte) ([]uint32, error) {
120 var out []uint32
121
122 err := r.run(ctx, func(s *chainsource.Nostrino) error {
123 heights, err := s.Match(ctx, scripts)
124 out = heights
125
126 return err
127 })
128
129 return out, err
130 }
131
132 func (r *resilient) Block(ctx chaindata.Context, height uint32) ([]byte, error) {
133 var out []byte
134
135 err := r.run(ctx, func(s *chainsource.Nostrino) error {
136 block, err := s.Block(ctx, height)
137 out = block
138
139 return err
140 })
141
142 return out, err
143 }
144
145 func (r *resilient) Broadcast(ctx chaindata.Context, raw []byte) error {
146 return r.run(ctx, func(s *chainsource.Nostrino) error {
147 return s.Broadcast(ctx, raw)
148 })
149 }
150
151 func (r *resilient) Filters(ctx chaindata.Context, from, count uint32) ([]chaindata.Filter, error) {
152 var out []chaindata.Filter
153
154 err := r.run(ctx, func(s *chainsource.Nostrino) error {
155 filters, err := s.Filters(ctx, from, count)
156 out = filters
157
158 return err
159 })
160
161 return out, err
162 }
163
164 func (r *resilient) Backup(ctx chaindata.Context, kind uint8, from, maxBytes uint32) (chaindata.Chunk, error) {
165 var out chaindata.Chunk
166
167 err := r.run(ctx, func(s *chainsource.Nostrino) error {
168 chunk, err := s.Backup(ctx, kind, from, maxBytes)
169 out = chunk
170
171 return err
172 })
173
174 return out, err
175 }
176
177 func (r *resilient) Restore(ctx chaindata.Context, raw []byte) (int, error) {
178 var out int
179
180 err := r.run(ctx, func(s *chainsource.Nostrino) error {
181 count, err := s.Restore(ctx, raw)
182 out = count
183
184 return err
185 })
186
187 return out, err
188 }
189
190 // Prepare downloads the chain's headers and filter headers before the first sync. It is an
191 // optional capability of a chain source, discovered by a type assertion in chaindata, so a wrapper
192 // that forgets it turns every wallet into "this chain source cannot prepare" — the wrapper has to
193 // forward the capability, not only the interface it was written for.
194 func (r *resilient) Prepare(ctx chaindata.Context) (chaindata.Position, error) {
195 var out chaindata.Position
196
197 err := r.run(ctx, func(s *chainsource.Nostrino) error {
198 position, err := s.Prepare(ctx)
199 out = position
200
201 return err
202 })
203
204 return out, err
205 }
206