Skip to content

Commit

Permalink
Merge branch 'staging' into feat/repair-cancel
Browse files Browse the repository at this point in the history
  • Loading branch information
Hitenjain14 authored Jan 2, 2025
2 parents 3ad86b3 + 37a5ac0 commit be31a3c
Show file tree
Hide file tree
Showing 13 changed files with 144 additions and 22 deletions.
2 changes: 2 additions & 0 deletions core/sys/fs.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ type FS interface {
// RemoveProgress remove progress
RemoveProgress(progressID string) error

StoreLogs(key string, data string) error

// Create Directory
CreateDirectory(dirID string) error

Expand Down
4 changes: 4 additions & 0 deletions core/sys/fs_disk.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,10 @@ func (dfs *DiskFS) RemoveProgress(progressID string) error {
return dfs.Remove(progressID)
}

func (dfs *DiskFS) StoreLogs(key string, data string) error {
return nil
}

func (dfs *DiskFS) CreateDirectory(_ string) error {
return nil
}
Expand Down
19 changes: 19 additions & 0 deletions core/sys/fs_mem.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
package sys

import (
"encoding/json"
"errors"
"io/fs"
"os"
Expand Down Expand Up @@ -152,6 +153,24 @@ func (mfs *MemFS) RemoveProgress(progressID string) error {
return nil
}

func (mfs *MemFS) StoreLogs(key string, data string) error {
//get existing logs
var logs []string
val := js.Global().Get("localStorage").Call("getItem", key)
if val.Truthy() {
json.Unmarshal([]byte(val.String()), &logs)
}
//append new logs
logs = append(logs, data)
//store logs
encodedData, err := json.Marshal(logs)
if err != nil {
return err
}
js.Global().Get("localStorage").Call("setItem", key, string(encodedData))
return nil
}

func (mfs *MemFS) CreateDirectory(dirID string) error {
if !js.Global().Get("showDirectoryPicker").Truthy() || !js.Global().Get("WritableStream").Truthy() {
return errors.New("dir_picker: not supported")
Expand Down
2 changes: 1 addition & 1 deletion mobilesdk/sdk/sdk.go
Original file line number Diff line number Diff line change
Expand Up @@ -369,7 +369,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
30 changes: 21 additions & 9 deletions wasmsdk/allocation.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,8 +168,9 @@ func UpdateForbidAllocation(allocationID string, forbidupload, forbiddelete, for
"", //addBlobberId,
"", //addBlobberAuthTicket
"", //removeBlobberId,
"", //thirdPartyExtendable,
false, // ownerSigninPublicKey
"", //owner,
"", //ownerSigninPublicKey
false, // thirdPartyExtendable
&sdk.FileOptionsParameters{
ForbidUpload: sdk.FileOptionParam{Changed: forbidupload, Value: forbidupload},
ForbidDelete: sdk.FileOptionParam{Changed: forbiddelete, Value: forbiddelete},
Expand All @@ -178,6 +179,7 @@ func UpdateForbidAllocation(allocationID string, forbidupload, forbiddelete, for
ForbidCopy: sdk.FileOptionParam{Changed: forbidcopy, Value: forbidcopy},
ForbidRename: sdk.FileOptionParam{Changed: forbidrename, Value: forbidrename},
},
"",
)

return hash, err
Expand All @@ -197,8 +199,9 @@ func freezeAllocation(allocationID string) (string, error) {
"", //addBlobberId,
"", //addBlobberAuthTicket
"", //removeBlobberId,
"", //thirdPartyExtendable,
false, // ownerSigninPublicKey
"", //owner,
"", //ownerSigninPublicKey
false, // thirdPartyExtendable
&sdk.FileOptionsParameters{
ForbidUpload: sdk.FileOptionParam{Changed: true, Value: true},
ForbidDelete: sdk.FileOptionParam{Changed: true, Value: true},
Expand All @@ -207,6 +210,7 @@ func freezeAllocation(allocationID string) (string, error) {
ForbidCopy: sdk.FileOptionParam{Changed: true, Value: true},
ForbidRename: sdk.FileOptionParam{Changed: true, Value: true},
},
"",
)

if err == nil {
Expand Down Expand Up @@ -244,7 +248,7 @@ func updateAllocationWithRepair(allocationID string,
size int64,
extend bool,
lock int64,
addBlobberId, addBlobberAuthTicket, removeBlobberId, ownerSigninPublicKey, callbackFuncName string) (string, error) {
addBlobberId, addBlobberAuthTicket, removeBlobberId, ownerSigninPublicKey, updateAllocTicket, callbackFuncName string) (string, error) {
sdk.SetWasm()
allocationObj, err := sdk.GetAllocation(allocationID)
if err != nil {
Expand All @@ -261,7 +265,7 @@ func updateAllocationWithRepair(allocationID string,
}
}

alloc, hash, isRepairRequired, err := allocationObj.UpdateWithStatus(size, extend, uint64(lock), addBlobberId, addBlobberAuthTicket, removeBlobberId, ownerSigninPublicKey, false, &sdk.FileOptionsParameters{}, statusBar)
alloc, hash, isRepairRequired, err := allocationObj.UpdateWithStatus(size, extend, uint64(lock), addBlobberId, addBlobberAuthTicket, removeBlobberId, ownerSigninPublicKey, false, &sdk.FileOptionsParameters{}, updateAllocTicket)
if err != nil {
return hash, err
}
Expand Down Expand Up @@ -298,7 +302,7 @@ func updateAllocation(allocationID string,
size int64, extend bool,
lock int64,
addBlobberId, addBlobberAuthTicket, removeBlobberId, ownerSigninPublicKey string, setThirdPartyExtendable bool) (string, error) {
hash, _, err := sdk.UpdateAllocation(size, extend, allocationID, uint64(lock), addBlobberId, addBlobberAuthTicket, removeBlobberId, ownerSigninPublicKey, setThirdPartyExtendable, &sdk.FileOptionsParameters{})
hash, _, err := sdk.UpdateAllocation(size, extend, allocationID, uint64(lock), addBlobberId, addBlobberAuthTicket, removeBlobberId, "", ownerSigninPublicKey, setThirdPartyExtendable, &sdk.FileOptionsParameters{}, "")

if err == nil {
clearAllocation(allocationID)
Expand All @@ -307,6 +311,14 @@ func updateAllocation(allocationID string,
return hash, err
}

func getUpdateAllocTicket(allocationID, userID, operationType string, roundExpiry int64) (string, error) {
sign, err := sdk.GetUpdateAllocTicket(allocationID, userID, operationType, roundExpiry)
if err != nil {
return "", err
}
return sign, err
}

// getAllocationMinLock retrieves the minimum lock value for the allocation creation, as calculated by the network.
// Lock value is the amount of tokens that the client needs to lock in the allocation's write pool
// to be able to pay for the write operations.
Expand Down Expand Up @@ -401,8 +413,8 @@ func lockStakePool(providerType, tokens, fee uint64, providerID string) (string,
// - providerType: provider type (1: miner, 2:sharder, 3:blobber, 4:validator, 5:authorizer)
// - fee: transaction fees (in SAS)
// - providerID: provider id
func unlockStakePool(providerType, fee uint64, providerID string) (int64, error) {
unstake, _, err := sdk.StakePoolUnlock(sdk.ProviderType(providerType), providerID, fee)
func unlockStakePool(providerType, fee uint64, providerID, clientID string) (int64, error) {
unstake, _, err := sdk.StakePoolUnlock(sdk.ProviderType(providerType), providerID, clientID, fee)
return unstake, err
}

Expand Down
4 changes: 4 additions & 0 deletions wasmsdk/blobber.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
"time"

"github.com/0chain/gosdk/constants"
"github.com/0chain/gosdk/core/client"
"github.com/0chain/gosdk/core/common"
"github.com/0chain/gosdk/core/encryption"
"github.com/0chain/gosdk/core/pathutil"
Expand Down Expand Up @@ -1184,6 +1185,9 @@ func checkAllocStatus(allocationID string) (string, error) {
if err != nil {
return "", err
}
if client.Wallet().ClientID != alloc.Owner {
return "", errors.New("client id does not match with the allocation owner")
}
status, blobberStatus, err := alloc.CheckAllocStatus()
var statusStr string
switch status {
Expand Down
1 change: 1 addition & 0 deletions wasmsdk/proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,7 @@ func main() {
"getUpdateAllocationMinLock": getUpdateAllocationMinLock,
"getAllocationWith": getAllocationWith,
"createfreeallocation": createfreeallocation,
"getUpdateAllocTicket": getUpdateAllocTicket,

// claim rewards
"collectRewards": collectRewards,
Expand Down
50 changes: 45 additions & 5 deletions zboxcore/sdk/allocation.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ var (
MultiOpBatchSize = 50
RepairBatchSize = 50
Workdir string
logChanMap = make(map[string]chan logEntry)
logMapMutex = &sync.Mutex{}
)

const (
Expand Down Expand Up @@ -437,6 +439,9 @@ func (a *Allocation) InitAllocation() {
}
}
}
for _, blobber := range a.Blobbers {
addLogChan(blobber.Baseurl)
}
a.generateAndSetOwnerSigningPublicKey()
a.startWorker(a.ctx)
InitCommitWorker(a.Blobbers)
Expand All @@ -460,7 +465,7 @@ func (a *Allocation) generateAndSetOwnerSigningPublicKey() {
if a.OwnerSigningPublicKey == "" && !a.Finalized && !a.Canceled && client.Wallet().IsSplit {
pubKey := privateSigningKey.Public().(ed25519.PublicKey)
a.OwnerSigningPublicKey = hex.EncodeToString(pubKey)
hash, _, err := UpdateAllocation(0, false, a.ID, 0, "", "", "", a.OwnerSigningPublicKey, false, nil)
hash, _, err := UpdateAllocation(0, false, a.ID, 0, "", "", "", "", a.OwnerSigningPublicKey, false, nil, "")
if err != nil {
l.Logger.Error("Failed to update owner signing public key ", err, " allocationID: ", a.ID, " hash: ", hash)
return
Expand Down Expand Up @@ -3198,10 +3203,10 @@ func (a *Allocation) UpdateWithRepair(
extend bool,
lock uint64,
addBlobberId, addBlobberAuthTicket, removeBlobberId, ownerSigninPublicKey string,
setThirdPartyExtendable bool, fileOptionsParams *FileOptionsParameters,
setThirdPartyExtendable bool, fileOptionsParams *FileOptionsParameters, updateAllocTicket string,
statusCB StatusCallback,
) (string, error) {
updatedAlloc, hash, isRepairRequired, err := a.UpdateWithStatus(size, extend, lock, addBlobberId, addBlobberAuthTicket, removeBlobberId, ownerSigninPublicKey, setThirdPartyExtendable, fileOptionsParams, statusCB)
updatedAlloc, hash, isRepairRequired, err := a.UpdateWithStatus(size, extend, lock, addBlobberId, addBlobberAuthTicket, removeBlobberId, ownerSigninPublicKey, setThirdPartyExtendable, fileOptionsParams, updateAllocTicket)
if err != nil {
return hash, err
}
Expand Down Expand Up @@ -3234,7 +3239,7 @@ func (a *Allocation) UpdateWithStatus(
lock uint64,
addBlobberId, addBlobberAuthTicket, removeBlobberId, ownerSigninPublicKey string,
setThirdPartyExtendable bool, fileOptionsParams *FileOptionsParameters,
statusCB StatusCallback,
updateAllocTicket string,
) (*Allocation, string, bool, error) {
var (
alloc *Allocation
Expand All @@ -3245,7 +3250,7 @@ func (a *Allocation) UpdateWithStatus(
}

l.Logger.Info("Updating allocation")
hash, _, err := UpdateAllocation(size, extend, a.ID, lock, addBlobberId, addBlobberAuthTicket, removeBlobberId, ownerSigninPublicKey, setThirdPartyExtendable, fileOptionsParams)
hash, _, err := UpdateAllocation(size, extend, a.ID, lock, addBlobberId, addBlobberAuthTicket, removeBlobberId, "", ownerSigninPublicKey, setThirdPartyExtendable, fileOptionsParams, updateAllocTicket)
if err != nil {
return alloc, "", isRepairRequired, err
}
Expand Down Expand Up @@ -3420,3 +3425,38 @@ func contextCanceled(ctx context.Context) bool {
return false
}
}

type logEntry struct {
OpType string
DataSize int
TimeTaken int64
}

func addLogChan(blobberURL string) {
logMapMutex.Lock()
defer logMapMutex.Unlock()
if _, ok := logChanMap[blobberURL]; ok {
return
}
logChan := make(chan logEntry, 200)
logChanMap[blobberURL] = logChan
go logWorker(blobberURL, logChan)
}

func getLogChan(blobberURL string) chan logEntry {
logMapMutex.Lock()
defer logMapMutex.Unlock()
return logChanMap[blobberURL]
}

func logWorker(key string, logChan chan logEntry) {
for log := range logChan {
data, _ := json.Marshal(log)
sys.Files.StoreLogs(key, string(data))
}
}

func writeLogEntry(blobberURL string, log logEntry) {
logChan := getLogChan(blobberURL)
logChan <- log
}
41 changes: 37 additions & 4 deletions zboxcore/sdk/blobber_operations.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,9 +130,12 @@ func UpdateAllocation(
extend bool,
allocationID string,
lock uint64,
addBlobberId, addBlobberAuthTicket, removeBlobberId, ownerSigninPublicKey string,
setThirdPartyExtendable bool, fileOptionsParams *FileOptionsParameters,
addBlobberId, addBlobberAuthTicket, removeBlobberId, ownerID, ownerSigninPublicKey string,
setThirdPartyExtendable bool, fileOptionsParams *FileOptionsParameters, ticket string,
) (hash string, nonce int64, err error) {
if ownerID == "" {
ownerID = client.Id()
}

if lock > math.MaxInt64 {
return "", 0, errors.New("invalid_lock", "int64 overflow on lock value")
Expand All @@ -148,7 +151,7 @@ func UpdateAllocation(
}

updateAllocationRequest := make(map[string]interface{})
updateAllocationRequest["owner_id"] = client.Id()
updateAllocationRequest["owner_id"] = ownerID
updateAllocationRequest["owner_public_key"] = ""
updateAllocationRequest["id"] = allocationID
updateAllocationRequest["size"] = size
Expand All @@ -160,6 +163,24 @@ func UpdateAllocation(
updateAllocationRequest["owner_signing_public_key"] = ownerSigninPublicKey
updateAllocationRequest["file_options_changed"], updateAllocationRequest["file_options"] = calculateAllocationFileOptions(alloc.FileOptions, fileOptionsParams)

if ticket != "" {

type Ticket struct {
AllocationID string `json:"allocation_id"`
UserID string `json:"user_id"`
RoundExpiry int64 `json:"round_expiry"`
OperationType string `json:"operation_type"`
Signature string `json:"signature"`
}

ticketData := &Ticket{}
err := json.Unmarshal([]byte(ticket), ticketData)
if err != nil {
return "", 0, errors.New("invalid_ticket", "invalid ticket")
}
updateAllocationRequest["update_ticket"] = ticketData
}

sn := transaction.SmartContractTxnData{
Name: transaction.STORAGESC_UPDATE_ALLOCATION,
InputArgs: updateAllocationRequest,
Expand All @@ -168,6 +189,17 @@ func UpdateAllocation(
return
}

func GetUpdateAllocTicket(allocationID, userID, operationType string, roundExpiry int64) (string, error) {
payload := fmt.Sprintf("%s:%d:%s:%s", allocationID, roundExpiry, userID, operationType)

signature, err := client.Sign(hex.EncodeToString([]byte(payload)))
if err != nil {
return "", err
}

return signature, nil
}

// StakePoolLock locks tokens in a stake pool.
// This function is the entry point for the staking operation.
// Provided the provider type and provider ID, the value is locked in the stake pool between the SDK client and the provider.
Expand Down Expand Up @@ -226,7 +258,7 @@ func StakePoolLock(providerType ProviderType, providerID string, value, fee uint
// - providerType: provider type
// - providerID: provider ID
// - fee: transaction fee
func StakePoolUnlock(providerType ProviderType, providerID string, fee uint64) (unstake int64, nonce int64, err error) {
func StakePoolUnlock(providerType ProviderType, providerID, clientID string, fee uint64) (unstake int64, nonce int64, err error) {
if !client.IsSDKInitialized() {
return 0, 0, sdkNotInitialized
}
Expand All @@ -242,6 +274,7 @@ func StakePoolUnlock(providerType ProviderType, providerID string, fee uint64) (
spr := stakePoolRequest{
ProviderType: providerType,
ProviderID: providerID,
ClientID: clientID,
}

var sn = transaction.SmartContractTxnData{
Expand Down
7 changes: 6 additions & 1 deletion zboxcore/sdk/blockdownloadworker.go
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,12 @@ func (req *BlockDownloadRequest) downloadBlobberBlock(fastClient *fasthttp.Clien
}
return errors.New("response_error", string(respBuf))
}

entry := logEntry{
OpType: "download",
TimeTaken: timeTaken,
DataSize: len(req.respBuf),
}
writeLogEntry(req.blobber.Baseurl, entry)
dR := downloadResponse{}
if req.shouldVerify {
err = json.Unmarshal(respBuf, &dR)
Expand Down
4 changes: 2 additions & 2 deletions zboxcore/sdk/rollback.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,9 @@ func GetWritemarker(allocID, allocTx, sig, id, baseUrl string, clientId ...strin
if err != nil {
return nil, err
}
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
for retries := 0; retries < 3; retries++ {
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()

resp, err := zboxutil.Client.Do(req.WithContext(ctx))
if err != nil {
Expand Down
1 change: 1 addition & 0 deletions zboxcore/sdk/sdk.go
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,7 @@ func GetStakePoolUserInfo(clientID string, offset, limit int) (info *StakePoolUs
type stakePoolRequest struct {
ProviderType ProviderType `json:"provider_type,omitempty"`
ProviderID string `json:"provider_id,omitempty"`
ClientID string `json:"client_id,omitempty"`
}

// stakePoolLock is stake pool unlock response in case where tokens
Expand Down
Loading

0 comments on commit be31a3c

Please sign in to comment.