Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 2 additions & 4 deletions code/go/0chain.net/blobbercore/handler/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,7 @@ func WithHandler(handler func(ctx *Context) (interface{}, error)) func(w http.Re
statusCode = http.StatusInternalServerError
}

buf, _ := json.Marshal(err)
http.Error(w, string(buf), statusCode)
http.Error(w, err.Error(), statusCode)
return
}

Expand All @@ -97,8 +96,7 @@ func WithHandler(handler func(ctx *Context) (interface{}, error)) func(w http.Re
statusCode = http.StatusInternalServerError
}

buf, _ := json.Marshal(err)
http.Error(w, string(buf), statusCode)
http.Error(w, err.Error(), statusCode)
return
}

Expand Down
6 changes: 4 additions & 2 deletions code/go/0chain.net/blobbercore/handler/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,10 @@ func SetupHandlers(r *mux.Router) {

// lightweight http handler without heavy postgres transaction to improve performance

r.HandleFunc("/v1/writemarker/lock/{allocation}", WithHandler(LockWriteMarker)).Methods(http.MethodPost)
r.HandleFunc("/v1/writemarker/lock/{allocation}", WithHandler(UnlockWriteMarker)).Methods(http.MethodDelete)
r.HandleFunc("/v1/writemarker/lock/{allocation}", WithHandler(LockWriteMarker)).Methods(http.MethodPost, http.MethodOptions)
r.HandleFunc("/v1/writemarker/lock/{allocation}", WithHandler(UnlockWriteMarker)).Methods(http.MethodDelete, http.MethodOptions)

r.HandleFunc("/v1/hashnode/root/{allocation}", WithHandler(LoadRootHashnode)).Methods(http.MethodGet, http.MethodOptions)
}

