Skip to content

Commit

Permalink
Merge pull request #96 from everFinance/feature/support-nestbundle
Browse files Browse the repository at this point in the history
feat(): support nestbundle
  • Loading branch information
zyjblockchain authored Feb 27, 2024
2 parents f7b29de + 4a22b58 commit 4bfa7c1
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 7 deletions.
48 changes: 42 additions & 6 deletions manifest.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)

Expand All @@ -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
}
34 changes: 33 additions & 1 deletion manifets_test.go
Original file line number Diff line number Diff line change
@@ -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"
)

Expand All @@ -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)
}

0 comments on commit 4bfa7c1

Please sign in to comment.