From 4a22b58260056541a6d3d47ca707549eb4bf7508 Mon Sep 17 00:00:00 2001 From: sandy <18382255942@163.com> Date: Tue, 27 Feb 2024 11:27:57 +0800 Subject: [PATCH] feat(): support nestbundle --- manifest.go | 48 ++++++++++++++++++++++++++++++++++++++++++------ manifets_test.go | 34 +++++++++++++++++++++++++++++++++- 2 files changed, 75 insertions(+), 7 deletions(-) diff --git a/manifest.go b/manifest.go index aaed828..9cf1190 100644 --- a/manifest.go +++ b/manifest.go @@ -199,11 +199,21 @@ func syncManifestData(id string, s *Arseeding) (err error) { c := goar.NewClient("https://arweave.net") for bundleId, itemIds := range bundleInItemsMap { log.Debug("syncManifestData GetBundleItems ", "bundleId", bundleId, "itemIds", len(itemIds)) - // GetBundleItems - items, err := c.GetBundleItems(bundleId, itemIds) - - if err != nil { - return errors.New("GetBundleItems error:" + err.Error()) + var items []*types.BundleItem + // check bundleId is nestBundle + isNestBundle, dataSize, err := checkNestBundle(bundleId, gq) + if err != nil || !isNestBundle { + // GetBundleItems + items, err = c.GetBundleItems(bundleId, itemIds) + if err != nil { + return errors.New("GetBundleItems error:" + err.Error()) + } + } else { + log.Debug("nestBundle dataSize...", "size", dataSize) + items, err = getNestBundle(bundleId, itemIds) + if err != nil { + return errors.New("getNestBundle error:" + err.Error()) + } } // for each item @@ -223,7 +233,6 @@ func syncManifestData(id string, s *Arseeding) (err error) { } func getRawById(id string) (data []byte, contentType string, err error) { - var arGateway = "https://arweave.net" client := gentleman.New().URL(arGateway) @@ -242,3 +251,30 @@ func getRawById(id string) (data []byte, contentType string, err error) { 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) { + items = append(items, &item) + } + } + 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 +} diff --git a/manifets_test.go b/manifets_test.go index 2d5542a..c74dc6e 100644 --- a/manifets_test.go +++ b/manifets_test.go @@ -1,8 +1,13 @@ package arseeding import ( + "context" "encoding/json" + "github.com/everFinance/arseeding/argraphql" "github.com/everFinance/arseeding/schema" + "github.com/everFinance/goar/utils" + "github.com/stretchr/testify/assert" + "net/http" "testing" ) @@ -23,7 +28,34 @@ func Test_getRawById(t *testing.T) { } func TestNewS3Store(t *testing.T) { - err := syncManifestData("U1FqvR_xTuL2qxrJDw20oIghpGt1eTumJ9ZfCczc5_M", nil) + err := syncManifestData("yy8F4i6jKVKtQuOw2q8RwxjQyUwJ4QtGRkdUXd8jbEw", nil) // err := syncManifestData("WUg9McRBT1i_F6utVYjFYS_pP6dzKcrC1FRccjH6inE", nil) t.Log(err) } + +func Test_getRawById1(t *testing.T) { + data, contentType, err := getRawById("AjV6oRKHh5PPI8Ehu9hIyWEz3oFAm5K0I0UYkxjwLdE") + assert.NoError(t, err) + t.Log(contentType) + bundle, err := utils.DecodeBundle(data) + assert.NoError(t, err) + assert.Equal(t, 2, len(bundle.Items)) + +} + +func Test_getNestBundle(t *testing.T) { + itemIds := []string{"_5nCBFfMpHbpxRw6_hpoJyu8bV62S6flhUGrkdHbV8o"} + items, err := getNestBundle("AjV6oRKHh5PPI8Ehu9hIyWEz3oFAm5K0I0UYkxjwLdE", itemIds) + assert.NoError(t, err) + assert.Equal(t, 1, len(items)) +} + +func TestNewCache(t *testing.T) { + nestBundle := "AjV6oRKHh5PPI8Ehu9hIyWEz3oFAm5K0I0UYkxjwLdE" + gq := argraphql.NewARGraphQL("https://arweave.net/graphql", http.Client{}) + res, err := gq.QueryTransaction(context.Background(), nestBundle) + assert.NoError(t, err) + t.Log(res.GetTransaction().BundledIn.Id) + t.Log(res.Transaction.Data.Size) + t.Log(res.Transaction.Id) +}