Skip to content

Commit 8ca74e0

Browse files
Merge pull request #4 from 0chain/new_function_download_and_commit
New function to download and commit the metadata
2 parents 9e077c7 + 1d2d1de commit 8ca74e0

File tree

2 files changed

+73
-3
lines changed

2 files changed

+73
-3
lines changed

zboxcore/sdk/allocation.go

Lines changed: 66 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,13 @@ import (
1010
"path/filepath"
1111
"strings"
1212
"sync"
13+
"time"
1314

15+
"github.com/0chain/gosdk/zboxcore/client"
1416
"github.com/0chain/gosdk/zboxcore/fileref"
1517

1618
"github.com/0chain/gosdk/core/common"
19+
"github.com/0chain/gosdk/core/transaction"
1720
"github.com/0chain/gosdk/zboxcore/blockchain"
1821
. "github.com/0chain/gosdk/zboxcore/logger"
1922
"github.com/0chain/gosdk/zboxcore/marker"
@@ -25,6 +28,16 @@ var (
2528
notInitialized = common.NewError("sdk_not_initialized", "Please call InitStorageSDK Init and use GetAllocation to get the allocation object")
2629
)
2730

31+
type MetaOperation struct {
32+
CrudType string
33+
MetaData *ConsolidatedFileMeta
34+
}
35+
36+
type MetaTransactionData struct {
37+
TxnID string
38+
MetaData *ConsolidatedFileMeta
39+
}
40+
2841
type ConsolidatedFileMeta struct {
2942
Name string
3043
Type string
@@ -210,6 +223,7 @@ func (a *Allocation) uploadOrUpdateFile(localpath string, remotepath string, sta
210223
}()
211224
return nil
212225
}
226+
213227
func (a *Allocation) DownloadFile(localPath string, remotePath string, status StatusCallback) error {
214228
return a.downloadFile(localPath, remotePath, DOWNLOAD_CONTENT_FULL, status)
215229
}
@@ -532,7 +546,7 @@ func (a *Allocation) GetAuthTicket(path string, filename string, referenceType s
532546
authTicket, err := shareReq.GetAuthTicketForEncryptedFile(refereeClientID, refereeEncryptionPublicKey)
533547
if err != nil {
534548
return "", err
535-
}
549+
}
536550
return authTicket, nil
537551

538552
}
@@ -615,3 +629,54 @@ func (a *Allocation) downloadFromAuthTicket(localPath string, authTicket string,
615629
}()
616630
return nil
617631
}
632+
633+
func (a *Allocation) CommitMetaTransaction(path, crudOperation string) (*MetaTransactionData, error) {
634+
fileMeta, err := a.GetFileMeta(path)
635+
if err != nil {
636+
return nil, err
637+
}
638+
639+
metaOperationData := &MetaOperation{
640+
CrudType: crudOperation,
641+
MetaData: fileMeta,
642+
}
643+
metaOperationBytes, err := json.Marshal(metaOperationData)
644+
if err != nil {
645+
return nil, err
646+
}
647+
648+
txn := transaction.NewTransactionEntity(client.GetClientID(), blockchain.GetChainID(), client.GetClientPublicKey())
649+
txn.TransactionData = string(metaOperationBytes)
650+
txn.TransactionType = transaction.TxnTypeData
651+
err = txn.ComputeHashAndSign(client.Sign)
652+
if err != nil {
653+
return nil, err
654+
}
655+
transaction.SendTransactionSync(txn, blockchain.GetMiners())
656+
time.Sleep(5 * time.Second)
657+
retries := 0
658+
var t *transaction.Transaction
659+
for retries < 5 {
660+
t, err = transaction.VerifyTransaction(txn.Hash, blockchain.GetSharders())
661+
if err == nil {
662+
break
663+
}
664+
retries++
665+
time.Sleep(5 * time.Second)
666+
}
667+
668+
if err != nil {
669+
Logger.Error("Error verifying the commit transaction", err.Error(), txn.Hash)
670+
return nil, err
671+
}
672+
if t == nil {
673+
return nil, common.NewError("transaction_validation_failed", "Failed to get the transaction confirmation")
674+
}
675+
676+
metaTransactionData := &MetaTransactionData{
677+
TxnID: t.Hash,
678+
MetaData: metaOperationData.MetaData,
679+
}
680+
681+
return metaTransactionData, nil
682+
}

zcncore/wallet.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package zcncore
33
import (
44
"encoding/json"
55
"fmt"
6+
"math"
67
"net/http"
78
"os"
89
"strconv"
@@ -173,15 +174,19 @@ func assertConfig() {
173174
}
174175
}
175176
func getMinMinersSubmit() int {
176-
return util.MaxInt((_config.chain.MinSubmit * len(_config.chain.Miners) / 100), 1)
177+
return util.MaxInt(calculateMinRequired(float64(_config.chain.MinSubmit), float64(len(_config.chain.Miners))/100), 1)
177178
}
178179
func getMinShardersVerify() int {
179-
return util.MaxInt((_config.chain.MinConfirmation * len(_config.chain.Sharders) / 100), 1)
180+
return util.MaxInt(calculateMinRequired(float64(_config.chain.MinConfirmation), float64(len(_config.chain.Sharders))/100), 1)
180181
}
181182
func getMinRequiredChainLength() int64 {
182183
return int64(_config.chain.ConfirmationChainLength)
183184
}
184185

186+
func calculateMinRequired(minRequired, percent float64) int {
187+
return int(math.Round(minRequired * percent))
188+
}
189+
185190
// GetVersion - returns version string
186191
func GetVersion() string {
187192
return version.VERSIONSTR

0 commit comments

Comments
 (0)