-
Notifications
You must be signed in to change notification settings - Fork 17
/
storage.go
233 lines (219 loc) · 6.88 KB
/
storage.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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
package main
import (
"bytes"
"context"
"encoding/binary"
"errors"
"fmt"
"strings"
bin "github.com/gagliardetto/binary"
"github.com/gagliardetto/solana-go"
"github.com/ipfs/go-cid"
"github.com/ipld/go-car/util"
carv2 "github.com/ipld/go-car/v2"
cidlink "github.com/ipld/go-ipld-prime/linking/cid"
"github.com/rpcpool/yellowstone-faithful/ipld/ipldbindcode"
solanatxmetaparsers "github.com/rpcpool/yellowstone-faithful/solana-tx-meta-parsers"
"golang.org/x/exp/mmap"
"k8s.io/klog/v2"
)
// openIndexStorage open a compactindex from a local file, or from a remote URL.
// Supported protocols are:
// - http://
// - https://
func openIndexStorage(
ctx context.Context,
where string,
debug bool,
) (ReaderAtCloser, error) {
where = strings.TrimSpace(where)
if strings.HasPrefix(where, "http://") || strings.HasPrefix(where, "https://") {
klog.Infof("opening index file from %q as HTTP remote file", where)
rac, err := remoteHTTPFileAsIoReaderAt(ctx, where)
if err != nil {
return nil, fmt.Errorf("failed to open remote index file: %w", err)
}
if !debug {
return rac, nil
}
return &readCloserWrapper{
rac: rac,
name: where,
isRemote: true,
}, nil
}
// TODO: add support for IPFS gateways.
// TODO: add support for Filecoin gateways.
rac, err := mmap.Open(where)
if err != nil {
return nil, fmt.Errorf("failed to open local index file: %w", err)
}
if !debug {
return rac, nil
}
return &readCloserWrapper{
rac: rac,
name: where,
isRemote: false,
}, nil
}
func openCarStorage(ctx context.Context, where string) (*carv2.Reader, ReaderAtCloser, error) {
where = strings.TrimSpace(where)
if strings.HasPrefix(where, "http://") || strings.HasPrefix(where, "https://") {
klog.Infof("opening CAR file from %q as HTTP remote file", where)
rem, err := remoteHTTPFileAsIoReaderAt(ctx, where)
if err != nil {
return nil, nil, fmt.Errorf("failed to open remote CAR file: %w", err)
}
return nil, &readCloserWrapper{
rac: rem,
name: where,
}, nil
}
// TODO: add support for IPFS gateways.
// TODO: add support for Filecoin gateways.
carReader, err := carv2.OpenReader(where)
if err != nil {
return nil, nil, fmt.Errorf("failed to open CAR file: %w", err)
}
return carReader, nil, nil
}
func readSectionFromReaderAt(reader ReaderAtCloser, offset uint64, length uint64) ([]byte, error) {
data := make([]byte, length)
_, err := reader.ReadAt(data, int64(offset))
if err != nil {
return nil, err
}
return data, nil
}
func readNodeFromReaderAt(reader ReaderAtCloser, wantedCid cid.Cid, offset uint64) ([]byte, error) {
// read MaxVarintLen64 bytes
lenBuf := make([]byte, binary.MaxVarintLen64)
_, err := reader.ReadAt(lenBuf, int64(offset))
if err != nil {
return nil, err
}
// read uvarint
dataLen, n := binary.Uvarint(lenBuf)
offset += uint64(n)
if dataLen > uint64(util.MaxAllowedSectionSize) { // Don't OOM
return nil, errors.New("malformed car; header is bigger than util.MaxAllowedSectionSize")
}
data := make([]byte, dataLen)
_, err = reader.ReadAt(data, int64(offset))
if err != nil {
return nil, err
}
n, gotCid, err := cid.CidFromReader(bytes.NewReader(data))
if err != nil {
return nil, err
}
// verify that the CID we read matches the one we expected.
if !gotCid.Equals(wantedCid) {
return nil, fmt.Errorf("CID mismatch: expected %s, got %s", wantedCid, gotCid)
}
return data[n:], nil
}
type GetBlockResponse struct {
BlockHeight *uint64 `json:"blockHeight"`
BlockTime *uint64 `json:"blockTime"`
Blockhash string `json:"blockhash"`
ParentSlot uint64 `json:"parentSlot"`
PreviousBlockhash *string `json:"previousBlockhash"`
Rewards any `json:"rewards"` // TODO: use same format as solana
Transactions []GetTransactionResponse `json:"transactions"`
}
type GetTransactionResponse struct {
// TODO: use same format as solana
Blocktime *uint64 `json:"blockTime,omitempty"`
Meta any `json:"meta"`
Slot *uint64 `json:"slot,omitempty"`
Transaction any `json:"transaction"`
Version any `json:"version"`
Position uint64 `json:"-"` // TODO: enable this
Signatures []solana.Signature `json:"-"` // TODO: enable this
}
func loadDataFromDataFrames(
firstDataFrame *ipldbindcode.DataFrame,
dataFrameGetter func(ctx context.Context, wantedCid cid.Cid) (*ipldbindcode.DataFrame, error),
) ([]byte, error) {
dataBuffer := new(bytes.Buffer)
allFrames, err := getAllFramesFromDataFrame(firstDataFrame, dataFrameGetter)
if err != nil {
return nil, err
}
for _, frame := range allFrames {
dataBuffer.Write(frame.Bytes())
}
// verify the data hash (if present)
bufHash, ok := firstDataFrame.GetHash()
if !ok {
return dataBuffer.Bytes(), nil
}
err = ipldbindcode.VerifyHash(dataBuffer.Bytes(), bufHash)
if err != nil {
return nil, err
}
return dataBuffer.Bytes(), nil
}
func getAllFramesFromDataFrame(
firstDataFrame *ipldbindcode.DataFrame,
dataFrameGetter func(ctx context.Context, wantedCid cid.Cid) (*ipldbindcode.DataFrame, error),
) ([]*ipldbindcode.DataFrame, error) {
frames := []*ipldbindcode.DataFrame{firstDataFrame}
// get the next data frames
next, ok := firstDataFrame.GetNext()
if !ok || len(next) == 0 {
return frames, nil
}
for _, cid := range next {
nextDataFrame, err := dataFrameGetter(context.Background(), cid.(cidlink.Link).Cid)
if err != nil {
return nil, err
}
nextFrames, err := getAllFramesFromDataFrame(nextDataFrame, dataFrameGetter)
if err != nil {
return nil, err
}
frames = append(frames, nextFrames...)
}
return frames, nil
}
func parseTransactionAndMetaFromNode(
transactionNode *ipldbindcode.Transaction,
dataFrameGetter func(ctx context.Context, wantedCid cid.Cid) (*ipldbindcode.DataFrame, error),
) (tx solana.Transaction, meta any, _ error) {
{
transactionBuffer, err := loadDataFromDataFrames(&transactionNode.Data, dataFrameGetter)
if err != nil {
return solana.Transaction{}, nil, err
}
if err := bin.UnmarshalBin(&tx, transactionBuffer); err != nil {
klog.Errorf("failed to unmarshal transaction: %v", err)
return solana.Transaction{}, nil, err
} else if len(tx.Signatures) == 0 {
klog.Errorf("transaction has no signatures")
return solana.Transaction{}, nil, err
}
}
{
metaBuffer, err := loadDataFromDataFrames(&transactionNode.Metadata, dataFrameGetter)
if err != nil {
return solana.Transaction{}, nil, err
}
if len(metaBuffer) > 0 {
uncompressedMeta, err := decompressZstd(metaBuffer)
if err != nil {
klog.Errorf("failed to decompress metadata: %v", err)
return
}
status, err := solanatxmetaparsers.ParseAnyTransactionStatusMeta(uncompressedMeta)
if err != nil {
klog.Errorf("failed to parse metadata: %v", err)
return
}
meta = status
}
}
return
}