-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathmanifest.go
295 lines (262 loc) · 7.92 KB
/
manifest.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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
package arseeding
import (
"context"
"encoding/json"
"errors"
"fmt"
"github.com/everFinance/arseeding/argraphql"
"github.com/everFinance/goar"
"gopkg.in/h2non/gentleman.v2"
"io"
"net/http"
"os"
"strings"
"github.com/everFinance/arseeding/schema"
"github.com/everFinance/goar/types"
"github.com/everFinance/goar/utils"
)
func handleManifest(maniData []byte, path string, db *Store) ([]types.Tag, []byte, error) {
mani := schema.ManifestData{}
if err := json.Unmarshal(maniData, &mani); err != nil {
return nil, nil, err
}
path = strings.TrimPrefix(path, "/")
path = strings.TrimSuffix(path, "/")
if path == "" {
if mani.Index.Path == "" {
return nil, nil, schema.ErrPageNotFound
}
path = mani.Index.Path
}
txId, ok := mani.Paths[path]
if !ok {
// could ignore index.html, so add index.html and try again
txId, ok = mani.Paths[path+"/"+"index.html"]
}
if !ok {
return nil, nil, schema.ErrPageNotFound
}
tags, dataReader, data, err := getArTxOrItemData(txId.TxId, db)
if dataReader != nil {
data, err = io.ReadAll(dataReader)
dataReader.Close()
os.Remove(dataReader.Name())
}
return tags, data, err
}
func getArTxOrItemData(id string, db *Store) (decodeTags []types.Tag, binaryReader *os.File, data []byte, err error) {
// find bundle item
_, err = db.LoadItemMeta(id)
if err == nil {
return getBundleItemData(id, db)
}
// not bundle item
// find arId
txMeta, err := db.LoadTxMeta(id)
if err == nil { // arTx id
data, err = txDataByMeta(txMeta, db)
if err != nil {
return
}
decodeTags, err = utils.TagsDecode(txMeta.Tags)
return
}
// txId not found in local, need proxy to gateway
return nil, nil, nil, schema.ErrLocalNotExist
}
func getArTxOrItemDataForManifest(id string, db *Store, s *Arseeding) (decodeTags []types.Tag, binaryReader *os.File, data []byte, err error) {
// find bundle item form local
decodeTags, binaryReader, data, err = getArTxOrItemData(id, db)
log.Error("getArTxOrItemDataForManifest getArTxOrItemData", "err", err)
// if err equal ErrLocalNotExist, put task to queue to sync data
if err == schema.ErrLocalNotExist {
// registerTask
if err = s.registerTask(id, schema.TaskTypeSyncManifest); err != nil {
log.Error("registerTask sync_manifest error", "err", err)
}
log.Debug("registerTask sync_manifest...", "id", id)
return nil, nil, nil, schema.ErrLocalNotExist
}
return decodeTags, binaryReader, data, err
}
func getArTxOrItemTags(id string, db *Store) (decodeTags []types.Tag, err error) {
itemMeta, err := db.LoadItemMeta(id)
if err == nil {
decodeTags = itemMeta.Tags
return
}
// not bundle item
// find arId
txMeta, err := db.LoadTxMeta(id)
if err == nil { // arTx id
decodeTags, err = utils.TagsDecode(txMeta.Tags)
return
}
return nil, schema.ErrLocalNotExist
}
func getBundleItemData(id string, db *Store) (decodeTags []types.Tag, dataReader *os.File, data []byte, err error) {
binaryReader, itemBinary, err := db.LoadItemBinary(id)
item := &types.BundleItem{}
if err == nil {
item, err = parseBundleItem(binaryReader, itemBinary)
if err != nil {
return nil, nil, nil, err
}
decodeTags = item.Tags
if binaryReader != nil {
binaryReader.Close()
os.Remove(binaryReader.Name())
dataReader = item.DataReader
} else {
data, err = utils.Base64Decode(item.Data)
}
return
}
return
}
func parseBundleItem(binaryReader *os.File, itemBinary []byte) (item *types.BundleItem, err error) {
if binaryReader != nil {
item, err = utils.DecodeBundleItemStream(binaryReader)
} else {
item, err = utils.DecodeBundleItem(itemBinary)
}
return
}
func syncManifestData(id string, s *Arseeding) (err error) {
// get manifest data
data, contentType, err := getRawById(id)
if err != nil {
return err
}
allItemIds := []string{id}
// if contentType == "application/x.arweave-manifest+json" parse it
if contentType == "application/x.arweave-manifest+json" {
// is manifest data parse it
mani := schema.ManifestData{}
if err := json.Unmarshal(data, &mani); err != nil {
return err
}
log.Debug("total txId in manifest", "len", len(mani.Paths))
// for each path in manifest
for _, txId := range mani.Paths {
allItemIds = append(allItemIds, txId.TxId)
}
}
log.Debug("allItemIds", "ids", len(allItemIds))
// filter exist item
itemIds := make([]string, 0, 10)
for _, i := range allItemIds {
if s.store.IsExistItemBinary(i) {
log.Debug("syncManifestData existItem", "item", i)
continue
}
itemIds = append(itemIds, i)
}
// query itemIds from graphql
gq := argraphql.NewARGraphQL("https://arweave.net/graphql", http.Client{})
total := len(itemIds)
var txs []argraphql.BatchGetItemsBundleInTransactionsTransactionConnectionEdgesTransactionEdge
// 90 items per query
for i := 0; i < total; i += 90 {
end := i + 90
if end > total {
end = total
}
log.Debug("BatchGetItemsBundleIn", "start", i, "end", end, "itemIds[i:end]", len(itemIds[i:end]))
resp, err := gq.BatchGetItemsBundleIn(context.Background(), itemIds[i:end], 90, "")
if err != nil {
return errors.New("BatchGetItemsBundleIn error:" + err.Error())
}
txs = append(txs, resp.Transactions.Edges...)
}
log.Debug("txs", "txs", len(txs))
// for each txs to bundleInItemsMap
var (
bundleInItemsMap = make(map[string][]string)
L1Artxs = make([]string, 0)
)
for _, tx := range txs {
if tx.Node.BundledIn.Id == "" {
L1Artxs = append(L1Artxs, tx.Node.Id)
} else {
if _, ok := bundleInItemsMap[tx.Node.BundledIn.Id]; !ok {
bundleInItemsMap[tx.Node.BundledIn.Id] = make([]string, 0)
}
bundleInItemsMap[tx.Node.BundledIn.Id] = append(bundleInItemsMap[tx.Node.BundledIn.Id], tx.Node.Id)
}
}
log.Debug("syncManifestData bundleInItemsMap", "bundleInItemsMap", len(bundleInItemsMap), "L1Artxs", len(L1Artxs))
// get bundle item form goar
c := goar.NewClient("https://arweave.net")
for bundleId, itemIdss := range bundleInItemsMap {
log.Debug("syncManifestData GetBundleItems", "bundleId", bundleId, "itemIds", len(itemIdss))
var items []*types.BundleItem
// check bundleId is nestBundle
isNestBundle, dataSize, err := checkNestBundle(bundleId, gq)
if err != nil || !isNestBundle {
// GetBundleItems
items, err = c.GetBundleItems(bundleId, itemIdss)
if err != nil {
return errors.New("GetBundleItems error:" + err.Error())
}
} else {
log.Debug("nestBundle dataSize...", "size", dataSize)
items, err = getNestBundle(bundleId, itemIdss)
if err != nil {
return errors.New("getNestBundle error:" + err.Error())
}
}
// for each item
for _, item := range items {
log.Debug("syncManifestData save ", "item id", item.Id)
// save item to store
if err := s.saveItem(*item); err != nil {
return fmt.Errorf("saveItem failed, err: %v", err)
}
}
}
return err
}
func getRawById(id string) (data []byte, contentType string, err error) {
var arGateway = "https://arweave.net"
client := gentleman.New().URL(arGateway)
res, err := client.Get().AddPath(fmt.Sprintf("/raw/%s", id)).Send()
if err != nil {
return nil, "", err
}
if !res.Ok {
return nil, "", fmt.Errorf("get raw data statuscode: %d id: %s", res.StatusCode, id)
}
contentType = res.Header.Get("Content-Type")
data = res.Bytes()
return
}
func getNestBundle(nestBundle string, itemIds []string) (items []*types.BundleItem, err error) {
data, _, err := getRawById(nestBundle)
if err != nil {
return nil, err
}
bundle, err := utils.DecodeBundle(data)
if err != nil {
return nil, err
}
for _, item := range bundle.Items {
if utils.ContainsInSlice(itemIds, item.Id) {
itemBy, err := utils.DecodeBundleItem(item.ItemBinary)
if err != nil {
return nil, err
}
items = append(items, itemBy)
}
}
return
}
func checkNestBundle(bundleId string, gq *argraphql.ARGraphQL) (isNestBundle bool, dataSize string, err error) {
res, err := gq.QueryTransaction(context.Background(), bundleId)
if err != nil {
return
}
isNestBundle = res.Transaction.BundledIn.Id != ""
dataSize = res.Transaction.Data.Size
return
}