Skip to content

Commit

Permalink
wasm refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
smaulik13 committed Jan 16, 2025
1 parent fcc7676 commit 4273608
Show file tree
Hide file tree
Showing 22 changed files with 180 additions and 127 deletions.
2 changes: 1 addition & 1 deletion wasmsdk/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
# wasmsdk

```go
import "github.com/0chain/gosdk_common/wasmsdk"
import "github.com/0chain/gosdk/wasmsdk"
```

## Index
Expand Down
50 changes: 34 additions & 16 deletions wasmsdk/allocation.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ import (
"sync"
"syscall/js"

"github.com/0chain/gosdk/core/transaction"
"github.com/0chain/gosdk/wasmsdk/jsbridge"
"github.com/0chain/gosdk/zboxcore/sdk"
"github.com/0chain/gosdk_common/core/transaction"
"github.com/0chain/gosdk_common/wasmsdk/jsbridge"
)

const TOKEN_UNIT int64 = 1e10
Expand Down Expand Up @@ -68,7 +68,7 @@ func getAllocationBlobbers(preferredBlobberURLs []string,
return sdk.GetBlobberIds(preferredBlobberURLs)
}

return sdk.GetAllocationBlobbers(dataShards, parityShards, size, isRestricted, sdk.PriceRange{
return sdk.GetAllocationBlobbers(sdk.StorageV2, dataShards, parityShards, size, isRestricted, sdk.PriceRange{
Min: uint64(minReadPrice),
Max: uint64(maxReadPrice),
}, sdk.PriceRange{
Expand All @@ -88,7 +88,7 @@ func getAllocationBlobbers(preferredBlobberURLs []string,
// - lock is the lock value to add to the allocation.
// - blobberIds is the list of blobber ids.
// - blobberAuthTickets is the list of blobber auth tickets in case of using restricted blobbers.
func createAllocation(datashards, parityshards int, size int64,
func createAllocation(datashards, parityshards int, size, authRoundExpiry int64,
minReadPrice, maxReadPrice, minWritePrice, maxWritePrice int64, lock int64, blobberIds, blobberAuthTickets []string, setThirdPartyExtendable, IsEnterprise, force bool) (
*transaction.Transaction, error) {

Expand All @@ -108,8 +108,10 @@ func createAllocation(datashards, parityshards int, size int64,
BlobberIds: blobberIds,
ThirdPartyExtendable: setThirdPartyExtendable,
IsEnterprise: IsEnterprise,
StorageVersion: sdk.StorageV2,
BlobberAuthTickets: blobberAuthTickets,
Force: force,
AuthRoundExpiry: authRoundExpiry,
}

sdkLogger.Info(options)
Expand Down Expand Up @@ -160,14 +162,17 @@ 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, //size,
0,
false, //extend,
allocationID, // allocID,
0, //lock,
"", //addBlobberId,
"", //addBlobberAuthTicket
"", //removeBlobberId,
false, //thirdPartyExtendable,
"", //owner,
"", //ownerSigninPublicKey
false, // thirdPartyExtendable
&sdk.FileOptionsParameters{
ForbidUpload: sdk.FileOptionParam{Changed: forbidupload, Value: forbidupload},
ForbidDelete: sdk.FileOptionParam{Changed: forbiddelete, Value: forbiddelete},
Expand All @@ -176,6 +181,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 @@ -188,14 +194,17 @@ func UpdateForbidAllocation(allocationID string, forbidupload, forbiddelete, for
func freezeAllocation(allocationID string) (string, error) {

hash, _, err := sdk.UpdateAllocation(
0, //size,
0, //size,
0,
false, //extend,
allocationID, // allocID,
0, //lock,
"", //addBlobberId,
"", //addBlobberAuthTicket
"", //removeBlobberId,
false, //thirdPartyExtendable,
"", //owner,
"", //ownerSigninPublicKey
false, // thirdPartyExtendable
&sdk.FileOptionsParameters{
ForbidUpload: sdk.FileOptionParam{Changed: true, Value: true},
ForbidDelete: sdk.FileOptionParam{Changed: true, Value: true},
Expand All @@ -204,6 +213,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 @@ -238,10 +248,10 @@ func cancelAllocation(allocationID string) (string, error) {
// - addBlobberAuthTicket: blobber auth ticket to add to the allocation, in case of restricted blobbers
// - removeBlobberId: blobber ID to remove from the allocation
func updateAllocationWithRepair(allocationID string,
size int64,
size, authRoundExpiry int64,
extend bool,
lock int64,
addBlobberId, addBlobberAuthTicket, removeBlobberId, 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 @@ -258,7 +268,7 @@ func updateAllocationWithRepair(allocationID string,
}
}

alloc, hash, isRepairRequired, err := allocationObj.UpdateWithStatus(size, extend, uint64(lock), addBlobberId, addBlobberAuthTicket, removeBlobberId, false, &sdk.FileOptionsParameters{}, statusBar)
alloc, hash, isRepairRequired, err := allocationObj.UpdateWithStatus(size, authRoundExpiry, extend, uint64(lock), addBlobberId, addBlobberAuthTicket, removeBlobberId, ownerSigninPublicKey, false, &sdk.FileOptionsParameters{}, updateAllocTicket)
if err != nil {
return hash, err
}
Expand Down Expand Up @@ -292,10 +302,10 @@ func updateAllocationWithRepair(allocationID string,
// - removeBlobberId: blobber ID to remove from the allocation
// - setThirdPartyExtendable: third party extendable flag, if true, the allocation can be extended (in terms of size) by a non-owner client
func updateAllocation(allocationID string,
size int64, extend bool,
size, authRoundExpiry int64, extend bool,
lock int64,
addBlobberId, addBlobberAuthTicket, removeBlobberId string, setThirdPartyExtendable bool) (string, error) {
hash, _, err := sdk.UpdateAllocation(size, extend, allocationID, uint64(lock), addBlobberId, addBlobberAuthTicket, removeBlobberId, setThirdPartyExtendable, &sdk.FileOptionsParameters{})
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{}, "")

if err == nil {
clearAllocation(allocationID)
Expand All @@ -304,6 +314,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 @@ -398,8 +416,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
6 changes: 3 additions & 3 deletions wasmsdk/auth_txn.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ import (
"fmt"
"syscall/js"

"github.com/0chain/gosdk_common/core/sys"
"github.com/0chain/gosdk_common/wasmsdk/jsbridge"
"github.com/0chain/gosdk_common/zcncore"
"github.com/0chain/gosdk/core/sys"
"github.com/0chain/gosdk/wasmsdk/jsbridge"
"github.com/0chain/gosdk/zcncore"
)

type AuthCallbackFunc func(msg string) string
Expand Down
35 changes: 25 additions & 10 deletions wasmsdk/blobber.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,19 @@ import (
"syscall/js"
"time"

"github.com/0chain/gosdk_common/constants"
"github.com/0chain/gosdk_common/core/common"
"github.com/0chain/gosdk_common/core/encryption"
"github.com/0chain/gosdk_common/core/pathutil"
"github.com/0chain/gosdk_common/core/sys"
"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"
"github.com/0chain/gosdk/core/sys"
"github.com/hack-pad/safejs"

"github.com/0chain/gosdk/core/transaction"
"github.com/0chain/gosdk/wasmsdk/jsbridge"
"github.com/0chain/gosdk/zboxcore/fileref"
"github.com/0chain/gosdk/zboxcore/sdk"
"github.com/0chain/gosdk_common/core/transaction"
"github.com/0chain/gosdk_common/wasmsdk/jsbridge"
"github.com/0chain/gosdk_common/zboxcore/fileref"
"github.com/0chain/gosdk_common/zboxcore/zboxutil"
"github.com/0chain/gosdk/zboxcore/zboxutil"

"github.com/hack-pad/go-webworkers/worker"
)
Expand Down Expand Up @@ -478,7 +479,9 @@ func multiDownload(allocationID, jsonMultiDownloadOptions, authTicket, callbackF
}
var mf sys.File
if option.DownloadToDisk {
terminateWorkersWithAllocation(alloc)
if option.SuggestedName != "" {
fileName = option.SuggestedName
}
mf, err = jsbridge.NewFileWriter(fileName)
if err != nil {
PrintError(err.Error())
Expand Down Expand Up @@ -589,6 +592,7 @@ type MultiDownloadOption struct {
RemoteFileName string `json:"remoteFileName"` //Required only for file download with auth ticket
RemoteLookupHash string `json:"remoteLookupHash,omitempty"` //Required only for file download with auth ticket
DownloadToDisk bool `json:"downloadToDisk"`
SuggestedName string `json:"suggestedName,omitempty"` //Suggested name for the file when downloading to disk, if empty will use base of remote path
}

// MultiOperation do copy, move, delete and createdir operation together
Expand Down Expand Up @@ -1133,6 +1137,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 Expand Up @@ -1245,6 +1252,14 @@ func cancelDownloadDirectory(remotePath string) {
downloadDirLock.Unlock()
}

func cancelDownloadBlocks(allocationID, remotePath string, start, end int64) error {
alloc, err := getAllocation(allocationID)
if err != nil {
return err
}
return alloc.CancelDownloadBlocks(remotePath, start, end)
}

func startListener(respChan chan string) error {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
Expand Down
28 changes: 14 additions & 14 deletions wasmsdk/bridge.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,17 @@ import (
"context"
"encoding/base64"
"encoding/json"
"github.com/0chain/gosdk/core/client"
"github.com/0chain/gosdk/zcnbridge"
"github.com/0chain/gosdk/zcnbridge/errors"
"github.com/0chain/gosdk/zcnbridge/log"
"github.com/0chain/gosdk/zcncore"
"github.com/ethereum/go-ethereum/ethclient"
"path"
"strconv"

"github.com/0chain/gosdk_common/core/client"
"github.com/0chain/gosdk_common/zcnbridge"
"github.com/0chain/gosdk_common/zcnbridge/errors"
"github.com/0chain/gosdk_common/zcnbridge/log"
"github.com/0chain/gosdk_common/zcncore"
"github.com/ethereum/go-ethereum/ethclient"
)

var bridge *zcnbridge.BridgeClient
var bridge *zcnbridge.BridgeClient //nolint:unused

// initBridge initializes the bridge client
// - ethereumAddress: ethereum address of the wallet owner
Expand All @@ -26,7 +25,7 @@ var bridge *zcnbridge.BridgeClient
// - gasLimit: gas limit for the transactions
// - value: value to be sent with the transaction (unused)
// - consensusThreshold: consensus threshold for the transactions
func initBridge(
func initBridge( //nolint:unused
ethereumAddress string,
bridgeAddress string,
authorizersAddress string,
Expand Down Expand Up @@ -84,7 +83,9 @@ func burnZCN(amount uint64) string { //nolint
// - burnTrxHash: hash of the burn transaction
// - timeout: timeout in seconds
func mintZCN(burnTrxHash string, timeout int) string { //nolint
mintPayload, err := bridge.QueryZChainMintPayload(burnTrxHash)
mintPayload,

err := bridge.QueryZChainMintPayload(burnTrxHash)
if err != nil {
return errors.Wrap("mintZCN", "failed to QueryZChainMintPayload", err).Error()
}
Expand Down Expand Up @@ -114,19 +115,18 @@ func getMintWZCNPayload(burnTrxHash string) string { //nolint:unused
}

// getNotProcessedWZCNBurnEvents returns all not processed WZCN burn events from the Ethereum network
func getNotProcessedWZCNBurnEvents() string {
func getNotProcessedWZCNBurnEvents() string { //nolint:unused
var (
mintNonce int64
res []byte
err error
)

if res, err = zcncore.GetMintNonce(); err != nil {
return errors.Wrap("getNotProcessedWZCNBurnEvents", "failed to retreive last ZCN processed mint nonce", err).Error()
}

if err = json.Unmarshal(res, &mintNonce); err != nil {
return errors.New("getNotProcessedWZCNBurnEvents", "failed to unmarshall last ZCN processed mint nonce").Error()
return errors.Wrap("getNotProcessedWZCNBurnEvents", "failed to unmarshal last ZCN processed mint nonce", err).Error()
}

log.Logger.Debug("MintNonce = " + strconv.Itoa(int(mintNonce)))
Expand Down Expand Up @@ -162,7 +162,7 @@ func getNotProcessedZCNBurnTickets() string { //nolint:unused
}

if err = json.Unmarshal(res, &burnTickets); err != nil {
return errors.New("getNotProcessedZCNBurnTickets", "failed to unmarshall ZCN burn tickets").Error()
return errors.Wrap("getNotProcessedZCNBurnTickets", "failed to unmarshal ZCN burn tickets", err).Error()
}

var result []byte
Expand Down
4 changes: 2 additions & 2 deletions wasmsdk/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ import (
"errors"
"time"

"github.com/0chain/gosdk/core/client"
"github.com/0chain/gosdk/wasmsdk/jsbridge"
"github.com/0chain/gosdk/zboxcore/sdk"
"github.com/0chain/gosdk_common/core/client"
"github.com/0chain/gosdk_common/wasmsdk/jsbridge"
lru "github.com/hashicorp/golang-lru/v2"
)

Expand Down
2 changes: 1 addition & 1 deletion wasmsdk/chunked_upload_progress_storer.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"sync"

"github.com/0chain/gosdk/zboxcore/sdk"
"github.com/0chain/gosdk_common/zboxcore/zboxutil"
"github.com/0chain/gosdk/zboxcore/zboxutil"
)

// chunkedUploadProgressStorer load and save upload progress
Expand Down
10 changes: 1 addition & 9 deletions wasmsdk/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import (
"errors"
"syscall/js"

"github.com/0chain/gosdk/wasmsdk/jsbridge"
"github.com/0chain/gosdk/zboxcore/sdk"
"github.com/0chain/gosdk_common/wasmsdk/jsbridge"
)

// PrintError is to print to stderr
Expand Down Expand Up @@ -89,11 +89,3 @@ func (h *hasher) WriteToValidationMT(_ []byte) error {
func (h *hasher) Finalize() error {
return nil
}

func (h *hasher) GetBlockHash() (string, error) {
return "", nil
}

func (h *hasher) WriteToBlockHasher(buf []byte) error {
return nil
}
Loading

0 comments on commit 4273608

Please sign in to comment.