Skip to content

Commit

Permalink
Merge branch 'sprint-june-5' into buff-download
Browse files Browse the repository at this point in the history
  • Loading branch information
din-mukhammed authored Jun 30, 2023
2 parents c900fdc + 9f45f6c commit f096e1d
Show file tree
Hide file tree
Showing 8 changed files with 71 additions and 33 deletions.
1 change: 0 additions & 1 deletion .github/workflows/build-sdks.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ on:
tags:
- 'v*.*.*'
pull_request:
branches: [ master, staging, qa ]
workflow_dispatch:

env:
Expand Down
1 change: 0 additions & 1 deletion .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ on:
push:
branches: [ master, staging ]
pull_request:
branches: [ master, staging ]
workflow_dispatch:

env:
Expand Down
62 changes: 53 additions & 9 deletions core/transaction/entity.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,14 @@ import (
"net/http"
"strings"
"sync"
"time"

"github.com/0chain/common/core/logging"
"github.com/0chain/errors"
"github.com/0chain/gosdk/core/common"
"github.com/0chain/gosdk/core/encryption"
"github.com/0chain/gosdk/core/util"
lru "github.com/hashicorp/golang-lru"
)

const TXN_SUBMIT_URL = "v1/transaction/put"
Expand Down Expand Up @@ -54,15 +57,16 @@ type SmartContractTxnData struct {
}

