Skip to content

Commit

Permalink
Merge pull request #1687 from 0chain/feature/third-party-update-alloc
Browse files Browse the repository at this point in the history
Feature third party update alloc
  • Loading branch information
dabasov authored Dec 26, 2024
2 parents 49c9e5d + 0f105d9 commit 1a30cbd
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 16 deletions.
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
26 changes: 19 additions & 7 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
1 change: 1 addition & 0 deletions wasmsdk/proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,7 @@ func main() {
"getUpdateAllocationMinLock": getUpdateAllocationMinLock,
"getAllocationWith": getAllocationWith,
"createfreeallocation": createfreeallocation,
"getUpdateAllocTicket": getUpdateAllocTicket,

// claim rewards
"collectRewards": collectRewards,
Expand Down
10 changes: 5 additions & 5 deletions zboxcore/sdk/allocation.go
Original file line number Diff line number Diff line change
Expand Up @@ -460,7 +460,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 @@ -3184,10 +3184,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 @@ -3220,7 +3220,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 @@ -3231,7 +3231,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
38 changes: 35 additions & 3 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

0 comments on commit 1a30cbd

Please sign in to comment.