diff --git a/.github/workflows/build-sdks.yml b/.github/workflows/build-sdks.yml index af614cf7d..c17e192cd 100644 --- a/.github/workflows/build-sdks.yml +++ b/.github/workflows/build-sdks.yml @@ -10,7 +10,6 @@ on: tags: - 'v*.*.*' pull_request: - branches: [ master, staging, qa ] workflow_dispatch: env: diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index 05169e4e1..5d7fd4e34 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -8,7 +8,6 @@ on: push: branches: [ master, staging ] pull_request: - branches: [ master, staging ] workflow_dispatch: env: diff --git a/core/transaction/entity.go b/core/transaction/entity.go index 6b2b89fac..922ec9203 100644 --- a/core/transaction/entity.go +++ b/core/transaction/entity.go @@ -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" @@ -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"` @@ -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" @@ -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 @@ -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 { @@ -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 } diff --git a/mobilesdk/zbox/allocation.go b/mobilesdk/zbox/allocation.go index 779f146d5..51e1c6153 100644 --- a/mobilesdk/zbox/allocation.go +++ b/mobilesdk/zbox/allocation.go @@ -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:"-"` } diff --git a/zboxcore/sdk/allocation.go b/zboxcore/sdk/allocation.go index 4fdd28dcf..197554828 100644 --- a/zboxcore/sdk/allocation.go +++ b/zboxcore/sdk/allocation.go @@ -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"` } @@ -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"` diff --git a/zboxcore/sdk/sdk.go b/zboxcore/sdk/sdk.go index 4ece59f0a..a7ebc890e 100644 --- a/zboxcore/sdk/sdk.go +++ b/zboxcore/sdk/sdk.go @@ -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 { diff --git a/zcncore/transaction.go b/zcncore/transaction.go index 67923d7df..19dfed39c 100644 --- a/zcncore/transaction.go +++ b/zcncore/transaction.go @@ -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"` } @@ -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 { diff --git a/zcncore/transaction_mobile.go b/zcncore/transaction_mobile.go index 3f8711df6..2d1bf7380 100644 --- a/zcncore/transaction_mobile.go +++ b/zcncore/transaction_mobile.go @@ -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 { @@ -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) { @@ -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 {