func WithReadOnlyConnection(handler common.JSONResponderF) common.JSONResponderF {
Expand Down
16 changes: 16 additions & 0 deletions code/go/0chain.net/blobbercore/handler/handler_hashnode.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
//go:build !integration_tests
// +build !integration_tests

package handler

import "github.com/0chain/blobber/code/go/0chain.net/blobbercore/reference"

// LoadRootHashnode load root node with its descendant nodes
func LoadRootHashnode(ctx *Context) (interface{}, error) {

root, err := reference.LoadRootHashnode(ctx, ctx.AllocationTx)
if err != nil {
return nil, err
}
return root, nil
}
113 changes: 113 additions & 0 deletions code/go/0chain.net/blobbercore/handler/handler_hashnode_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
package handler

import (
"encoding/json"
"net/http"
"net/http/httptest"
"testing"

"github.com/0chain/blobber/code/go/0chain.net/blobbercore/datastore"
"github.com/0chain/blobber/code/go/0chain.net/blobbercore/reference"
"github.com/gorilla/mux"
gomocket "github.com/selvatico/go-mocket"
"github.com/stretchr/testify/require"
)

func TestHashnodeHanders_LoadRootHashnode(t *testing.T) {

datastore.UseMocket(true)

gomocket.Catcher.NewMock().
WithQuery(`SELECT allocation_id, type, name, path, content_hash, merkle_root, actual_file_hash, attributes, chunk_size,size,actual_file_size, parent_path
FROM reference_objects`).
WithArgs("allocation_handler_load_root").
WithReply([]map[string]interface{}{
{
"allocation_id": "allocation_handler_load_root",
"type": "D",
"name": "/",
"path": "/",
"content_hash": "",
"merkle_root": "",
"actual_file_hash": "",
"attributes": []byte("null"),
"chunk_size": 0,
"size": 0,
"actual_file_size": 0,
"parent_path": "",
},
{
"allocation_id": "allocation_handler_load_root",
"type": "D",
"name": "sub1",
"path": "/sub1",
"content_hash": "",
"merkle_root": "",
"actual_file_hash": "",
"attributes": []byte("null"),
"chunk_size": 0,
"size": 0,
"actual_file_size": 0,
"parent_path": "/",
},
{
"allocation_id": "allocation_handler_load_root",
"type": "D",
"name": "sub2",
"path": "/sub2",
"content_hash": "",
"merkle_root": "",
"actual_file_hash": "",
"attributes": []byte("null"),
"chunk_size": 0,
"size": 0,
"actual_file_size": 0,
"parent_path": "/",
},
{
"allocation_id": "allocation_handler_load_root",
"type": "D",
"name": "file1",
"path": "/sub1/file1",
"content_hash": "",
"merkle_root": "",
"actual_file_hash": "",
"attributes": []byte("null"),
"chunk_size": 0,
"size": 0,
"actual_file_size": 0,
"parent_path": "/sub1",
},
})

r := mux.NewRouter()
SetupHandlers(r)

req, err := http.NewRequest(http.MethodGet, "/v1/refs/root/{allocation}", nil)
if err != nil {
t.Fatal(err)
}

rr := httptest.NewRecorder()
handler := http.HandlerFunc(WithHandler(func(ctx *Context) (interface{}, error) {
ctx.AllocationTx = "allocation_handler_load_root"
return LoadRootHashnode(ctx)
}))

handler.ServeHTTP(rr, req)

require.Equal(t, http.StatusOK, rr.Code)

var root reference.Hashnode

err = json.Unmarshal(rr.Body.Bytes(), &root)
require.Nil(t, err)

require.NotNil(t, root)
require.Len(t, root.Children, 2)

require.Equal(t, root.Children[0].Name, "sub1")
require.Len(t, root.Children[0].Children, 1)
require.Equal(t, root.Children[0].Children[0].Name, "file1")
require.Equal(t, root.Children[1].Name, "sub2")
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ var WriteMarkerMutext = &writemarker.Mutex{}

// LockWriteMarker try to lock writemarker for specified allocation id, and return latest RefTree
func LockWriteMarker(ctx *Context) (interface{}, error) {
sessionID := ctx.FormValue("session_id")
connectionID := ctx.FormValue("connection_id")
requestTime := ctx.FormTime("request_time")

result, err := WriteMarkerMutext.Lock(ctx, ctx.AllocationTx, sessionID, requestTime)
result, err := WriteMarkerMutext.Lock(ctx, ctx.AllocationTx, connectionID, requestTime)
if err != nil {
return nil, err
}
Expand All @@ -24,7 +24,7 @@ func LockWriteMarker(ctx *Context) (interface{}, error) {

// UnlockWriteMarker release WriteMarkerMutex
func UnlockWriteMarker(ctx *Context) (interface{}, error) {
sessionID := ctx.FormValue("session_id")
sessionID := ctx.FormValue("connection_id")

err := WriteMarkerMutext.Unlock(ctx, ctx.AllocationTx, sessionID)
if err != nil {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func TestWriteMarkerHandlers_Lock(t *testing.T) {

now := time.Now()

formWriter.WriteField("session_id", "session_id") //nolint: errcheck
formWriter.WriteField("connection_id", "connection_id") //nolint: errcheck
formWriter.WriteField("request_time", strconv.FormatInt(now.Unix(), 10)) //nolint: errcheck
formWriter.Close()

Expand Down Expand Up @@ -68,7 +68,7 @@ func TestWriteMarkerHandlers_Unlock(t *testing.T) {

now := time.Now()

formWriter.WriteField("session_id", "session_id") //nolint: errcheck
formWriter.WriteField("connection_id", "connection_id") //nolint: errcheck
formWriter.WriteField("request_time", strconv.FormatInt(now.Unix(), 10)) //nolint: errcheck
formWriter.Close()

Expand Down
8 changes: 6 additions & 2 deletions code/go/0chain.net/blobbercore/mock/init.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package mock

import (
"encoding/json"
"io"
"net/http"
"net/http/httptest"

"github.com/0chain/gosdk/core/zcncrypto"
"github.com/0chain/gosdk/sdks"
"github.com/0chain/gosdk/sdks/blobber"
)
Expand All @@ -18,11 +20,13 @@ const (
// )

func NewBlobberClient() *blobber.Blobber {
z := sdks.New("9a566aa4f8e8c342fed97c8928040a21f21b8f574e5782c28568635ba9c75a85", "40cd10039913ceabacf05a7c60e1ad69bb2964987bc50f77495e514dc451f907c3d8ebcdab20eedde9c8f39b9a1d66609a637352f318552fb69d4b3672516d1a", "bls0chain")
err := z.InitWallet(zboxWallet)
wallet := &zcncrypto.Wallet{}
err := json.Unmarshal([]byte(zboxWallet), wallet) //nolint: errcheck
if err != nil {
panic("mock: z.InitWallet " + err.Error())
}
z := sdks.New("9a566aa4f8e8c342fed97c8928040a21f21b8f574e5782c28568635ba9c75a85", "40cd10039913ceabacf05a7c60e1ad69bb2964987bc50f77495e514dc451f907c3d8ebcdab20eedde9c8f39b9a1d66609a637352f318552fb69d4b3672516d1a", "bls0chain", wallet)

z.NewRequest = func(method, url string, body io.Reader) (*http.Request, error) {
return httptest.NewRequest(method, url, body), nil
}
Expand Down
73 changes: 0 additions & 73 deletions code/go/0chain.net/blobbercore/reference/dao.go

This file was deleted.

40 changes: 7 additions & 33 deletions code/go/0chain.net/blobbercore/reference/entity.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
package reference

import (
"strconv"
"strings"

"gorm.io/datatypes"
)

// HashNode ref node in hash tree
type HashNode struct {
// Hashnode ref node in hash tree
type Hashnode struct {
// hash data
AllocationID string `gorm:"column:allocation_id" json:"allocation_id,omitempty"`
Type string `gorm:"column:type" json:"type,omitempty"`
Expand All @@ -24,46 +21,23 @@ type HashNode struct {

// other data
ParentPath string `gorm:"parent_path" json:"-"`
Children []*HashNode `gorm:"-" json:"children,omitempty"`
Children []*Hashnode `gorm:"-" json:"children,omitempty"`
}

// TableName get table name of Ref
func (HashNode) TableName() string {
func (Hashnode) TableName() string {
return TableNameReferenceObjects
}

func (n *HashNode) AddChild(c *HashNode) {
func (n *Hashnode) AddChild(c *Hashnode) {
if n.Children == nil {
n.Children = make([]*HashNode, 0, 10)
n.Children = make([]*Hashnode, 0, 10)
}

n.Children = append(n.Children, c)
}

// GetLookupHash get lookuphash
func (n *HashNode) GetLookupHash() string {
func (n *Hashnode) GetLookupHash() string {
return GetReferenceLookup(n.AllocationID, n.Path)
}

// GetHashCode get hash code
func (n *HashNode) GetHashCode() string {

if len(n.Attributes) == 0 {
n.Attributes = datatypes.JSON("{}")
}
hashArray := []string{
n.AllocationID,
n.Type,
n.Name,
n.Path,
strconv.FormatInt(n.Size, 10),
n.ContentHash,
n.MerkleRoot,
strconv.FormatInt(n.ActualFileSize, 10),
n.ActualFileHash,
string(n.Attributes),
strconv.FormatInt(n.ChunkSize, 10),
}

return strings.Join(hashArray, ":")
}
Loading