type StorageAllocation struct {
ID string `json:"id"`
DataShards int `json:"data_shards"`
ParityShards int `json:"parity_shards"`
Size int64 `json:"size"`
Expiration int64 `json:"expiration_date"`
Owner string `json:"owner_id"`
OwnerPublicKey string `json:"owner_public_key"`
ReadRatio *Ratio `json:"read_ratio"`
WriteRatio *Ratio `json:"write_ratio"`
ID string `json:"id"`
DataShards int `json:"data_shards"`
ParityShards int `json:"parity_shards"`
Size int64 `json:"size"`
Expiration int64 `json:"expiration_date"`
Owner string `json:"owner_id"`
OwnerPublicKey string `json:"owner_public_key"`
ReadRatio *Ratio `json:"read_ratio"`
WriteRatio *Ratio `json:"write_ratio"`
MinLockDemand float64 `json:"min_lock_demand"`
}
type Ratio struct {
ZCN int64 `json:"zcn"`
Expand Down Expand Up @@ -173,6 +177,16 @@ type SignFunc = func(msg string) (string, error)
type VerifyFunc = func(signature, msgHash, publicKey string) (bool, error)
type SignWithWallet = func(msg string, wallet interface{}) (string, error)

var cache *lru.Cache

func init() {
var err error
cache, err = lru.New(100)
if err != nil {
fmt.Println("caching Initilization failed, err:", err)
}
}

func NewTransactionEntity(clientID string, chainID string, publicKey string, nonce int64) *Transaction {
txn := &Transaction{}
txn.Version = "1.0"
Expand Down Expand Up @@ -269,8 +283,14 @@ func sendTransactionToURL(url string, txn *Transaction, wg *sync.WaitGroup) ([]b
return nil, errors.Wrap(err, errors.New("transaction_send_error", postResponse.Body))
}

type cachedObject struct {
Expiration time.Duration
Value interface{}
}

// EstimateFee estimates transaction fee
func EstimateFee(txn *Transaction, miners []string, reqPercent ...float32) (uint64, error) {

const minReqNum = 3
var reqN int

Expand All @@ -294,6 +314,20 @@ func EstimateFee(txn *Transaction, miners []string, reqPercent ...float32) (uint
go func(minerUrl string) {
defer wg.Done()

// Retrieve the object from the cache
cached, ok := cache.Get(STORAGESC_CREATE_ALLOCATION)

if ok {
cachedObj, ok := cached.(*cachedObject)
if !ok {
logging.Logger.Error("Object of bad type")
return
}
val := cachedObj.Value.(map[string]interface{})["fee"].(int)
feeC <- uint64(val)
return
}

url := minerUrl + ESTIMATE_TRANSACTION_COST
req, err := util.NewHTTPPostRequest(url, txn)
if err != nil {
Expand Down Expand Up @@ -343,6 +377,16 @@ func EstimateFee(txn *Transaction, miners []string, reqPercent ...float32) (uint
}
}

// adding response to cache
obj := map[string]interface{}{
"fee": fee,
}

cache.Add(STORAGESC_CREATE_ALLOCATION, &cachedObject{
Expiration: 30 * time.Hour,
Value: obj,
})

return fee, nil
}

Expand Down
16 changes: 8 additions & 8 deletions mobilesdk/zbox/allocation.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,14 @@ var ErrInvalidAllocation = errors.New("zbox: invalid allocation")

// Allocation - structure for allocation object
type Allocation struct {
ID string `json:"id"`
DataShards int `json:"data_shards"`
ParityShards int `json:"parity_shards"`
Size int64 `json:"size"`
Expiration int64 `json:"expiration_date"`
Name string `json:"name"`
Stats string `json:"stats"`

ID string `json:"id"`
DataShards int `json:"data_shards"`
ParityShards int `json:"parity_shards"`
Size int64 `json:"size"`
Expiration int64 `json:"expiration_date"`
Name string `json:"name"`
Stats string `json:"stats"`
MinLockDemand float64 `json:"min_lock_demand"`
blobbers []*blockchain.StorageNode `json:"-"`
sdkAllocation *sdk.Allocation `json:"-"`
}
Expand Down
5 changes: 2 additions & 3 deletions zboxcore/sdk/allocation.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,6 @@ func (pr *PriceRange) IsValid() bool {
type Terms struct {
ReadPrice common.Balance `json:"read_price"` // tokens / GB
WritePrice common.Balance `json:"write_price"` // tokens / GB
MinLockDemand float64 `json:"min_lock_demand"`
MaxOfferDuration time.Duration `json:"max_offer_duration"`
}

Expand Down Expand Up @@ -171,8 +170,8 @@ type Allocation struct {
// ReadPriceRange is requested reading prices range.
ReadPriceRange PriceRange `json:"read_price_range"`
// WritePriceRange is requested writing prices range.
WritePriceRange PriceRange `json:"write_price_range"`

WritePriceRange PriceRange `json:"write_price_range"`
MinLockDemand float64 `json:"min_lock_demand"`
ChallengeCompletionTime time.Duration `json:"challenge_completion_time"`
StartTime common.Timestamp `json:"start_time"`
Finalized bool `json:"finalized,omitempty"`
Expand Down
2 changes: 1 addition & 1 deletion zboxcore/sdk/sdk.go
Original file line number Diff line number Diff line change
Expand Up @@ -580,7 +580,7 @@ type Blobber struct {
UncollectedServiceCharge int64 `json:"uncollected_service_charge"`
IsKilled bool `json:"is_killed"`
IsShutdown bool `json:"is_shutdown"`
IsAvailable bool `json:"is_available"`
NotAvailable bool `json:"not_available"`
}

type Validator struct {
Expand Down
5 changes: 2 additions & 3 deletions zcncore/transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -197,8 +197,7 @@ type StakePoolSettings struct {

type Terms struct {
ReadPrice common.Balance `json:"read_price"` // tokens / GB
WritePrice common.Balance `json:"write_price"` // tokens / GB
MinLockDemand float64 `json:"min_lock_demand"`
WritePrice common.Balance `json:"write_price"` // tokens / GB `
MaxOfferDuration time.Duration `json:"max_offer_duration"`
}

Expand All @@ -210,7 +209,7 @@ type Blobber struct {
Allocated common.Size `json:"allocated"`
LastHealthCheck common.Timestamp `json:"last_health_check"`
StakePoolSettings StakePoolSettings `json:"stake_pool_settings"`
IsAvailable bool `json:"is_available"`
NotAvailable bool `json:"not_available"`
}

type Validator struct {
Expand Down
12 changes: 5 additions & 7 deletions zcncore/transaction_mobile.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,10 +149,9 @@ type StakePoolSettings struct {
}

type Terms struct {
ReadPrice int64 `json:"read_price"` // tokens / GB
WritePrice int64 `json:"write_price"` // tokens / GB
MinLockDemand float64 `json:"min_lock_demand"`
MaxOfferDuration int64 `json:"max_offer_duration"`
ReadPrice int64 `json:"read_price"` // tokens / GB
WritePrice int64 `json:"write_price"` // tokens / GB
MaxOfferDuration int64 `json:"max_offer_duration"`
}

type Blobber interface {
Expand All @@ -179,7 +178,7 @@ type blobber struct {
LastHealthCheck int64 `json:"last_health_check"`
Terms Terms `json:"terms"`
StakePoolSettings StakePoolSettings `json:"stake_pool_settings"`
IsAvailable bool `json:"is_available"`
NotAvailable bool `json:"not_available"`
}

func (b *blobber) SetStakePoolSettings(delegateWallet string, minStake int64, maxStake int64, numDelegates int, serviceCharge float64) {
Expand All @@ -196,13 +195,12 @@ func (b *blobber) SetTerms(readPrice int64, writePrice int64, minLockDemand floa
b.Terms = Terms{
ReadPrice: readPrice,
WritePrice: writePrice,
MinLockDemand: minLockDemand,
MaxOfferDuration: maxOfferDuration,
}
}

func (b *blobber) SetAvailable(availability bool) {
b.IsAvailable = availability
b.NotAvailable = availability
}

type Validator interface {
Expand Down

0 comments on commit f096e1d

Please sign in to comment.