From 7db2dff0750657b7455b26182560c1f9cbfd3c62 Mon Sep 17 00:00:00 2001 From: Jayash Satolia Date: Sun, 17 Nov 2024 03:31:31 +0530 Subject: [PATCH 01/13] Fix multi wallet --- wasmsdk/allocation.go | 2 +- zboxcore/sdk/allocation.go | 9 ++-- zboxcore/sdk/blobber_operations.go | 30 ++++++------- zboxcore/sdk/sdk.go | 70 ++++++++++++++---------------- 4 files changed, 52 insertions(+), 59 deletions(-) diff --git a/wasmsdk/allocation.go b/wasmsdk/allocation.go index 5ee6458c6..c87c060bb 100644 --- a/wasmsdk/allocation.go +++ b/wasmsdk/allocation.go @@ -261,7 +261,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{}) if err != nil { return hash, err } diff --git a/zboxcore/sdk/allocation.go b/zboxcore/sdk/allocation.go index 19ab21241..16500a347 100644 --- a/zboxcore/sdk/allocation.go +++ b/zboxcore/sdk/allocation.go @@ -457,7 +457,7 @@ func (a *Allocation) generateAndSetOwnerSigningPublicKey() { pubKey := privateSigningKey.Public().(ed25519.PublicKey) a.OwnerSigningPublicKey = hex.EncodeToString(pubKey) //TODO: save this public key to blockchain - hash, _, err := UpdateAllocation(0, false, a.ID, 0, "", "", "", a.OwnerSigningPublicKey, false, nil) + hash, _, err := UpdateAllocation(0, false, a.ID, 0, "", "", "", a.OwnerSigningPublicKey, false, nil, a.Owner) if err != nil { l.Logger.Error("Failed to update owner signing public key ", err, " allocationID: ", a.ID, " hash: ", hash) return @@ -3152,7 +3152,7 @@ func (a *Allocation) UpdateWithRepair( setThirdPartyExtendable bool, fileOptionsParams *FileOptionsParameters, 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) if err != nil { return hash, err } @@ -3184,8 +3184,7 @@ func (a *Allocation) UpdateWithStatus( extend bool, lock uint64, addBlobberId, addBlobberAuthTicket, removeBlobberId, ownerSigninPublicKey string, - setThirdPartyExtendable bool, fileOptionsParams *FileOptionsParameters, - statusCB StatusCallback, + setThirdPartyExtendable bool, fileOptionsParams *FileOptionsParameters, clients ...string, ) (*Allocation, string, bool, error) { var ( alloc *Allocation @@ -3196,7 +3195,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, clients...) if err != nil { return alloc, "", isRepairRequired, err } diff --git a/zboxcore/sdk/blobber_operations.go b/zboxcore/sdk/blobber_operations.go index e546d6cd8..4ebe1765f 100644 --- a/zboxcore/sdk/blobber_operations.go +++ b/zboxcore/sdk/blobber_operations.go @@ -34,7 +34,7 @@ func CreateAllocationForOwner( owner, ownerpublickey string, datashards, parityshards int, size int64, readPrice, writePrice PriceRange, - lock uint64, preferredBlobberIds, blobberAuthTickets []string, thirdPartyExtendable, IsEnterprise, force bool, fileOptionsParams *FileOptionsParameters, + lock uint64, preferredBlobberIds, blobberAuthTickets []string, thirdPartyExtendable, IsEnterprise, force bool, fileOptionsParams *FileOptionsParameters, clients ...string, ) (hash string, nonce int64, txn *transaction.Transaction, err error) { if lock > math.MaxInt64 { @@ -76,7 +76,7 @@ func CreateAllocationForOwner( Name: transaction.NEW_ALLOCATION_REQUEST, InputArgs: allocationRequest, } - hash, _, nonce, txn, err = storageSmartContractTxnValue(sn, lock) + hash, _, nonce, txn, err = StorageSmartContractTxnValue(sn, lock, clients...) return } @@ -85,7 +85,7 @@ func CreateAllocationForOwner( // - value is the value of the free allocation. // // returns the hash of the transaction, the nonce of the transaction and an error if any. -func CreateFreeAllocation(marker string, value uint64) (string, int64, error) { +func CreateFreeAllocation(marker string, value uint64, clients ...string) (string, int64, error) { if !client.IsSDKInitialized() { return "", 0, sdkNotInitialized } @@ -108,7 +108,7 @@ func CreateFreeAllocation(marker string, value uint64) (string, int64, error) { Name: transaction.NEW_FREE_ALLOCATION, InputArgs: input, } - hash, _, n, _, err := storageSmartContractTxnValue(sn, value) + hash, _, n, _, err := StorageSmartContractTxnValue(sn, value, clients...) return hash, n, err } @@ -131,7 +131,7 @@ func UpdateAllocation( allocationID string, lock uint64, addBlobberId, addBlobberAuthTicket, removeBlobberId, ownerSigninPublicKey string, - setThirdPartyExtendable bool, fileOptionsParams *FileOptionsParameters, + setThirdPartyExtendable bool, fileOptionsParams *FileOptionsParameters, clients ...string, ) (hash string, nonce int64, err error) { if lock > math.MaxInt64 { @@ -148,7 +148,7 @@ func UpdateAllocation( } updateAllocationRequest := make(map[string]interface{}) - updateAllocationRequest["owner_id"] = client.Id() + updateAllocationRequest["owner_id"] = client.Id(clients...) updateAllocationRequest["owner_public_key"] = "" updateAllocationRequest["id"] = allocationID updateAllocationRequest["size"] = size @@ -164,7 +164,7 @@ func UpdateAllocation( Name: transaction.STORAGESC_UPDATE_ALLOCATION, InputArgs: updateAllocationRequest, } - hash, _, nonce, _, err = storageSmartContractTxnValue(sn, lock) + hash, _, nonce, _, err = StorageSmartContractTxnValue(sn, lock, clients...) return } @@ -176,7 +176,7 @@ func UpdateAllocation( // - providerID: provider ID // - value: value to lock // - fee: transaction fee -func StakePoolLock(providerType ProviderType, providerID string, value, fee uint64) (hash string, nonce int64, err error) { +func StakePoolLock(providerType ProviderType, providerID string, value, fee uint64, clients ...string) (hash string, nonce int64, err error) { if !client.IsSDKInitialized() { return "", 0, sdkNotInitialized } @@ -213,7 +213,7 @@ func StakePoolLock(providerType ProviderType, providerID string, value, fee uint return "", 0, errors.Newf("stake_pool_lock", "unsupported provider type: %v", providerType) } - hash, _, nonce, _, err = transaction.SmartContractTxnValueFeeWithRetry(scAddress, sn, value, fee, true) + hash, _, nonce, _, err = transaction.SmartContractTxnValueFeeWithRetry(scAddress, sn, value, fee, true, clients...) return } @@ -226,7 +226,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 string, fee uint64, clients ...string) (unstake int64, nonce int64, err error) { if !client.IsSDKInitialized() { return 0, 0, sdkNotInitialized } @@ -264,7 +264,7 @@ func StakePoolUnlock(providerType ProviderType, providerID string, fee uint64) ( } var out string - if _, out, nonce, _, err = transaction.SmartContractTxnValueFeeWithRetry(scAddress, sn, 0, fee, true); err != nil { + if _, out, nonce, _, err = transaction.SmartContractTxnValueFeeWithRetry(scAddress, sn, 0, fee, true, clients...); err != nil { return // an error } @@ -280,7 +280,7 @@ func StakePoolUnlock(providerType ProviderType, providerID string, fee uint64) ( // - allocID: allocation ID // - tokens: number of tokens to lock // - fee: transaction fee -func WritePoolLock(allocID string, tokens, fee uint64) (hash string, nonce int64, err error) { +func WritePoolLock(allocID string, tokens, fee uint64, clients ...string) (hash string, nonce int64, err error) { if !client.IsSDKInitialized() { return "", 0, sdkNotInitialized } @@ -297,14 +297,14 @@ func WritePoolLock(allocID string, tokens, fee uint64) (hash string, nonce int64 InputArgs: &req, } - hash, _, nonce, _, err = transaction.SmartContractTxnValueFeeWithRetry(STORAGE_SCADDRESS, sn, tokens, fee, true) + hash, _, nonce, _, err = transaction.SmartContractTxnValueFeeWithRetry(STORAGE_SCADDRESS, sn, tokens, fee, true, clients...) return } // WritePoolUnlock unlocks ALL tokens of a write pool. Needs to be cancelled first. // - allocID: allocation ID // - fee: transaction fee -func WritePoolUnlock(allocID string, fee uint64) (hash string, nonce int64, err error) { +func WritePoolUnlock(allocID string, fee uint64, clients ...string) (hash string, nonce int64, err error) { if !client.IsSDKInitialized() { return "", 0, sdkNotInitialized } @@ -320,7 +320,7 @@ func WritePoolUnlock(allocID string, fee uint64) (hash string, nonce int64, err Name: transaction.STORAGESC_WRITE_POOL_UNLOCK, InputArgs: &req, } - hash, _, nonce, _, err = transaction.SmartContractTxnValueFeeWithRetry(STORAGE_SCADDRESS, sn, 0, fee, true) + hash, _, nonce, _, err = transaction.SmartContractTxnValueFeeWithRetry(STORAGE_SCADDRESS, sn, 0, fee, true, clients...) return } diff --git a/zboxcore/sdk/sdk.go b/zboxcore/sdk/sdk.go index 65948326a..1bd51a780 100644 --- a/zboxcore/sdk/sdk.go +++ b/zboxcore/sdk/sdk.go @@ -802,11 +802,11 @@ type CreateAllocationOptions struct { // - options is the options struct instance for creating the allocation. // // returns the hash of the new_allocation_request transaction, the nonce of the transaction, the transaction object and an error if any. -func CreateAllocationWith(options CreateAllocationOptions) ( +func CreateAllocationWith(options CreateAllocationOptions, clients ...string) ( string, int64, *transaction.Transaction, error) { - return CreateAllocationForOwner(client.Id(), - client.PublicKey(), options.DataShards, options.ParityShards, + return CreateAllocationForOwner(client.Id(clients...), + client.PublicKey(clients...), options.DataShards, options.ParityShards, options.Size, options.ReadPrice, options.WritePrice, options.Lock, options.BlobberIds, options.BlobberAuthTickets, options.ThirdPartyExtendable, options.IsEnterprise, options.Force, options.FileOptionsParams) } @@ -981,7 +981,7 @@ func GetFreeAllocationBlobbers(request map[string]interface{}) ([]string, error) // - totalLimit is the total limit of the assigner for all free allocation requests. // // returns the hash of the transaction, the nonce of the transaction and an error if any. -func AddFreeStorageAssigner(name, publicKey string, individualLimit, totalLimit float64) (string, int64, error) { +func AddFreeStorageAssigner(name, publicKey string, individualLimit, totalLimit float64, clients ...string) (string, int64, error) { if !client.IsSDKInitialized() { return "", 0, sdkNotInitialized } @@ -997,7 +997,7 @@ func AddFreeStorageAssigner(name, publicKey string, individualLimit, totalLimit Name: transaction.ADD_FREE_ALLOCATION_ASSIGNER, InputArgs: input, } - hash, _, n, _, err := storageSmartContractTxn(sn) + hash, _, n, _, err := StorageSmartContractTxn(sn, clients...) return hash, n, err } @@ -1007,7 +1007,7 @@ func AddFreeStorageAssigner(name, publicKey string, individualLimit, totalLimit // - allocID is the id of the allocation. // // returns the hash of the transaction, the nonce of the transaction and an error if any. -func FinalizeAllocation(allocID string) (hash string, nonce int64, err error) { +func FinalizeAllocation(allocID string, clients ...string) (hash string, nonce int64, err error) { if !client.IsSDKInitialized() { return "", 0, sdkNotInitialized } @@ -1015,7 +1015,7 @@ func FinalizeAllocation(allocID string) (hash string, nonce int64, err error) { Name: transaction.STORAGESC_FINALIZE_ALLOCATION, InputArgs: map[string]interface{}{"allocation_id": allocID}, } - hash, _, nonce, _, err = storageSmartContractTxn(sn) + hash, _, nonce, _, err = StorageSmartContractTxn(sn, clients...) return } @@ -1024,7 +1024,7 @@ func FinalizeAllocation(allocID string) (hash string, nonce int64, err error) { // - allocID is the id of the allocation. // // returns the hash of the transaction, the nonce of the transaction and an error if any. -func CancelAllocation(allocID string) (hash string, nonce int64, err error) { +func CancelAllocation(allocID string, clients ...string) (hash string, nonce int64, err error) { if !client.IsSDKInitialized() { return "", 0, sdkNotInitialized } @@ -1032,7 +1032,7 @@ func CancelAllocation(allocID string) (hash string, nonce int64, err error) { Name: transaction.STORAGESC_CANCEL_ALLOCATION, InputArgs: map[string]interface{}{"allocation_id": allocID}, } - hash, _, nonce, _, err = storageSmartContractTxn(sn) + hash, _, nonce, _, err = StorageSmartContractTxn(sn, clients...) return } @@ -1050,7 +1050,7 @@ const ( // KillProvider kills a blobber or a validator (txn: `storagesc.kill_blobber` or `storagesc.kill_validator`) // - providerId is the id of the provider. // - providerType` is the type of the provider, either 3 for `ProviderBlobber` or 4 for `ProviderValidator. -func KillProvider(providerId string, providerType ProviderType) (string, int64, error) { +func KillProvider(providerId string, providerType ProviderType, clients ...string) (string, int64, error) { if !client.IsSDKInitialized() { return "", 0, sdkNotInitialized } @@ -1069,14 +1069,14 @@ func KillProvider(providerId string, providerType ProviderType) (string, int64, default: return "", 0, fmt.Errorf("kill provider type %v not implimented", providerType) } - hash, _, n, _, err := storageSmartContractTxn(sn) + hash, _, n, _, err := StorageSmartContractTxn(sn, clients...) return hash, n, err } // ShutdownProvider shuts down a blobber or a validator (txn: `storagesc.shutdown_blobber` or `storagesc.shutdown_validator`) // - providerId is the id of the provider. // - providerType` is the type of the provider, either 3 for `ProviderBlobber` or 4 for `ProviderValidator. -func ShutdownProvider(providerType ProviderType, providerID string) (string, int64, error) { +func ShutdownProvider(providerType ProviderType, providerID string, clients ...string) (string, int64, error) { if !client.IsSDKInitialized() { return "", 0, sdkNotInitialized } @@ -1096,14 +1096,14 @@ func ShutdownProvider(providerType ProviderType, providerID string) (string, int default: return "", 0, fmt.Errorf("shutdown provider type %v not implimented", providerType) } - hash, _, n, _, err := storageSmartContractTxn(sn) + hash, _, n, _, err := StorageSmartContractTxn(sn, clients...) return hash, n, err } // CollectRewards collects the rewards for a provider (txn: `storagesc.collect_reward`) // - providerId is the id of the provider. // - providerType is the type of the provider. -func CollectRewards(providerId string, providerType ProviderType) (string, int64, error) { +func CollectRewards(providerId string, providerType ProviderType, clients ...string) (string, int64, error) { if !client.IsSDKInitialized() { return "", 0, sdkNotInitialized } @@ -1143,7 +1143,7 @@ func CollectRewards(providerId string, providerType ProviderType) (string, int64 // - newOwnerPublicKey is the public key of the new owner. // // returns the hash of the transaction, the nonce of the transaction and an error if any. -func TransferAllocation(allocationId, newOwner, newOwnerPublicKey string) (string, int64, error) { +func TransferAllocation(allocationId, newOwner, newOwnerPublicKey string, clients ...string) (string, int64, error) { if !client.IsSDKInitialized() { return "", 0, sdkNotInitialized } @@ -1170,13 +1170,13 @@ func TransferAllocation(allocationId, newOwner, newOwnerPublicKey string) (strin Name: transaction.STORAGESC_UPDATE_ALLOCATION, InputArgs: allocationRequest, } - hash, _, n, _, err := storageSmartContractTxn(sn) + hash, _, n, _, err := StorageSmartContractTxn(sn, clients...) return hash, n, err } // UpdateBlobberSettings updates the settings of a blobber (txn: `storagesc.update_blobber_settings`) // - blob is the update blobber request inputs. -func UpdateBlobberSettings(blob *UpdateBlobber) (resp string, nonce int64, err error) { +func UpdateBlobberSettings(blob *UpdateBlobber, clients ...string) (resp string, nonce int64, err error) { if !client.IsSDKInitialized() { return "", 0, sdkNotInitialized } @@ -1184,13 +1184,13 @@ func UpdateBlobberSettings(blob *UpdateBlobber) (resp string, nonce int64, err e Name: transaction.STORAGESC_UPDATE_BLOBBER_SETTINGS, InputArgs: blob, } - resp, _, nonce, _, err = storageSmartContractTxn(sn) + resp, _, nonce, _, err = StorageSmartContractTxn(sn, clients...) return } // UpdateValidatorSettings updates the settings of a validator (txn: `storagesc.update_validator_settings`) // - v is the update validator request inputs. -func UpdateValidatorSettings(v *UpdateValidator) (resp string, nonce int64, err error) { +func UpdateValidatorSettings(v *UpdateValidator, clients ...string) (resp string, nonce int64, err error) { if !client.IsSDKInitialized() { return "", 0, sdkNotInitialized } @@ -1199,13 +1199,13 @@ func UpdateValidatorSettings(v *UpdateValidator) (resp string, nonce int64, err Name: transaction.STORAGESC_UPDATE_VALIDATOR_SETTINGS, InputArgs: v.ConvertToValidationNode(), } - resp, _, nonce, _, err = storageSmartContractTxn(sn) + resp, _, nonce, _, err = StorageSmartContractTxn(sn, clients...) return } // ResetBlobberStats resets the stats of a blobber (txn: `storagesc.reset_blobber_stats`) // - rbs is the reset blobber stats dto, contains the blobber id and its stats. -func ResetBlobberStats(rbs *ResetBlobberStatsDto) (string, int64, error) { +func ResetBlobberStats(rbs *ResetBlobberStatsDto, clients ...string) (string, int64, error) { if !client.IsSDKInitialized() { return "", 0, sdkNotInitialized } @@ -1214,7 +1214,7 @@ func ResetBlobberStats(rbs *ResetBlobberStatsDto) (string, int64, error) { Name: transaction.STORAGESC_RESET_BLOBBER_STATS, InputArgs: rbs, } - hash, _, n, _, err := storageSmartContractTxn(sn) + hash, _, n, _, err := StorageSmartContractTxn(sn, clients...) return hash, n, err } @@ -1222,7 +1222,7 @@ type StorageNodeIdField struct { Id string `json:"id"` } -func ResetBlobberVersion(snId *StorageNodeIdField) (string, int64, error) { +func ResetBlobberVersion(snId *StorageNodeIdField, clients ...string) (string, int64, error) { if !client.IsSDKInitialized() { return "", 0, sdkNotInitialized } @@ -1231,11 +1231,11 @@ func ResetBlobberVersion(snId *StorageNodeIdField) (string, int64, error) { Name: transaction.STORAGESC_RESET_BLOBBER_VERSION, InputArgs: snId, } - hash, _, n, _, err := storageSmartContractTxn(sn) + hash, _, n, _, err := StorageSmartContractTxn(sn, clients...) return hash, n, err } -func InsertKilledProviderID(snId *StorageNodeIdField) (string, int64, error) { +func InsertKilledProviderID(snId *StorageNodeIdField, clients ...string) (string, int64, error) { if !client.IsSDKInitialized() { return "", 0, sdkNotInitialized } @@ -1244,11 +1244,11 @@ func InsertKilledProviderID(snId *StorageNodeIdField) (string, int64, error) { Name: transaction.STORAGESC_INSERT_KILLED_PROVIDER_ID, InputArgs: snId, } - hash, _, n, _, err := storageSmartContractTxn(sn) + hash, _, n, _, err := StorageSmartContractTxn(sn, clients...) return hash, n, err } -func ResetAllocationStats(allocationId string) (string, int64, error) { +func ResetAllocationStats(allocationId string, clients ...string) (string, int64, error) { if !client.IsSDKInitialized() { return "", 0, sdkNotInitialized } @@ -1257,27 +1257,21 @@ func ResetAllocationStats(allocationId string) (string, int64, error) { Name: transaction.STORAGESC_RESET_ALLOCATION_STATS, InputArgs: allocationId, } - hash, _, n, _, err := storageSmartContractTxn(sn) + hash, _, n, _, err := StorageSmartContractTxn(sn, clients...) return hash, n, err } -func StorageSmartContractTxn(sn transaction.SmartContractTxnData) ( +func StorageSmartContractTxn(sn transaction.SmartContractTxnData, clients ...string) ( hash, out string, nonce int64, txn *transaction.Transaction, err error) { - return storageSmartContractTxnValue(sn, 0) + return StorageSmartContractTxnValue(sn, 0, clients...) } -func storageSmartContractTxn(sn transaction.SmartContractTxnData) ( - hash, out string, nonce int64, txn *transaction.Transaction, err error) { - - return storageSmartContractTxnValue(sn, 0) -} - -func storageSmartContractTxnValue(sn transaction.SmartContractTxnData, value uint64) ( +func StorageSmartContractTxnValue(sn transaction.SmartContractTxnData, value uint64, clients ...string) ( hash, out string, nonce int64, txn *transaction.Transaction, err error) { // Fee is set during sdk initialization. - return transaction.SmartContractTxnValueFeeWithRetry(STORAGE_SCADDRESS, sn, value, client.TxnFee(), true) + return transaction.SmartContractTxnValueFeeWithRetry(STORAGE_SCADDRESS, sn, value, client.TxnFee(), true, clients...) } func CommitToFabric(metaTxnData, fabricConfigJSON string) (string, error) { From 7f125fce9aceeb040f1e3d3921ccd4df26adf3a5 Mon Sep 17 00:00:00 2001 From: Jayash Satolia Date: Fri, 3 Jan 2025 20:54:16 +0530 Subject: [PATCH 02/13] Empty commit From 479aebcea18a1fa9fd120dee102db03f350a2734 Mon Sep 17 00:00:00 2001 From: yash10019coder Date: Fri, 24 Jan 2025 00:58:03 +0530 Subject: [PATCH 03/13] * SendWithCustom fee method. * UpdateAllocation fixes. * GetLatestFinalizedBlocks method. --- zboxcore/sdk/allocation.go | 2 +- zboxcore/sdk/blobber_operations.go | 13 +++++++------ zcncore/execute_transactions.go | 12 ++++++++++++ zcncore/get_data.go | 21 +++++++++++++++++---- 4 files changed, 37 insertions(+), 11 deletions(-) diff --git a/zboxcore/sdk/allocation.go b/zboxcore/sdk/allocation.go index 1162f2342..cf23ac20c 100644 --- a/zboxcore/sdk/allocation.go +++ b/zboxcore/sdk/allocation.go @@ -465,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, 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 diff --git a/zboxcore/sdk/blobber_operations.go b/zboxcore/sdk/blobber_operations.go index 456547d2e..d6f93f22a 100644 --- a/zboxcore/sdk/blobber_operations.go +++ b/zboxcore/sdk/blobber_operations.go @@ -34,7 +34,7 @@ func CreateAllocationForOwner( owner, ownerpublickey string, datashards, parityshards int, size int64, readPrice, writePrice PriceRange, - lock uint64, preferredBlobberIds, blobberAuthTickets []string, thirdPartyExtendable, IsEnterprise, force bool, fileOptionsParams *FileOptionsParameters, authRoundExpiry int64, clients ...string + lock uint64, preferredBlobberIds, blobberAuthTickets []string, thirdPartyExtendable, IsEnterprise, force bool, fileOptionsParams *FileOptionsParameters, authRoundExpiry int64, clients ...string, ) (hash string, nonce int64, txn *transaction.Transaction, err error) { if lock > math.MaxInt64 { @@ -76,7 +76,7 @@ func CreateAllocationForOwner( Name: transaction.NEW_ALLOCATION_REQUEST, InputArgs: allocationRequest, } - hash, _, nonce, txn, err = storageSmartContractTxnValue(sn, lock) + hash, _, nonce, txn, err = StorageSmartContractTxnValue(sn, lock) return } @@ -108,7 +108,7 @@ func CreateFreeAllocation(marker string, value uint64) (string, int64, error) { Name: transaction.NEW_FREE_ALLOCATION, InputArgs: input, } - hash, _, n, _, err := storageSmartContractTxnValue(sn, value) + hash, _, n, _, err := StorageSmartContractTxnValue(sn, value) return hash, n, err } @@ -132,6 +132,7 @@ func UpdateAllocation( lock uint64, addBlobberId, addBlobberAuthTicket, removeBlobberId, ownerID, ownerSigninPublicKey string, setThirdPartyExtendable bool, fileOptionsParams *FileOptionsParameters, ticket string, + clients ...string, ) (hash string, nonce int64, err error) { if ownerID == "" { ownerID = client.Id() @@ -185,7 +186,7 @@ func UpdateAllocation( Name: transaction.STORAGESC_UPDATE_ALLOCATION, InputArgs: updateAllocationRequest, } - hash, _, nonce, _, err = storageSmartContractTxnValue(sn, lock) + hash, _, nonce, _, err = StorageSmartContractTxnValue(sn, lock, clients...) return } @@ -208,7 +209,7 @@ func GetUpdateAllocTicket(allocationID, userID, operationType string, roundExpir // - providerID: provider ID // - value: value to lock // - fee: transaction fee -func StakePoolLock(providerType ProviderType, providerID string, value, fee uint64) (hash string, nonce int64, err error) { +func StakePoolLock(providerType ProviderType, providerID string, value, fee uint64, clients ...string) (hash string, nonce int64, err error) { if !client.IsSDKInitialized() { return "", 0, sdkNotInitialized } @@ -245,7 +246,7 @@ func StakePoolLock(providerType ProviderType, providerID string, value, fee uint return "", 0, errors.Newf("stake_pool_lock", "unsupported provider type: %v", providerType) } - hash, _, nonce, _, err = transaction.SmartContractTxnValueFeeWithRetry(scAddress, sn, value, fee, true) + hash, _, nonce, _, err = transaction.SmartContractTxnValueFeeWithRetry(scAddress, sn, value, fee, true, clients...) return } diff --git a/zcncore/execute_transactions.go b/zcncore/execute_transactions.go index 45c49a5ad..bdd80f4a1 100644 --- a/zcncore/execute_transactions.go +++ b/zcncore/execute_transactions.go @@ -213,6 +213,18 @@ func Send(toClientID string, tokens uint64, desc string, client ...string) (hash InputArgs: SendTxnData{Note: desc}, }, tokens, true, client...) } +func SendWithCustomFee(toClientID string, tokens, fee uint64, desc string, client ...string) (hash, out string, nonce int64, txn *transaction.Transaction, err error) { + if len(client) == 0 { + client = append(client, "") + client = append(client, toClientID) + } else { + client = append(client, toClientID) + } + return transaction.SmartContractTxnValueFee(MinerSmartContractAddress, transaction.SmartContractTxnData{ + Name: "transfer", + InputArgs: SendTxnData{Note: desc}, + }, tokens, fee, true, client...) +} func Faucet(tokens uint64, input string, client ...string) (hash, out string, nonce int64, txn *transaction.Transaction, err error) { return transaction.SmartContractTxnValue(FaucetSmartContractAddress, transaction.SmartContractTxnData{ diff --git a/zcncore/get_data.go b/zcncore/get_data.go index f6f752e76..2cf80c270 100644 --- a/zcncore/get_data.go +++ b/zcncore/get_data.go @@ -197,10 +197,6 @@ func (p Params) Query() string { return "?" + params.Encode() } -func withParams(uri string, params Params) string { //nolint:unused - return uri + params.Query() -} - // GetBlobberSnapshots obtains list of allocations of a blobber. // Blobber snapshots are historical records of the blobber instance to track its change over time and serve graph requests, // which are requests that need multiple data points, distributed over an interval of time, usually to plot them on a @@ -296,6 +292,23 @@ func GetLatestFinalizedMagicBlock() (m *block.MagicBlock, err error) { return resp.MagicBlock, nil } +// GetLatestFinalizedBlock gets latest finalized block +func GetLatestFinalizedBlock() (*block.Block, error) { + res, err := screstapi.MakeSCRestAPICall("", GET_LATEST_FINALIZED, nil, "") + if err != nil { + return nil, err + } + + var resp block.Block + + err = json.Unmarshal(res, &resp) + if err != nil { + return nil, err + } + + return &resp, nil +} + // GetMinerSCUserInfo retrieve user stake pools for the providers related to the Miner SC (miners/sharders). // - clientID: user's wallet id func GetMinerSCUserInfo(clientID string) ([]byte, error) { From b58111fc20174b1ce54484be96e41835c09ceaa4 Mon Sep 17 00:00:00 2001 From: yash10019coder Date: Sun, 26 Jan 2025 16:01:28 +0530 Subject: [PATCH 04/13] * add blobber sc --- core/transaction/entity.go | 1 + zcncore/execute_transactions.go | 7 +++++++ 2 files changed, 8 insertions(+) diff --git a/core/transaction/entity.go b/core/transaction/entity.go index 995a45954..2cbbdb9f0 100644 --- a/core/transaction/entity.go +++ b/core/transaction/entity.go @@ -135,6 +135,7 @@ const ( STORAGESC_RESET_ALLOCATION_STATS = "reset_allocation_stats" STORAGESC_RESET_BLOBBER_VERSION = "update_blobber_version" STORAGESC_INSERT_KILLED_PROVIDER_ID = "insert_killed_provider_id" + STORAGESC_ADD_BLOBBER = "add_blobber" MINERSC_LOCK = "addToDelegatePool" MINERSC_UNLOCK = "deleteFromDelegatePool" diff --git a/zcncore/execute_transactions.go b/zcncore/execute_transactions.go index 83ebdefdb..eb12b3f9d 100644 --- a/zcncore/execute_transactions.go +++ b/zcncore/execute_transactions.go @@ -114,6 +114,13 @@ func MinerScUpdateGlobals(input interface{}, client ...string) (hash, out string } +func StorageSCAddBlobber(input interface{}, client ...string) (hash, out string, nonce int64, txn *transaction.Transaction, err error) { + return transaction.SmartContractTxn(StorageSmartContractAddress, transaction.SmartContractTxnData{ + Name: transaction.STORAGESC_ADD_BLOBBER, + InputArgs: input, + }, true, client...) +} + func StorageScUpdateConfig(input interface{}, client ...string) (hash, out string, nonce int64, txn *transaction.Transaction, err error) { return transaction.SmartContractTxn(StorageSmartContractAddress, transaction.SmartContractTxnData{ Name: transaction.STORAGESC_UPDATE_SETTINGS, From ac5667c604127e759a645d5b2f1c9442416e1487 Mon Sep 17 00:00:00 2001 From: yash10019coder Date: Sun, 26 Jan 2025 17:15:07 +0530 Subject: [PATCH 05/13] nit fix update allocation. --- mobilesdk/sdk/sdk.go | 4 ++-- wasmsdk/allocation.go | 8 +++----- zboxcore/sdk/allocation.go | 2 +- 3 files changed, 6 insertions(+), 8 deletions(-) diff --git a/mobilesdk/sdk/sdk.go b/mobilesdk/sdk/sdk.go index f5dd299c8..1e168574d 100644 --- a/mobilesdk/sdk/sdk.go +++ b/mobilesdk/sdk/sdk.go @@ -373,12 +373,12 @@ func (s *StorageSDK) GetVersion() string { // - extend: extend allocation // - allocationID: allocation ID // - lock: Number of tokens to lock to the allocation after the update -func (s *StorageSDK) UpdateAllocation(size, authRoundExpiry int64, extend bool, allocationID string, lock uint64) (hash string, err error) { +func (s *StorageSDK) UpdateAllocation(size, extend bool, allocationID string, lock uint64) (hash string, err error) { if lock > math.MaxInt64 { return "", errors.Errorf("int64 overflow in lock") } - hash, _, err = sdk.UpdateAllocation(size, authRoundExpiry, extend, allocationID, lock, "", "", "", "", "", false, &sdk.FileOptionsParameters{}, "") + hash, _, err = sdk.UpdateAllocation(size, extend, allocationID, lock, "", "", "", "", "", false, &sdk.FileOptionsParameters{}, "") return hash, err } diff --git a/wasmsdk/allocation.go b/wasmsdk/allocation.go index 25aae7861..2285670d6 100644 --- a/wasmsdk/allocation.go +++ b/wasmsdk/allocation.go @@ -162,8 +162,7 @@ func transferAllocation(allocationID, newOwnerId, newOwnerPublicKey string) erro func UpdateForbidAllocation(allocationID string, forbidupload, forbiddelete, forbidupdate, forbidmove, forbidcopy, forbidrename bool) (string, error) { hash, _, err := sdk.UpdateAllocation( - 0, //size, - 0, + 0, //size, false, //extend, allocationID, // allocID, 0, //lock, @@ -194,8 +193,7 @@ func UpdateForbidAllocation(allocationID string, forbidupload, forbiddelete, for func freezeAllocation(allocationID string) (string, error) { hash, _, err := sdk.UpdateAllocation( - 0, //size, - 0, + 0, //size, false, //extend, allocationID, // allocID, 0, //lock, @@ -305,7 +303,7 @@ func updateAllocation(allocationID string, size, authRoundExpiry int64, extend bool, lock int64, addBlobberId, addBlobberAuthTicket, removeBlobberId, ownerSigninPublicKey string, setThirdPartyExtendable bool) (string, error) { - hash, _, err := sdk.UpdateAllocation(size, authRoundExpiry, 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) diff --git a/zboxcore/sdk/allocation.go b/zboxcore/sdk/allocation.go index cf23ac20c..89b35f456 100644 --- a/zboxcore/sdk/allocation.go +++ b/zboxcore/sdk/allocation.go @@ -3236,7 +3236,7 @@ func (a *Allocation) UpdateWithStatus( } l.Logger.Info("Updating allocation") - hash, _, err := UpdateAllocation(size, authRoundExpiry, extend, a.ID, lock, addBlobberId, addBlobberAuthTicket, removeBlobberId, "", ownerSigninPublicKey, setThirdPartyExtendable, fileOptionsParams, updateAllocTicket) + hash, _, err := UpdateAllocation(size, extend, a.ID, lock, addBlobberId, addBlobberAuthTicket, removeBlobberId, "", ownerSigninPublicKey, setThirdPartyExtendable, fileOptionsParams, updateAllocTicket) if err != nil { return alloc, "", isRepairRequired, err } From 63540e66f07b6971e796c3729d8af877c6d9ba82 Mon Sep 17 00:00:00 2001 From: yash10019coder Date: Sat, 8 Feb 2025 04:59:28 +0530 Subject: [PATCH 06/13] debug: memory leak/ --- core/util/httpnet.go | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/core/util/httpnet.go b/core/util/httpnet.go index bcc5f2b77..f514b5859 100644 --- a/core/util/httpnet.go +++ b/core/util/httpnet.go @@ -103,13 +103,15 @@ func httpDo(req *http.Request, ctx context.Context, cncl context.CancelFunc, f f // NewHTTPGetRequest create a GetRequest instance with 60s timeout func NewHTTPGetRequest(url string) (*GetRequest, error) { var ctx, cancel = context.WithTimeout(context.Background(), 60*time.Second) - go func() { - //call cancel to avoid memory leak here - <-ctx.Done() - - cancel() - - }() + defer cancel() + //TODO: debug memory leak. + //go func() { + // //call cancel to avoid memory leak here + // <-ctx.Done() + // + // cancel() + // + //}() return NewHTTPGetRequestContext(ctx, url) } From b16b85b7781ba88c0c13f675d16e0006694879d5 Mon Sep 17 00:00:00 2001 From: yash10019coder Date: Sat, 8 Feb 2025 05:09:37 +0530 Subject: [PATCH 07/13] fix: syntax issue. --- zcncore/get_data.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/zcncore/get_data.go b/zcncore/get_data.go index 35d67e995..c2a42b543 100644 --- a/zcncore/get_data.go +++ b/zcncore/get_data.go @@ -293,7 +293,7 @@ func GetLatestFinalizedMagicBlock() (m *block.MagicBlock, err error) { // GetLatestFinalizedBlock gets latest finalized block func GetLatestFinalizedBlock() (*block.Block, error) { - res, err := screstapi.MakeSCRestAPICall("", GET_LATEST_FINALIZED, nil, "") + res, err := client.MakeSCRestAPICall("", GET_LATEST_FINALIZED, nil, "") if err != nil { return nil, err } From d8022a6a1866cbb79ed0f0d6da278cbb091da01c Mon Sep 17 00:00:00 2001 From: yash10019coder Date: Sat, 8 Feb 2025 05:13:48 +0530 Subject: [PATCH 08/13] fix --- mobilesdk/sdk/sdk.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mobilesdk/sdk/sdk.go b/mobilesdk/sdk/sdk.go index 8c338422e..8e1aeb900 100644 --- a/mobilesdk/sdk/sdk.go +++ b/mobilesdk/sdk/sdk.go @@ -366,7 +366,7 @@ func (s *StorageSDK) GetVersion() string { // - extend: extend allocation // - allocationID: allocation ID // - lock: Number of tokens to lock to the allocation after the update -func (s *StorageSDK) UpdateAllocation(size, extend bool, allocationID string, lock uint64) (hash string, err error) { +func (s *StorageSDK) UpdateAllocation(size int64, extend bool, allocationID string, lock uint64) (hash string, err error) { if lock > math.MaxInt64 { return "", errors.Errorf("int64 overflow in lock") } From b42d0cbfd7402a842ffc69a2ed821e808a6b40ee Mon Sep 17 00:00:00 2001 From: yash10019coder Date: Sun, 9 Feb 2025 04:45:12 +0530 Subject: [PATCH 09/13] fix mobilesdk. --- zcncore/get_data.go | 2 +- zcncore/wallet.go | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/zcncore/get_data.go b/zcncore/get_data.go index c2a42b543..d332804d9 100644 --- a/zcncore/get_data.go +++ b/zcncore/get_data.go @@ -172,7 +172,7 @@ func getTokenUSDRate() (float64, error) { } // getWallet get a wallet object from a wallet string -func getWallet(walletStr string) (*zcncrypto.Wallet, error) { +func GetWalletZcncore(walletStr string) (*zcncrypto.Wallet, error) { var w zcncrypto.Wallet err := json.Unmarshal([]byte(walletStr), &w) if err != nil { diff --git a/zcncore/wallet.go b/zcncore/wallet.go index f564819e1..4c06fb2ee 100644 --- a/zcncore/wallet.go +++ b/zcncore/wallet.go @@ -10,7 +10,7 @@ import ( // GetWallet get a wallet object from a wallet string func GetWallet(walletStr string) (*zcncrypto.Wallet, error) { - return getWallet(walletStr) + return GetWalletZcncore(walletStr) } // Deprecated: use Sign() method in zcncrypto.Wallet From deea5875e0261bc4b574032207afed800173f2ee Mon Sep 17 00:00:00 2001 From: yash10019coder Date: Sun, 9 Feb 2025 05:04:42 +0530 Subject: [PATCH 10/13] fix unit tests. --- core/conf/config_test.go | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/core/conf/config_test.go b/core/conf/config_test.go index 8762195bd..b07f99410 100644 --- a/core/conf/config_test.go +++ b/core/conf/config_test.go @@ -11,6 +11,7 @@ func TestLoadConfig(t *testing.T) { var mockDefaultReader = func() Reader { reader := &mocks.Reader{} + reader.On("GetString", "ethereum_node_url").Return("https://127.0.0.1:8545") reader.On("GetString", "block_worker").Return("http://127.0.0.1:9091/dns") reader.On("GetString", "zauth_server").Return("http://127.0.0.1:8090/") reader.On("GetInt", "min_submit").Return(0) @@ -41,6 +42,7 @@ func TestLoadConfig(t *testing.T) { setup: func(t *testing.T) Reader { reader := &mocks.Reader{} + reader.On("GetString", "ethereum_node_url").Return("https://127.0.0.1:8545") reader.On("GetString", "block_worker").Return("") reader.On("GetString", "zauth_server").Return("") reader.On("GetInt", "min_submit").Return(0) @@ -86,6 +88,7 @@ func TestLoadConfig(t *testing.T) { setup: func(t *testing.T) Reader { reader := &mocks.Reader{} + reader.On("GetString", "ethereum_node_url").Return("https://127.0.0.1:8545") reader.On("GetString", "block_worker").Return("https://127.0.0.1:9091/dns") reader.On("GetString", "zauth_server").Return("http://127.0.0.1:8090/") reader.On("GetInt", "min_submit").Return(101) @@ -121,6 +124,7 @@ func TestLoadConfig(t *testing.T) { setup: func(t *testing.T) Reader { reader := &mocks.Reader{} + reader.On("GetString", "ethereum_node_url").Return("https://127.0.0.1:8545") reader.On("GetString", "block_worker").Return("https://127.0.0.1:9091/dns") reader.On("GetString", "zauth_server").Return("http://127.0.0.1:8090/") reader.On("GetInt", "min_submit").Return(0) From 8632996677add0b3677e736debd1f21fc062a2aa Mon Sep 17 00:00:00 2001 From: yash10019coder Date: Sun, 9 Feb 2025 06:48:08 +0530 Subject: [PATCH 11/13] revert memory leak code. --- core/util/httpnet.go | 16 +++++++--------- play.go | 36 ++++++++++++++++++++++++++++++++++-- 2 files changed, 41 insertions(+), 11 deletions(-) diff --git a/core/util/httpnet.go b/core/util/httpnet.go index f514b5859..bcc5f2b77 100644 --- a/core/util/httpnet.go +++ b/core/util/httpnet.go @@ -103,15 +103,13 @@ func httpDo(req *http.Request, ctx context.Context, cncl context.CancelFunc, f f // NewHTTPGetRequest create a GetRequest instance with 60s timeout func NewHTTPGetRequest(url string) (*GetRequest, error) { var ctx, cancel = context.WithTimeout(context.Background(), 60*time.Second) - defer cancel() - //TODO: debug memory leak. - //go func() { - // //call cancel to avoid memory leak here - // <-ctx.Done() - // - // cancel() - // - //}() + go func() { + //call cancel to avoid memory leak here + <-ctx.Done() + + cancel() + + }() return NewHTTPGetRequestContext(ctx, url) } diff --git a/play.go b/play.go index c88634db9..87703aba1 100644 --- a/play.go +++ b/play.go @@ -1,12 +1,44 @@ package main import ( - "fmt" + "github.com/0chain/gosdk/core/client" + "sync" "time" ) func main() { - fmt.Printf("Now = %v\n", Now()) + clientId := "90cbab94239ef9c74ec83f8e616fb921a5dd8b36214b1b514e5010b449e5f162" + + err := client.InitSDK("", "https://dev.zus.network/dns", "0afc093ffb509f059c55478bc1a60351cef7b4e9c008a53a6cc8241ca8617dfe", "bls0chain", 0, false) + if err != nil { + println(err.Error()) + return + } + + size := 1000 + resChan := make(chan *client.GetBalanceResponse, size) + + var wg sync.WaitGroup + for i := 0; i < size; i++ { + wg.Add(1) + go func() { + defer wg.Done() + res, err := client.GetBalance(clientId) + if err != nil { + println(i, err.Error()) + return + } + resChan <- res + }() + } + + wg.Wait() + + for i := 0; i < size; i++ { + res := <-resChan + println(i, res.Balance) + } + } // Timestamp represents Unix time (e.g. in seconds) From 03145dad466e7969d55eae13c544a81bae40d973 Mon Sep 17 00:00:00 2001 From: yash10019coder Date: Sun, 9 Feb 2025 08:15:48 +0530 Subject: [PATCH 12/13] fix: public key for multi wallet. --- core/client/set.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core/client/set.go b/core/client/set.go index 1c72c0625..ed3be78de 100644 --- a/core/client/set.go +++ b/core/client/set.go @@ -268,7 +268,7 @@ func PublicKey(clients ...string) string { fmt.Println("Public key is empty") return "" } - return client.wallets[clients[0]].ClientKey + return client.wallets[clients[0]].Keys[0].PublicKey } return client.wallet.ClientKey } From 79c07f024b96c0a946b76f900a8f5d6ae93eca45 Mon Sep 17 00:00:00 2001 From: yash10019coder Date: Thu, 20 Feb 2025 08:00:48 +0530 Subject: [PATCH 13/13] revert public key in set.go --- core/client/set.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core/client/set.go b/core/client/set.go index ed3be78de..1c72c0625 100644 --- a/core/client/set.go +++ b/core/client/set.go @@ -268,7 +268,7 @@ func PublicKey(clients ...string) string { fmt.Println("Public key is empty") return "" } - return client.wallets[clients[0]].Keys[0].PublicKey + return client.wallets[clients[0]].ClientKey } return client.wallet.ClientKey }