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/multi wallet #1619

Merged
merged 12 commits into from
Oct 22, 2024
2 changes: 1 addition & 1 deletion core/client/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@
if len(clientIDs) > 0 {
clientID = clientIDs[0]
} else {
clientID = ClientID()
clientID = Id()

Check failure on line 157 in core/client/http.go

View workflow job for this annotation

GitHub Actions / Build-android

undefined: Id

Check failure on line 157 in core/client/http.go

View workflow job for this annotation

GitHub Actions / Build-android

undefined: Id

Check failure on line 157 in core/client/http.go

View workflow job for this annotation

GitHub Actions / lint

undefined: Id) (typecheck)

Check failure on line 157 in core/client/http.go

View workflow job for this annotation

GitHub Actions / go-test

undefined: Id

Check failure on line 157 in core/client/http.go

View workflow job for this annotation

GitHub Actions / Build-windows

undefined: Id

Check failure on line 157 in core/client/http.go

View workflow job for this annotation

GitHub Actions / wasm-test

undefined: Id

Check failure on line 157 in core/client/http.go

View workflow job for this annotation

GitHub Actions / Build-wasm

undefined: Id
}

if res, err = MakeSCRestAPICall("", GetBalance, map[string]string{
Expand Down
34 changes: 21 additions & 13 deletions core/transaction/entity.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ const (
FEES_TABLE = `/v1/fees_table`
)

type SignFunc = func(msg string) (string, error)
type SignFunc = func(msg string, clientId ...string) (string, error)
type VerifyFunc = func(publicKey, signature, msgHash string) (bool, error)
type SignWithWallet = func(msg string, wallet interface{}) (string, error)

Expand Down Expand Up @@ -466,29 +466,34 @@ func GetFeesTable(miners []string, reqPercent ...float32) (map[string]map[string
return nil, errors.New("failed to get fees table", strings.Join(errs, ","))
}

func SmartContractTxn(scAddress string, sn SmartContractTxnData, toClient ...string) (
func SmartContractTxn(scAddress string, sn SmartContractTxnData, clients ...string) (
hash, out string, nonce int64, txn *Transaction, err error) {
return SmartContractTxnValue(scAddress, sn, 0, toClient...)
return SmartContractTxnValue(scAddress, sn, 0, clients...)
}

func SmartContractTxnValue(scAddress string, sn SmartContractTxnData, value uint64, toClient ...string) (
func SmartContractTxnValue(scAddress string, sn SmartContractTxnData, value uint64, clients ...string) (
hash, out string, nonce int64, txn *Transaction, err error) {

return SmartContractTxnValueFeeWithRetry(scAddress, sn, value, client.TxnFee(), toClient...)
return SmartContractTxnValueFeeWithRetry(scAddress, sn, value, client.TxnFee(), clients...)
}

func SmartContractTxnValueFeeWithRetry(scAddress string, sn SmartContractTxnData,
value, fee uint64, toClient ...string) (hash, out string, nonce int64, t *Transaction, err error) {
hash, out, nonce, t, err = SmartContractTxnValueFee(scAddress, sn, value, fee, toClient...)
value, fee uint64, clients ...string) (hash, out string, nonce int64, t *Transaction, err error) {
hash, out, nonce, t, err = SmartContractTxnValueFee(scAddress, sn, value, fee, clients...)

if err != nil && strings.Contains(err.Error(), "invalid transaction nonce") {
return SmartContractTxnValueFee(scAddress, sn, value, fee)
return SmartContractTxnValueFee(scAddress, sn, value, fee, clients...)
}
return
}

func SmartContractTxnValueFee(scAddress string, sn SmartContractTxnData,
value, fee uint64, toClient ...string) (hash, out string, nonce int64, t *Transaction, err error) {
value, fee uint64, clients ...string) (hash, out string, nonce int64, t *Transaction, err error) {

clientId := client.Id(clients...)
if len(clients) > 0 && clients[0] != "" {
clientId = clients[0]
}

var requestBytes []byte
if requestBytes, err = json.Marshal(sn); err != nil {
Expand All @@ -505,17 +510,20 @@ func SmartContractTxnValueFee(scAddress string, sn SmartContractTxnData,
return
}

txn := NewTransactionEntity(client.ClientID(),
cfg.ChainID, client.PublicKey(), nonce)
txn := NewTransactionEntity(client.Id(clientId),
cfg.ChainID, client.PublicKey(clientId), nonce)

txn.TransactionData = string(requestBytes)
txn.ToClientID = scAddress
txn.Value = value
txn.TransactionFee = fee
txn.TransactionType = TxnTypeSmartContract

if len(toClient) > 0 {
txn.ToClientID = toClient[0]
if len(clients) > 0 {
txn.ClientID = clients[0]
}
if len(clients) > 1 {
txn.ToClientID = clients[1]
txn.TransactionType = TxnTypeSend
}

Expand Down
4 changes: 2 additions & 2 deletions mobilesdk/zboxapi/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@
"context"
"errors"

"github.com/0chain/gosdk/core/client"

Check failure on line 16 in mobilesdk/zboxapi/client.go

View workflow job for this annotation

GitHub Actions / lint

could not import github.com/0chain/gosdk/core/client (-: # github.com/0chain/gosdk/core/client
"github.com/0chain/gosdk/core/logger"
"github.com/0chain/gosdk/zboxapi"
"github.com/0chain/gosdk/core/client"
"go.uber.org/zap"
)

Expand All @@ -33,7 +33,7 @@
zboxApiClient = zboxapi.NewClient()
zboxApiClient.SetRequest(baseUrl, appType)

err := SetWallet(client.ClientID(), client.PrivateKey(), client.PublicKey()) //nolint: errcheck
err := SetWallet(client.Id(), client.PrivateKey(), client.PublicKey()) //nolint: errcheck
if err != nil {
logging.Error("SetWallet", zap.Error(err))
}
Expand Down
2 changes: 1 addition & 1 deletion winsdk/zboxapi.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func InitZBox(zboxHost, zboxAppType *C.char) {
}

zboxApiClient.SetRequest(C.GoString(zboxHost), C.GoString(zboxAppType))
zboxApiClient.SetWallet(client.ClientID(), client.PrivateKey(), client.PublicKey())
zboxApiClient.SetWallet(client.Id(), client.PrivateKey(), client.PublicKey())
}

// SetZBoxWallet set wallet on zbox api
Expand Down
2 changes: 1 addition & 1 deletion zboxcore/allocationchange/updatefile.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ import (
"fmt"

"github.com/0chain/errors"
"github.com/0chain/gosdk/core/client"
"github.com/0chain/gosdk/core/common"
"github.com/0chain/gosdk/core/pathutil"
"github.com/0chain/gosdk/core/client"
"github.com/0chain/gosdk/zboxcore/fileref"
)

Expand Down
4 changes: 2 additions & 2 deletions zboxcore/marker/deletetoken.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ package marker
import (
"fmt"

"github.com/0chain/gosdk/core/encryption"
"github.com/0chain/gosdk/core/client"
"github.com/0chain/gosdk/core/encryption"
)

type DeleteToken struct {
Expand All @@ -25,6 +25,6 @@ func (dt *DeleteToken) GetHash() string {

func (dt *DeleteToken) Sign() error {
var err error
dt.Signature, err = client.Sign(dt.GetHash())
dt.Signature, err = client.Sign(dt.GetHash(), dt.ClientID)
return err
}
4 changes: 2 additions & 2 deletions zboxcore/marker/writemarker.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ import (
"fmt"

"github.com/0chain/errors"
"github.com/0chain/gosdk/core/client"
"github.com/0chain/gosdk/core/encryption"
"github.com/0chain/gosdk/core/sys"
"github.com/0chain/gosdk/core/client"
)

type WriteMarker struct {
Expand Down Expand Up @@ -48,7 +48,7 @@ func (wm *WriteMarker) GetHash() string {

func (wm *WriteMarker) Sign() error {
var err error
wm.Signature, err = client.Sign(wm.GetHash())
wm.Signature, err = client.Sign(wm.GetHash(), wm.ClientID)
return err
}

Expand Down
17 changes: 13 additions & 4 deletions zboxcore/sdk/allocation.go
Original file line number Diff line number Diff line change
Expand Up @@ -389,7 +389,7 @@ func (a *Allocation) GetBlobberStats() map[string]*BlobberAllocationStats {
wg.Add(numList)
rspCh := make(chan *BlobberAllocationStats, numList)
for _, blobber := range a.Blobbers {
go getAllocationDataFromBlobber(blobber, a.ID, a.Tx, rspCh, wg)
go getAllocationDataFromBlobber(blobber, a.ID, a.Tx, rspCh, wg, a.Owner)
}
wg.Wait()
result := make(map[string]*BlobberAllocationStats, len(a.Blobbers))
Expand Down Expand Up @@ -848,7 +848,7 @@ func (a *Allocation) GetCurrentVersion() (bool, error) {
go func(blobber *blockchain.StorageNode) {

defer wg.Done()
wr, err := GetWritemarker(a.ID, a.Tx, a.sig, blobber.ID, blobber.Baseurl)
wr, err := GetWritemarker(a.ID, a.Tx, a.sig, blobber.ID, blobber.Baseurl, a.Owner)
if err != nil {
atomic.AddInt32(&errCnt, 1)
logger.Logger.Error("error during getWritemarke", zap.Error(err))
Expand All @@ -857,6 +857,7 @@ func (a *Allocation) GetCurrentVersion() (bool, error) {
markerChan <- nil
} else {
markerChan <- &RollbackBlobber{
ClientId: a.Owner,
blobber: blobber,
lpm: wr,
commitResult: &CommitResult{},
Expand Down Expand Up @@ -952,6 +953,7 @@ func (a *Allocation) RepairRequired(remotepath string) (zboxutil.Uint128, zboxut
}

listReq := &ListRequest{Consensus: Consensus{RWMutex: &sync.RWMutex{}}}
listReq.ClientId = a.Owner
listReq.allocationID = a.ID
listReq.allocationTx = a.Tx
listReq.sig = a.sig
Expand Down Expand Up @@ -1561,6 +1563,7 @@ func (a *Allocation) ListDirFromAuthTicket(authTicket string, lookupHash string,
}

listReq := &ListRequest{Consensus: Consensus{RWMutex: &sync.RWMutex{}}}
listReq.ClientId = a.Owner
listReq.allocationID = a.ID
listReq.allocationTx = a.Tx
listReq.sig = a.sig
Expand Down Expand Up @@ -1601,6 +1604,7 @@ func (a *Allocation) ListDir(path string, opts ...ListRequestOptions) (*ListResu
return nil, errors.New("invalid_path", "Path should be valid and absolute")
}
listReq := &ListRequest{Consensus: Consensus{RWMutex: &sync.RWMutex{}}}
listReq.ClientId = a.Owner
listReq.allocationID = a.ID
listReq.allocationTx = a.Tx
listReq.sig = a.sig
Expand Down Expand Up @@ -1629,6 +1633,7 @@ func (a *Allocation) getRefs(path, pathHash, authToken, offsetPath, updatedDate,
}

oTreeReq := &ObjectTreeRequest{
ClientId: a.Owner,
allocationID: a.ID,
allocationTx: a.Tx,
sig: a.sig,
Expand Down Expand Up @@ -1850,6 +1855,7 @@ func (a *Allocation) GetRecentlyAddedRefs(page int, fromDate int64, pageLimit in

offset := int64(page-1) * int64(pageLimit)
req := &RecentlyAddedRefRequest{
ClientId: a.Owner,
allocationID: a.ID,
allocationTx: a.Tx,
sig: a.sig,
Expand Down Expand Up @@ -1878,6 +1884,7 @@ func (a *Allocation) GetFileMeta(path string) (*ConsolidatedFileMeta, error) {

result := &ConsolidatedFileMeta{}
listReq := &ListRequest{Consensus: Consensus{RWMutex: &sync.RWMutex{}}}
listReq.ClientId = a.Owner
listReq.allocationID = a.ID
listReq.allocationTx = a.Tx
listReq.sig = a.sig
Expand Down Expand Up @@ -1997,6 +2004,7 @@ func (a *Allocation) GetFileMetaFromAuthTicket(authTicket string, lookupHash str
}

listReq := &ListRequest{Consensus: Consensus{RWMutex: &sync.RWMutex{}}}
listReq.ClientId = a.Owner
listReq.allocationID = a.ID
listReq.allocationTx = a.Tx
listReq.sig = a.sig
Expand Down Expand Up @@ -2179,7 +2187,7 @@ func (a *Allocation) RevokeShare(path string, refereeClientID string) error {
query.Add("path", path)
query.Add("refereeClientID", refereeClientID)

httpreq, err := zboxutil.NewRevokeShareRequest(baseUrl, a.ID, a.Tx, a.sig, query)
httpreq, err := zboxutil.NewRevokeShareRequest(baseUrl, a.ID, a.Tx, a.sig, query, a.Owner)
if err != nil {
return err
}
Expand Down Expand Up @@ -2272,6 +2280,7 @@ func (a *Allocation) GetAuthTicket(path, filename string,
}

shareReq := &ShareRequest{
ClientId: a.Owner,
expirationSeconds: expiration,
allocationID: a.ID,
allocationTx: a.Tx,
Expand Down Expand Up @@ -2342,7 +2351,7 @@ func (a *Allocation) UploadAuthTicketToBlobber(authTicket string, clientEncPubKe
if err := formWriter.Close(); err != nil {
return err
}
httpreq, err := zboxutil.NewShareRequest(url, a.ID, a.Tx, a.sig, body)
httpreq, err := zboxutil.NewShareRequest(url, a.ID, a.Tx, a.sig, body, a.Owner)
if err != nil {
return err
}
Expand Down
2 changes: 2 additions & 0 deletions zboxcore/sdk/allocation_file_delete_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ func TestAllocation_DeleteFile(t *testing.T) {
DataShards: 2,
ParityShards: 2,
FileOptions: 63,
Owner: mockClientId,
}
a.InitAllocation()
client.SetSdkInitialized(true)
Expand Down Expand Up @@ -166,6 +167,7 @@ func TestAllocation_deleteFile(t *testing.T) {
DataShards: 2,
ParityShards: 2,
FileOptions: 63,
Owner: mockClientId,
}
a.InitAllocation()
client.SetSdkInitialized(true)
Expand Down
9 changes: 9 additions & 0 deletions zboxcore/sdk/allocation_file_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,7 @@ func TestAllocation_UpdateFile(t *testing.T) {
ParityShards: 2,
DataShards: 2,
Size: 2 * GB,
Owner: mockClientId,
}
setupMockAllocation(t, a)

Expand Down Expand Up @@ -289,6 +290,7 @@ func TestAllocation_UploadFile(t *testing.T) {
ParityShards: 2,
DataShards: 2,
Size: 2 * GB,
Owner: mockClientId,
}

setupMockAllocation(t, a)
Expand Down Expand Up @@ -327,6 +329,7 @@ func TestAllocation_UpdateFileWithThumbnail(t *testing.T) {
ParityShards: 2,
DataShards: 2,
Size: 2 * GB,
Owner: mockClientId,
}
setupMockAllocation(t, a)

Expand Down Expand Up @@ -406,6 +409,7 @@ func TestAllocation_UploadFileWithThumbnail(t *testing.T) {
ParityShards: 2,
DataShards: 2,
Size: 2 * GB,
Owner: mockClientId,
}

setupMockAllocation(t, a)
Expand Down Expand Up @@ -441,6 +445,7 @@ func TestAllocation_EncryptAndUpdateFile(t *testing.T) {
ParityShards: 2,
DataShards: 2,
Size: 2 * GB,
Owner: mockClientId,
}
setupMockAllocation(t, a)

Expand Down Expand Up @@ -512,6 +517,7 @@ func TestAllocation_EncryptAndUploadFile(t *testing.T) {
ParityShards: 2,
DataShards: 2,
Size: 2 * GB,
Owner: mockClientId,
}

setupMockAllocation(t, a)
Expand Down Expand Up @@ -560,6 +566,7 @@ func TestAllocation_EncryptAndUpdateFileWithThumbnail(t *testing.T) {
ParityShards: 2,
DataShards: 2,
Size: 2 * GB,
Owner: mockClientId,
}

setupMockAllocation(t, a)
Expand Down Expand Up @@ -636,6 +643,7 @@ func TestAllocation_EncryptAndUploadFileWithThumbnail(t *testing.T) {
DataShards: 2,
Size: 2 * GB,
ctx: context.TODO(),
Owner: mockClientId,
}

setupMockAllocation(t, a)
Expand Down Expand Up @@ -889,6 +897,7 @@ func TestAllocation_RepairFile(t *testing.T) {
ParityShards: tt.numBlobbers / 2,
DataShards: tt.numBlobbers / 2,
Size: 2 * GB,
Owner: mockClientId,
}
a.downloadChan = make(chan *DownloadRequest, 10)
a.repairChan = make(chan *RepairRequest, 1)
Expand Down
4 changes: 2 additions & 2 deletions zboxcore/sdk/blockdownloadworker.go
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ func (req *BlockDownloadRequest) downloadBlobberBlock(fastClient *fasthttp.Clien

var rspData downloadBlock
if statuscode != http.StatusOK {
zlogger.Logger.Error(fmt.Sprintf("downloadBlobberBlock FAIL - blobberID: %v, clientID: %v, blockNum: %d, retry: %d, response: %v", req.blobber.ID, client.ClientID(), header.BlockNum, retry, string(respBuf)))
zlogger.Logger.Error(fmt.Sprintf("downloadBlobberBlock FAIL - blobberID: %v, clientID: %v, blockNum: %d, retry: %d, response: %v", req.blobber.ID, client.Id(), header.BlockNum, retry, string(respBuf)))
if err = json.Unmarshal(respBuf, &rspData); err == nil {
return errors.New("download_error", fmt.Sprintf("Response status: %d, Error: %v,", statuscode, rspData.err))
}
Expand Down Expand Up @@ -232,7 +232,7 @@ func (req *BlockDownloadRequest) downloadBlobberBlock(fastClient *fasthttp.Clien
rspData.BlockChunks = splitData(dR.Data, req.chunkSize)
}

zlogger.Logger.Debug(fmt.Sprintf("downloadBlobberBlock 200 OK: blobberID: %v, clientID: %v, blockNum: %d", req.blobber.ID, client.ClientID(), header.BlockNum))
zlogger.Logger.Debug(fmt.Sprintf("downloadBlobberBlock 200 OK: blobberID: %v, clientID: %v, blockNum: %d", req.blobber.ID, client.Id(), header.BlockNum))

req.result <- &rspData
return nil
Expand Down
1 change: 1 addition & 0 deletions zboxcore/sdk/chunked_upload_bench_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ func BenchmarkChunkedUpload(b *testing.B) {
DataShards: 2,
ParityShards: 1,
ctx: context.TODO(),
Owner: mockClientId,
}
a.fullconsensus, a.consensusThreshold = a.getConsensuses()
for i := 0; i < (a.DataShards + a.ParityShards); i++ {
Expand Down
Loading
Loading