forked from proofxyz/solgo
/
firehose.go
194 lines (168 loc) · 5.32 KB
/
firehose.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
193
194
// Package firehose implements simplified APIs for accessing the StreamingFast
// Firehose gRPC service. See https://firehose.streamingfast.io/
package firehose
import (
"context"
"crypto/tls"
"crypto/x509"
"fmt"
"io"
"time"
"golang.org/x/oauth2"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials"
"google.golang.org/grpc/credentials/oauth"
"github.com/cxkoda/solgo/go/oauthsrc"
sfethpb "github.com/streamingfast/firehose-ethereum/types/pb/sf/ethereum/type/v2"
hosepb "github.com/streamingfast/pbgo/sf/firehose/v2"
solpb "github.com/streamingfast/sf-solana/types/pb/sf/solana/type/v2"
)
// Production URLs for the hosted Firehose service.
const (
TokenURL = "https://auth.dfuse.io/v1/auth/issue"
ETHMainnetURL = "mainnet.eth.streamingfast.io:443"
ETHGoerliURL = "goerli.eth.streamingfast.io:443"
)
// BlockProto is a type constraint limited to the types of blocks supported by
// Firehose.
type BlockProto interface {
*sfethpb.Block | *solpb.Block
}
// A Proxy provides access to the Firehose service through a modified API.
type Proxy[B BlockProto] struct {
conn *grpc.ClientConn
client hosepb.StreamClient
}
// Close closes the underlying connection to the Firehose service.
func (p *Proxy[_]) Close() error {
return p.conn.Close()
}
// DialETHMainnet is equivalent to Dial() with URLs to connect to the Ethereum
// Mainnet endpoint and sfethpb.Block as the type argument.
func DialETHMainnet(ctx context.Context, apiKey string, opts ...grpc.DialOption) (*Proxy[*sfethpb.Block], error) {
return Dial[*sfethpb.Block](ctx, ETHMainnetURL, TokenURL, apiKey, opts...)
}
// DialETHGoerli is equivalent to Dial() with URLs to connect to the Ethereum
// Goerli endpoint and sfethpb.Block as the type argument.
func DialETHGoerli(ctx context.Context, apiKey string, opts ...grpc.DialOption) (*Proxy[*sfethpb.Block], error) {
return Dial[*sfethpb.Block](ctx, ETHGoerliURL, TokenURL, apiKey, opts...)
}
// Dial dials the gRPC endpoint with tokens obtained from the token URL. If no
// gRPC DialOptions are provided then DefaultDialOptions(…) is used.
func Dial[B BlockProto](ctx context.Context, endpointURL, tokenURL, apiKey string, opts ...grpc.DialOption) (*Proxy[B], error) {
if len(opts) == 0 {
o, err := DefaultDialOptions(tokenURL, apiKey)
if err != nil {
return nil, err
}
opts = o
}
conn, err := grpc.DialContext(ctx, endpointURL, opts...)
if err != nil {
return nil, fmt.Errorf("grpc.DialContext(%q): %v", endpointURL, err)
}
return &Proxy[B]{
conn: conn,
client: hosepb.NewStreamClient(conn),
}, nil
}
// DefaultDialOptions returns gRPC DialOptions suitable for calling production
// Firehose servers as run by StreamingFast.
func DefaultDialOptions(tokenURL, apiKey string) ([]grpc.DialOption, error) {
roots, err := x509.SystemCertPool()
if err != nil {
return nil, fmt.Errorf("x509.SystemCertPool(): %v", err)
}
return []grpc.DialOption{
grpc.WithTransportCredentials(credentials.NewTLS(&tls.Config{
RootCAs: roots,
})),
grpc.WithPerRPCCredentials(oauth.TokenSource{
TokenSource: oauth2.ReuseTokenSource(
nil,
oauthsrc.NewHTTP(
"POST", tokenURL, fmt.Sprintf(`{"api_key":%q}`, apiKey), &dfuseToken{},
),
),
}),
}, nil
}
// dfuseToken represents the JSON token returned by the dfuse auth server.
type dfuseToken struct {
Payload string `json:"token"`
ExpiresAt int64 `json:"expires_at"`
}
// AsToken returns the dfuseToken as an oauth2.Token.
func (t *dfuseToken) AsToken() *oauth2.Token {
return &oauth2.Token{
AccessToken: t.Payload,
TokenType: "Bearer",
Expiry: time.Unix(t.ExpiresAt, 0),
}
}
// Blocks queries propagates the request to the Proxy's underlying client.
func (p *Proxy[B]) Blocks(ctx context.Context, req *hosepb.Request, opts ...grpc.CallOption) (*Blocks[B], error) {
stream, err := p.client.Blocks(ctx, req, opts...)
if err != nil {
return nil, fmt.Errorf("%T.Blocks(%+v): %v", p.client, req, err)
}
blocks := make(chan Block[B])
ret := &Blocks[B]{
C: blocks,
quit: make(chan struct{}),
}
go func() (retErr error) {
defer func() {
ret.err = retErr
close(blocks)
}()
for {
resp, err := stream.Recv()
if err == io.EOF {
return nil
}
if err != nil {
return fmt.Errorf("%T.Recv(): %v", stream, err)
}
pb, err := resp.Block.UnmarshalNew()
if err != nil {
return fmt.Errorf("%T.Block.UnmarshalNew(): %v", resp, err)
}
block, ok := pb.(B)
if !ok {
return fmt.Errorf("%T.Block.UnmarshalNew() got %T; want %T", resp, pb, block)
}
select {
case blocks <- Block[B]{Response: resp, Block: block}:
case <-ret.quit:
return nil
case <-ctx.Done():
return ctx.Err()
}
}
}()
return ret, nil
}
// A Blocks stream provides a channel of blocks.
type Blocks[B BlockProto] struct {
// C is a channel on which new Blocks are sent. It is closed when either the
// stream client ends or an error occurs.
C <-chan Block[B]
quit chan struct{}
err error
}
// An Block couples a Firehose response with an unmarshalled block of concrete
// type.
type Block[B BlockProto] struct {
Response *hosepb.Response
Block B
}
// Err returns any error that occurred during streaming. It SHOULD be called
// after C is closed.
func (b *Blocks[_]) Err() error {
return b.err
}
// Close MUST be called to release resources used by the block streamer.
func (b *Blocks[_]) Close() {
close(b.quit)
}