Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature blobber authorization #1395

Merged
merged 17 commits into from
Apr 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
36 changes: 19 additions & 17 deletions mobilesdk/sdk/sdk.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ func InitStorageSDK(clientJson string, configJson string) (*StorageSDK, error) {
// - size: size of space reserved on blobbers
// - expiration: duration to allocation expiration
// - lock: lock write pool with given number of tokens
func (s *StorageSDK) CreateAllocation(datashards, parityshards int, size, expiration int64, lock string) (*zbox.Allocation, error) {
func (s *StorageSDK) CreateAllocation(datashards, parityshards int, size, expiration int64, lock string, blobberAuthTickets []string) (*zbox.Allocation, error) {
readPrice := sdk.PriceRange{Min: 0, Max: math.MaxInt64}
writePrice := sdk.PriceRange{Min: 0, Max: math.MaxInt64}

Expand All @@ -158,14 +158,15 @@ func (s *StorageSDK) CreateAllocation(datashards, parityshards int, size, expira
}

options := sdk.CreateAllocationOptions{
DataShards: datashards,
ParityShards: parityshards,
Size: size,
ReadPrice: readPrice,
WritePrice: writePrice,
Lock: uint64(l),
BlobberIds: []string{},
FileOptionsParams: &sdk.FileOptionsParameters{},
DataShards: datashards,
ParityShards: parityshards,
Size: size,
ReadPrice: readPrice,
WritePrice: writePrice,
Lock: uint64(l),
BlobberIds: []string{},
FileOptionsParams: &sdk.FileOptionsParameters{},
BlobberAuthTickets: blobberAuthTickets,
}

sdkAllocationID, _, _, err := sdk.CreateAllocationWith(options)
Expand All @@ -190,7 +191,7 @@ func (s *StorageSDK) CreateAllocation(datashards, parityshards int, size, expira
// - lock: lock write pool with given number of tokens
// - blobberUrls: concat blobber urls with comma. leave it as empty if you don't have any preferred blobbers
// - blobberIds: concat blobber ids with comma. leave it as empty if you don't have any preferred blobbers
func (s *StorageSDK) CreateAllocationWithBlobbers(name string, datashards, parityshards int, size int64, lock string, blobberUrls, blobberIds string) (*zbox.Allocation, error) {
func (s *StorageSDK) CreateAllocationWithBlobbers(name string, datashards, parityshards int, size int64, lock string, blobberUrls, blobberIds string, blobberAuthTickets []string) (*zbox.Allocation, error) {
readPrice := sdk.PriceRange{Min: 0, Max: math.MaxInt64}
writePrice := sdk.PriceRange{Min: 0, Max: math.MaxInt64}

Expand All @@ -200,12 +201,13 @@ func (s *StorageSDK) CreateAllocationWithBlobbers(name string, datashards, parit
}

options := sdk.CreateAllocationOptions{
DataShards: datashards,
ParityShards: parityshards,
Size: size,
Lock: l,
WritePrice: writePrice,
ReadPrice: readPrice,
DataShards: datashards,
ParityShards: parityshards,
Size: size,
Lock: l,
WritePrice: writePrice,
ReadPrice: readPrice,
BlobberAuthTickets: blobberAuthTickets,
}

if blobberUrls != "" {
Expand Down Expand Up @@ -346,7 +348,7 @@ func (s *StorageSDK) UpdateAllocation(size int64, extend bool, allocationID stri
return "", errors.Errorf("int64 overflow in lock")
}

hash, _, err = sdk.UpdateAllocation(size, extend, allocationID, lock, "", "", false, &sdk.FileOptionsParameters{})
hash, _, err = sdk.UpdateAllocation(size, extend, allocationID, lock, "", "", "", false, &sdk.FileOptionsParameters{})
return hash, err
}

Expand Down
17 changes: 10 additions & 7 deletions wasmsdk/allocation.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ func createfreeallocation(freeStorageMarker string) (string, error) {

func getAllocationBlobbers(preferredBlobberURLs []string,
dataShards, parityShards int, size int64,
minReadPrice, maxReadPrice, minWritePrice, maxWritePrice int64, force bool) ([]string, error) {
minReadPrice, maxReadPrice, minWritePrice, maxWritePrice int64, isRestricted, force bool) ([]string, error) {

if len(preferredBlobberURLs) > 0 {
return sdk.GetBlobberIds(preferredBlobberURLs)
Expand All @@ -57,11 +57,11 @@ func getAllocationBlobbers(preferredBlobberURLs []string,
}, sdk.PriceRange{
Min: uint64(minWritePrice),
Max: uint64(maxWritePrice),
}, force)
}, isRestricted, force)
}

func createAllocation(datashards, parityshards int, size int64,
minReadPrice, maxReadPrice, minWritePrice, maxWritePrice int64, lock int64, blobberIds []string, setThirdPartyExtendable bool) (
minReadPrice, maxReadPrice, minWritePrice, maxWritePrice int64, lock int64, blobberIds, blobberAuthTickets []string, setThirdPartyExtendable bool) (
*transaction.Transaction, error) {

options := sdk.CreateAllocationOptions{
Expand All @@ -79,6 +79,7 @@ func createAllocation(datashards, parityshards int, size int64,
Lock: uint64(lock),
BlobberIds: blobberIds,
ThirdPartyExtendable: setThirdPartyExtendable,
BlobberAuthTickets: blobberAuthTickets,
}

sdkLogger.Info(options)
Expand Down Expand Up @@ -122,6 +123,7 @@ func UpdateForbidAllocation(allocationID string, forbidupload, forbiddelete, for
allocationID, // allocID,
0, //lock,
"", //addBlobberId,
"", //addBlobberAuthTicket
"", //removeBlobberId,
false, //thirdPartyExtendable,
&sdk.FileOptionsParameters{
Expand All @@ -146,6 +148,7 @@ func freezeAllocation(allocationID string) (string, error) {
allocationID, // allocID,
0, //lock,
"", //addBlobberId,
"", //addBlobberAuthTicket
"", //removeBlobberId,
false, //thirdPartyExtendable,
&sdk.FileOptionsParameters{
Expand Down Expand Up @@ -180,7 +183,7 @@ func updateAllocationWithRepair(allocationID string,
size int64,
extend bool,
lock int64,
addBlobberId, removeBlobberId string) (string, error) {
addBlobberId, addBlobberAuthTicket, removeBlobberId string) (string, error) {
sdk.SetWasm()
allocationObj, err := sdk.GetAllocation(allocationID)
if err != nil {
Expand All @@ -191,7 +194,7 @@ func updateAllocationWithRepair(allocationID string,
statusBar := &StatusBar{wg: wg, isRepair: true}
wg.Add(1)

hash, err := allocationObj.UpdateWithRepair(size, extend, uint64(lock), addBlobberId, removeBlobberId, false, &sdk.FileOptionsParameters{}, statusBar)
hash, err := allocationObj.UpdateWithRepair(size, extend, uint64(lock), addBlobberId, addBlobberAuthTicket, removeBlobberId, false, &sdk.FileOptionsParameters{}, statusBar)
if err == nil {
clearAllocation(allocationID)
wg.Wait()
Expand All @@ -206,8 +209,8 @@ func updateAllocationWithRepair(allocationID string,
func updateAllocation(allocationID string,
size int64, extend bool,
lock int64,
addBlobberId, removeBlobberId string, setThirdPartyExtendable bool) (string, error) {
hash, _, err := sdk.UpdateAllocation(size, extend, allocationID, uint64(lock), addBlobberId, removeBlobberId, setThirdPartyExtendable, &sdk.FileOptionsParameters{})
addBlobberId, addBlobberAuthTicket, removeBlobberId string, setThirdPartyExtendable bool) (string, error) {
hash, _, err := sdk.UpdateAllocation(size, extend, allocationID, uint64(lock), addBlobberId, addBlobberAuthTicket, removeBlobberId, setThirdPartyExtendable, &sdk.FileOptionsParameters{})

if err == nil {
clearAllocation(allocationID)
Expand Down
2 changes: 1 addition & 1 deletion wasmsdk/demo/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -308,7 +308,7 @@ <h2>please download zcn.wasm from https://github.com/0chain/gosdk/releases/lates
}
try {
const allocation = await goWasm.sdk.createAllocation(config.datashards, config.parityshards, config.size, config.minReadPrice, config.maxReadPrice, config.minWritePrice, config.maxWritePrice,
config.lock, [], false)
config.lock, [], [], false)

console.log(allocation)
allocations = await goWasm.sdk.listAllocations()
Expand Down
4 changes: 2 additions & 2 deletions zboxcore/sdk/allocation.go
Original file line number Diff line number Diff line change
Expand Up @@ -2471,7 +2471,7 @@ func (a *Allocation) UpdateWithRepair(
size int64,
extend bool,
lock uint64,
addBlobberId, removeBlobberId string,
addBlobberId, addBlobberAuthTicket, removeBlobberId string,
setThirdPartyExtendable bool, fileOptionsParams *FileOptionsParameters,
statusCB StatusCallback,
) (string, error) {
Expand All @@ -2480,7 +2480,7 @@ func (a *Allocation) UpdateWithRepair(
}

l.Logger.Info("Updating allocation")
hash, _, err := UpdateAllocation(size, extend, a.ID, lock, addBlobberId, removeBlobberId, setThirdPartyExtendable, fileOptionsParams)
hash, _, err := UpdateAllocation(size, extend, a.ID, lock, addBlobberId, addBlobberAuthTicket, removeBlobberId, setThirdPartyExtendable, fileOptionsParams)
if err != nil {
return "", err
}
Expand Down
2 changes: 1 addition & 1 deletion zboxcore/sdk/blockdownloadworker.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ func (req *BlockDownloadRequest) downloadBlobberBlock(hostClient *fasthttp.HostC

err = func() error {
now := time.Now()
statuscode, respBuf, err := hostClient.GetWithRequestTimeout(httpreq, req.respBuf, 30*time.Second)
statuscode, respBuf, err := hostClient.GetWithRequestTimeout(httpreq, req.respBuf, 45*time.Second)
fasthttp.ReleaseRequest(httpreq)
timeTaken := time.Since(now).Milliseconds()
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion zboxcore/sdk/chunked_upload.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ import (
)

const (
DefaultUploadTimeOut = 45 * time.Second
DefaultUploadTimeOut = 90 * time.Second
)

var (
Expand Down
3 changes: 2 additions & 1 deletion zboxcore/sdk/downloadworker.go
Original file line number Diff line number Diff line change
Expand Up @@ -747,8 +747,9 @@ func (req *DownloadRequest) processDownload() {
}
if err := eg.Wait(); err != nil {
writeCancel()
req.errorCB(err, remotePathCB)
close(blocks)
wg.Wait()
req.errorCB(err, remotePathCB)
return
}

Expand Down
41 changes: 27 additions & 14 deletions zboxcore/sdk/sdk.go
Original file line number Diff line number Diff line change
Expand Up @@ -621,6 +621,7 @@ type Blobber struct {
IsKilled bool `json:"is_killed"`
IsShutdown bool `json:"is_shutdown"`
NotAvailable bool `json:"not_available"`
IsRestricted bool `json:"is_restricted"`
}

// UpdateBlobber is used during update blobber settings calls.
Expand All @@ -642,6 +643,7 @@ type UpdateBlobber struct {
IsKilled *bool `json:"is_killed,omitempty"`
IsShutdown *bool `json:"is_shutdown,omitempty"`
NotAvailable *bool `json:"not_available,omitempty"`
IsRestricted *bool `json:"is_restricted,omitempty"`
}

type ResetBlobberStatsDto struct {
Expand Down Expand Up @@ -1014,6 +1016,7 @@ type CreateAllocationOptions struct {
WritePrice PriceRange
Lock uint64
BlobberIds []string
BlobberAuthTickets []string
ThirdPartyExtendable bool
FileOptionsParams *FileOptionsParameters
}
Expand All @@ -1024,14 +1027,14 @@ func CreateAllocationWith(options CreateAllocationOptions) (
return CreateAllocationForOwner(client.GetClientID(),
client.GetClientPublicKey(), options.DataShards, options.ParityShards,
options.Size, options.ReadPrice, options.WritePrice, options.Lock,
options.BlobberIds, options.ThirdPartyExtendable, options.FileOptionsParams)
options.BlobberIds, options.BlobberAuthTickets, options.ThirdPartyExtendable, options.FileOptionsParams)
}

func CreateAllocationForOwner(
owner, ownerpublickey string,
datashards, parityshards int, size int64,
readPrice, writePrice PriceRange,
lock uint64, preferredBlobberIds []string, thirdPartyExtendable bool, fileOptionsParams *FileOptionsParameters,
lock uint64, preferredBlobberIds, blobberAuthTickets []string, thirdPartyExtendable bool, fileOptionsParams *FileOptionsParameters,
) (hash string, nonce int64, txn *transaction.Transaction, err error) {

if lock > math.MaxInt64 {
Expand All @@ -1043,7 +1046,7 @@ func CreateAllocationForOwner(
}

allocationRequest, err := getNewAllocationBlobbers(
datashards, parityshards, size, readPrice, writePrice, preferredBlobberIds)
datashards, parityshards, size, readPrice, writePrice, preferredBlobberIds, blobberAuthTickets)
if err != nil {
return "", 0, nil, errors.New("failed_get_allocation_blobbers", "failed to get blobbers for allocation: "+err.Error())
}
Expand All @@ -1068,7 +1071,7 @@ func CreateAllocationForOwner(
func GetAllocationBlobbers(
datashards, parityshards int,
size int64,
readPrice, writePrice PriceRange,
readPrice, writePrice PriceRange, isRestricted bool,
force ...bool,
) ([]string, error) {
var allocationRequest = map[string]interface{}{
Expand All @@ -1077,6 +1080,7 @@ func GetAllocationBlobbers(
"size": size,
"read_price_range": readPrice,
"write_price_range": writePrice,
"is_restricted": isRestricted,
}

allocationData, _ := json.Marshal(allocationRequest)
Expand Down Expand Up @@ -1105,10 +1109,10 @@ func getNewAllocationBlobbers(
datashards, parityshards int,
size int64,
readPrice, writePrice PriceRange,
preferredBlobberIds []string,
preferredBlobberIds, blobberAuthTickets []string,
) (map[string]interface{}, error) {
allocBlobberIDs, err := GetAllocationBlobbers(
datashards, parityshards, size, readPrice, writePrice,
datashards, parityshards, size, readPrice, writePrice, false, true,
)
if err != nil {
return nil, err
Expand All @@ -1119,20 +1123,28 @@ func getNewAllocationBlobbers(
// filter duplicates
ids := make(map[string]bool)
uniqueBlobbers := []string{}
for _, b := range blobbers {
uniqueBlobberAuthTickets := []string{}

for i, b := range blobbers {
if !ids[b] {
uniqueBlobbers = append(uniqueBlobbers, b)
if i < len(blobberAuthTickets) {
uniqueBlobberAuthTickets = append(uniqueBlobberAuthTickets, blobberAuthTickets[i])
} else {
uniqueBlobberAuthTickets = append(uniqueBlobberAuthTickets, "")
}
ids[b] = true
}
}

return map[string]interface{}{
"data_shards": datashards,
"parity_shards": parityshards,
"size": size,
"blobbers": uniqueBlobbers,
"read_price_range": readPrice,
"write_price_range": writePrice,
"data_shards": datashards,
"parity_shards": parityshards,
"size": size,
"blobbers": uniqueBlobbers,
"blobber_auth_tickets": uniqueBlobberAuthTickets,
"read_price_range": readPrice,
"write_price_range": writePrice,
}, nil
}

Expand Down Expand Up @@ -1236,7 +1248,7 @@ func UpdateAllocation(
extend bool,
allocationID string,
lock uint64,
addBlobberId, removeBlobberId string,
addBlobberId, addBlobberAuthTicket, removeBlobberId string,
setThirdPartyExtendable bool, fileOptionsParams *FileOptionsParameters,
) (hash string, nonce int64, err error) {

Expand All @@ -1260,6 +1272,7 @@ func UpdateAllocation(
updateAllocationRequest["size"] = size
updateAllocationRequest["extend"] = extend
updateAllocationRequest["add_blobber_id"] = addBlobberId
updateAllocationRequest["add_blobber_auth_ticket"] = addBlobberAuthTicket
updateAllocationRequest["remove_blobber_id"] = removeBlobberId
updateAllocationRequest["set_third_party_extendable"] = setThirdPartyExtendable
updateAllocationRequest["file_options_changed"], updateAllocationRequest["file_options"] = calculateAllocationFileOptions(alloc.FileOptions, fileOptionsParams)
Expand Down
4 changes: 2 additions & 2 deletions zboxcore/zboxutil/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -189,8 +189,8 @@ func init() {
Concurrency: 4096,
DNSCacheDuration: time.Hour,
}).Dial,
ReadTimeout: 60 * time.Second,
WriteTimeout: 60 * time.Second,
ReadTimeout: 90 * time.Second,
WriteTimeout: 90 * time.Second,
}
envProxy.initialize()
log.Init(logger.DEBUG, "0box-sdk")
Expand Down
1 change: 1 addition & 0 deletions zcncore/transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,7 @@ type Blobber struct {
LastHealthCheck common.Timestamp `json:"last_health_check"`
StakePoolSettings StakePoolSettings `json:"stake_pool_settings"`
NotAvailable bool `json:"not_available"`
IsRestricted bool `json:"is_restricted"`
}

type Validator struct {
Expand Down
5 changes: 5 additions & 0 deletions zmagmacore/node/self.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,11 @@ func PublicKey() string {
return self.wallet.PublicKey()
}

// PrivateKey returns id of Node.
func PrivateKey() string {
return self.wallet.PrivateKey()
}

// StartTime returns time when Node is started.
func StartTime() time.Time {
return self.startTime
Expand Down
5 changes: 5 additions & 0 deletions zmagmacore/wallet/wallet.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,11 @@ func (w *Wallet) PublicKey() string {
return w.ZCNWallet.Keys[0].PublicKey
}

// PrivateKey returns the public key.
func (w *Wallet) PrivateKey() string {
return w.ZCNWallet.Keys[0].PrivateKey
}

// ID returns the client id.
//
// NOTE: client id represents hex encoded SHA3-256 hash of the raw public key.
Expand Down
Loading